@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@svadmin/core",
3
- "version": "0.18.0",
3
+ "version": "0.18.1",
4
4
  "author": "zuohuadong",
5
5
  "repository": {
6
6
  "type": "git",
@@ -48,13 +48,20 @@ export function createCasbinAccessControl(
48
48
  options: CasbinAdapterOptions,
49
49
  ): AccessControlProvider {
50
50
  return {
51
- can: async ({ resource, action }: CanParams): Promise<CanResult> => {
52
- const user = options.getUser();
53
- const allowed = await enforcer.enforce(user, resource, action);
54
- return {
55
- can: allowed,
56
- reason: allowed ? undefined : `User "${user}" cannot "${action}" on "${resource}"`,
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
  };
@@ -38,13 +38,22 @@ export function createCaslAccessControl(
38
38
  options?: AccessControlProvider['options'],
39
39
  ): AccessControlProvider {
40
40
  return {
41
- can: async ({ resource, action, params }: CanParams): Promise<CanResult> => {
42
- const field = params?.field as string | undefined;
43
- const allowed = ability.can(action, resource, field);
44
- return {
45
- can: allowed,
46
- reason: allowed ? undefined : `Cannot "${action}" on "${resource}"${field ? ` (field: ${field})` : ''}`,
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
  };