@nocobase/plugin-acl 0.10.0-alpha.5 → 0.10.1-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.
@@ -42,9 +42,6 @@ function checkAction(_x2, _x3) {
42
42
  function _checkAction() {
43
43
  _checkAction = _asyncToGenerator(function* (ctx, next) {
44
44
  const currentRole = ctx.state.currentRole;
45
- if (!currentRole) {
46
- throw new Error('User role not found');
47
- }
48
45
  const roleInstance = yield ctx.db.getRepository('roles').findOne({
49
46
  filter: {
50
47
  name: currentRole
@@ -1 +1,2 @@
1
- export declare function setCurrentRole(ctx: any, next: any): Promise<any>;
1
+ import { Context } from '@nocobase/actions';
2
+ export declare function setCurrentRole(ctx: Context, next: any): Promise<any>;
@@ -11,7 +11,7 @@ function setCurrentRole(_x, _x2) {
11
11
  }
12
12
  function _setCurrentRole() {
13
13
  _setCurrentRole = _asyncToGenerator(function* (ctx, next) {
14
- let currentRole = ctx.get('X-Role');
14
+ const currentRole = ctx.get('X-Role');
15
15
  if (currentRole === 'anonymous') {
16
16
  ctx.state.currentRole = currentRole;
17
17
  return next();
@@ -22,21 +22,22 @@ function _setCurrentRole() {
22
22
  const repository = ctx.db.getRepository('users.roles', ctx.state.currentUser.id);
23
23
  const roles = yield repository.find();
24
24
  ctx.state.currentUser.setDataValue('roles', roles);
25
- if (roles.length == 1) {
26
- currentRole = roles[0].name;
27
- } else if (roles.length > 1) {
28
- const role = roles.find(item => item.name === currentRole);
29
- if (!role) {
30
- var _ref;
31
- const defaultRole = roles.find(item => {
32
- var _item$rolesUsers;
33
- return item === null || item === void 0 ? void 0 : (_item$rolesUsers = item.rolesUsers) === null || _item$rolesUsers === void 0 ? void 0 : _item$rolesUsers.default;
34
- });
35
- currentRole = (_ref = defaultRole || roles[0]) === null || _ref === void 0 ? void 0 : _ref.name;
36
- }
37
- }
25
+ // 1. If the X-Role is set, use the specified role
38
26
  if (currentRole) {
39
- ctx.state.currentRole = currentRole;
27
+ var _roles$find;
28
+ ctx.state.currentRole = (_roles$find = roles.find(role => role.name === currentRole)) === null || _roles$find === void 0 ? void 0 : _roles$find.name;
29
+ }
30
+ // 2. If the X-Role is not set, use the default role
31
+ else {
32
+ var _ref;
33
+ const defaultRole = roles.find(item => {
34
+ var _item$rolesUsers;
35
+ return item === null || item === void 0 ? void 0 : (_item$rolesUsers = item.rolesUsers) === null || _item$rolesUsers === void 0 ? void 0 : _item$rolesUsers.default;
36
+ });
37
+ ctx.state.currentRole = (_ref = defaultRole || roles[0]) === null || _ref === void 0 ? void 0 : _ref.name;
38
+ }
39
+ if (!ctx.state.currentRole) {
40
+ return ctx.throw(401, 'User role not found');
40
41
  }
41
42
  yield next();
42
43
  });
package/package.json CHANGED
@@ -4,20 +4,20 @@
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.10.0-alpha.5",
7
+ "version": "0.10.1-alpha.1",
8
8
  "license": "AGPL-3.0",
9
9
  "main": "./lib/index.js",
10
10
  "types": "./lib/index.d.ts",
11
11
  "dependencies": {
12
- "@nocobase/acl": "0.10.0-alpha.5",
13
- "@nocobase/database": "0.10.0-alpha.5",
14
- "@nocobase/plugin-users": "0.10.0-alpha.5",
15
- "@nocobase/server": "0.10.0-alpha.5"
12
+ "@nocobase/acl": "0.10.1-alpha.1",
13
+ "@nocobase/database": "0.10.1-alpha.1",
14
+ "@nocobase/plugin-users": "0.10.1-alpha.1",
15
+ "@nocobase/server": "0.10.1-alpha.1"
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",
19
19
  "url": "git+https://github.com/nocobase/nocobase.git",
20
20
  "directory": "packages/plugins/acl"
21
21
  },
22
- "gitHead": "1be8fcdad42688064460761cea22830cf392c7c0"
22
+ "gitHead": "8f415f5e0ee2e72d681f9ab16af5911b52c374a9"
23
23
  }