Skip to content

Commit 28fed71

Browse files
committed
Auto stash before merge of "master" and "origin/master"
1 parent a77a46b commit 28fed71

File tree

5 files changed

+87
-87
lines changed

5 files changed

+87
-87
lines changed

md/test-exam-practice-question.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
# Question 23
2-
@@@ #answer
3-
## Webservices, REST, SQL-Databases
4-
@@@
52
## Introduction
6-
@@@ #practice
7-
### Practice-question
3+
@@@ #exam
4+
## Exam-question: Webservices, REST, SQL-Databases
5+
86
```plantuml
9-
class practice
7+
class exam
108
```
119
```bash
12-
class practice
10+
class exam
1311
```
14-
You are a student making an internship at a big company that wants to re-write the legacy software.
12+
You are employed at a company that wants to re-structure their legacy software.
1513
@@@
16-
@@@ #exam
17-
### Exam-question
14+
@@@ #practice
15+
## Practice-question: Webservices, REST, SQL-Databases
16+
1817
```plantuml
19-
class exam
18+
class practice
2019
```
2120
```bash
22-
class exam
21+
class practice
2322
```
24-
You are employed at a company that wants to re-structure their legacy software.
23+
You are a student making an internship at a big company that wants to re-write the legacy software.
2524
@@@
2625
## Question
2726
```plantuml

md/test-md-file.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ This is a sample text...
1010
@@@ teacher
1111
Only visible to the teachers.
1212
@@@
13-
@@@ 4bhif
13+
@@@ teacher, 4bhif
1414
Only visible to 4bhif and teachers.
1515
@@@
1616
@@@ admin
1717
Admins only!!!
1818
@@@
1919
@@@ 4bhif,5bhif, 4ahif
20-
Special visibility
20+
Special visibility (4bhif, 5bhif and 4ahif only)
2121
@@@
2222

2323
## Test PlantUML

md/test-perms.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,28 @@
22
The following paragraph is only visible to teachers:
33
@@@ teacher
44
>Only visible to teachers.
5-
65
@@@
76

87
# Simple Group Permission
98
The following paragraph is only visible to 5BHIF:
109
@@@ 5bhif
1110
>Only visible to 5BHIF.
12-
1311
@@@
1412

1513
# User Permission
16-
The following paragraph is only visible to Stu Dent
17-
@@@ Stu Dent
14+
The following paragraph is only visible to Stu Dent:
15+
@@@ stu dent
1816
>Only visible to Stu Dent
19-
2017
@@@
2118
# NO Nested Permissions!
2219
Nested permissions don't work due to the way we parse the permission-declarations (regexp).
2320

2421
The following paragraph is only visible to 5BHIF:
2522
@@@ 5bhif
2623
>Only visible to users in 5BHIF group.
24+
@@@
2725

2826
The following paragraph is only visible to teachers in group 5BHIF:
2927
@@@ teacher
3028
>Only visible to users being in teachers AND 5bhif groups.
31-
32-
@@@
3329
@@@

obsidian.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@ async function getTopdownMenu(req) {
12171217
</label>
12181218
</div>
12191219
${
1220-
(await hasSomeRoles(req, ["teacher"]))
1220+
(await hasSomeRoles(req, ["teacher"], true))
12211221
? `<div class="flipswitch menu" style="display: inline-block; top: 16px; margin-top: 12px; margin-left: 3px;">
12221222
<input checked="" onchange="toggleViewExam()" id="examFs" class="flipswitch-cb" name="flipswitch" type="checkbox">
12231223
<label for="examFs" class="flipswitch-label">

utils.js

Lines changed: 69 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import { getUserAttributes } from "./middlewares/keycloak-middleware.js";
44
/**
55
* hasAllRoles(req, ["teacher", "student", "admin", "gluppy"])
66
*/
7-
export async function hasAllRoles(req, clientRoles, override = false) {
8-
return hasRoles(req, clientRoles, true, override);
7+
export async function hasAllRoles(req, clientRoles, allowOverride = false) {
8+
return hasRoles(req, clientRoles, true, allowOverride);
99
}
1010

1111
/**
1212
* hasSomeRoles(req, ["teacher", "student", "admin", "gluppy"])
1313
*/
14-
export async function hasSomeRoles(req, clientRoles, override = false) {
15-
return hasRoles(req, clientRoles, false, override);
14+
export async function hasSomeRoles(req, clientRoles, allowOverride = false) {
15+
return hasRoles(req, clientRoles, false, allowOverride);
1616
}
1717

1818
/**
@@ -84,84 +84,89 @@ async function hasClientRoles(req, clientRoles, all) {
8484
/**
8585
* Fetches all Keycloak roles of the client and all LDAP roles of the user, previously calculated in the Keycloak-middleware and checks for permissions.
8686
*/
87-
async function hasRoles(req, clientRoles, all, override) {
87+
async function hasRoles(req, clientRoles, all, allowOverride) {
8888
try {
89+
//console.log("Checking roles", clientRoles, "all", all, "allowOverride", allowOverride);
90+
// The roles to check are empty. So we return true.
8991
if (
9092
clientRoles === undefined ||
9193
clientRoles === null ||
9294
clientRoles.length == 0
9395
) {
9496
return true;
9597
}
98+
9699
let clientAccess = null;
97100
const attributes = await getUserAttributes(req);
98101
const ccr = await getClientRoles(req, clientRoles);
99102
// console.log("Client roles", ccr);
100103
// console.log("Request user rolesCalculated", req.user.rolesCalculated);
101-
if (
102-
req.user.rolesCalculated !== undefined &&
103-
req.user.rolesCalculated !== null
104-
) {
105-
// console.log("attributes", attributes);
106-
if (attributes && attributes.attributes && attributes.attributes.config) {
107-
const a = JSON.parse(attributes.attributes.config);
108-
let r = JSON.parse(req.user.rolesCalculated);
109-
// console.log("Roles Calculated", r);
110-
if (r === undefined || r === null) {
111-
r = {};
112-
}
113-
let name = req.user.name
114-
name = name.trim()
115-
name = name.toLowerCase()
116-
r[name] = true;
117-
const cr = await getClientRoles(req, clientRoles);
118-
if (cr) {
119-
for (const role of cr) {
120-
r[role] = "cr";
121-
}
122-
}
123-
const clientViews = clientRoles.filter((role) => role.startsWith("#"));
124-
clientRoles = clientRoles.filter((role) => !role.startsWith("#"));
125-
if ((r.admin || r.teacher) && override && a.vt == 0) {
126-
// Downgrade teacher and admin to student.
127-
r.admin = false;
128-
r.teacher = false;
129-
}
130-
if (r.admin) {
131-
clientAccess = true;
104+
// console.log("attributes", attributes);
105+
let a = {ve: 0, vt: 0, va: 0};
106+
if (attributes?.attributes?.config) {
107+
a = JSON.parse(attributes.attributes.config);
108+
}
109+
let r = JSON.parse(req.user.rolesCalculated);
110+
// console.log("Roles Calculated", r);
111+
if (r === undefined || r === null) {
112+
r = {};
113+
}
114+
let name = req.user.name
115+
name = name.trim()
116+
name = name.toLowerCase()
117+
r[name] = true;
118+
const cr = await getClientRoles(req, clientRoles);
119+
if (cr) {
120+
for (const role of cr) {
121+
r[role] = true;
122+
}
123+
}
124+
const clientViews = clientRoles.filter((role) => role.startsWith("#"));
125+
clientRoles = clientRoles.filter((role) => !role.startsWith("#"));
126+
let isAdmin = r.admin || clientRoles.includes("admin");
127+
let isTeacher = r.teacher || clientRoles.includes("teacher");
128+
if (isTeacher) {
129+
r.teachers = true;
130+
}
131+
if ((isAdmin || isTeacher) && allowOverride && a.vt == 0) {
132+
// Downgrade teacher and admin to student.
133+
isAdmin = false;
134+
isTeacher = false;
135+
delete r["teacher"];
136+
}
137+
if (isAdmin) {
138+
clientAccess = true;
139+
} else {
140+
if (clientRoles.length > 0) {
141+
if (all) {
142+
clientAccess = clientRoles.every((role) => r[role]);
132143
} else {
133-
if (!clientRoles.includes("admin") && r.teacher) {
134-
clientAccess = true;
135-
} else {
136-
if (clientRoles.length > 0) {
137-
if (all) {
138-
clientAccess = clientRoles.every((role) => r[role]);
139-
} else {
140-
clientAccess = clientRoles.some((role) => r[role]);
141-
}
142-
}
143-
}
144+
clientAccess = clientRoles.some((role) => r[role]);
144145
}
145-
if (clientAccess === null || clientAccess) {
146-
for (const view of clientViews) {
147-
const viewRole = view.substring(1);
148-
switch (viewRole) {
149-
case "exam":
150-
// For security reasons hardcoded to only allow teachers and admins to view exam-questions.
151-
clientAccess = a.ve == 1 && (r.admin || r.teacher);
152-
break;
153-
case "practice":
154-
clientAccess = a.ve == 0;
155-
break;
156-
case "answer":
157-
clientAccess = a.va == 1;
158-
break;
159-
}
160-
}
146+
}
147+
}
148+
if (clientAccess === null || clientAccess) {
149+
for (const view of clientViews) {
150+
const viewRole = view.substring(1);
151+
switch (viewRole) {
152+
case "exam":
153+
// For security reasons hardcoded to only allow teachers and admins to view exam-questions.
154+
clientAccess = a.ve == 1 && (isAdmin || isTeacher);
155+
break;
156+
case "practice":
157+
clientAccess = a.ve == 0;
158+
break;
159+
case "answer":
160+
clientAccess = a.va == 1;
161+
break;
161162
}
162163
}
163164
}
164-
return clientAccess || false;
165+
console.log("Checking roles:", clientRoles, "all:", all, "allowOverride:", allowOverride, "isAdmin:", isAdmin, "isTeacher:", isTeacher, "studOvr:", a.vt == 0, "Client access:", clientAccess);
166+
if (clientAccess === null) {
167+
clientAccess = false;
168+
}
169+
return clientAccess;
165170
} catch (error) {
166171
console.error(`Error checking client roles: ${error}`);
167172
return null;

0 commit comments

Comments
 (0)