@nocobase/plugin-acl 0.7.7-alpha.1 → 0.8.0-alpha.4

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/lib/server.d.ts CHANGED
@@ -28,6 +28,5 @@ export declare class PluginACL extends Plugin {
28
28
  beforeLoad(): Promise<void>;
29
29
  install(): Promise<void>;
30
30
  load(): Promise<void>;
31
- getName(): string;
32
31
  }
33
32
  export default PluginACL;
package/lib/server.js CHANGED
@@ -427,13 +427,74 @@ class PluginACL extends _server().Plugin {
427
427
  }()); // sync database role data to acl
428
428
 
429
429
 
430
- _this4.app.on('beforeStart', /*#__PURE__*/_asyncToGenerator(function* () {
431
- yield _this4.writeRolesToACL();
432
- }));
430
+ _this4.app.on('afterLoad', /*#__PURE__*/function () {
431
+ var _ref9 = _asyncToGenerator(function* (app, options) {
432
+ if ((options === null || options === void 0 ? void 0 : options.method) === 'install') {
433
+ return;
434
+ }
435
+
436
+ const exists = yield _this4.app.db.collectionExistsInDb('roles');
437
+
438
+ if (exists) {
439
+ yield _this4.writeRolesToACL();
440
+ }
441
+ });
442
+
443
+ return function (_x17, _x18) {
444
+ return _ref9.apply(this, arguments);
445
+ };
446
+ }());
447
+
448
+ _this4.app.on('afterInstall', /*#__PURE__*/function () {
449
+ var _ref10 = _asyncToGenerator(function* (app, options) {
450
+ const exists = yield _this4.app.db.collectionExistsInDb('roles');
451
+
452
+ if (exists) {
453
+ yield _this4.writeRolesToACL();
454
+ }
455
+ });
456
+
457
+ return function (_x19, _x20) {
458
+ return _ref10.apply(this, arguments);
459
+ };
460
+ }());
461
+
462
+ _this4.app.on('afterInstallPlugin', /*#__PURE__*/function () {
463
+ var _ref11 = _asyncToGenerator(function* (plugin) {
464
+ if (plugin.getName() !== 'users') {
465
+ return;
466
+ }
467
+
468
+ const User = _this4.db.getCollection('users');
469
+
470
+ yield User.repository.update({
471
+ values: {
472
+ roles: ['root', 'admin', 'member']
473
+ },
474
+ forceUpdate: true
475
+ });
476
+
477
+ const RolesUsers = _this4.db.getCollection('rolesUsers');
478
+
479
+ yield RolesUsers.repository.update({
480
+ filter: {
481
+ userId: 1,
482
+ roleName: 'root'
483
+ },
484
+ values: {
485
+ default: true
486
+ }
487
+ });
488
+ });
489
+
490
+ return function (_x21) {
491
+ return _ref11.apply(this, arguments);
492
+ };
493
+ }());
433
494
 
434
495
  _this4.app.on('beforeInstallPlugin', /*#__PURE__*/function () {
435
- var _ref10 = _asyncToGenerator(function* (plugin) {
436
- if (plugin.constructor.name !== 'UsersPlugin') {
496
+ var _ref12 = _asyncToGenerator(function* (plugin) {
497
+ if (plugin.getName() !== 'users') {
437
498
  return;
438
499
  }
439
500
 
@@ -480,14 +541,16 @@ class PluginACL extends _server().Plugin {
480
541
  });
481
542
  });
482
543
 
483
- return function (_x17) {
484
- return _ref10.apply(this, arguments);
544
+ return function (_x22) {
545
+ return _ref12.apply(this, arguments);
485
546
  };
486
547
  }());
487
548
 
488
- const usersPlugin = _this4.app.pm.get('@nocobase/plugin-users');
489
-
490
- usersPlugin.tokenMiddleware.use(_setCurrentRole.setCurrentRole);
549
+ _this4.app.resourcer.use(_setCurrentRole.setCurrentRole, {
550
+ tag: 'setCurrentRole',
551
+ before: 'acl',
552
+ after: 'parseToken'
553
+ });
491
554
 
492
555
  _this4.app.acl.allow('users', 'setDefaultRole', 'loggedIn');
493
556
 
@@ -502,14 +565,14 @@ class PluginACL extends _server().Plugin {
502
565
  });
503
566
 
504
567
  _this4.app.resourcer.use( /*#__PURE__*/function () {
505
- var _ref11 = _asyncToGenerator(function* (ctx, next) {
568
+ var _ref13 = _asyncToGenerator(function* (ctx, next) {
506
569
  const _ctx$action = ctx.action,
507
570
  actionName = _ctx$action.actionName,
508
571
  resourceName = _ctx$action.resourceName,
509
572
  params = _ctx$action.params;
510
573
 
511
- const _ref12 = params || {},
512
- showAnonymous = _ref12.showAnonymous;
574
+ const _ref14 = params || {},
575
+ showAnonymous = _ref14.showAnonymous;
513
576
 
514
577
  if (actionName === 'list' && resourceName === 'roles') {
515
578
  if (!showAnonymous) {
@@ -530,13 +593,13 @@ class PluginACL extends _server().Plugin {
530
593
  yield next();
531
594
  });
532
595
 
533
- return function (_x18, _x19) {
534
- return _ref11.apply(this, arguments);
596
+ return function (_x23, _x24) {
597
+ return _ref13.apply(this, arguments);
535
598
  };
536
599
  }());
537
600
 
538
601
  _this4.app.acl.use( /*#__PURE__*/function () {
539
- var _ref13 = _asyncToGenerator(function* (ctx, next) {
602
+ var _ref15 = _asyncToGenerator(function* (ctx, next) {
540
603
  const _ctx$action2 = ctx.action,
541
604
  actionName = _ctx$action2.actionName,
542
605
  resourceName = _ctx$action2.resourceName;
@@ -575,15 +638,15 @@ class PluginACL extends _server().Plugin {
575
638
  return next();
576
639
  });
577
640
 
578
- return function (_x20, _x21) {
579
- return _ref13.apply(this, arguments);
641
+ return function (_x25, _x26) {
642
+ return _ref15.apply(this, arguments);
580
643
  };
581
644
  }());
582
645
 
583
646
  const parseJsonTemplate = _this4.app.acl.parseJsonTemplate;
584
647
 
585
648
  _this4.app.acl.use( /*#__PURE__*/function () {
586
- var _ref14 = _asyncToGenerator(function* (ctx, next) {
649
+ var _ref16 = _asyncToGenerator(function* (ctx, next) {
587
650
  const _ctx$action3 = ctx.action,
588
651
  actionName = _ctx$action3.actionName,
589
652
  resourceName = _ctx$action3.resourceName,
@@ -634,8 +697,8 @@ class PluginACL extends _server().Plugin {
634
697
  yield next();
635
698
  });
636
699
 
637
- return function (_x22, _x23) {
638
- return _ref14.apply(this, arguments);
700
+ return function (_x27, _x28) {
701
+ return _ref16.apply(this, arguments);
639
702
  };
640
703
  }());
641
704
  })();
@@ -650,27 +713,6 @@ class PluginACL extends _server().Plugin {
650
713
  if (repo) {
651
714
  yield repo.db2cm('roles');
652
715
  }
653
-
654
- const User = _this5.db.getCollection('users');
655
-
656
- yield User.repository.update({
657
- values: {
658
- roles: ['root', 'admin', 'member']
659
- },
660
- forceUpdate: true
661
- });
662
-
663
- const RolesUsers = _this5.db.getCollection('rolesUsers');
664
-
665
- yield RolesUsers.repository.update({
666
- filter: {
667
- userId: 1,
668
- roleName: 'root'
669
- },
670
- values: {
671
- default: true
672
- }
673
- });
674
716
  })();
675
717
  }
676
718
 
@@ -681,15 +723,9 @@ class PluginACL extends _server().Plugin {
681
723
  yield _this6.app.db.import({
682
724
  directory: (0, _path().resolve)(__dirname, 'collections')
683
725
  });
684
-
685
- _this6.app.resourcer.use(_this6.acl.middleware());
686
726
  })();
687
727
  }
688
728
 
689
- getName() {
690
- return this.getPackageName(__dirname);
691
- }
692
-
693
729
  }
694
730
 
695
731
  exports.PluginACL = PluginACL;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/plugin-acl",
3
- "version": "0.7.7-alpha.1",
3
+ "version": "0.8.0-alpha.4",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "licenses": [
@@ -12,15 +12,15 @@
12
12
  "main": "./lib/index.js",
13
13
  "types": "./lib/index.d.ts",
14
14
  "dependencies": {
15
- "@nocobase/acl": "0.7.7-alpha.1",
16
- "@nocobase/database": "0.7.7-alpha.1",
17
- "@nocobase/plugin-users": "0.7.7-alpha.1",
18
- "@nocobase/server": "0.7.7-alpha.1"
15
+ "@nocobase/acl": "0.8.0-alpha.4",
16
+ "@nocobase/database": "0.8.0-alpha.4",
17
+ "@nocobase/plugin-users": "0.8.0-alpha.4",
18
+ "@nocobase/server": "0.8.0-alpha.4"
19
19
  },
20
20
  "repository": {
21
21
  "type": "git",
22
22
  "url": "git+https://github.com/nocobase/nocobase.git",
23
23
  "directory": "packages/plugins/acl"
24
24
  },
25
- "gitHead": "f82374e6f9daaf71ba63eaf156468ea7ddc042da"
25
+ "gitHead": "433f068ce4479a53f51d3ceac3f22b710e7bb13e"
26
26
  }
@@ -1,8 +1,8 @@
1
1
  import { ACL } from '@nocobase/acl';
2
2
  import { Database } from '@nocobase/database';
3
- import { MockServer } from '@nocobase/test';
4
- import UsersPlugin from '@nocobase/plugin-users';
5
3
  import { UiSchemaRepository } from '@nocobase/plugin-ui-schema-storage';
4
+ import UsersPlugin from '@nocobase/plugin-users';
5
+ import { MockServer } from '@nocobase/test';
6
6
  import { prepareApp } from './prepare';
7
7
 
8
8
  describe('acl', () => {
@@ -30,7 +30,7 @@ describe('acl', () => {
30
30
  },
31
31
  });
32
32
 
33
- const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin;
33
+ const userPlugin = app.getPlugin('users') as UsersPlugin;
34
34
 
35
35
  adminAgent = app.agent().auth(
36
36
  userPlugin.jwtService.sign({
@@ -483,7 +483,7 @@ describe('acl', () => {
483
483
  },
484
484
  });
485
485
 
486
- const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin;
486
+ const userPlugin = app.getPlugin('users') as UsersPlugin;
487
487
  const userAgent = app.agent().auth(
488
488
  userPlugin.jwtService.sign({
489
489
  userId: user.get('id'),
@@ -507,7 +507,7 @@ describe('acl', () => {
507
507
  expect(response.statusCode).toEqual(200);
508
508
  });
509
509
 
510
- it('should sync data to acl before app start', async () => {
510
+ it('should sync data to acl after app reload', async () => {
511
511
  const role = await db.getRepository('roles').create({
512
512
  values: {
513
513
  name: 'new',
@@ -527,14 +527,14 @@ describe('acl', () => {
527
527
  hooks: false,
528
528
  });
529
529
 
530
- expect(acl.getRole('new')).toBeUndefined();
530
+ expect(app.acl.getRole('new')).toBeUndefined();
531
531
 
532
- await app.start();
532
+ await app.reload();
533
533
 
534
- expect(acl.getRole('new')).toBeDefined();
534
+ expect(app.acl.getRole('new')).toBeDefined();
535
535
 
536
536
  expect(
537
- acl.can({
537
+ app.acl.can({
538
538
  role: 'new',
539
539
  resource: 'posts',
540
540
  action: 'view',
@@ -48,7 +48,7 @@ describe('association field acl', () => {
48
48
  },
49
49
  });
50
50
 
51
- const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin;
51
+ const userPlugin = app.getPlugin('users') as UsersPlugin;
52
52
  userAgent = app.agent().auth(
53
53
  userPlugin.jwtService.sign({
54
54
  userId: user.get('id'),
@@ -45,7 +45,7 @@ describe('configuration', () => {
45
45
  }
46
46
  });
47
47
 
48
- const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin;
48
+ const userPlugin = app.getPlugin('users') as UsersPlugin;
49
49
  adminAgent = app.agent().auth(userPlugin.jwtService.sign({
50
50
  userId: admin.get('id'),
51
51
  }), { type: 'bearer' });
@@ -1,7 +1,7 @@
1
1
  import { ACL } from '@nocobase/acl';
2
2
  import { Database, Model } from '@nocobase/database';
3
- import { MockServer } from '@nocobase/test';
4
3
  import UsersPlugin from '@nocobase/plugin-users';
4
+ import { MockServer } from '@nocobase/test';
5
5
  import { prepareApp } from './prepare';
6
6
 
7
7
  describe('middleware', () => {
@@ -30,7 +30,7 @@ describe('middleware', () => {
30
30
  }
31
31
  });
32
32
 
33
- const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin;
33
+ const userPlugin = app.getPlugin('users') as UsersPlugin;
34
34
  adminAgent = app.agent().auth(userPlugin.jwtService.sign({
35
35
  userId: admin.get('id'),
36
36
  }), { type: 'bearer' });
@@ -67,7 +67,7 @@ describe('own test', () => {
67
67
 
68
68
  admin = await db.getRepository('users').findOne();
69
69
 
70
- pluginUser = app.getPlugin('@nocobase/plugin-users');
70
+ pluginUser = app.getPlugin('users');
71
71
 
72
72
  adminToken = pluginUser.jwtService.sign({ userId: admin.get('id') });
73
73
 
@@ -1,23 +1,18 @@
1
- import PluginUsers from '@nocobase/plugin-users';
2
- import PluginErrorHandler from '@nocobase/plugin-error-handler';
3
- import PluginCollectionManager from '@nocobase/plugin-collection-manager';
4
- import PluginUiSchema from '@nocobase/plugin-ui-schema-storage';
5
1
  import { mockServer } from '@nocobase/test';
6
2
  import PluginACL from '../server';
7
3
 
8
4
  export async function prepareApp() {
9
5
  const app = mockServer({
10
6
  registerActions: true,
7
+ acl: true,
8
+ plugins: ['error-handler', 'users', 'ui-schema-storage', 'collection-manager'],
11
9
  });
12
10
 
13
- await app.cleanDb();
11
+ app.plugin(PluginACL, {
12
+ name: 'acl',
13
+ });
14
14
 
15
- app.plugin(PluginUsers);
16
- app.plugin(PluginUiSchema);
17
- app.plugin(PluginErrorHandler);
18
- app.plugin(PluginCollectionManager);
19
- app.plugin(PluginACL);
20
- await app.loadAndInstall();
15
+ await app.loadAndInstall({ clean: true });
21
16
 
22
17
  await app.db.sync();
23
18
 
@@ -1,6 +1,6 @@
1
- import { MockServer } from '@nocobase/test';
2
1
  import { Database } from '@nocobase/database';
3
2
  import UsersPlugin from '@nocobase/plugin-users';
3
+ import { MockServer } from '@nocobase/test';
4
4
 
5
5
  import { prepareApp } from './prepare';
6
6
 
@@ -28,7 +28,7 @@ describe('role check action', () => {
28
28
  roles: ['test']
29
29
  }
30
30
  });
31
- const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin;
31
+ const userPlugin = app.getPlugin('users') as UsersPlugin;
32
32
  const agent = app.agent().auth(userPlugin.jwtService.sign({
33
33
  userId: user.get('id'),
34
34
  }), { type: 'bearer' });
@@ -1,6 +1,6 @@
1
1
  import { Database, Model } from '@nocobase/database';
2
- import UsersPlugin from '@nocobase/plugin-users';
3
2
  import { CollectionRepository } from '@nocobase/plugin-collection-manager';
3
+ import UsersPlugin from '@nocobase/plugin-users';
4
4
  import { MockServer } from '@nocobase/test';
5
5
  import { prepareApp } from './prepare';
6
6
 
@@ -32,7 +32,7 @@ describe('role resource api', () => {
32
32
  }
33
33
  });
34
34
 
35
- const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin;
35
+ const userPlugin = app.getPlugin('users') as UsersPlugin;
36
36
  adminAgent = app.agent().auth(userPlugin.jwtService.sign({
37
37
  userId: admin.get('id'),
38
38
  }), { type: 'bearer' });
@@ -12,12 +12,12 @@ describe('role', () => {
12
12
  beforeEach(async () => {
13
13
  api = mockServer();
14
14
  await api.cleanDb();
15
- api.plugin(UsersPlugin);
16
- api.plugin(PluginACL);
15
+ api.plugin(UsersPlugin, { name: 'users' });
16
+ api.plugin(PluginACL, { name: 'acl' });
17
17
  await api.loadAndInstall();
18
18
 
19
19
  db = api.db;
20
- usersPlugin = api.getPlugin('@nocobase/plugin-users');
20
+ usersPlugin = api.getPlugin('users');
21
21
  });
22
22
 
23
23
  afterEach(async () => {
@@ -1,6 +1,6 @@
1
- import { MockServer } from '@nocobase/test';
2
1
  import { Database, Model } from '@nocobase/database';
3
2
  import UsersPlugin from '@nocobase/plugin-users';
3
+ import { MockServer } from '@nocobase/test';
4
4
 
5
5
  import { prepareApp } from './prepare';
6
6
 
@@ -36,7 +36,7 @@ describe('role api', () => {
36
36
  },
37
37
  });
38
38
 
39
- const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin;
39
+ const userPlugin = app.getPlugin('users') as UsersPlugin;
40
40
  adminAgent = app.agent().auth(
41
41
  userPlugin.jwtService.sign({
42
42
  userId: admin.get('id'),
@@ -1,7 +1,7 @@
1
- import { prepareApp } from './prepare';
2
- import { MockServer } from '@nocobase/test';
3
1
  import { Database } from '@nocobase/database';
4
2
  import UsersPlugin from '@nocobase/plugin-users';
3
+ import { MockServer } from '@nocobase/test';
4
+ import { prepareApp } from './prepare';
5
5
 
6
6
  describe('scope api', () => {
7
7
  let app: MockServer;
@@ -25,7 +25,7 @@ describe('scope api', () => {
25
25
  }
26
26
  });
27
27
 
28
- const userPlugin = app.getPlugin('@nocobase/plugin-users') as UsersPlugin;
28
+ const userPlugin = app.getPlugin('users') as UsersPlugin;
29
29
  adminAgent = app.agent().auth(userPlugin.jwtService.sign({
30
30
  userId: admin.get('id'),
31
31
  }), { type: 'bearer' });
@@ -15,7 +15,7 @@ describe('role', () => {
15
15
  api = await prepareApp();
16
16
 
17
17
  db = api.db;
18
- usersPlugin = api.getPlugin('@nocobase/plugin-users');
18
+ usersPlugin = api.getPlugin('users');
19
19
 
20
20
  ctx = {
21
21
  db,
@@ -18,7 +18,7 @@ describe('actions', () => {
18
18
  app = await prepareApp();
19
19
  db = app.db;
20
20
 
21
- pluginUser = app.getPlugin('@nocobase/plugin-users');
21
+ pluginUser = app.getPlugin('users');
22
22
  adminUser = await db.getRepository('users').findOne({
23
23
  filter: {
24
24
  email: process.env.INIT_ROOT_EMAIL
package/src/server.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { Context } from '@nocobase/actions';
2
2
  import { Collection } from '@nocobase/database';
3
- import UsersPlugin from '@nocobase/plugin-users';
4
3
  import { Plugin } from '@nocobase/server';
5
4
  import { resolve } from 'path';
6
5
  import { availableActionResource } from './actions/available-actions';
@@ -269,12 +268,49 @@ export class PluginACL extends Plugin {
269
268
  });
270
269
 
271
270
  // sync database role data to acl
272
- this.app.on('beforeStart', async () => {
273
- await this.writeRolesToACL();
271
+ this.app.on('afterLoad', async (app, options) => {
272
+ if (options?.method === 'install') {
273
+ return;
274
+ }
275
+ const exists = await this.app.db.collectionExistsInDb('roles');
276
+ if (exists) {
277
+ await this.writeRolesToACL();
278
+ }
279
+ });
280
+
281
+ this.app.on('afterInstall', async (app, options) => {
282
+ const exists = await this.app.db.collectionExistsInDb('roles');
283
+ if (exists) {
284
+ await this.writeRolesToACL();
285
+ }
286
+ });
287
+
288
+ this.app.on('afterInstallPlugin', async (plugin) => {
289
+ if (plugin.getName() !== 'users') {
290
+ return;
291
+ }
292
+ const User = this.db.getCollection('users');
293
+ await User.repository.update({
294
+ values: {
295
+ roles: ['root', 'admin', 'member'],
296
+ },
297
+ forceUpdate: true,
298
+ });
299
+
300
+ const RolesUsers = this.db.getCollection('rolesUsers');
301
+ await RolesUsers.repository.update({
302
+ filter: {
303
+ userId: 1,
304
+ roleName: 'root',
305
+ },
306
+ values: {
307
+ default: true,
308
+ },
309
+ });
274
310
  });
275
311
 
276
312
  this.app.on('beforeInstallPlugin', async (plugin) => {
277
- if (plugin.constructor.name !== 'UsersPlugin') {
313
+ if (plugin.getName() !== 'users') {
278
314
  return;
279
315
  }
280
316
  const roles = this.app.db.getRepository('roles');
@@ -320,8 +356,7 @@ export class PluginACL extends Plugin {
320
356
  });
321
357
  });
322
358
 
323
- const usersPlugin = this.app.pm.get('@nocobase/plugin-users') as UsersPlugin;
324
- usersPlugin.tokenMiddleware.use(setCurrentRole);
359
+ this.app.resourcer.use(setCurrentRole, { tag: 'setCurrentRole', before: 'acl', after: 'parseToken' });
325
360
 
326
361
  this.app.acl.allow('users', 'setDefaultRole', 'loggedIn');
327
362
 
@@ -416,38 +451,12 @@ export class PluginACL extends Plugin {
416
451
  if (repo) {
417
452
  await repo.db2cm('roles');
418
453
  }
419
-
420
- const User = this.db.getCollection('users');
421
-
422
- await User.repository.update({
423
- values: {
424
- roles: ['root', 'admin', 'member'],
425
- },
426
- forceUpdate: true,
427
- });
428
-
429
- const RolesUsers = this.db.getCollection('rolesUsers');
430
- await RolesUsers.repository.update({
431
- filter: {
432
- userId: 1,
433
- roleName: 'root',
434
- },
435
- values: {
436
- default: true,
437
- },
438
- });
439
454
  }
440
455
 
441
456
  async load() {
442
457
  await this.app.db.import({
443
458
  directory: resolve(__dirname, 'collections'),
444
459
  });
445
-
446
- this.app.resourcer.use(this.acl.middleware());
447
- }
448
-
449
- getName(): string {
450
- return this.getPackageName(__dirname);
451
460
  }
452
461
  }
453
462