-
Notifications
You must be signed in to change notification settings - Fork 4
/
Admin.ts
94 lines (85 loc) · 2.24 KB
/
Admin.ts
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
import { AdminRoleData } from "../interfaces";
export default class Admin {
roles: AdminRoleData[];
email: string | null = null;
private systemAdmin: boolean = false;
private sitewideLibraryManager: boolean = false;
private sitewideLibrarian: boolean = false;
private manager: boolean = false;
private libraryRoles: { [library: string]: string } = {};
constructor(roles: AdminRoleData[], email?: string) {
this.roles = roles;
this.email = email;
for (const role of roles) {
switch (role.role) {
case "system": {
this.systemAdmin = true;
}
// eslint-disable-next-line no-fallthrough
case "manager-all": {
this.sitewideLibraryManager = true;
this.manager = true;
}
// eslint-disable-next-line no-fallthrough
case "librarian-all": {
this.sitewideLibrarian = true;
break;
}
case "manager": {
this.manager = true;
this.libraryRoles[role.library] = "manager";
break;
}
case "librarian": {
this.libraryRoles[role.library] = "librarian";
break;
}
default: {
break;
}
}
}
}
hasRole(role: string, library?: string) {
if (this.isSystemAdmin()) {
return true;
}
if (role !== "system" && this.isSitewideLibraryManager()) {
return true;
}
if (role === "librarian-all" && this.isSitewideLibrarian()) {
return true;
}
if (role === "manager" && this.isLibraryManager(library)) {
return true;
}
if (role === "librarian" && this.isLibrarian(library)) {
return true;
}
return false;
}
isSystemAdmin() {
return this.systemAdmin;
}
isSitewideLibraryManager() {
return this.sitewideLibraryManager;
}
isSitewideLibrarian() {
return this.sitewideLibrarian;
}
isLibraryManager(library: string) {
return (
this.sitewideLibraryManager || this.libraryRoles[library] === "manager"
);
}
isLibrarian(library: string) {
return (
this.isLibraryManager(library) ||
this.sitewideLibrarian ||
this.libraryRoles[library] === "librarian"
);
}
isLibraryManagerOfSomeLibrary() {
return this.manager;
}
}