Create authorization.js
Browse files- auth/authorization.js +82 -0
auth/authorization.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Authorization {
|
| 2 |
+
constructor() {
|
| 3 |
+
this.roles = {
|
| 4 |
+
'student': 1,
|
| 5 |
+
'instructor': 2,
|
| 6 |
+
'admin': 3
|
| 7 |
+
};
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
getCurrentUserRole() {
|
| 11 |
+
const currentUser = authManager.getCurrentUser();
|
| 12 |
+
return currentUser ? currentUser.role : 'guest';
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
hasRole(requiredRole) {
|
| 16 |
+
const currentRole = this.getCurrentUserRole();
|
| 17 |
+
return this.roles[currentRole] >= this.roles[requiredRole];
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
hasAnyRole(requiredRoles) {
|
| 21 |
+
const currentRole = this.getCurrentUserRole();
|
| 22 |
+
return requiredRoles.includes(currentRole);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
requireRole(requiredRole) {
|
| 26 |
+
return (callback) => {
|
| 27 |
+
if (this.hasRole(requiredRole)) {
|
| 28 |
+
return callback();
|
| 29 |
+
} else {
|
| 30 |
+
this.showAccessDenied();
|
| 31 |
+
return false;
|
| 32 |
+
}
|
| 33 |
+
};
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
requireAuth(callback) {
|
| 37 |
+
return () => {
|
| 38 |
+
if (authManager.isAuthenticated()) {
|
| 39 |
+
return callback();
|
| 40 |
+
} else {
|
| 41 |
+
this.showLoginRequired();
|
| 42 |
+
return false;
|
| 43 |
+
}
|
| 44 |
+
};
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
showAccessDenied() {
|
| 48 |
+
alert('دسترسی denied. شما permission لازم برای این بخش را ندارید.');
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
showLoginRequired() {
|
| 52 |
+
alert('لطفاً برای دسترسی به این بخش وارد شوید.');
|
| 53 |
+
// در برنامه واقعی، کاربر به صفحه ورود هدایت میشود
|
| 54 |
+
window.location.hash = '#/login';
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
// بررسی دسترسی به منابع
|
| 58 |
+
canAccessLesson(lessonId) {
|
| 59 |
+
const currentUser = authManager.getCurrentUser();
|
| 60 |
+
if (!currentUser) return false;
|
| 61 |
+
|
| 62 |
+
// در اینجا میتوان منطق پیچیدهتری برای دسترسی به درسها پیادهسازی کرد
|
| 63 |
+
return true;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
canEditContent() {
|
| 67 |
+
return this.hasAnyRole(['instructor', 'admin']);
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
canManageUsers() {
|
| 71 |
+
return this.hasRole('admin');
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
// دکوراتورهای سریع
|
| 76 |
+
const requireStudent = (callback) => new Authorization().requireRole('student')(callback);
|
| 77 |
+
const requireInstructor = (callback) => new Authorization().requireRole('instructor')(callback);
|
| 78 |
+
const requireAdmin = (callback) => new Authorization().requireRole('admin')(callback);
|
| 79 |
+
const requireAuth = (callback) => new Authorization().requireAuth(callback);
|
| 80 |
+
|
| 81 |
+
// ایجاد نمونه singleton
|
| 82 |
+
const authzManager = new Authorization();
|