@nocobase/plugin-acl 0.7.2-alpha.6 → 0.7.4-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.
@@ -0,0 +1,2 @@
1
+ import { Context, Next } from '@nocobase/actions';
2
+ export declare function setDefaultRole(ctx: Context, next: Next): Promise<any>;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.setDefaultRole = setDefaultRole;
7
+
8
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
9
+
10
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
11
+
12
+ function setDefaultRole(_x, _x2) {
13
+ return _setDefaultRole.apply(this, arguments);
14
+ }
15
+
16
+ function _setDefaultRole() {
17
+ _setDefaultRole = _asyncToGenerator(function* (ctx, next) {
18
+ const roleName = ctx.action.params.values.roleName;
19
+ const db = ctx.db,
20
+ currentUser = ctx.state.currentUser,
21
+ values = ctx.action.params.values;
22
+
23
+ if (values.roleName == 'anonymous') {
24
+ return next();
25
+ }
26
+
27
+ const repository = db.getRepository('rolesUsers');
28
+ yield db.sequelize.transaction( /*#__PURE__*/function () {
29
+ var _ref = _asyncToGenerator(function* (transaction) {
30
+ yield repository.update({
31
+ filter: {
32
+ userId: currentUser.get('id')
33
+ },
34
+ values: {
35
+ default: false
36
+ },
37
+ transaction
38
+ });
39
+ yield repository.update({
40
+ filter: {
41
+ userId: currentUser.get('id'),
42
+ roleName
43
+ },
44
+ values: {
45
+ default: true
46
+ },
47
+ transaction
48
+ });
49
+ });
50
+
51
+ return function (_x3) {
52
+ return _ref.apply(this, arguments);
53
+ };
54
+ }());
55
+ ctx.body = 'ok';
56
+ yield next();
57
+ });
58
+ return _setDefaultRole.apply(this, arguments);
59
+ }
@@ -0,0 +1,3 @@
1
+ import { CollectionOptions } from '@nocobase/database';
2
+ declare const _default: CollectionOptions;
3
+ export default _default;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _default = {
8
+ name: 'rolesUsers',
9
+ fields: [{
10
+ type: 'boolean',
11
+ name: 'default'
12
+ }]
13
+ };
14
+ exports.default = _default;
@@ -0,0 +1,6 @@
1
+ declare const _default: {
2
+ collectionOptions: import("@nocobase/database").CollectionOptions;
3
+ mergeOptions: import("@nocobase/database").MergeOptions;
4
+ extend: boolean;
5
+ };
6
+ export default _default;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ function _database() {
9
+ const data = require("@nocobase/database");
10
+
11
+ _database = function _database() {
12
+ return data;
13
+ };
14
+
15
+ return data;
16
+ }
17
+
18
+ var _default = (0, _database().extend)({
19
+ name: 'users',
20
+ fields: [{
21
+ interface: 'm2m',
22
+ type: 'belongsToMany',
23
+ name: 'roles',
24
+ target: 'roles',
25
+ foreignKey: 'userId',
26
+ otherKey: 'roleName',
27
+ sourceKey: 'id',
28
+ targetKey: 'name',
29
+ through: 'rolesUsers',
30
+ uiSchema: {
31
+ type: 'array',
32
+ title: '{{t("Roles")}}',
33
+ 'x-component': 'RecordPicker',
34
+ 'x-component-props': {
35
+ multiple: true,
36
+ fieldNames: {
37
+ label: 'title',
38
+ value: 'name'
39
+ }
40
+ }
41
+ }
42
+ }]
43
+ });
44
+
45
+ exports.default = _default;
@@ -0,0 +1 @@
1
+ export declare function setCurrentRole(ctx: any, next: any): Promise<any>;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.setCurrentRole = setCurrentRole;
7
+
8
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
9
+
10
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
11
+
12
+ function setCurrentRole(_x, _x2) {
13
+ return _setCurrentRole.apply(this, arguments);
14
+ }
15
+
16
+ function _setCurrentRole() {
17
+ _setCurrentRole = _asyncToGenerator(function* (ctx, next) {
18
+ let currentRole = ctx.get('X-Role');
19
+
20
+ if (currentRole === 'anonymous') {
21
+ ctx.state.currentRole = currentRole;
22
+ return next();
23
+ }
24
+
25
+ if (!ctx.state.currentUser) {
26
+ return next();
27
+ }
28
+
29
+ const repository = ctx.db.getRepository('users.roles', ctx.state.currentUser.id);
30
+ const roles = yield repository.find();
31
+ ctx.state.currentUser.setDataValue('roles', roles);
32
+
33
+ if (roles.length == 1) {
34
+ currentRole = roles[0].name;
35
+ } else if (roles.length > 1) {
36
+ const role = roles.find(item => item.name === currentRole);
37
+
38
+ if (!role) {
39
+ var _ref;
40
+
41
+ const defaultRole = roles.find(item => {
42
+ var _item$rolesUsers;
43
+
44
+ return item === null || item === void 0 ? void 0 : (_item$rolesUsers = item.rolesUsers) === null || _item$rolesUsers === void 0 ? void 0 : _item$rolesUsers.default;
45
+ });
46
+ currentRole = (_ref = defaultRole || roles[0]) === null || _ref === void 0 ? void 0 : _ref.name;
47
+ }
48
+ }
49
+
50
+ if (currentRole) {
51
+ ctx.state.currentRole = currentRole;
52
+ }
53
+
54
+ yield next();
55
+ });
56
+ return _setCurrentRole.apply(this, arguments);
57
+ }
package/lib/server.js CHANGED
@@ -31,6 +31,10 @@ var _roleCheck = require("./actions/role-check");
31
31
 
32
32
  var _roleCollections = require("./actions/role-collections");
33
33
 
34
+ var _userSetDefaultRole = require("./actions/user-setDefaultRole");
35
+
36
+ var _setCurrentRole = require("./middlewares/setCurrentRole");
37
+
34
38
  var _RoleModel = require("./model/RoleModel");
35
39
 
36
40
  var _RoleResourceActionModel = require("./model/RoleResourceActionModel");
@@ -209,9 +213,38 @@ class PluginACL extends _server().Plugin {
209
213
 
210
214
  _this4.app.resourcer.registerActionHandler('roles:check', _roleCheck.checkAction);
211
215
 
212
- _this4.app.db.on('roles.afterSaveWithAssociations', /*#__PURE__*/function () {
216
+ _this4.app.resourcer.registerActionHandler(`users:setDefaultRole`, _userSetDefaultRole.setDefaultRole);
217
+
218
+ _this4.db.on('users.afterCreateWithAssociations', /*#__PURE__*/function () {
213
219
  var _ref = _asyncToGenerator(function* (model, options) {
214
220
  const transaction = options.transaction;
221
+
222
+ const repository = _this4.app.db.getRepository('roles');
223
+
224
+ const defaultRole = yield repository.findOne({
225
+ filter: {
226
+ default: true
227
+ },
228
+ transaction
229
+ });
230
+
231
+ if (defaultRole && (yield model.countRoles({
232
+ transaction
233
+ })) == 0) {
234
+ yield model.addRoles(defaultRole, {
235
+ transaction
236
+ });
237
+ }
238
+ });
239
+
240
+ return function (_x, _x2) {
241
+ return _ref.apply(this, arguments);
242
+ };
243
+ }());
244
+
245
+ _this4.app.db.on('roles.afterSaveWithAssociations', /*#__PURE__*/function () {
246
+ var _ref2 = _asyncToGenerator(function* (model, options) {
247
+ const transaction = options.transaction;
215
248
  model.writeToAcl({
216
249
  acl: _this4.acl
217
250
  });
@@ -247,8 +280,8 @@ class PluginACL extends _server().Plugin {
247
280
  }
248
281
  });
249
282
 
250
- return function (_x, _x2) {
251
- return _ref.apply(this, arguments);
283
+ return function (_x3, _x4) {
284
+ return _ref2.apply(this, arguments);
252
285
  };
253
286
  }());
254
287
 
@@ -259,17 +292,17 @@ class PluginACL extends _server().Plugin {
259
292
  });
260
293
 
261
294
  _this4.app.db.on('rolesResources.afterSaveWithAssociations', /*#__PURE__*/function () {
262
- var _ref2 = _asyncToGenerator(function* (model, options) {
295
+ var _ref3 = _asyncToGenerator(function* (model, options) {
263
296
  yield _this4.writeResourceToACL(model, options.transaction);
264
297
  });
265
298
 
266
- return function (_x3, _x4) {
267
- return _ref2.apply(this, arguments);
299
+ return function (_x5, _x6) {
300
+ return _ref3.apply(this, arguments);
268
301
  };
269
302
  }());
270
303
 
271
304
  _this4.app.db.on('rolesResourcesActions.afterUpdateWithAssociations', /*#__PURE__*/function () {
272
- var _ref3 = _asyncToGenerator(function* (model, options) {
305
+ var _ref4 = _asyncToGenerator(function* (model, options) {
273
306
  const transaction = options.transaction;
274
307
  const resource = yield model.getResource({
275
308
  transaction
@@ -277,13 +310,13 @@ class PluginACL extends _server().Plugin {
277
310
  yield _this4.writeResourceToACL(resource, transaction);
278
311
  });
279
312
 
280
- return function (_x5, _x6) {
281
- return _ref3.apply(this, arguments);
313
+ return function (_x7, _x8) {
314
+ return _ref4.apply(this, arguments);
282
315
  };
283
316
  }());
284
317
 
285
318
  _this4.app.db.on('rolesResources.afterDestroy', /*#__PURE__*/function () {
286
- var _ref4 = _asyncToGenerator(function* (model, options) {
319
+ var _ref5 = _asyncToGenerator(function* (model, options) {
287
320
  const role = _this4.acl.getRole(model.get('roleName'));
288
321
 
289
322
  if (role) {
@@ -291,13 +324,13 @@ class PluginACL extends _server().Plugin {
291
324
  }
292
325
  });
293
326
 
294
- return function (_x7, _x8) {
295
- return _ref4.apply(this, arguments);
327
+ return function (_x9, _x10) {
328
+ return _ref5.apply(this, arguments);
296
329
  };
297
330
  }());
298
331
 
299
332
  _this4.app.db.on('collections.afterDestroy', /*#__PURE__*/function () {
300
- var _ref5 = _asyncToGenerator(function* (model, options) {
333
+ var _ref6 = _asyncToGenerator(function* (model, options) {
301
334
  const transaction = options.transaction;
302
335
  yield _this4.app.db.getRepository('rolesResources').destroy({
303
336
  filter: {
@@ -307,13 +340,13 @@ class PluginACL extends _server().Plugin {
307
340
  });
308
341
  });
309
342
 
310
- return function (_x9, _x10) {
311
- return _ref5.apply(this, arguments);
343
+ return function (_x11, _x12) {
344
+ return _ref6.apply(this, arguments);
312
345
  };
313
346
  }());
314
347
 
315
348
  _this4.app.db.on('fields.afterCreate', /*#__PURE__*/function () {
316
- var _ref6 = _asyncToGenerator(function* (model, options) {
349
+ var _ref7 = _asyncToGenerator(function* (model, options) {
317
350
  const transaction = options.transaction;
318
351
  const collectionName = model.get('collectionName');
319
352
  const fieldName = model.get('name');
@@ -348,13 +381,13 @@ class PluginACL extends _server().Plugin {
348
381
  }
349
382
  });
350
383
 
351
- return function (_x11, _x12) {
352
- return _ref6.apply(this, arguments);
384
+ return function (_x13, _x14) {
385
+ return _ref7.apply(this, arguments);
353
386
  };
354
387
  }());
355
388
 
356
389
  _this4.app.db.on('fields.afterDestroy', /*#__PURE__*/function () {
357
- var _ref7 = _asyncToGenerator(function* (model, options) {
390
+ var _ref8 = _asyncToGenerator(function* (model, options) {
358
391
  const collectionName = model.get('collectionName');
359
392
  const fieldName = model.get('name');
360
393
  const resourceActions = yield _this4.app.db.getRepository('rolesResourcesActions').find({
@@ -388,8 +421,8 @@ class PluginACL extends _server().Plugin {
388
421
  }
389
422
  });
390
423
 
391
- return function (_x13, _x14) {
392
- return _ref7.apply(this, arguments);
424
+ return function (_x15, _x16) {
425
+ return _ref8.apply(this, arguments);
393
426
  };
394
427
  }()); // sync database role data to acl
395
428
 
@@ -399,7 +432,7 @@ class PluginACL extends _server().Plugin {
399
432
  }));
400
433
 
401
434
  _this4.app.on('beforeInstallPlugin', /*#__PURE__*/function () {
402
- var _ref9 = _asyncToGenerator(function* (plugin) {
435
+ var _ref10 = _asyncToGenerator(function* (plugin) {
403
436
  if (plugin.constructor.name !== 'UsersPlugin') {
404
437
  return;
405
438
  }
@@ -417,7 +450,7 @@ class PluginACL extends _server().Plugin {
417
450
  allowConfigure: true,
418
451
  allowNewMenu: true,
419
452
  strategy: {
420
- actions: ['create', 'export', 'view', 'update', 'destroy']
453
+ actions: ['create', 'view', 'update', 'destroy']
421
454
  }
422
455
  }, {
423
456
  name: 'member',
@@ -447,11 +480,17 @@ class PluginACL extends _server().Plugin {
447
480
  });
448
481
  });
449
482
 
450
- return function (_x15) {
451
- return _ref9.apply(this, arguments);
483
+ return function (_x17) {
484
+ return _ref10.apply(this, arguments);
452
485
  };
453
486
  }());
454
487
 
488
+ const usersPlugin = _this4.app.pm.get('@nocobase/plugin-users');
489
+
490
+ usersPlugin.tokenMiddleware.use(_setCurrentRole.setCurrentRole);
491
+
492
+ _this4.app.acl.allow('users', 'setDefaultRole', 'loggedIn');
493
+
455
494
  _this4.app.acl.allow('roles', 'check', 'loggedIn');
456
495
 
457
496
  _this4.app.acl.allow('roles', ['create', 'update', 'destroy'], 'allowConfigure');
@@ -463,14 +502,14 @@ class PluginACL extends _server().Plugin {
463
502
  });
464
503
 
465
504
  _this4.app.resourcer.use( /*#__PURE__*/function () {
466
- var _ref10 = _asyncToGenerator(function* (ctx, next) {
505
+ var _ref11 = _asyncToGenerator(function* (ctx, next) {
467
506
  const _ctx$action = ctx.action,
468
507
  actionName = _ctx$action.actionName,
469
508
  resourceName = _ctx$action.resourceName,
470
509
  params = _ctx$action.params;
471
510
 
472
- const _ref11 = params || {},
473
- showAnonymous = _ref11.showAnonymous;
511
+ const _ref12 = params || {},
512
+ showAnonymous = _ref12.showAnonymous;
474
513
 
475
514
  if (actionName === 'list' && resourceName === 'roles') {
476
515
  if (!showAnonymous) {
@@ -491,13 +530,13 @@ class PluginACL extends _server().Plugin {
491
530
  yield next();
492
531
  });
493
532
 
494
- return function (_x16, _x17) {
495
- return _ref10.apply(this, arguments);
533
+ return function (_x18, _x19) {
534
+ return _ref11.apply(this, arguments);
496
535
  };
497
536
  }());
498
537
 
499
538
  _this4.app.acl.use( /*#__PURE__*/function () {
500
- var _ref12 = _asyncToGenerator(function* (ctx, next) {
539
+ var _ref13 = _asyncToGenerator(function* (ctx, next) {
501
540
  const _ctx$action2 = ctx.action,
502
541
  actionName = _ctx$action2.actionName,
503
542
  resourceName = _ctx$action2.resourceName;
@@ -536,15 +575,15 @@ class PluginACL extends _server().Plugin {
536
575
  return next();
537
576
  });
538
577
 
539
- return function (_x18, _x19) {
540
- return _ref12.apply(this, arguments);
578
+ return function (_x20, _x21) {
579
+ return _ref13.apply(this, arguments);
541
580
  };
542
581
  }());
543
582
 
544
583
  const parseJsonTemplate = _this4.app.acl.parseJsonTemplate;
545
584
 
546
585
  _this4.app.acl.use( /*#__PURE__*/function () {
547
- var _ref13 = _asyncToGenerator(function* (ctx, next) {
586
+ var _ref14 = _asyncToGenerator(function* (ctx, next) {
548
587
  const _ctx$action3 = ctx.action,
549
588
  actionName = _ctx$action3.actionName,
550
589
  resourceName = _ctx$action3.resourceName,
@@ -595,8 +634,8 @@ class PluginACL extends _server().Plugin {
595
634
  yield next();
596
635
  });
597
636
 
598
- return function (_x20, _x21) {
599
- return _ref13.apply(this, arguments);
637
+ return function (_x22, _x23) {
638
+ return _ref14.apply(this, arguments);
600
639
  };
601
640
  }());
602
641
  })();
@@ -611,6 +650,26 @@ class PluginACL extends _server().Plugin {
611
650
  if (repo) {
612
651
  yield repo.db2cm('roles');
613
652
  }
653
+
654
+ const User = _this5.db.getCollection('users');
655
+
656
+ yield User.repository.update({
657
+ values: {
658
+ roles: ['root', 'admin', 'member']
659
+ }
660
+ });
661
+
662
+ const RolesUsers = _this5.db.getCollection('rolesUsers');
663
+
664
+ yield RolesUsers.repository.update({
665
+ filter: {
666
+ userId: 1,
667
+ roleName: 'root'
668
+ },
669
+ values: {
670
+ default: true
671
+ }
672
+ });
614
673
  })();
615
674
  }
616
675
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/plugin-acl",
3
- "version": "0.7.2-alpha.6",
3
+ "version": "0.7.4-alpha.1",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "licenses": [
@@ -12,14 +12,15 @@
12
12
  "main": "./lib/index.js",
13
13
  "types": "./lib/index.d.ts",
14
14
  "dependencies": {
15
- "@nocobase/acl": "0.7.2-alpha.6",
16
- "@nocobase/database": "0.7.2-alpha.6",
17
- "@nocobase/server": "0.7.2-alpha.6"
15
+ "@nocobase/acl": "0.7.4-alpha.1",
16
+ "@nocobase/database": "0.7.4-alpha.1",
17
+ "@nocobase/plugin-users": "0.7.4-alpha.1",
18
+ "@nocobase/server": "0.7.4-alpha.1"
18
19
  },
19
20
  "repository": {
20
21
  "type": "git",
21
22
  "url": "git+https://github.com/nocobase/nocobase.git",
22
23
  "directory": "packages/plugins/acl"
23
24
  },
24
- "gitHead": "c5f7034351f0c85857d0756f033566570a53df54"
25
+ "gitHead": "543eb1c0308bb72a5ba54f208586cd78bfda8fa9"
25
26
  }