@nocobase/plugin-acl 0.17.0-alpha.4 → 0.17.0-alpha.6

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.
@@ -1,9 +1,10 @@
1
1
  module.exports = {
2
- "@nocobase/client": "0.17.0-alpha.4",
3
- "@nocobase/acl": "0.17.0-alpha.4",
4
- "@nocobase/actions": "0.17.0-alpha.4",
5
- "@nocobase/database": "0.17.0-alpha.4",
6
- "@nocobase/server": "0.17.0-alpha.4",
2
+ "@nocobase/client": "0.17.0-alpha.6",
3
+ "@nocobase/acl": "0.17.0-alpha.6",
4
+ "@nocobase/actions": "0.17.0-alpha.6",
5
+ "@nocobase/database": "0.17.0-alpha.6",
6
+ "@nocobase/server": "0.17.0-alpha.6",
7
7
  "async-mutex": "0.3.2",
8
- "lodash": "4.17.21"
8
+ "lodash": "4.17.21",
9
+ "@nocobase/cache": "0.17.0-alpha.6"
9
10
  };
@@ -38,7 +38,7 @@ async function setDefaultRole(ctx, next) {
38
38
  await db.sequelize.transaction(async (transaction) => {
39
39
  await repository.update({
40
40
  filter: {
41
- userId: currentUser.get("id")
41
+ userId: currentUser.id
42
42
  },
43
43
  values: {
44
44
  default: false
@@ -47,7 +47,7 @@ async function setDefaultRole(ctx, next) {
47
47
  });
48
48
  await repository.update({
49
49
  filter: {
50
- userId: currentUser.get("id"),
50
+ userId: currentUser.id,
51
51
  roleName
52
52
  },
53
53
  values: {
@@ -30,9 +30,15 @@ async function setCurrentRole(ctx, next) {
30
30
  if (!ctx.state.currentUser) {
31
31
  return next();
32
32
  }
33
+ const cache = ctx.cache;
33
34
  const repository = ctx.db.getRepository("users.roles", ctx.state.currentUser.id);
34
- const roles = await repository.find();
35
- ctx.state.currentUser.setDataValue("roles", roles);
35
+ const roles = await cache.wrap(
36
+ `roles:${ctx.state.currentUser.id}`,
37
+ () => repository.find({
38
+ raw: true
39
+ })
40
+ );
41
+ ctx.state.currentUser.roles = roles;
36
42
  if (currentRole) {
37
43
  ctx.state.currentRole = (_a = roles.find((role) => role.name === currentRole)) == null ? void 0 : _a.name;
38
44
  } else {
@@ -307,6 +307,14 @@ class PluginACL extends import_server.Plugin {
307
307
  }
308
308
  });
309
309
  });
310
+ this.app.db.on("rolesUsers.afterSave", async (model) => {
311
+ const cache = this.app.cache;
312
+ await cache.del(`roles:${model.get("userId")}`);
313
+ });
314
+ this.app.db.on("rolesUsers.afterDestroy", async (model) => {
315
+ const cache = this.app.cache;
316
+ await cache.del(`roles:${model.get("userId")}`);
317
+ });
310
318
  const writeRolesToACL = async (app, options) => {
311
319
  const exists = await this.app.db.collectionExistsInDb("roles");
312
320
  if (exists) {
@@ -387,6 +395,9 @@ class PluginACL extends import_server.Plugin {
387
395
  ]
388
396
  });
389
397
  });
398
+ this.app.on("beforeSignOut", ({ userId }) => {
399
+ this.app.cache.del(`roles:${userId}`);
400
+ });
390
401
  this.app.resourcer.use(import_setCurrentRole.setCurrentRole, { tag: "setCurrentRole", before: "acl", after: "auth" });
391
402
  this.app.acl.allow("users", "setDefaultRole", "loggedIn");
392
403
  this.app.acl.allow("roles", "check", "loggedIn");
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "displayName.zh-CN": "权限控制",
5
5
  "description": "A simple access control based on roles, resources and actions",
6
6
  "description.zh-CN": "基于角色、资源和操作的权限控制。",
7
- "version": "0.17.0-alpha.4",
7
+ "version": "0.17.0-alpha.6",
8
8
  "license": "AGPL-3.0",
9
9
  "main": "./dist/server/index.js",
10
10
  "devDependencies": {
@@ -17,6 +17,7 @@
17
17
  "peerDependencies": {
18
18
  "@nocobase/acl": "0.x",
19
19
  "@nocobase/actions": "0.x",
20
+ "@nocobase/cache": "0.x",
20
21
  "@nocobase/client": "0.x",
21
22
  "@nocobase/database": "0.x",
22
23
  "@nocobase/server": "0.x",
@@ -28,5 +29,5 @@
28
29
  "url": "git+https://github.com/nocobase/nocobase.git",
29
30
  "directory": "packages/plugins/acl"
30
31
  },
31
- "gitHead": "663b03a3799a70ba1a2bc6a0d686e679331a50ad"
32
+ "gitHead": "f92f8bcdf6d5baf07566381e9425ebca11e19626"
32
33
  }