@nocobase/plugin-acl 0.7.2-alpha.7 → 0.7.4-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.
@@ -0,0 +1,32 @@
1
+ export async function setCurrentRole(ctx, next) {
2
+ let currentRole = ctx.get('X-Role');
3
+
4
+ if (currentRole === 'anonymous') {
5
+ ctx.state.currentRole = currentRole;
6
+ return next();
7
+ }
8
+
9
+ if (!ctx.state.currentUser) {
10
+ return next();
11
+ }
12
+
13
+ const repository = ctx.db.getRepository('users.roles', ctx.state.currentUser.id);
14
+ const roles = await repository.find();
15
+ ctx.state.currentUser.setDataValue('roles', roles);
16
+
17
+ if (roles.length == 1) {
18
+ currentRole = roles[0].name;
19
+ } else if (roles.length > 1) {
20
+ const role = roles.find((item) => item.name === currentRole);
21
+ if (!role) {
22
+ const defaultRole = roles.find((item) => item?.rolesUsers?.default);
23
+ currentRole = (defaultRole || roles[0])?.name;
24
+ }
25
+ }
26
+
27
+ if (currentRole) {
28
+ ctx.state.currentRole = currentRole;
29
+ }
30
+
31
+ await next();
32
+ }
package/src/server.ts CHANGED
@@ -1,10 +1,13 @@
1
1
  import { Context } from '@nocobase/actions';
2
2
  import { Collection } from '@nocobase/database';
3
+ import UsersPlugin from '@nocobase/plugin-users';
3
4
  import { Plugin } from '@nocobase/server';
4
5
  import { resolve } from 'path';
5
6
  import { availableActionResource } from './actions/available-actions';
6
7
  import { checkAction } from './actions/role-check';
7
8
  import { roleCollectionsResource } from './actions/role-collections';
9
+ import { setDefaultRole } from './actions/user-setDefaultRole';
10
+ import { setCurrentRole } from './middlewares/setCurrentRole';
8
11
  import { RoleModel } from './model/RoleModel';
9
12
  import { RoleResourceActionModel } from './model/RoleResourceActionModel';
10
13
  import { RoleResourceModel } from './model/RoleResourceModel';
@@ -134,6 +137,22 @@ export class PluginACL extends Plugin {
134
137
 
135
138
  this.app.resourcer.registerActionHandler('roles:check', checkAction);
136
139
 
140
+ this.app.resourcer.registerActionHandler(`users:setDefaultRole`, setDefaultRole);
141
+
142
+ this.db.on('users.afterCreateWithAssociations', async (model, options) => {
143
+ const { transaction } = options;
144
+ const repository = this.app.db.getRepository('roles');
145
+ const defaultRole = await repository.findOne({
146
+ filter: {
147
+ default: true,
148
+ },
149
+ transaction,
150
+ });
151
+ if (defaultRole && (await model.countRoles({ transaction })) == 0) {
152
+ await model.addRoles(defaultRole, { transaction });
153
+ }
154
+ });
155
+
137
156
  this.app.db.on('roles.afterSaveWithAssociations', async (model, options) => {
138
157
  const { transaction } = options;
139
158
 
@@ -271,7 +290,7 @@ export class PluginACL extends Plugin {
271
290
  title: '{{t("Admin")}}',
272
291
  allowConfigure: true,
273
292
  allowNewMenu: true,
274
- strategy: { actions: ['create', 'export', 'view', 'update', 'destroy'] },
293
+ strategy: { actions: ['create', 'view', 'update', 'destroy'] },
275
294
  },
276
295
  {
277
296
  name: 'member',
@@ -301,6 +320,11 @@ export class PluginACL extends Plugin {
301
320
  });
302
321
  });
303
322
 
323
+ const usersPlugin = this.app.pm.get('@nocobase/plugin-users') as UsersPlugin;
324
+ usersPlugin.tokenMiddleware.use(setCurrentRole);
325
+
326
+ this.app.acl.allow('users', 'setDefaultRole', 'loggedIn');
327
+
304
328
  this.app.acl.allow('roles', 'check', 'loggedIn');
305
329
  this.app.acl.allow('roles', ['create', 'update', 'destroy'], 'allowConfigure');
306
330
 
@@ -392,6 +416,24 @@ export class PluginACL extends Plugin {
392
416
  if (repo) {
393
417
  await repo.db2cm('roles');
394
418
  }
419
+
420
+ const User = this.db.getCollection('users');
421
+ await User.repository.update({
422
+ values: {
423
+ roles: ['root', 'admin', 'member']
424
+ }
425
+ });
426
+
427
+ const RolesUsers = this.db.getCollection('rolesUsers');
428
+ await RolesUsers.repository.update({
429
+ filter: {
430
+ userId: 1,
431
+ roleName: 'root'
432
+ },
433
+ values: {
434
+ default: true
435
+ }
436
+ });
395
437
  }
396
438
 
397
439
  async load() {