-
Notifications
You must be signed in to change notification settings - Fork 0
/
vanilla.ts
114 lines (104 loc) · 3.31 KB
/
vanilla.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
import { CAuthorizer } from "../src";
import { getPermissionsFromBackend } from "./configs";
const runExample = async () => {
const permissions = await getPermissionsFromBackend();
// ! If you will not init authorizer, the methods like "can" will throw error
// You can check does authorizer initialized with "isInited" method
const authorizer = new CAuthorizer();
await authorizer.init(permissions.m, permissions.p);
const allSubjects = await authorizer.getEnforcer()?.getAllSubjects();
const allActinos = await authorizer.getEnforcer()?.getAllActions();
const allObjects = await authorizer.getEnforcer()?.getAllObjects();
const allRoles = await authorizer.getEnforcer()?.getAllRoles();
console.log("- inited authorizer data:");
console.log("all subjects (sub):", allSubjects);
console.log("all actions (act):", allActinos);
console.log("all objects (obj):", allObjects);
console.log("all roles (RBAC):", allRoles);
console.log();
console.log("- check permissions with authorizer.can");
console.log(
"fish fly air?".padEnd(30),
await authorizer.can(["fish", "fly", "air"])
);
console.log(
"fish walk ground?".padEnd(30),
await authorizer.can(["fish", "swim", "ground"])
);
console.log(
"fish swim water?".padEnd(30),
await authorizer.can(["fish", "swim", "water"])
);
console.log(
"cat swim water?".padEnd(30),
await authorizer.can(["cat", "swim", "water"])
);
console.log(
"bird run ground?".padEnd(30),
await authorizer.can(["bird", "run", "ground"])
);
console.log(
"cat run ground?".padEnd(30),
await authorizer.can(["cat", "run", "ground"])
);
console.log();
console.log("- check permissions with authorizer.canAll");
console.log(
"cat AND fish breathe air?".padEnd(30),
await authorizer.canAll([
["cat", "breathe", "air"],
["fish", "breathe", "air"],
])
);
console.log(
"cat AND bird breathe air?".padEnd(30),
await authorizer.canAll([
["cat", "breathe", "air"],
["bird", "breathe", "air"],
])
);
console.log();
console.log("- check permissions with authorizer.canAny");
console.log(
"cat OR fish breathe air?".padEnd(30),
await authorizer.canAny([
["cat", "breathe", "air"],
["fish", "breathe", "air"],
])
);
console.log(
"cat OR fish fly air?".padEnd(30),
await authorizer.canAny([
["cat", "fly", "air"],
["fish", "fly", "air"],
])
);
console.log();
// ! The order of your request elements must follow the rules which you set in `model`
// ? See more: https://casbin.org/docs/syntax-for-models#request-definition
console.log("- the order of requests are important");
console.log(
"invalid request example".padEnd(30),
await authorizer.can(["fish", "water", "swim"])
);
console.log(
"invalid request example".padEnd(30),
await authorizer.can(["water", "swim", "fish"])
);
console.log(
"correct request example".padEnd(30),
await authorizer.can(["fish", "swim", "water"])
);
console.log();
console.log("- Use filterByCan to filter allowed list of rvals");
const listRvals = [
["cat", "breathe", "air"],
["fish", "breathe", "air"],
];
console.log("listRvals:", listRvals);
console.log(
"listRvals filterByCan:",
await authorizer.filterByCan(listRvals)
);
};
runExample();