-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathroleGuard.js
More file actions
149 lines (133 loc) · 4.5 KB
/
Copy pathroleGuard.js
File metadata and controls
149 lines (133 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* roleGuard.js — Centralized Role-Based Access Control (RBAC) Guard
*
* Implements client-side views/actions gating, testing overrides,
* and a blueprint for server-side verification.
*/
(function () {
const ROLE_STORAGE_KEY = "learnsphere_user_role_override"; // For local testing overrides
function getUser() {
try {
const u = localStorage.getItem("user");
return u ? JSON.parse(u) : null;
} catch {
return null;
}
}
function getCurrentRole() {
// 1. Allow testing override
const override = localStorage.getItem(ROLE_STORAGE_KEY);
if (override) return override;
// 2. Read from user object
const user = getUser();
return user && user.role ? user.role : "learner";
}
function setRoleOverride(role) {
if (role) {
localStorage.setItem(ROLE_STORAGE_KEY, role);
} else {
localStorage.removeItem(ROLE_STORAGE_KEY);
}
}
// Permission rules for pages (matching against location.pathname)
const PAGE_RULES = {
"teachers.html": ["teacher"],
"teacher_analytics.html": ["teacher"],
"parents.html": ["parent"],
"my_progress.html": ["learner"],
"review.html": ["learner"],
"learnerassignments.html": ["learner", "teacher"],
};
// Permission rules for actions
const ACTION_RULES = {
"create_class": ["teacher"],
"build_assignment": ["teacher"],
"export_progress": ["learner", "parent", "teacher"],
"submit_quiz": ["learner"],
"clear_progress": ["learner"],
"manage_sync": ["learner"],
};
function hasPageAccess(pathname) {
const filename = pathname.split("/").pop().toLowerCase() || "index.html";
if (filename === "" || filename === "index.html" || filename === "home.html") {
return true; // public or main landing page
}
// Find matching rule
for (const [page, roles] of Object.entries(PAGE_RULES)) {
if (filename.includes(page.toLowerCase())) {
const currentRole = getCurrentRole();
return roles.includes(currentRole);
}
}
return true; // default public
}
function hasPermission(action) {
const roles = ACTION_RULES[action];
if (!roles) return true; // action not guarded
return roles.includes(getCurrentRole());
}
function checkPageAccess() {
if (typeof window === "undefined") return;
const pathname = window.location.pathname;
if (!hasPageAccess(pathname)) {
console.warn(`Access Denied to ${pathname} for role ${getCurrentRole()}`);
const isNested = pathname.includes("/log/") ||
pathname.includes("/quiz/") ||
pathname.includes("/sub/") ||
pathname.includes("/chemistryquiz/") ||
pathname.includes("/mathsquiz/");
const redirectUrl = isNested ? "../home.html?error=unauthorized" : "home.html?error=unauthorized";
window.location.href = redirectUrl;
}
}
function gateAction(action, fn) {
return function (...args) {
if (!hasPermission(action)) {
const err = `Unauthorized action: ${action} is not allowed for role ${getCurrentRole()}`;
console.error(err);
alert(err);
throw new Error(err);
}
return fn.apply(this, args);
};
}
/**
* Server-Side Plan / Verification Stub
* Since this application is client-side only, this helper serves as the architecture blueprint
* for server-side role validation. In production, roles are validated by verifying a JWT
* payload signature containing the user's role claim on every backend API request.
*/
async function validateRoleOnServer(action, token) {
if (!token) {
return { success: false, error: "No token provided" };
}
// Blueprint for server request:
// const response = await fetch('/api/verify-permission', {
// method: 'POST',
// headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' },
// body: JSON.stringify({ action })
// });
// return response.json();
return { success: true, mocked: true, action, role: getCurrentRole() };
}
const roleGuard = {
getCurrentRole,
getUser,
setRoleOverride,
hasPageAccess,
hasPermission,
checkPageAccess,
gateAction,
validateRoleOnServer,
PAGE_RULES,
ACTION_RULES
};
if (typeof window !== "undefined") {
window.roleGuard = roleGuard;
// Auto execute page check if loaded in browser
checkPageAccess();
}
if (typeof module !== "undefined" && module.exports) {
module.exports = roleGuard;
}
})();