-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathuser-permissions.provider.ts
34 lines (31 loc) · 1.14 KB
/
user-permissions.provider.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
import {Provider} from '@loopback/context';
import {UserPermission, UserPermissionsFn} from '../types';
export class UserPermissionsProvider
implements Provider<UserPermissionsFn<string>>
{
value(): UserPermissionsFn<string> {
return (userPermissions, rolePermissions) =>
this.action(userPermissions, rolePermissions);
}
action(
userPermissions: UserPermission<string>[],
rolePermissions: string[],
): string[] {
let perms: string[] = [];
// First add all permissions associated with role
perms = perms.concat(rolePermissions);
// Now update permissions based on user permissions
userPermissions.forEach((userPerm: UserPermission<string>) => {
if (userPerm.allowed && perms.indexOf(userPerm.permission) < 0) {
// Add permission if it is not part of role but allowed to user
perms.push(userPerm.permission);
} else if (!userPerm.allowed && perms.indexOf(userPerm.permission) >= 0) {
// Remove permission if it is disallowed for user
perms.splice(perms.indexOf(userPerm.permission), 1);
} else {
//this is intentional
}
});
return perms;
}
}