-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmemory-store.ts
153 lines (133 loc) · 5.16 KB
/
memory-store.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
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
150
151
152
153
import * as Lodash from 'lodash';
import * as UUID from 'uuid';
import { IStore } from '../interfaces/store';
import { ISubject } from '../interfaces/subject';
import { TCreatePermission } from '../types/create-permission';
import { TPermission } from '../types/permission';
import { TPermissionId } from '../types/permission-id';
import { TPermissionOrId } from '../types/permission-or-id';
import { TRole } from '../types/role';
import { TSubjectOrPrincipal } from '../types/subject-or-principal';
import { TSubjectPrincipal } from '../types/subject-principal';
import { toPermissionId } from '../utils/to-permission-id';
import { toSubjectPrincipal } from '../utils/to-subject-principal';
import { Subject } from './subject';
export class MemoryStore implements IStore {
private permissions: Map<TPermissionId, TPermission>;
private subjects: Map<TSubjectPrincipal, ISubject<any>>;
private subjectRoles: Map<TSubjectPrincipal, Set<TRole>>;
private rolePermissions: Map<TRole, Set<TPermissionId>>;
public constructor() {
this.subjects = new Map();
this.permissions = new Map();
this.subjectRoles = new Map();
this.rolePermissions = new Map();
}
public createPermission(permission: TCreatePermission): TPermission {
if (!permission.id) {
permission.id = UUID.v4();
}
this.permissions.set(permission.id, permission as TPermission);
return permission as TPermission;
}
public deletePermission(permissionOrId: TPermissionOrId): this {
const id = toPermissionId(permissionOrId);
this.permissions.delete(id);
for (const set of this.rolePermissions.values()) {
set.delete(id);
}
return this;
}
public replacePermission(permission: TPermission): this {
this.permissions.set(permission.id, permission);
return this;
}
public addPermissionToRole(role: TRole, permissionOrId: TPermissionOrId): this {
const id = toPermissionId(permissionOrId);
if (!this.permissions.has(id)) {
if (Lodash.isPlainObject(permissionOrId)) {
this.createPermission(permissionOrId as TPermission);
} else {
throw new Error(`Not an existing permission: ${id}.`);
}
}
if (this.rolePermissions.get(role)) {
(<Set<TPermissionId>>this.rolePermissions.get(role)).add(id);
} else {
this.rolePermissions.set(role, new Set([id]));
}
return this;
}
public addPermissionsToRole(role: TRole, permissions: TPermissionOrId[]): this {
permissions.forEach(permission => this.addPermissionToRole(role, permission));
return this;
}
public removePermissionFromRole(role: string, permissionOrId: TPermissionOrId): this {
const id = toPermissionId(permissionOrId);
if (this.rolePermissions.has(role)) {
(<Set<TPermissionId>>this.rolePermissions.get(role)).delete(id);
}
return this;
}
public addRoleToSubject(subjectOrPrincipal: TSubjectOrPrincipal, role: string): this {
const principal = toSubjectPrincipal(subjectOrPrincipal);
if (!this.subjects.has(principal)) {
if (subjectOrPrincipal instanceof Subject) {
this.createSubject(subjectOrPrincipal);
} else {
throw new Error(`Not an existing subject: ${principal}`);
}
}
if (this.subjectRoles.has(principal)) {
(<Set<TRole>>this.subjectRoles.get(principal)).add(role);
} else {
this.subjectRoles.set(principal, new Set([role]));
}
return this;
}
public removeRoleFromSubject(subjectOrPrincipal: TSubjectOrPrincipal, role: string): this {
const principal = toSubjectPrincipal(subjectOrPrincipal);
if (this.subjectRoles.has(principal)) {
(<Set<TRole>>this.subjectRoles.get(principal)).delete(role);
}
return this;
}
public getRolesForSubject(subjectOrPrincipal: TSubjectOrPrincipal): string[] {
return Array.from(this.subjectRoles.get(toSubjectPrincipal(subjectOrPrincipal)) || []);
}
public getPermissionsForRole(role: string): TPermission[] {
return (Array.from(this.rolePermissions.get(role) || [])).map(id => {
return this.permissions.get(id) as TPermission;
});
}
public getPermissionsForSubject(subject: ISubject<{}>): TPermission[] {
const roles = this.getRolesForSubject(subject);
return this.getPermissionsForRoles(roles);
}
public createSubject(subject: ISubject<{}>): this {
this.subjects.set(subject.getPrincipal(), subject);
return this;
}
public deleteSubject(subjectOrPrincipal: TSubjectOrPrincipal): this {
const principal = toSubjectPrincipal(subjectOrPrincipal);
this.subjects.delete(principal);
return this;
}
public getPermissionsForRoles(roles: string[]): TPermission[] {
return roles.reduce((acc, role) => {
return acc.concat(this.getPermissionsForRole(role));
}, [] as TPermission[]);
}
public getPermissions(): TPermission[] {
return Array.from(this.permissions.values());
}
public getPermissionById(id: TPermissionId): TPermission | undefined {
return this.permissions.get(id);
}
public getSubjects(): ISubject<{}>[] {
return Array.from(this.subjects.values());
}
public getSubjectByPrincipal(subjectPrincipal: TSubjectPrincipal): ISubject<{}> | undefined {
return this.subjects.get(subjectPrincipal);
}
}