@nocobase/plugin-acl 0.11.1-alpha.5 → 0.12.0-alpha.2
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/client.d.ts +2 -3
- package/client.js +1 -1
- package/dist/client/index.js +11 -0
- package/{lib/server → dist}/index.d.ts +1 -0
- package/dist/index.js +18 -0
- package/{src/server/actions/available-actions.ts → dist/server/actions/available-actions.js} +7 -5
- package/{src/server/actions/role-check.ts → dist/server/actions/role-check.js} +17 -22
- package/dist/server/actions/role-collections.js +53 -0
- package/dist/server/actions/user-setDefaultRole.js +43 -0
- package/dist/server/collections/roles-users.js +10 -0
- package/dist/server/collections/roles.js +103 -0
- package/dist/server/collections/rolesResources.js +35 -0
- package/dist/server/collections/rolesResourcesActions.js +33 -0
- package/dist/server/collections/rolesResourcesScopes.js +27 -0
- package/dist/server/collections/users.js +35 -0
- package/dist/server/index.js +11 -0
- package/dist/server/middlewares/setCurrentRole.js +31 -0
- package/dist/server/migrations/20221214072638-set-role-snippets.js +25 -0
- package/dist/server/model/RoleModel.js +23 -0
- package/dist/server/model/RoleResourceActionModel.js +64 -0
- package/dist/server/model/RoleResourceModel.js +55 -0
- package/dist/server/server.js +709 -0
- package/package.json +12 -21
- package/server.d.ts +3 -3
- package/server.js +1 -1
- package/lib/client/index.js +0 -22
- package/lib/index.js +0 -13
- package/lib/server/actions/available-actions.js +0 -34
- package/lib/server/actions/role-check.js +0 -77
- package/lib/server/actions/role-collections.js +0 -98
- package/lib/server/actions/user-setDefaultRole.js +0 -52
- package/lib/server/collections/roles-users.js +0 -16
- package/lib/server/collections/roles.js +0 -92
- package/lib/server/collections/rolesResources.js +0 -31
- package/lib/server/collections/rolesResourcesActions.js +0 -31
- package/lib/server/collections/rolesResourcesScopes.js +0 -25
- package/lib/server/collections/users.js +0 -41
- package/lib/server/index.js +0 -13
- package/lib/server/middlewares/setCurrentRole.js +0 -45
- package/lib/server/migrations/20221214072638-set-role-snippets.js +0 -43
- package/lib/server/model/RoleModel.js +0 -35
- package/lib/server/model/RoleResourceActionModel.js +0 -91
- package/lib/server/model/RoleResourceModel.js +0 -106
- package/lib/server/server.js +0 -947
- package/src/client/index.ts +0 -8
- package/src/index.ts +0 -1
- package/src/server/__tests__/acl.test.ts +0 -835
- package/src/server/__tests__/actions.test.ts +0 -141
- package/src/server/__tests__/association-field.test.ts +0 -413
- package/src/server/__tests__/configuration.test.ts +0 -70
- package/src/server/__tests__/list-action.test.ts +0 -446
- package/src/server/__tests__/middleware.test.ts +0 -210
- package/src/server/__tests__/own.test.ts +0 -124
- package/src/server/__tests__/prepare.ts +0 -20
- package/src/server/__tests__/role-check.test.ts +0 -46
- package/src/server/__tests__/role-resource.test.ts +0 -177
- package/src/server/__tests__/role-user.test.ts +0 -127
- package/src/server/__tests__/role.test.ts +0 -118
- package/src/server/__tests__/scope.test.ts +0 -55
- package/src/server/__tests__/setCurrentRole.test.ts +0 -86
- package/src/server/__tests__/snippets.test.ts +0 -35
- package/src/server/__tests__/users.test.ts +0 -136
- package/src/server/__tests__/write-role-to-acl.test.ts +0 -41
- package/src/server/actions/role-collections.ts +0 -95
- package/src/server/actions/user-setDefaultRole.ts +0 -47
- package/src/server/collections/roles-users.ts +0 -8
- package/src/server/collections/roles.ts +0 -101
- package/src/server/collections/rolesResources.ts +0 -33
- package/src/server/collections/rolesResourcesActions.ts +0 -31
- package/src/server/collections/rolesResourcesScopes.ts +0 -25
- package/src/server/collections/users.ts +0 -31
- package/src/server/index.ts +0 -1
- package/src/server/middlewares/setCurrentRole.ts +0 -35
- package/src/server/migrations/20221214072638-set-role-snippets.ts +0 -23
- package/src/server/model/RoleModel.ts +0 -23
- package/src/server/model/RoleResourceActionModel.ts +0 -95
- package/src/server/model/RoleResourceModel.ts +0 -74
- package/src/server/server.ts +0 -854
- /package/{lib → dist}/client/index.d.ts +0 -0
- /package/{lib → dist}/server/actions/available-actions.d.ts +0 -0
- /package/{lib → dist}/server/actions/role-check.d.ts +0 -0
- /package/{lib → dist}/server/actions/role-collections.d.ts +0 -0
- /package/{lib → dist}/server/actions/user-setDefaultRole.d.ts +0 -0
- /package/{lib → dist}/server/collections/roles-users.d.ts +0 -0
- /package/{lib → dist}/server/collections/roles.d.ts +0 -0
- /package/{lib → dist}/server/collections/rolesResources.d.ts +0 -0
- /package/{lib → dist}/server/collections/rolesResourcesActions.d.ts +0 -0
- /package/{lib → dist}/server/collections/rolesResourcesScopes.d.ts +0 -0
- /package/{lib → dist}/server/collections/users.d.ts +0 -0
- /package/{lib → dist/server}/index.d.ts +0 -0
- /package/{lib → dist}/server/middlewares/setCurrentRole.d.ts +0 -0
- /package/{lib → dist}/server/migrations/20221214072638-set-role-snippets.d.ts +0 -0
- /package/{lib → dist}/server/model/RoleModel.d.ts +0 -0
- /package/{lib → dist}/server/model/RoleResourceActionModel.d.ts +0 -0
- /package/{lib → dist}/server/model/RoleResourceModel.d.ts +0 -0
- /package/{lib → dist}/server/server.d.ts +0 -0
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { Context } from '@nocobase/actions';
|
|
2
|
-
import { Repository } from '@nocobase/database';
|
|
3
|
-
|
|
4
|
-
export async function setCurrentRole(ctx: Context, next) {
|
|
5
|
-
const currentRole = ctx.get('X-Role');
|
|
6
|
-
|
|
7
|
-
if (currentRole === 'anonymous') {
|
|
8
|
-
ctx.state.currentRole = currentRole;
|
|
9
|
-
return next();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (!ctx.state.currentUser) {
|
|
13
|
-
return next();
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const repository = ctx.db.getRepository('users.roles', ctx.state.currentUser.id) as unknown as Repository;
|
|
17
|
-
const roles = await repository.find();
|
|
18
|
-
ctx.state.currentUser.setDataValue('roles', roles);
|
|
19
|
-
|
|
20
|
-
// 1. If the X-Role is set, use the specified role
|
|
21
|
-
if (currentRole) {
|
|
22
|
-
ctx.state.currentRole = roles.find((role) => role.name === currentRole)?.name;
|
|
23
|
-
}
|
|
24
|
-
// 2. If the X-Role is not set, use the default role
|
|
25
|
-
else {
|
|
26
|
-
const defaultRole = roles.find((item) => item?.rolesUsers?.default);
|
|
27
|
-
ctx.state.currentRole = (defaultRole || roles[0])?.name;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (!ctx.state.currentRole) {
|
|
31
|
-
return ctx.throw(401, 'User role not found');
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
await next();
|
|
35
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { Migration } from '@nocobase/server';
|
|
2
|
-
|
|
3
|
-
export default class extends Migration {
|
|
4
|
-
async up() {
|
|
5
|
-
const result = await this.app.version.satisfies('<0.9.3-alpha.1');
|
|
6
|
-
|
|
7
|
-
if (!result) {
|
|
8
|
-
return;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
await this.app.db.getRepository('roles').update({
|
|
12
|
-
filter: {
|
|
13
|
-
$or: [{ allowConfigure: true }, { name: 'root' }],
|
|
14
|
-
},
|
|
15
|
-
values: {
|
|
16
|
-
snippets: ['ui.*', 'pm', 'pm.*'],
|
|
17
|
-
allowConfigure: false,
|
|
18
|
-
},
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async down() {}
|
|
23
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { Model } from '@nocobase/database';
|
|
2
|
-
import { ACL } from '@nocobase/acl';
|
|
3
|
-
|
|
4
|
-
export class RoleModel extends Model {
|
|
5
|
-
writeToAcl(options: { acl: ACL }) {
|
|
6
|
-
const { acl } = options;
|
|
7
|
-
const roleName = this.get('name') as string;
|
|
8
|
-
let role = acl.getRole(roleName);
|
|
9
|
-
|
|
10
|
-
if (!role) {
|
|
11
|
-
role = acl.define({
|
|
12
|
-
role: roleName,
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
role.setStrategy({
|
|
17
|
-
...((this.get('strategy') as object) || {}),
|
|
18
|
-
allowConfigure: this.get('allowConfigure') as boolean,
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
role.snippets = new Set(this.get('snippets'));
|
|
22
|
-
}
|
|
23
|
-
}
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { ACL, ACLRole } from '@nocobase/acl';
|
|
2
|
-
import { Database, Model } from '@nocobase/database';
|
|
3
|
-
import { AssociationFieldAction, AssociationFieldsActions, GrantHelper } from '../server';
|
|
4
|
-
|
|
5
|
-
export class RoleResourceActionModel extends Model {
|
|
6
|
-
async writeToACL(options: {
|
|
7
|
-
acl: ACL;
|
|
8
|
-
role: ACLRole;
|
|
9
|
-
resourceName: string;
|
|
10
|
-
associationFieldsActions: AssociationFieldsActions;
|
|
11
|
-
grantHelper: GrantHelper;
|
|
12
|
-
}) {
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
const db: Database = this.constructor.database;
|
|
15
|
-
|
|
16
|
-
const { resourceName, role, acl, associationFieldsActions, grantHelper } = options;
|
|
17
|
-
|
|
18
|
-
const actionName = this.get('name') as string;
|
|
19
|
-
|
|
20
|
-
const fields = this.get('fields') as any;
|
|
21
|
-
|
|
22
|
-
const actionPath = `${resourceName}:${actionName}`;
|
|
23
|
-
const actionParams = {
|
|
24
|
-
fields,
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
// @ts-ignore
|
|
28
|
-
const scope = await this.getScope();
|
|
29
|
-
|
|
30
|
-
if (scope) {
|
|
31
|
-
actionParams['own'] = scope.get('key') === 'own';
|
|
32
|
-
actionParams['filter'] = scope.get('scope');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
role.grantAction(actionPath, actionParams);
|
|
36
|
-
|
|
37
|
-
const collection = db.getCollection(resourceName);
|
|
38
|
-
|
|
39
|
-
if (!collection) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const availableAction = acl.resolveActionAlias(actionName);
|
|
44
|
-
|
|
45
|
-
for (const field of fields) {
|
|
46
|
-
const collectionField = collection.getField(field);
|
|
47
|
-
|
|
48
|
-
if (!collectionField) {
|
|
49
|
-
console.log(`field ${field} does not exist at ${collection.name}`);
|
|
50
|
-
continue;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const fieldType = collectionField.get('type') as string;
|
|
54
|
-
|
|
55
|
-
const fieldActions: AssociationFieldAction = associationFieldsActions?.[fieldType]?.[availableAction];
|
|
56
|
-
|
|
57
|
-
const fieldTarget = collectionField.get('target');
|
|
58
|
-
|
|
59
|
-
if (fieldActions) {
|
|
60
|
-
// grant association actions to role
|
|
61
|
-
const associationActions = fieldActions.associationActions || [];
|
|
62
|
-
|
|
63
|
-
associationActions.forEach((associationAction) => {
|
|
64
|
-
const actionName = `${resourceName}.${collectionField.get('name')}:${associationAction}`;
|
|
65
|
-
role.grantAction(actionName);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
const targetActions = fieldActions.targetActions || [];
|
|
69
|
-
|
|
70
|
-
targetActions.forEach((targetAction) => {
|
|
71
|
-
const targetActionPath = `${fieldTarget}:${targetAction}`;
|
|
72
|
-
|
|
73
|
-
const existsAction = role.getActionParams(targetActionPath);
|
|
74
|
-
|
|
75
|
-
if (existsAction) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// set resource target action with current resourceName
|
|
80
|
-
grantHelper.resourceTargetActionMap.set(`${role.name}.${resourceName}`, [
|
|
81
|
-
...(grantHelper.resourceTargetActionMap.get(resourceName) || []),
|
|
82
|
-
targetActionPath,
|
|
83
|
-
]);
|
|
84
|
-
|
|
85
|
-
grantHelper.targetActionResourceMap.set(targetActionPath, [
|
|
86
|
-
...(grantHelper.targetActionResourceMap.get(targetActionPath) || []),
|
|
87
|
-
`${role.name}.${resourceName}`,
|
|
88
|
-
]);
|
|
89
|
-
|
|
90
|
-
role.grantAction(targetActionPath);
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { ACL, ACLResource, ACLRole } from '@nocobase/acl';
|
|
2
|
-
import { Model } from '@nocobase/database';
|
|
3
|
-
import { AssociationFieldsActions, GrantHelper } from '../server';
|
|
4
|
-
import { RoleResourceActionModel } from './RoleResourceActionModel';
|
|
5
|
-
|
|
6
|
-
export class RoleResourceModel extends Model {
|
|
7
|
-
async revoke(options: { role: ACLRole; resourceName: string; grantHelper: GrantHelper }) {
|
|
8
|
-
const { role, resourceName, grantHelper } = options;
|
|
9
|
-
role.revokeResource(resourceName);
|
|
10
|
-
|
|
11
|
-
const targetActions = grantHelper.resourceTargetActionMap.get(`${role.name}.${resourceName}`) || [];
|
|
12
|
-
|
|
13
|
-
for (const targetAction of targetActions) {
|
|
14
|
-
const targetActionResource = (grantHelper.targetActionResourceMap.get(targetAction) || []).filter(
|
|
15
|
-
(item) => `${role.name}.${resourceName}` !== item,
|
|
16
|
-
);
|
|
17
|
-
|
|
18
|
-
grantHelper.targetActionResourceMap.set(targetAction, targetActionResource);
|
|
19
|
-
|
|
20
|
-
if (targetActionResource.length == 0) {
|
|
21
|
-
role.revokeAction(targetAction);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
grantHelper.resourceTargetActionMap.set(`${role.name}.${resourceName}`, []);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async writeToACL(options: {
|
|
29
|
-
acl: ACL;
|
|
30
|
-
associationFieldsActions: AssociationFieldsActions;
|
|
31
|
-
grantHelper: GrantHelper;
|
|
32
|
-
transaction: any;
|
|
33
|
-
}) {
|
|
34
|
-
const { acl, associationFieldsActions, grantHelper } = options;
|
|
35
|
-
const resourceName = this.get('name') as string;
|
|
36
|
-
const roleName = this.get('roleName') as string;
|
|
37
|
-
const role = acl.getRole(roleName);
|
|
38
|
-
|
|
39
|
-
if (!role) {
|
|
40
|
-
console.log(`${roleName} role does not exist`);
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// revoke resource of role
|
|
45
|
-
await this.revoke({ role, resourceName, grantHelper });
|
|
46
|
-
|
|
47
|
-
// @ts-ignore
|
|
48
|
-
if (this.usingActionsConfig === false) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const resource = new ACLResource({
|
|
53
|
-
role,
|
|
54
|
-
name: resourceName,
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
role.resources.set(resourceName, resource);
|
|
58
|
-
|
|
59
|
-
// @ts-ignore
|
|
60
|
-
const actions: RoleResourceActionModel[] = await this.getActions({
|
|
61
|
-
transaction: options.transaction,
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
for (const action of actions) {
|
|
65
|
-
await action.writeToACL({
|
|
66
|
-
acl,
|
|
67
|
-
role,
|
|
68
|
-
resourceName,
|
|
69
|
-
associationFieldsActions,
|
|
70
|
-
grantHelper: options.grantHelper,
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|