@svadmin/core 0.18.0 → 0.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/adapters/casbin.ts +13 -6
- package/src/adapters/casl.ts +15 -6
package/package.json
CHANGED
package/src/adapters/casbin.ts
CHANGED
|
@@ -48,13 +48,20 @@ export function createCasbinAccessControl(
|
|
|
48
48
|
options: CasbinAdapterOptions,
|
|
49
49
|
): AccessControlProvider {
|
|
50
50
|
return {
|
|
51
|
-
can: async (
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
can: async (params: CanParams | CanParams[]): Promise<CanResult | CanResult[]> => {
|
|
52
|
+
const executeCheck = async ({ resource, action }: CanParams) => {
|
|
53
|
+
const user = options.getUser();
|
|
54
|
+
const allowed = await enforcer.enforce(user, resource, action);
|
|
55
|
+
return {
|
|
56
|
+
can: allowed,
|
|
57
|
+
reason: allowed ? undefined : `User "${user}" cannot "${action}" on "${resource}"`,
|
|
58
|
+
};
|
|
57
59
|
};
|
|
60
|
+
|
|
61
|
+
if (Array.isArray(params)) {
|
|
62
|
+
return Promise.all(params.map(p => executeCheck(p)));
|
|
63
|
+
}
|
|
64
|
+
return executeCheck(params);
|
|
58
65
|
},
|
|
59
66
|
options: options.providerOptions,
|
|
60
67
|
};
|
package/src/adapters/casl.ts
CHANGED
|
@@ -38,13 +38,22 @@ export function createCaslAccessControl(
|
|
|
38
38
|
options?: AccessControlProvider['options'],
|
|
39
39
|
): AccessControlProvider {
|
|
40
40
|
return {
|
|
41
|
-
can: async (
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
can: async (params: CanParams | CanParams[]): Promise<CanResult | CanResult[]> => {
|
|
42
|
+
const executeCheck = async ({ resource, action, params: actionParams }: CanParams) => {
|
|
43
|
+
const field = actionParams?.field as string | undefined;
|
|
44
|
+
// CaslAbility's can only accepts string for subject based on its internal definition in this file
|
|
45
|
+
// To support object subjects when CanParams might pass them, cast it to any here to satisfy the local interface
|
|
46
|
+
const allowed = ability.can(action as string, resource as any, field);
|
|
47
|
+
return {
|
|
48
|
+
can: allowed,
|
|
49
|
+
reason: allowed ? undefined : `Cannot "${action}" on "${resource}"${field ? ` (field: ${field})` : ''}`,
|
|
50
|
+
};
|
|
47
51
|
};
|
|
52
|
+
|
|
53
|
+
if (Array.isArray(params)) {
|
|
54
|
+
return Promise.all(params.map(p => executeCheck(p)));
|
|
55
|
+
}
|
|
56
|
+
return executeCheck(params);
|
|
48
57
|
},
|
|
49
58
|
options,
|
|
50
59
|
};
|