@nocobase/plugin-acl 0.11.1-alpha.5 → 0.12.0-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.
- package/client.d.ts +2 -3
- package/client.js +1 -1
- package/dist/client/index.js +11 -0
- package/{lib/server → dist}/index.d.ts +1 -0
- package/dist/index.js +18 -0
- package/{src/server/actions/available-actions.ts → dist/server/actions/available-actions.js} +7 -5
- package/{src/server/actions/role-check.ts → dist/server/actions/role-check.js} +17 -22
- package/dist/server/actions/role-collections.js +53 -0
- package/dist/server/actions/user-setDefaultRole.js +43 -0
- package/dist/server/collections/roles-users.js +10 -0
- package/dist/server/collections/roles.js +103 -0
- package/dist/server/collections/rolesResources.js +35 -0
- package/dist/server/collections/rolesResourcesActions.js +33 -0
- package/dist/server/collections/rolesResourcesScopes.js +27 -0
- package/dist/server/collections/users.js +35 -0
- package/dist/server/index.js +11 -0
- package/dist/server/middlewares/setCurrentRole.js +31 -0
- package/dist/server/migrations/20221214072638-set-role-snippets.js +25 -0
- package/dist/server/model/RoleModel.js +23 -0
- package/dist/server/model/RoleResourceActionModel.js +64 -0
- package/dist/server/model/RoleResourceModel.js +55 -0
- package/dist/server/server.js +709 -0
- package/package.json +12 -21
- package/server.d.ts +3 -3
- package/server.js +1 -1
- package/lib/client/index.js +0 -22
- package/lib/index.js +0 -13
- package/lib/server/actions/available-actions.js +0 -34
- package/lib/server/actions/role-check.js +0 -77
- package/lib/server/actions/role-collections.js +0 -98
- package/lib/server/actions/user-setDefaultRole.js +0 -52
- package/lib/server/collections/roles-users.js +0 -16
- package/lib/server/collections/roles.js +0 -92
- package/lib/server/collections/rolesResources.js +0 -31
- package/lib/server/collections/rolesResourcesActions.js +0 -31
- package/lib/server/collections/rolesResourcesScopes.js +0 -25
- package/lib/server/collections/users.js +0 -41
- package/lib/server/index.js +0 -13
- package/lib/server/middlewares/setCurrentRole.js +0 -45
- package/lib/server/migrations/20221214072638-set-role-snippets.js +0 -43
- package/lib/server/model/RoleModel.js +0 -35
- package/lib/server/model/RoleResourceActionModel.js +0 -91
- package/lib/server/model/RoleResourceModel.js +0 -106
- package/lib/server/server.js +0 -947
- package/src/client/index.ts +0 -8
- package/src/index.ts +0 -1
- package/src/server/__tests__/acl.test.ts +0 -835
- package/src/server/__tests__/actions.test.ts +0 -141
- package/src/server/__tests__/association-field.test.ts +0 -413
- package/src/server/__tests__/configuration.test.ts +0 -70
- package/src/server/__tests__/list-action.test.ts +0 -446
- package/src/server/__tests__/middleware.test.ts +0 -210
- package/src/server/__tests__/own.test.ts +0 -124
- package/src/server/__tests__/prepare.ts +0 -20
- package/src/server/__tests__/role-check.test.ts +0 -46
- package/src/server/__tests__/role-resource.test.ts +0 -177
- package/src/server/__tests__/role-user.test.ts +0 -127
- package/src/server/__tests__/role.test.ts +0 -118
- package/src/server/__tests__/scope.test.ts +0 -55
- package/src/server/__tests__/setCurrentRole.test.ts +0 -86
- package/src/server/__tests__/snippets.test.ts +0 -35
- package/src/server/__tests__/users.test.ts +0 -136
- package/src/server/__tests__/write-role-to-acl.test.ts +0 -41
- package/src/server/actions/role-collections.ts +0 -95
- package/src/server/actions/user-setDefaultRole.ts +0 -47
- package/src/server/collections/roles-users.ts +0 -8
- package/src/server/collections/roles.ts +0 -101
- package/src/server/collections/rolesResources.ts +0 -33
- package/src/server/collections/rolesResourcesActions.ts +0 -31
- package/src/server/collections/rolesResourcesScopes.ts +0 -25
- package/src/server/collections/users.ts +0 -31
- package/src/server/index.ts +0 -1
- package/src/server/middlewares/setCurrentRole.ts +0 -35
- package/src/server/migrations/20221214072638-set-role-snippets.ts +0 -23
- package/src/server/model/RoleModel.ts +0 -23
- package/src/server/model/RoleResourceActionModel.ts +0 -95
- package/src/server/model/RoleResourceModel.ts +0 -74
- package/src/server/server.ts +0 -854
- /package/{lib → dist}/client/index.d.ts +0 -0
- /package/{lib → dist}/server/actions/available-actions.d.ts +0 -0
- /package/{lib → dist}/server/actions/role-check.d.ts +0 -0
- /package/{lib → dist}/server/actions/role-collections.d.ts +0 -0
- /package/{lib → dist}/server/actions/user-setDefaultRole.d.ts +0 -0
- /package/{lib → dist}/server/collections/roles-users.d.ts +0 -0
- /package/{lib → dist}/server/collections/roles.d.ts +0 -0
- /package/{lib → dist}/server/collections/rolesResources.d.ts +0 -0
- /package/{lib → dist}/server/collections/rolesResourcesActions.d.ts +0 -0
- /package/{lib → dist}/server/collections/rolesResourcesScopes.d.ts +0 -0
- /package/{lib → dist}/server/collections/users.d.ts +0 -0
- /package/{lib → dist/server}/index.d.ts +0 -0
- /package/{lib → dist}/server/middlewares/setCurrentRole.d.ts +0 -0
- /package/{lib → dist}/server/migrations/20221214072638-set-role-snippets.d.ts +0 -0
- /package/{lib → dist}/server/model/RoleModel.d.ts +0 -0
- /package/{lib → dist}/server/model/RoleResourceActionModel.d.ts +0 -0
- /package/{lib → dist}/server/model/RoleResourceModel.d.ts +0 -0
- /package/{lib → dist}/server/server.d.ts +0 -0
package/client.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export { default } from './
|
|
3
|
-
|
|
1
|
+
export * from './dist/client';
|
|
2
|
+
export { default } from './dist/client';
|
package/client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports = require('./
|
|
1
|
+
module.exports = require('./dist/client/index.js');
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
(function(global, factory) {
|
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("@nocobase/client")) : typeof define === "function" && define.amd ? define(["exports", "@nocobase/client"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["@nocobase/plugin-acl"] = {}, global["@nocobase/client"]));
|
|
3
|
+
})(this, function(exports2, client) {
|
|
4
|
+
"use strict";
|
|
5
|
+
class AclPlugin extends client.Plugin {
|
|
6
|
+
async load() {
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
exports2.default = AclPlugin;
|
|
10
|
+
Object.defineProperties(exports2, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
11
|
+
});
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var server = require('./server');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
Object.defineProperty(exports, 'default', {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function () { return server__namespace.default; }
|
|
12
|
+
});
|
|
13
|
+
Object.keys(server).forEach(function (k) {
|
|
14
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () { return server[k]; }
|
|
17
|
+
});
|
|
18
|
+
});
|
package/{src/server/actions/available-actions.ts → dist/server/actions/available-actions.js}
RENAMED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
1
3
|
const availableActionResource = {
|
|
2
|
-
name:
|
|
4
|
+
name: "availableActions",
|
|
3
5
|
actions: {
|
|
4
6
|
async list(ctx, next) {
|
|
5
7
|
const acl = ctx.app.acl;
|
|
@@ -7,12 +9,12 @@ const availableActionResource = {
|
|
|
7
9
|
ctx.body = Array.from(availableActions.entries()).map(([, { name, options }]) => {
|
|
8
10
|
return {
|
|
9
11
|
...options,
|
|
10
|
-
name
|
|
12
|
+
name
|
|
11
13
|
};
|
|
12
14
|
});
|
|
13
15
|
await next();
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
16
18
|
};
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
exports.availableActionResource = availableActionResource;
|
|
@@ -1,50 +1,45 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const map2obj = (map) => {
|
|
2
4
|
const obj = {};
|
|
3
5
|
for (const [key, value] of map) {
|
|
4
6
|
obj[key] = value;
|
|
5
7
|
}
|
|
6
8
|
return obj;
|
|
7
9
|
};
|
|
8
|
-
|
|
9
|
-
export async function checkAction(ctx, next) {
|
|
10
|
+
async function checkAction(ctx, next) {
|
|
10
11
|
const currentRole = ctx.state.currentRole;
|
|
11
|
-
|
|
12
|
-
const roleInstance = await ctx.db.getRepository('roles').findOne({
|
|
12
|
+
const roleInstance = await ctx.db.getRepository("roles").findOne({
|
|
13
13
|
filter: {
|
|
14
|
-
name: currentRole
|
|
14
|
+
name: currentRole
|
|
15
15
|
},
|
|
16
|
-
appends: [
|
|
16
|
+
appends: ["menuUiSchemas"]
|
|
17
17
|
});
|
|
18
|
-
|
|
19
18
|
if (!roleInstance) {
|
|
20
19
|
throw new Error(`Role ${currentRole} not exists`);
|
|
21
20
|
}
|
|
22
|
-
|
|
23
|
-
const anonymous = await ctx.db.getRepository('roles').findOne({
|
|
21
|
+
const anonymous = await ctx.db.getRepository("roles").findOne({
|
|
24
22
|
filter: {
|
|
25
|
-
name:
|
|
26
|
-
}
|
|
23
|
+
name: "anonymous"
|
|
24
|
+
}
|
|
27
25
|
});
|
|
28
|
-
|
|
29
26
|
let role = ctx.app.acl.getRole(currentRole);
|
|
30
|
-
|
|
31
27
|
if (!role) {
|
|
32
|
-
await ctx.app.emitAsync(
|
|
28
|
+
await ctx.app.emitAsync("acl:writeRoleToACL", roleInstance);
|
|
33
29
|
role = ctx.app.acl.getRole(currentRole);
|
|
34
30
|
}
|
|
35
|
-
|
|
36
31
|
const availableActions = ctx.app.acl.getAvailableActions();
|
|
37
|
-
|
|
38
32
|
ctx.body = {
|
|
39
33
|
...role.toJSON(),
|
|
40
34
|
availableActions: [...availableActions.keys()],
|
|
41
35
|
resources: [...role.resources.keys()],
|
|
42
36
|
actionAlias: map2obj(ctx.app.acl.actionAlias),
|
|
43
|
-
allowAll: currentRole ===
|
|
44
|
-
allowConfigure: roleInstance.get(
|
|
45
|
-
allowMenuItemIds: roleInstance.get(
|
|
46
|
-
allowAnonymous: !!anonymous
|
|
37
|
+
allowAll: currentRole === "root",
|
|
38
|
+
allowConfigure: roleInstance.get("allowConfigure"),
|
|
39
|
+
allowMenuItemIds: roleInstance.get("menuUiSchemas").map((uiSchema) => uiSchema.get("x-uid")),
|
|
40
|
+
allowAnonymous: !!anonymous
|
|
47
41
|
};
|
|
48
|
-
|
|
49
42
|
await next();
|
|
50
43
|
}
|
|
44
|
+
|
|
45
|
+
exports.checkAction = checkAction;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function totalPage(total, pageSize) {
|
|
4
|
+
return Math.ceil(total / pageSize);
|
|
5
|
+
}
|
|
6
|
+
const roleCollectionsResource = {
|
|
7
|
+
name: "roles.collections",
|
|
8
|
+
actions: {
|
|
9
|
+
async list(ctx, next) {
|
|
10
|
+
const role = ctx.action.params.associatedIndex;
|
|
11
|
+
const { page = 1, pageSize = 20 } = ctx.action.params;
|
|
12
|
+
const db = ctx.db;
|
|
13
|
+
const collectionRepository = db.getRepository("collections");
|
|
14
|
+
db.getRepository("fields");
|
|
15
|
+
const [collections, count] = await collectionRepository.findAndCount({
|
|
16
|
+
filter: ctx.action.params.filter,
|
|
17
|
+
sort: "sort"
|
|
18
|
+
});
|
|
19
|
+
const roleResources = await db.getRepository("rolesResources").find({
|
|
20
|
+
filter: {
|
|
21
|
+
roleName: role
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
const roleResourcesNames = roleResources.map((roleResource) => roleResource.get("name"));
|
|
25
|
+
const roleResourceActionResourceNames = roleResources.filter((roleResources2) => roleResources2.get("usingActionsConfig")).map((roleResources2) => roleResources2.get("name"));
|
|
26
|
+
const items = collections.map((collection, i) => {
|
|
27
|
+
const exists = roleResourcesNames.includes(collection.get("name"));
|
|
28
|
+
const usingConfig = roleResourceActionResourceNames.includes(collection.get("name")) ? "resourceAction" : "strategy";
|
|
29
|
+
db.getCollection(collection.get("name"));
|
|
30
|
+
return {
|
|
31
|
+
type: "collection",
|
|
32
|
+
name: collection.get("name"),
|
|
33
|
+
collectionName: collection.get("name"),
|
|
34
|
+
title: collection.get("title"),
|
|
35
|
+
roleName: role,
|
|
36
|
+
usingConfig,
|
|
37
|
+
exists
|
|
38
|
+
// children: children.length > 0 ? children : null,
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
ctx.body = {
|
|
42
|
+
count,
|
|
43
|
+
rows: items,
|
|
44
|
+
page: Number(page),
|
|
45
|
+
pageSize: Number(pageSize),
|
|
46
|
+
totalPage: totalPage(count, pageSize)
|
|
47
|
+
};
|
|
48
|
+
await next();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
exports.roleCollectionsResource = roleCollectionsResource;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
async function setDefaultRole(ctx, next) {
|
|
4
|
+
const {
|
|
5
|
+
values: { roleName }
|
|
6
|
+
} = ctx.action.params;
|
|
7
|
+
const {
|
|
8
|
+
db,
|
|
9
|
+
state: { currentUser },
|
|
10
|
+
action: {
|
|
11
|
+
params: { values }
|
|
12
|
+
}
|
|
13
|
+
} = ctx;
|
|
14
|
+
if (values.roleName == "anonymous") {
|
|
15
|
+
return next();
|
|
16
|
+
}
|
|
17
|
+
const repository = db.getRepository("rolesUsers");
|
|
18
|
+
await db.sequelize.transaction(async (transaction) => {
|
|
19
|
+
await repository.update({
|
|
20
|
+
filter: {
|
|
21
|
+
userId: currentUser.get("id")
|
|
22
|
+
},
|
|
23
|
+
values: {
|
|
24
|
+
default: false
|
|
25
|
+
},
|
|
26
|
+
transaction
|
|
27
|
+
});
|
|
28
|
+
await repository.update({
|
|
29
|
+
filter: {
|
|
30
|
+
userId: currentUser.get("id"),
|
|
31
|
+
roleName
|
|
32
|
+
},
|
|
33
|
+
values: {
|
|
34
|
+
default: true
|
|
35
|
+
},
|
|
36
|
+
transaction
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
ctx.body = "ok";
|
|
40
|
+
await next();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
exports.setDefaultRole = setDefaultRole;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var roles_default = {
|
|
4
|
+
namespace: "acl.acl",
|
|
5
|
+
duplicator: {
|
|
6
|
+
dumpable: "required",
|
|
7
|
+
with: "uiSchemas"
|
|
8
|
+
},
|
|
9
|
+
name: "roles",
|
|
10
|
+
title: '{{t("Roles")}}',
|
|
11
|
+
autoGenId: false,
|
|
12
|
+
model: "RoleModel",
|
|
13
|
+
filterTargetKey: "name",
|
|
14
|
+
// targetKey: 'name',
|
|
15
|
+
sortable: true,
|
|
16
|
+
fields: [
|
|
17
|
+
{
|
|
18
|
+
type: "uid",
|
|
19
|
+
name: "name",
|
|
20
|
+
prefix: "r_",
|
|
21
|
+
primaryKey: true,
|
|
22
|
+
interface: "input",
|
|
23
|
+
uiSchema: {
|
|
24
|
+
type: "string",
|
|
25
|
+
title: '{{t("Role UID")}}',
|
|
26
|
+
"x-component": "Input"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
type: "string",
|
|
31
|
+
name: "title",
|
|
32
|
+
unique: true,
|
|
33
|
+
interface: "input",
|
|
34
|
+
uiSchema: {
|
|
35
|
+
type: "string",
|
|
36
|
+
title: '{{t("Role name")}}',
|
|
37
|
+
"x-component": "Input"
|
|
38
|
+
},
|
|
39
|
+
translation: true
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
type: "boolean",
|
|
43
|
+
name: "default"
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
type: "string",
|
|
47
|
+
name: "description"
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
type: "json",
|
|
51
|
+
name: "strategy"
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
type: "boolean",
|
|
55
|
+
name: "default",
|
|
56
|
+
defaultValue: false
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
type: "boolean",
|
|
60
|
+
name: "hidden",
|
|
61
|
+
defaultValue: false
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
type: "boolean",
|
|
65
|
+
name: "allowConfigure"
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
type: "boolean",
|
|
69
|
+
name: "allowNewMenu"
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
type: "belongsToMany",
|
|
73
|
+
name: "menuUiSchemas",
|
|
74
|
+
target: "uiSchemas",
|
|
75
|
+
targetKey: "x-uid"
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
type: "hasMany",
|
|
79
|
+
name: "resources",
|
|
80
|
+
target: "rolesResources",
|
|
81
|
+
sourceKey: "name",
|
|
82
|
+
targetKey: "name"
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
type: "set",
|
|
86
|
+
name: "snippets",
|
|
87
|
+
defaultValue: ["!ui.*", "!pm", "!pm.*"]
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
type: "belongsToMany",
|
|
91
|
+
name: "users",
|
|
92
|
+
target: "users",
|
|
93
|
+
foreignKey: "roleName",
|
|
94
|
+
otherKey: "userId",
|
|
95
|
+
onDelete: "CASCADE",
|
|
96
|
+
sourceKey: "name",
|
|
97
|
+
targetKey: "id",
|
|
98
|
+
through: "rolesUsers"
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
module.exports = roles_default;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var rolesResources_default = {
|
|
4
|
+
namespace: "acl.acl",
|
|
5
|
+
duplicator: "required",
|
|
6
|
+
name: "rolesResources",
|
|
7
|
+
model: "RoleResourceModel",
|
|
8
|
+
indexes: [
|
|
9
|
+
{
|
|
10
|
+
unique: true,
|
|
11
|
+
fields: ["roleName", "name"]
|
|
12
|
+
}
|
|
13
|
+
],
|
|
14
|
+
fields: [
|
|
15
|
+
{
|
|
16
|
+
type: "belongsTo",
|
|
17
|
+
name: "role"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
type: "string",
|
|
21
|
+
name: "name"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
type: "boolean",
|
|
25
|
+
name: "usingActionsConfig"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
type: "hasMany",
|
|
29
|
+
name: "actions",
|
|
30
|
+
target: "rolesResourcesActions"
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
module.exports = rolesResources_default;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var rolesResourcesActions_default = {
|
|
4
|
+
namespace: "acl.acl",
|
|
5
|
+
duplicator: "required",
|
|
6
|
+
name: "rolesResourcesActions",
|
|
7
|
+
model: "RoleResourceActionModel",
|
|
8
|
+
fields: [
|
|
9
|
+
{
|
|
10
|
+
type: "belongsTo",
|
|
11
|
+
name: "resource",
|
|
12
|
+
foreignKey: "rolesResourceId",
|
|
13
|
+
target: "rolesResources"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
type: "string",
|
|
17
|
+
name: "name"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
type: "array",
|
|
21
|
+
name: "fields",
|
|
22
|
+
defaultValue: []
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
type: "belongsTo",
|
|
26
|
+
name: "scope",
|
|
27
|
+
target: "rolesResourcesScopes",
|
|
28
|
+
onDelete: "RESTRICT"
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
module.exports = rolesResourcesActions_default;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var rolesResourcesScopes_default = {
|
|
4
|
+
namespace: "acl.acl",
|
|
5
|
+
duplicator: "required",
|
|
6
|
+
name: "rolesResourcesScopes",
|
|
7
|
+
fields: [
|
|
8
|
+
{
|
|
9
|
+
type: "uid",
|
|
10
|
+
name: "key"
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
type: "string",
|
|
14
|
+
name: "name"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
type: "string",
|
|
18
|
+
name: "resourceName"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
type: "json",
|
|
22
|
+
name: "scope"
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
module.exports = rolesResourcesScopes_default;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var database = require('@nocobase/database');
|
|
4
|
+
|
|
5
|
+
var users_default = database.extend({
|
|
6
|
+
name: "users",
|
|
7
|
+
fields: [
|
|
8
|
+
{
|
|
9
|
+
interface: "m2m",
|
|
10
|
+
type: "belongsToMany",
|
|
11
|
+
name: "roles",
|
|
12
|
+
target: "roles",
|
|
13
|
+
foreignKey: "userId",
|
|
14
|
+
otherKey: "roleName",
|
|
15
|
+
onDelete: "CASCADE",
|
|
16
|
+
sourceKey: "id",
|
|
17
|
+
targetKey: "name",
|
|
18
|
+
through: "rolesUsers",
|
|
19
|
+
uiSchema: {
|
|
20
|
+
type: "array",
|
|
21
|
+
title: '{{t("Roles")}}',
|
|
22
|
+
"x-component": "AssociationField",
|
|
23
|
+
"x-component-props": {
|
|
24
|
+
multiple: true,
|
|
25
|
+
fieldNames: {
|
|
26
|
+
label: "title",
|
|
27
|
+
value: "name"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
module.exports = users_default;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
async function setCurrentRole(ctx, next) {
|
|
4
|
+
var _a, _b;
|
|
5
|
+
const currentRole = ctx.get("X-Role");
|
|
6
|
+
if (currentRole === "anonymous") {
|
|
7
|
+
ctx.state.currentRole = currentRole;
|
|
8
|
+
return next();
|
|
9
|
+
}
|
|
10
|
+
if (!ctx.state.currentUser) {
|
|
11
|
+
return next();
|
|
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
|
+
if (currentRole) {
|
|
17
|
+
ctx.state.currentRole = (_a = roles.find((role) => role.name === currentRole)) == null ? void 0 : _a.name;
|
|
18
|
+
} else {
|
|
19
|
+
const defaultRole = roles.find((item) => {
|
|
20
|
+
var _a2;
|
|
21
|
+
return (_a2 = item == null ? void 0 : item.rolesUsers) == null ? void 0 : _a2.default;
|
|
22
|
+
});
|
|
23
|
+
ctx.state.currentRole = (_b = defaultRole || roles[0]) == null ? void 0 : _b.name;
|
|
24
|
+
}
|
|
25
|
+
if (!ctx.state.currentRole) {
|
|
26
|
+
return ctx.throw(401, "User role not found");
|
|
27
|
+
}
|
|
28
|
+
await next();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
exports.setCurrentRole = setCurrentRole;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var server = require('@nocobase/server');
|
|
4
|
+
|
|
5
|
+
class set_role_snippets_default extends server.Migration {
|
|
6
|
+
async up() {
|
|
7
|
+
const result = await this.app.version.satisfies("<0.9.3-alpha.1");
|
|
8
|
+
if (!result) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
await this.app.db.getRepository("roles").update({
|
|
12
|
+
filter: {
|
|
13
|
+
$or: [{ allowConfigure: true }, { name: "root" }]
|
|
14
|
+
},
|
|
15
|
+
values: {
|
|
16
|
+
snippets: ["ui.*", "pm", "pm.*"],
|
|
17
|
+
allowConfigure: false
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
async down() {
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = set_role_snippets_default;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var database = require('@nocobase/database');
|
|
4
|
+
|
|
5
|
+
class RoleModel extends database.Model {
|
|
6
|
+
writeToAcl(options) {
|
|
7
|
+
const { acl } = options;
|
|
8
|
+
const roleName = this.get("name");
|
|
9
|
+
let role = acl.getRole(roleName);
|
|
10
|
+
if (!role) {
|
|
11
|
+
role = acl.define({
|
|
12
|
+
role: roleName
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
role.setStrategy({
|
|
16
|
+
...this.get("strategy") || {},
|
|
17
|
+
allowConfigure: this.get("allowConfigure")
|
|
18
|
+
});
|
|
19
|
+
role.snippets = new Set(this.get("snippets"));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
exports.RoleModel = RoleModel;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var database = require('@nocobase/database');
|
|
4
|
+
|
|
5
|
+
class RoleResourceActionModel extends database.Model {
|
|
6
|
+
async writeToACL(options) {
|
|
7
|
+
var _a;
|
|
8
|
+
const db = this.constructor.database;
|
|
9
|
+
const { resourceName, role, acl, associationFieldsActions, grantHelper } = options;
|
|
10
|
+
const actionName = this.get("name");
|
|
11
|
+
const fields = this.get("fields");
|
|
12
|
+
const actionPath = `${resourceName}:${actionName}`;
|
|
13
|
+
const actionParams = {
|
|
14
|
+
fields
|
|
15
|
+
};
|
|
16
|
+
const scope = await this.getScope();
|
|
17
|
+
if (scope) {
|
|
18
|
+
actionParams["own"] = scope.get("key") === "own";
|
|
19
|
+
actionParams["filter"] = scope.get("scope");
|
|
20
|
+
}
|
|
21
|
+
role.grantAction(actionPath, actionParams);
|
|
22
|
+
const collection = db.getCollection(resourceName);
|
|
23
|
+
if (!collection) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const availableAction = acl.resolveActionAlias(actionName);
|
|
27
|
+
for (const field of fields) {
|
|
28
|
+
const collectionField = collection.getField(field);
|
|
29
|
+
if (!collectionField) {
|
|
30
|
+
console.log(`field ${field} does not exist at ${collection.name}`);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const fieldType = collectionField.get("type");
|
|
34
|
+
const fieldActions = (_a = associationFieldsActions == null ? void 0 : associationFieldsActions[fieldType]) == null ? void 0 : _a[availableAction];
|
|
35
|
+
const fieldTarget = collectionField.get("target");
|
|
36
|
+
if (fieldActions) {
|
|
37
|
+
const associationActions = fieldActions.associationActions || [];
|
|
38
|
+
associationActions.forEach((associationAction) => {
|
|
39
|
+
const actionName2 = `${resourceName}.${collectionField.get("name")}:${associationAction}`;
|
|
40
|
+
role.grantAction(actionName2);
|
|
41
|
+
});
|
|
42
|
+
const targetActions = fieldActions.targetActions || [];
|
|
43
|
+
targetActions.forEach((targetAction) => {
|
|
44
|
+
const targetActionPath = `${fieldTarget}:${targetAction}`;
|
|
45
|
+
const existsAction = role.getActionParams(targetActionPath);
|
|
46
|
+
if (existsAction) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
grantHelper.resourceTargetActionMap.set(`${role.name}.${resourceName}`, [
|
|
50
|
+
...grantHelper.resourceTargetActionMap.get(resourceName) || [],
|
|
51
|
+
targetActionPath
|
|
52
|
+
]);
|
|
53
|
+
grantHelper.targetActionResourceMap.set(targetActionPath, [
|
|
54
|
+
...grantHelper.targetActionResourceMap.get(targetActionPath) || [],
|
|
55
|
+
`${role.name}.${resourceName}`
|
|
56
|
+
]);
|
|
57
|
+
role.grantAction(targetActionPath);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
exports.RoleResourceActionModel = RoleResourceActionModel;
|