-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasbin-data.ts
More file actions
39 lines (32 loc) · 1.09 KB
/
casbin-data.ts
File metadata and controls
39 lines (32 loc) · 1.09 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
import {Enforcer} from "casbin";
import { Sequelize, Model, DataTypes } from 'sequelize';
class ErrObjCondition extends Error {
constructor() {
super("Need to meet the prefix required by the object condition");
this.name = "ErrObjCondition";
}
}
class ErrEmptyCondition extends Error {
constructor() {
super("GetAllowedObjectConditions have an empty condition");
this.name = "ErrEmptyCondition";
}
}
async function getAllowedObjectConditions(enforcer: Enforcer, user: string, action: string, prefix: string): Promise<string[]> {
const permissions = await enforcer.getImplicitPermissionsForUser(user);
const objectConditions: string[] = [];
for (const policy of permissions) {
// policy [sub, obj, act]
if (policy[2] === action) {
if (!policy[1].startsWith(prefix)) {
throw new ErrObjCondition();
}
objectConditions.push(policy[1].substring(prefix.length));
}
}
if (objectConditions.length === 0) {
throw new ErrEmptyCondition();
}
return objectConditions;
}
export { getAllowedObjectConditions, ErrObjCondition, ErrEmptyCondition };