@nocobase/plugin-acl 0.7.0-alpha.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/LICENSE +201 -0
- package/esm/actions/available-actions.d.ts +7 -0
- package/esm/actions/available-actions.js +26 -0
- package/esm/actions/available-actions.js.map +1 -0
- package/esm/actions/role-check.d.ts +1 -0
- package/esm/actions/role-check.js +38 -0
- package/esm/actions/role-check.js.map +1 -0
- package/esm/actions/role-collections.d.ts +7 -0
- package/esm/actions/role-collections.js +54 -0
- package/esm/actions/role-collections.js.map +1 -0
- package/esm/collections/roles.d.ts +3 -0
- package/esm/collections/roles.js +78 -0
- package/esm/collections/roles.js.map +1 -0
- package/esm/collections/rolesResources.d.ts +3 -0
- package/esm/collections/rolesResources.js +30 -0
- package/esm/collections/rolesResources.js.map +1 -0
- package/esm/collections/rolesResourcesActions.d.ts +3 -0
- package/esm/collections/rolesResourcesActions.js +27 -0
- package/esm/collections/rolesResourcesActions.js.map +1 -0
- package/esm/collections/rolesResourcesScopes.d.ts +3 -0
- package/esm/collections/rolesResourcesScopes.js +22 -0
- package/esm/collections/rolesResourcesScopes.js.map +1 -0
- package/esm/index.d.ts +1 -0
- package/esm/index.js +2 -0
- package/esm/index.js.map +1 -0
- package/esm/model/RoleModel.d.ts +7 -0
- package/esm/model/RoleModel.js +15 -0
- package/esm/model/RoleModel.js.map +1 -0
- package/esm/model/RoleResourceActionModel.d.ts +12 -0
- package/esm/model/RoleResourceActionModel.js +65 -0
- package/esm/model/RoleResourceActionModel.js.map +1 -0
- package/esm/model/RoleResourceModel.d.ts +16 -0
- package/esm/model/RoleResourceModel.js +55 -0
- package/esm/model/RoleResourceModel.js.map +1 -0
- package/esm/server.d.ts +33 -0
- package/esm/server.js +366 -0
- package/esm/server.js.map +1 -0
- package/lib/actions/available-actions.d.ts +7 -0
- package/lib/actions/available-actions.js +29 -0
- package/lib/actions/available-actions.js.map +1 -0
- package/lib/actions/role-check.d.ts +1 -0
- package/lib/actions/role-check.js +42 -0
- package/lib/actions/role-check.js.map +1 -0
- package/lib/actions/role-collections.d.ts +7 -0
- package/lib/actions/role-collections.js +57 -0
- package/lib/actions/role-collections.js.map +1 -0
- package/lib/collections/roles.d.ts +3 -0
- package/lib/collections/roles.js +80 -0
- package/lib/collections/roles.js.map +1 -0
- package/lib/collections/rolesResources.d.ts +3 -0
- package/lib/collections/rolesResources.js +32 -0
- package/lib/collections/rolesResources.js.map +1 -0
- package/lib/collections/rolesResourcesActions.d.ts +3 -0
- package/lib/collections/rolesResourcesActions.js +29 -0
- package/lib/collections/rolesResourcesActions.js.map +1 -0
- package/lib/collections/rolesResourcesScopes.d.ts +3 -0
- package/lib/collections/rolesResourcesScopes.js +24 -0
- package/lib/collections/rolesResourcesScopes.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +9 -0
- package/lib/index.js.map +1 -0
- package/lib/model/RoleModel.d.ts +7 -0
- package/lib/model/RoleModel.js +19 -0
- package/lib/model/RoleModel.js.map +1 -0
- package/lib/model/RoleResourceActionModel.d.ts +12 -0
- package/lib/model/RoleResourceActionModel.js +69 -0
- package/lib/model/RoleResourceActionModel.js.map +1 -0
- package/lib/model/RoleResourceModel.d.ts +16 -0
- package/lib/model/RoleResourceModel.js +59 -0
- package/lib/model/RoleResourceModel.js.map +1 -0
- package/lib/server.d.ts +33 -0
- package/lib/server.js +371 -0
- package/lib/server.js.map +1 -0
- package/package.json +30 -0
- package/src/__tests__/acl.test.ts +533 -0
- package/src/__tests__/association-field.test.ts +297 -0
- package/src/__tests__/configuration.test.ts +45 -0
- package/src/__tests__/middleware.test.ts +233 -0
- package/src/__tests__/own.test.ts +140 -0
- package/src/__tests__/prepare.ts +39 -0
- package/src/__tests__/role-check.test.ts +35 -0
- package/src/__tests__/role-resource.test.ts +187 -0
- package/src/__tests__/role.test.ts +92 -0
- package/src/__tests__/scope.test.ts +51 -0
- package/src/actions/available-actions.ts +18 -0
- package/src/actions/role-check.ts +39 -0
- package/src/actions/role-collections.ts +55 -0
- package/src/collections/roles.ts +79 -0
- package/src/collections/rolesResources.ts +31 -0
- package/src/collections/rolesResourcesActions.ts +28 -0
- package/src/collections/rolesResourcesScopes.ts +23 -0
- package/src/index.ts +2 -0
- package/src/model/RoleModel.ts +21 -0
- package/src/model/RoleResourceActionModel.ts +80 -0
- package/src/model/RoleResourceModel.ts +61 -0
- package/src/server.ts +398 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +5 -0
package/lib/server.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Plugin } from '@nocobase/server';
|
|
2
|
+
import { RoleResourceActionModel } from './model/RoleResourceActionModel';
|
|
3
|
+
import { RoleResourceModel } from './model/RoleResourceModel';
|
|
4
|
+
export interface AssociationFieldAction {
|
|
5
|
+
associationActions: string[];
|
|
6
|
+
targetActions?: string[];
|
|
7
|
+
}
|
|
8
|
+
interface AssociationFieldActions {
|
|
9
|
+
[availableActionName: string]: AssociationFieldAction;
|
|
10
|
+
}
|
|
11
|
+
export interface AssociationFieldsActions {
|
|
12
|
+
[associationType: string]: AssociationFieldActions;
|
|
13
|
+
}
|
|
14
|
+
export declare class GrantHelper {
|
|
15
|
+
resourceTargetActionMap: Map<string, string[]>;
|
|
16
|
+
targetActionResourceMap: Map<string, string[]>;
|
|
17
|
+
constructor();
|
|
18
|
+
}
|
|
19
|
+
export declare class PluginACL extends Plugin {
|
|
20
|
+
associationFieldsActions: AssociationFieldsActions;
|
|
21
|
+
grantHelper: GrantHelper;
|
|
22
|
+
get acl(): import("@nocobase/acl").ACL;
|
|
23
|
+
registerAssociationFieldAction(associationType: string, value: AssociationFieldActions): void;
|
|
24
|
+
registerAssociationFieldsActions(): void;
|
|
25
|
+
writeResourceToACL(resourceModel: RoleResourceModel, transaction: any): Promise<void>;
|
|
26
|
+
writeActionToACL(actionModel: RoleResourceActionModel, transaction: any): Promise<void>;
|
|
27
|
+
writeRolesToACL(): Promise<void>;
|
|
28
|
+
beforeLoad(): Promise<void>;
|
|
29
|
+
install(): Promise<void>;
|
|
30
|
+
load(): Promise<void>;
|
|
31
|
+
getName(): string;
|
|
32
|
+
}
|
|
33
|
+
export default PluginACL;
|
package/lib/server.js
ADDED
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.PluginACL = exports.GrantHelper = void 0;
|
|
13
|
+
const server_1 = require("@nocobase/server");
|
|
14
|
+
const path_1 = require("path");
|
|
15
|
+
const available_actions_1 = require("./actions/available-actions");
|
|
16
|
+
const role_check_1 = require("./actions/role-check");
|
|
17
|
+
const role_collections_1 = require("./actions/role-collections");
|
|
18
|
+
const RoleModel_1 = require("./model/RoleModel");
|
|
19
|
+
const RoleResourceActionModel_1 = require("./model/RoleResourceActionModel");
|
|
20
|
+
const RoleResourceModel_1 = require("./model/RoleResourceModel");
|
|
21
|
+
class GrantHelper {
|
|
22
|
+
constructor() {
|
|
23
|
+
this.resourceTargetActionMap = new Map();
|
|
24
|
+
this.targetActionResourceMap = new Map();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.GrantHelper = GrantHelper;
|
|
28
|
+
class PluginACL extends server_1.Plugin {
|
|
29
|
+
constructor() {
|
|
30
|
+
super(...arguments);
|
|
31
|
+
this.associationFieldsActions = {};
|
|
32
|
+
this.grantHelper = new GrantHelper();
|
|
33
|
+
}
|
|
34
|
+
get acl() {
|
|
35
|
+
return this.app.acl;
|
|
36
|
+
}
|
|
37
|
+
registerAssociationFieldAction(associationType, value) {
|
|
38
|
+
this.associationFieldsActions[associationType] = value;
|
|
39
|
+
}
|
|
40
|
+
registerAssociationFieldsActions() {
|
|
41
|
+
this.registerAssociationFieldAction('linkTo', {
|
|
42
|
+
view: {
|
|
43
|
+
associationActions: ['list', 'get'],
|
|
44
|
+
},
|
|
45
|
+
create: {
|
|
46
|
+
associationActions: ['add'],
|
|
47
|
+
targetActions: ['view'],
|
|
48
|
+
},
|
|
49
|
+
update: {
|
|
50
|
+
associationActions: ['add', 'remove', 'toggle'],
|
|
51
|
+
targetActions: ['view'],
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
this.registerAssociationFieldAction('attachments', {
|
|
55
|
+
view: {
|
|
56
|
+
associationActions: ['list', 'get'],
|
|
57
|
+
},
|
|
58
|
+
add: {
|
|
59
|
+
associationActions: ['upload', 'add'],
|
|
60
|
+
},
|
|
61
|
+
update: {
|
|
62
|
+
associationActions: ['update', 'add', 'remove', 'toggle'],
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
this.registerAssociationFieldAction('subTable', {
|
|
66
|
+
view: {
|
|
67
|
+
associationActions: ['list', 'get'],
|
|
68
|
+
},
|
|
69
|
+
create: {
|
|
70
|
+
associationActions: ['create'],
|
|
71
|
+
},
|
|
72
|
+
update: {
|
|
73
|
+
associationActions: ['update', 'destroy'],
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
writeResourceToACL(resourceModel, transaction) {
|
|
78
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
yield resourceModel.writeToACL({
|
|
80
|
+
acl: this.acl,
|
|
81
|
+
associationFieldsActions: this.associationFieldsActions,
|
|
82
|
+
transaction: transaction,
|
|
83
|
+
grantHelper: this.grantHelper,
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
writeActionToACL(actionModel, transaction) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
const resource = actionModel.get('resource');
|
|
90
|
+
const role = this.acl.getRole(resource.get('roleName'));
|
|
91
|
+
yield actionModel.writeToACL({
|
|
92
|
+
acl: this.acl,
|
|
93
|
+
role,
|
|
94
|
+
resourceName: resource.get('name'),
|
|
95
|
+
associationFieldsActions: this.associationFieldsActions,
|
|
96
|
+
grantHelper: this.grantHelper,
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
writeRolesToACL() {
|
|
101
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
const roles = (yield this.app.db.getRepository('roles').find({
|
|
103
|
+
appends: ['resources', 'resources.actions'],
|
|
104
|
+
}));
|
|
105
|
+
for (const role of roles) {
|
|
106
|
+
role.writeToAcl({ acl: this.acl });
|
|
107
|
+
for (const resource of role.get('resources')) {
|
|
108
|
+
yield this.writeResourceToACL(resource, null);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
beforeLoad() {
|
|
114
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
this.app.db.registerModels({
|
|
116
|
+
RoleResourceActionModel: RoleResourceActionModel_1.RoleResourceActionModel,
|
|
117
|
+
RoleResourceModel: RoleResourceModel_1.RoleResourceModel,
|
|
118
|
+
RoleModel: RoleModel_1.RoleModel,
|
|
119
|
+
});
|
|
120
|
+
this.registerAssociationFieldsActions();
|
|
121
|
+
this.app.resourcer.define(available_actions_1.availableActionResource);
|
|
122
|
+
this.app.resourcer.define(role_collections_1.roleCollectionsResource);
|
|
123
|
+
this.app.resourcer.registerActionHandler('roles:check', role_check_1.checkAction);
|
|
124
|
+
this.app.db.on('roles.afterSaveWithAssociations', (model, options) => __awaiter(this, void 0, void 0, function* () {
|
|
125
|
+
const { transaction } = options;
|
|
126
|
+
model.writeToAcl({
|
|
127
|
+
acl: this.acl,
|
|
128
|
+
});
|
|
129
|
+
for (const resource of (yield model.getResources({ transaction }))) {
|
|
130
|
+
yield this.writeResourceToACL(resource, transaction);
|
|
131
|
+
}
|
|
132
|
+
// model is default
|
|
133
|
+
if (model.get('default')) {
|
|
134
|
+
yield this.app.db.getRepository('roles').update({
|
|
135
|
+
values: {
|
|
136
|
+
default: false,
|
|
137
|
+
},
|
|
138
|
+
filter: {
|
|
139
|
+
'name.$ne': model.get('name'),
|
|
140
|
+
},
|
|
141
|
+
hooks: false,
|
|
142
|
+
transaction,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}));
|
|
146
|
+
this.app.db.on('roles.afterDestroy', (model) => {
|
|
147
|
+
const roleName = model.get('name');
|
|
148
|
+
this.acl.removeRole(roleName);
|
|
149
|
+
});
|
|
150
|
+
this.app.db.on('rolesResources.afterSaveWithAssociations', (model, options) => __awaiter(this, void 0, void 0, function* () {
|
|
151
|
+
yield this.writeResourceToACL(model, options.transaction);
|
|
152
|
+
}));
|
|
153
|
+
this.app.db.on('rolesResourcesActions.afterUpdateWithAssociations', (model, options) => __awaiter(this, void 0, void 0, function* () {
|
|
154
|
+
const { transaction } = options;
|
|
155
|
+
const resource = yield model.getResource({
|
|
156
|
+
transaction,
|
|
157
|
+
});
|
|
158
|
+
yield this.writeResourceToACL(resource, transaction);
|
|
159
|
+
}));
|
|
160
|
+
this.app.db.on('rolesResources.afterDestroy', (model, options) => __awaiter(this, void 0, void 0, function* () {
|
|
161
|
+
const role = this.acl.getRole(model.get('roleName'));
|
|
162
|
+
role.revokeResource(model.get('name'));
|
|
163
|
+
}));
|
|
164
|
+
this.app.db.on('collections.afterDestroy', (model, options) => __awaiter(this, void 0, void 0, function* () {
|
|
165
|
+
const { transaction } = options;
|
|
166
|
+
yield this.app.db.getRepository('rolesResources').destroy({
|
|
167
|
+
filter: {
|
|
168
|
+
name: model.get('name'),
|
|
169
|
+
},
|
|
170
|
+
transaction,
|
|
171
|
+
});
|
|
172
|
+
}));
|
|
173
|
+
this.app.db.on('fields.afterCreate', (model, options) => __awaiter(this, void 0, void 0, function* () {
|
|
174
|
+
const { transaction } = options;
|
|
175
|
+
const collectionName = model.get('collectionName');
|
|
176
|
+
const fieldName = model.get('name');
|
|
177
|
+
const resourceActions = (yield this.app.db.getRepository('rolesResourcesActions').find({
|
|
178
|
+
filter: {
|
|
179
|
+
'resource.name': collectionName,
|
|
180
|
+
},
|
|
181
|
+
transaction,
|
|
182
|
+
appends: ['resource'],
|
|
183
|
+
}));
|
|
184
|
+
for (const resourceAction of resourceActions) {
|
|
185
|
+
const fields = resourceAction.get('fields');
|
|
186
|
+
const newFields = [...fields, fieldName];
|
|
187
|
+
yield this.app.db.getRepository('rolesResourcesActions').update({
|
|
188
|
+
filterByTk: resourceAction.get('id'),
|
|
189
|
+
values: {
|
|
190
|
+
fields: newFields,
|
|
191
|
+
},
|
|
192
|
+
transaction,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}));
|
|
196
|
+
this.app.db.on('fields.afterDestroy', (model, options) => __awaiter(this, void 0, void 0, function* () {
|
|
197
|
+
const collectionName = model.get('collectionName');
|
|
198
|
+
const fieldName = model.get('name');
|
|
199
|
+
const resourceActions = yield this.app.db.getRepository('rolesResourcesActions').find({
|
|
200
|
+
filter: {
|
|
201
|
+
'resource.name': collectionName,
|
|
202
|
+
'fields.$anyOf': [fieldName],
|
|
203
|
+
},
|
|
204
|
+
transaction: options.transaction,
|
|
205
|
+
});
|
|
206
|
+
for (const resourceAction of resourceActions) {
|
|
207
|
+
const fields = resourceAction.get('fields');
|
|
208
|
+
const newFields = fields.filter((field) => field != fieldName);
|
|
209
|
+
yield this.app.db.getRepository('rolesResourcesActions').update({
|
|
210
|
+
filterByTk: resourceAction.get('id'),
|
|
211
|
+
values: {
|
|
212
|
+
fields: newFields,
|
|
213
|
+
},
|
|
214
|
+
transaction: options.transaction,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}));
|
|
218
|
+
// sync database role data to acl
|
|
219
|
+
this.app.on('beforeStart', () => __awaiter(this, void 0, void 0, function* () {
|
|
220
|
+
yield this.writeRolesToACL();
|
|
221
|
+
}));
|
|
222
|
+
this.app.on('beforeInstallPlugin', (plugin) => __awaiter(this, void 0, void 0, function* () {
|
|
223
|
+
if (plugin.constructor.name !== 'UsersPlugin') {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const roles = this.app.db.getRepository('roles');
|
|
227
|
+
yield roles.createMany({
|
|
228
|
+
records: [
|
|
229
|
+
{
|
|
230
|
+
name: 'root',
|
|
231
|
+
title: '{{t("Root")}}',
|
|
232
|
+
hidden: true,
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
name: 'admin',
|
|
236
|
+
title: '{{t("Admin")}}',
|
|
237
|
+
allowConfigure: true,
|
|
238
|
+
allowNewMenu: true,
|
|
239
|
+
strategy: { actions: ['create', 'export', 'view', 'update', 'destroy'] },
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
name: 'member',
|
|
243
|
+
title: '{{t("Member")}}',
|
|
244
|
+
allowNewMenu: true,
|
|
245
|
+
strategy: { actions: ['view', 'update:own', 'destroy:own', 'create'] },
|
|
246
|
+
default: true,
|
|
247
|
+
},
|
|
248
|
+
],
|
|
249
|
+
});
|
|
250
|
+
const rolesResourcesScopes = this.app.db.getRepository('rolesResourcesScopes');
|
|
251
|
+
yield rolesResourcesScopes.createMany({
|
|
252
|
+
records: [
|
|
253
|
+
{
|
|
254
|
+
key: 'all',
|
|
255
|
+
name: '{{t("All records")}}',
|
|
256
|
+
scope: {},
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
key: 'own',
|
|
260
|
+
name: '{{t("Own records")}}',
|
|
261
|
+
scope: {
|
|
262
|
+
createdById: '{{ ctx.state.currentUser.id }}',
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
],
|
|
266
|
+
});
|
|
267
|
+
}));
|
|
268
|
+
this.app.acl.allow('roles', 'check', 'loggedIn');
|
|
269
|
+
this.app.acl.allow('roles', ['create', 'update', 'destroy'], 'allowConfigure');
|
|
270
|
+
this.app.acl.allow('roles.menuUiSchemas', ['set', 'toggle', 'list'], 'allowConfigure');
|
|
271
|
+
this.app.acl.allow('*', '*', (ctx) => {
|
|
272
|
+
return ctx.state.currentRole === 'root';
|
|
273
|
+
});
|
|
274
|
+
this.app.resourcer.use((ctx, next) => __awaiter(this, void 0, void 0, function* () {
|
|
275
|
+
const { actionName, resourceName, params } = ctx.action;
|
|
276
|
+
const { showAnonymous } = params || {};
|
|
277
|
+
if (actionName === 'list' && resourceName === 'roles') {
|
|
278
|
+
if (!showAnonymous) {
|
|
279
|
+
ctx.action.mergeParams({
|
|
280
|
+
filter: {
|
|
281
|
+
'name.$ne': 'anonymous',
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
yield next();
|
|
287
|
+
}));
|
|
288
|
+
this.app.acl.use((ctx, next) => __awaiter(this, void 0, void 0, function* () {
|
|
289
|
+
var _a, _b, _c, _d, _e;
|
|
290
|
+
const { actionName, resourceName } = ctx.action;
|
|
291
|
+
if (actionName === 'get' || actionName === 'list') {
|
|
292
|
+
if (!Array.isArray((_c = (_b = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.permission) === null || _a === void 0 ? void 0 : _a.can) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c.fields)) {
|
|
293
|
+
return next();
|
|
294
|
+
}
|
|
295
|
+
let collection;
|
|
296
|
+
if (resourceName.includes('.')) {
|
|
297
|
+
const [collectionName, associationName] = resourceName.split('.');
|
|
298
|
+
const field = (_e = (_d = ctx.db.getCollection(collectionName)) === null || _d === void 0 ? void 0 : _d.getField) === null || _e === void 0 ? void 0 : _e.call(_d, associationName);
|
|
299
|
+
if (field.target) {
|
|
300
|
+
collection = ctx.db.getCollection(field.target);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
collection = ctx.db.getCollection(resourceName);
|
|
305
|
+
}
|
|
306
|
+
if (collection && collection.hasField('createdById')) {
|
|
307
|
+
ctx.permission.can.params.fields.push('createdById');
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return next();
|
|
311
|
+
}));
|
|
312
|
+
const parseJsonTemplate = this.app.acl.parseJsonTemplate;
|
|
313
|
+
this.app.acl.use((ctx, next) => __awaiter(this, void 0, void 0, function* () {
|
|
314
|
+
var _f, _g, _h, _j;
|
|
315
|
+
const { actionName, resourceName, resourceOf } = ctx.action;
|
|
316
|
+
if (resourceName.includes('.') && resourceOf) {
|
|
317
|
+
if (!((_g = (_f = ctx === null || ctx === void 0 ? void 0 : ctx.permission) === null || _f === void 0 ? void 0 : _f.can) === null || _g === void 0 ? void 0 : _g.params)) {
|
|
318
|
+
return next();
|
|
319
|
+
}
|
|
320
|
+
// 关联数据去掉 filter
|
|
321
|
+
delete ctx.permission.can.params.filter;
|
|
322
|
+
// 关联数据能不能处理取决于 source 是否有权限
|
|
323
|
+
const [collectionName] = resourceName.split('.');
|
|
324
|
+
const action = ctx.can({ resource: collectionName, action: actionName });
|
|
325
|
+
const availableAction = this.app.acl.getAvailableAction(actionName);
|
|
326
|
+
if ((_h = availableAction === null || availableAction === void 0 ? void 0 : availableAction.options) === null || _h === void 0 ? void 0 : _h.onNewRecord) {
|
|
327
|
+
if (action) {
|
|
328
|
+
ctx.permission.skip = true;
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
ctx.permission.can = false;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
const filter = parseJsonTemplate(((_j = action === null || action === void 0 ? void 0 : action.params) === null || _j === void 0 ? void 0 : _j.filter) || {}, ctx);
|
|
336
|
+
const sourceInstance = yield ctx.db.getRepository(collectionName).findOne({
|
|
337
|
+
filterByTk: resourceOf,
|
|
338
|
+
filter,
|
|
339
|
+
});
|
|
340
|
+
if (!sourceInstance) {
|
|
341
|
+
ctx.permission.can = false;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
yield next();
|
|
346
|
+
}));
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
install() {
|
|
350
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
351
|
+
const repo = this.db.getRepository('collections');
|
|
352
|
+
if (repo) {
|
|
353
|
+
yield repo.db2cm('roles');
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
load() {
|
|
358
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
359
|
+
yield this.app.db.import({
|
|
360
|
+
directory: (0, path_1.resolve)(__dirname, 'collections'),
|
|
361
|
+
});
|
|
362
|
+
this.app.resourcer.use(this.acl.middleware());
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
getName() {
|
|
366
|
+
return this.getPackageName(__dirname);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
exports.PluginACL = PluginACL;
|
|
370
|
+
exports.default = PluginACL;
|
|
371
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,6CAA0C;AAC1C,+BAA+B;AAC/B,mEAAsE;AACtE,qDAAmD;AACnD,iEAAqE;AACrE,iDAA8C;AAC9C,6EAA0E;AAC1E,iEAA8D;AAe9D,MAAa,WAAW;IAItB;QAHA,4BAAuB,GAAG,IAAI,GAAG,EAAoB,CAAC;QACtD,4BAAuB,GAAG,IAAI,GAAG,EAAoB,CAAC;IAEvC,CAAC;CACjB;AALD,kCAKC;AAED,MAAa,SAAU,SAAQ,eAAM;IAArC;;QACE,6BAAwB,GAA6B,EAAE,CAAC;QAExD,gBAAW,GAAG,I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,UAAU,EAAE,UAAU;4BACtB,MAAM;yBACP,CAAC,CAAC;wBACH,IAAI,CAAC,cAAc,EAAE;4BACnB,GAAG,CAAC,UAAU,CAAC,GAAG,GAAG,KAAK,CAAC;yBAC5B;qBACF;iBACF;gBACD,MAAM,IAAI,EAAE,CAAC;YACf,CAAC,CAAA,CAAC,CAAC;QACL,CAAC;KAAA;IAEK,OAAO;;YACX,MAAM,IAAI,GAAG,IAAI,CAAC,EAAE,CAAC,aAAa,CAAM,aAAa,CAAC,CAAC;YACvD,IAAI,IAAI,EAAE;gBACR,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;aAC3B;QACH,CAAC;KAAA;IAEK,IAAI;;YACR,MAAM,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC;gBACvB,SAAS,EAAE,IAAA,cAAO,EAAC,SAAS,EAAE,aAAa,CAAC;aAC7C,CAAC,CAAC;YAEH,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC;QAChD,CAAC;KAAA;IAED,OAAO;QACL,OAAO,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC;IACxC,CAAC;CACF;AA5WD,8BA4WC;AAED,kBAAe,SAAS,CAAC","sourcesContent":["import { Context } from '@nocobase/actions';\nimport { Collection } from '@nocobase/database';\nimport { Plugin } from '@nocobase/server';\nimport { resolve } from 'path';\nimport { availableActionResource } from './actions/available-actions';\nimport { checkAction } from './actions/role-check';\nimport { roleCollectionsResource } from './actions/role-collections';\nimport { RoleModel } from './model/RoleModel';\nimport { RoleResourceActionModel } from './model/RoleResourceActionModel';\nimport { RoleResourceModel } from './model/RoleResourceModel';\n\nexport interface AssociationFieldAction {\n associationActions: string[];\n targetActions?: string[];\n}\n\ninterface AssociationFieldActions {\n [availableActionName: string]: AssociationFieldAction;\n}\n\nexport interface AssociationFieldsActions {\n [associationType: string]: AssociationFieldActions;\n}\n\nexport class GrantHelper {\n resourceTargetActionMap = new Map<string, string[]>();\n targetActionResourceMap = new Map<string, string[]>();\n\n constructor() {}\n}\n\nexport class PluginACL extends Plugin {\n associationFieldsActions: AssociationFieldsActions = {};\n\n grantHelper = new GrantHelper();\n\n get acl() {\n return this.app.acl;\n }\n\n registerAssociationFieldAction(associationType: string, value: AssociationFieldActions) {\n this.associationFieldsActions[associationType] = value;\n }\n\n registerAssociationFieldsActions() {\n this.registerAssociationFieldAction('linkTo', {\n view: {\n associationActions: ['list', 'get'],\n },\n create: {\n associationActions: ['add'],\n targetActions: ['view'],\n },\n update: {\n associationActions: ['add', 'remove', 'toggle'],\n targetActions: ['view'],\n },\n });\n\n this.registerAssociationFieldAction('attachments', {\n view: {\n associationActions: ['list', 'get'],\n },\n add: {\n associationActions: ['upload', 'add'],\n },\n update: {\n associationActions: ['update', 'add', 'remove', 'toggle'],\n },\n });\n\n this.registerAssociationFieldAction('subTable', {\n view: {\n associationActions: ['list', 'get'],\n },\n create: {\n associationActions: ['create'],\n },\n update: {\n associationActions: ['update', 'destroy'],\n },\n });\n }\n\n async writeResourceToACL(resourceModel: RoleResourceModel, transaction) {\n await resourceModel.writeToACL({\n acl: this.acl,\n associationFieldsActions: this.associationFieldsActions,\n transaction: transaction,\n grantHelper: this.grantHelper,\n });\n }\n\n async writeActionToACL(actionModel: RoleResourceActionModel, transaction) {\n const resource = actionModel.get('resource') as RoleResourceModel;\n const role = this.acl.getRole(resource.get('roleName') as string);\n await actionModel.writeToACL({\n acl: this.acl,\n role,\n resourceName: resource.get('name') as string,\n associationFieldsActions: this.associationFieldsActions,\n grantHelper: this.grantHelper,\n });\n }\n\n async writeRolesToACL() {\n const roles = (await this.app.db.getRepository('roles').find({\n appends: ['resources', 'resources.actions'],\n })) as RoleModel[];\n for (const role of roles) {\n role.writeToAcl({ acl: this.acl });\n for (const resource of role.get('resources') as RoleResourceModel[]) {\n await this.writeResourceToACL(resource, null);\n }\n }\n }\n\n async beforeLoad() {\n this.app.db.registerModels({\n RoleResourceActionModel,\n RoleResourceModel,\n RoleModel,\n });\n\n this.registerAssociationFieldsActions();\n\n this.app.resourcer.define(availableActionResource);\n this.app.resourcer.define(roleCollectionsResource);\n\n this.app.resourcer.registerActionHandler('roles:check', checkAction);\n\n this.app.db.on('roles.afterSaveWithAssociations', async (model, options) => {\n const { transaction } = options;\n\n model.writeToAcl({\n acl: this.acl,\n });\n\n for (const resource of (await model.getResources({ transaction })) as RoleResourceModel[]) {\n await this.writeResourceToACL(resource, transaction);\n }\n\n // model is default\n if (model.get('default')) {\n await this.app.db.getRepository('roles').update({\n values: {\n default: false,\n },\n filter: {\n 'name.$ne': model.get('name'),\n },\n hooks: false,\n transaction,\n });\n }\n });\n\n this.app.db.on('roles.afterDestroy', (model) => {\n const roleName = model.get('name');\n this.acl.removeRole(roleName);\n });\n\n this.app.db.on('rolesResources.afterSaveWithAssociations', async (model: RoleResourceModel, options) => {\n await this.writeResourceToACL(model, options.transaction);\n });\n\n this.app.db.on('rolesResourcesActions.afterUpdateWithAssociations', async (model, options) => {\n const { transaction } = options;\n const resource = await model.getResource({\n transaction,\n });\n\n await this.writeResourceToACL(resource, transaction);\n });\n\n this.app.db.on('rolesResources.afterDestroy', async (model, options) => {\n const role = this.acl.getRole(model.get('roleName'));\n role.revokeResource(model.get('name'));\n });\n\n this.app.db.on('collections.afterDestroy', async (model, options) => {\n const { transaction } = options;\n await this.app.db.getRepository('rolesResources').destroy({\n filter: {\n name: model.get('name'),\n },\n transaction,\n });\n });\n\n this.app.db.on('fields.afterCreate', async (model, options) => {\n const { transaction } = options;\n\n const collectionName = model.get('collectionName');\n const fieldName = model.get('name');\n\n const resourceActions = (await this.app.db.getRepository('rolesResourcesActions').find({\n filter: {\n 'resource.name': collectionName,\n },\n transaction,\n appends: ['resource'],\n })) as RoleResourceActionModel[];\n\n for (const resourceAction of resourceActions) {\n const fields = resourceAction.get('fields') as string[];\n const newFields = [...fields, fieldName];\n\n await this.app.db.getRepository('rolesResourcesActions').update({\n filterByTk: resourceAction.get('id') as number,\n values: {\n fields: newFields,\n },\n transaction,\n });\n }\n });\n\n this.app.db.on('fields.afterDestroy', async (model, options) => {\n const collectionName = model.get('collectionName');\n const fieldName = model.get('name');\n\n const resourceActions = await this.app.db.getRepository('rolesResourcesActions').find({\n filter: {\n 'resource.name': collectionName,\n 'fields.$anyOf': [fieldName],\n },\n transaction: options.transaction,\n });\n\n for (const resourceAction of resourceActions) {\n const fields = resourceAction.get('fields') as string[];\n const newFields = fields.filter((field) => field != fieldName);\n\n await this.app.db.getRepository('rolesResourcesActions').update({\n filterByTk: resourceAction.get('id') as number,\n values: {\n fields: newFields,\n },\n transaction: options.transaction,\n });\n }\n });\n\n // sync database role data to acl\n this.app.on('beforeStart', async () => {\n await this.writeRolesToACL();\n });\n\n this.app.on('beforeInstallPlugin', async (plugin) => {\n if (plugin.constructor.name !== 'UsersPlugin') {\n return;\n }\n const roles = this.app.db.getRepository('roles');\n await roles.createMany({\n records: [\n {\n name: 'root',\n title: '{{t(\"Root\")}}',\n hidden: true,\n },\n {\n name: 'admin',\n title: '{{t(\"Admin\")}}',\n allowConfigure: true,\n allowNewMenu: true,\n strategy: { actions: ['create', 'export', 'view', 'update', 'destroy'] },\n },\n {\n name: 'member',\n title: '{{t(\"Member\")}}',\n allowNewMenu: true,\n strategy: { actions: ['view', 'update:own', 'destroy:own', 'create'] },\n default: true,\n },\n ],\n });\n const rolesResourcesScopes = this.app.db.getRepository('rolesResourcesScopes');\n await rolesResourcesScopes.createMany({\n records: [\n {\n key: 'all',\n name: '{{t(\"All records\")}}',\n scope: {},\n },\n {\n key: 'own',\n name: '{{t(\"Own records\")}}',\n scope: {\n createdById: '{{ ctx.state.currentUser.id }}',\n },\n },\n ],\n });\n });\n\n this.app.acl.allow('roles', 'check', 'loggedIn');\n this.app.acl.allow('roles', ['create', 'update', 'destroy'], 'allowConfigure');\n\n this.app.acl.allow('roles.menuUiSchemas', ['set', 'toggle', 'list'], 'allowConfigure');\n\n this.app.acl.allow('*', '*', (ctx) => {\n return ctx.state.currentRole === 'root';\n });\n\n this.app.resourcer.use(async (ctx, next) => {\n const { actionName, resourceName, params } = ctx.action;\n const { showAnonymous } = params || {};\n if (actionName === 'list' && resourceName === 'roles') {\n if (!showAnonymous) {\n ctx.action.mergeParams({\n filter: {\n 'name.$ne': 'anonymous',\n },\n });\n }\n }\n await next();\n });\n\n this.app.acl.use(async (ctx: Context, next) => {\n const { actionName, resourceName } = ctx.action;\n if (actionName === 'get' || actionName === 'list') {\n if (!Array.isArray(ctx?.permission?.can?.params?.fields)) {\n return next();\n }\n let collection: Collection;\n if (resourceName.includes('.')) {\n const [collectionName, associationName] = resourceName.split('.');\n const field = ctx.db.getCollection(collectionName)?.getField?.(associationName);\n if (field.target) {\n collection = ctx.db.getCollection(field.target);\n }\n } else {\n collection = ctx.db.getCollection(resourceName);\n }\n if (collection && collection.hasField('createdById')) {\n ctx.permission.can.params.fields.push('createdById');\n }\n }\n return next();\n });\n\n const parseJsonTemplate = this.app.acl.parseJsonTemplate;\n this.app.acl.use(async (ctx: Context, next) => {\n const { actionName, resourceName, resourceOf } = ctx.action;\n if (resourceName.includes('.') && resourceOf) {\n if (!ctx?.permission?.can?.params) {\n return next();\n }\n // 关联数据去掉 filter\n delete ctx.permission.can.params.filter;\n // 关联数据能不能处理取决于 source 是否有权限\n const [collectionName] = resourceName.split('.');\n const action = ctx.can({ resource: collectionName, action: actionName });\n const availableAction = this.app.acl.getAvailableAction(actionName);\n if (availableAction?.options?.onNewRecord) {\n if (action) {\n ctx.permission.skip = true;\n } else {\n ctx.permission.can = false;\n }\n } else {\n const filter = parseJsonTemplate(action?.params?.filter || {}, ctx);\n const sourceInstance = await ctx.db.getRepository(collectionName).findOne({\n filterByTk: resourceOf,\n filter,\n });\n if (!sourceInstance) {\n ctx.permission.can = false;\n }\n }\n }\n await next();\n });\n }\n\n async install() {\n const repo = this.db.getRepository<any>('collections');\n if (repo) {\n await repo.db2cm('roles');\n }\n }\n\n async load() {\n await this.app.db.import({\n directory: resolve(__dirname, 'collections'),\n });\n\n this.app.resourcer.use(this.acl.middleware());\n }\n\n getName(): string {\n return this.getPackageName(__dirname);\n }\n}\n\nexport default PluginACL;\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nocobase/plugin-acl",
|
|
3
|
+
"version": "0.7.0-alpha.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"licenses": [
|
|
7
|
+
{
|
|
8
|
+
"type": "Apache-2.0",
|
|
9
|
+
"url": "http://www.apache.org/licenses/LICENSE-2.0"
|
|
10
|
+
}
|
|
11
|
+
],
|
|
12
|
+
"main": "./lib/index.js",
|
|
13
|
+
"types": "./lib/index.d.ts",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "rimraf -rf lib esm dist && npm run build:cjs && npm run build:esm",
|
|
16
|
+
"build:cjs": "tsc --project tsconfig.build.json",
|
|
17
|
+
"build:esm": "tsc --project tsconfig.build.json --module es2015 --outDir esm"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@nocobase/acl": "0.7.0-alpha.1",
|
|
21
|
+
"@nocobase/database": "0.7.0-alpha.1",
|
|
22
|
+
"@nocobase/server": "0.7.0-alpha.1"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
27
|
+
"directory": "packages/plugins/acl"
|
|
28
|
+
},
|
|
29
|
+
"gitHead": "e7293ad7aadbdf2084042f7800a232af6e0b7a8a"
|
|
30
|
+
}
|