@tachybase/module-acl 0.23.8
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/.turbo/turbo-build.log +14 -0
- package/LICENSE +201 -0
- package/README.md +11 -0
- package/README.zh-CN.md +11 -0
- package/client.d.ts +2 -0
- package/client.js +1 -0
- package/dist/client/NewRole.d.ts +2 -0
- package/dist/client/RolesManagement.d.ts +2 -0
- package/dist/client/RolesManagerProvider.d.ts +4 -0
- package/dist/client/RolesMenu.d.ts +7 -0
- package/dist/client/hooks/load-more-observer.d.ts +7 -0
- package/dist/client/index.d.ts +8 -0
- package/dist/client/index.js +9 -0
- package/dist/client/locale.d.ts +1 -0
- package/dist/client/permissions/ActionPermissions.d.ts +4 -0
- package/dist/client/permissions/AvailableActions.d.ts +3 -0
- package/dist/client/permissions/GeneralPermissions.d.ts +4 -0
- package/dist/client/permissions/MenuItemsProvider.d.ts +4 -0
- package/dist/client/permissions/MenuPermissions.d.ts +4 -0
- package/dist/client/permissions/Permissions.d.ts +4 -0
- package/dist/client/permissions/PluginPermissions.d.ts +4 -0
- package/dist/client/permissions/RolesResourcesActions.d.ts +3 -0
- package/dist/client/permissions/ScopeSelect.d.ts +3 -0
- package/dist/client/permissions/StrategyActions.d.ts +2 -0
- package/dist/client/permissions/style.d.ts +1 -0
- package/dist/client/roles-manager.d.ts +10 -0
- package/dist/client/schemas/roles.d.ts +55 -0
- package/dist/client/schemas/scopes.d.ts +11 -0
- package/dist/externalVersion.js +19 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +39 -0
- package/dist/locale/en-US.json +6 -0
- package/dist/locale/ko_KR.json +4 -0
- package/dist/locale/zh-CN.json +11 -0
- package/dist/server/actions/available-actions.d.ts +7 -0
- package/dist/server/actions/available-actions.js +42 -0
- package/dist/server/actions/role-check.d.ts +1 -0
- package/dist/server/actions/role-check.js +67 -0
- package/dist/server/actions/role-collections.d.ts +7 -0
- package/dist/server/actions/role-collections.js +75 -0
- package/dist/server/actions/user-setDefaultRole.d.ts +2 -0
- package/dist/server/actions/user-setDefaultRole.js +65 -0
- package/dist/server/collections/roles-users.d.ts +2 -0
- package/dist/server/collections/roles-users.js +30 -0
- package/dist/server/collections/roles.d.ts +2 -0
- package/dist/server/collections/roles.js +119 -0
- package/dist/server/collections/rolesResources.d.ts +2 -0
- package/dist/server/collections/rolesResources.js +55 -0
- package/dist/server/collections/rolesResourcesActions.d.ts +2 -0
- package/dist/server/collections/rolesResourcesActions.js +51 -0
- package/dist/server/collections/rolesResourcesScopes.d.ts +2 -0
- package/dist/server/collections/rolesResourcesScopes.js +45 -0
- package/dist/server/collections/users.d.ts +2 -0
- package/dist/server/collections/users.js +52 -0
- package/dist/server/index.d.ts +6 -0
- package/dist/server/index.js +50 -0
- package/dist/server/middlewares/setCurrentRole.d.ts +2 -0
- package/dist/server/middlewares/setCurrentRole.js +74 -0
- package/dist/server/middlewares/with-acl-meta.d.ts +2 -0
- package/dist/server/middlewares/with-acl-meta.js +242 -0
- package/dist/server/migrations/20221214072638-set-role-snippets.d.ts +6 -0
- package/dist/server/migrations/20221214072638-set-role-snippets.js +43 -0
- package/dist/server/model/RoleModel.d.ts +8 -0
- package/dist/server/model/RoleModel.js +46 -0
- package/dist/server/model/RoleResourceActionModel.d.ts +12 -0
- package/dist/server/model/RoleResourceActionModel.js +85 -0
- package/dist/server/model/RoleResourceModel.d.ts +18 -0
- package/dist/server/model/RoleResourceModel.js +79 -0
- package/dist/server/server.d.ts +34 -0
- package/dist/server/server.js +648 -0
- package/dist/swagger/index.d.ts +366 -0
- package/dist/swagger/index.js +395 -0
- package/package.json +40 -0
- package/server.d.ts +3 -0
- package/server.js +1 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var server_exports = {};
|
|
30
|
+
__export(server_exports, {
|
|
31
|
+
RoleModel: () => import_RoleModel.RoleModel,
|
|
32
|
+
RoleResourceActionModel: () => import_RoleResourceActionModel.RoleResourceActionModel,
|
|
33
|
+
RoleResourceModel: () => import_RoleResourceModel.RoleResourceModel,
|
|
34
|
+
default: () => import_server.default
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(server_exports);
|
|
37
|
+
__reExport(server_exports, require("./middlewares/setCurrentRole"), module.exports);
|
|
38
|
+
__reExport(server_exports, require("./middlewares/with-acl-meta"), module.exports);
|
|
39
|
+
var import_RoleResourceActionModel = require("./model/RoleResourceActionModel");
|
|
40
|
+
var import_RoleResourceModel = require("./model/RoleResourceModel");
|
|
41
|
+
var import_server = __toESM(require("./server"));
|
|
42
|
+
var import_RoleModel = require("./model/RoleModel");
|
|
43
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
44
|
+
0 && (module.exports = {
|
|
45
|
+
RoleModel,
|
|
46
|
+
RoleResourceActionModel,
|
|
47
|
+
RoleResourceModel,
|
|
48
|
+
...require("./middlewares/setCurrentRole"),
|
|
49
|
+
...require("./middlewares/with-acl-meta")
|
|
50
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var setCurrentRole_exports = {};
|
|
19
|
+
__export(setCurrentRole_exports, {
|
|
20
|
+
setCurrentRole: () => setCurrentRole
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(setCurrentRole_exports);
|
|
23
|
+
async function setCurrentRole(ctx, next) {
|
|
24
|
+
var _a, _b;
|
|
25
|
+
const currentRole = ctx.get("X-Role");
|
|
26
|
+
if (currentRole === "anonymous") {
|
|
27
|
+
ctx.state.currentRole = currentRole;
|
|
28
|
+
return next();
|
|
29
|
+
}
|
|
30
|
+
if (!ctx.state.currentUser) {
|
|
31
|
+
return next();
|
|
32
|
+
}
|
|
33
|
+
const attachRoles = ctx.state.attachRoles || [];
|
|
34
|
+
const cache = ctx.cache;
|
|
35
|
+
const repository = ctx.db.getRepository("users.roles", ctx.state.currentUser.id);
|
|
36
|
+
const roles = await cache.wrap(
|
|
37
|
+
`roles:${ctx.state.currentUser.id}`,
|
|
38
|
+
() => repository.find({
|
|
39
|
+
raw: true
|
|
40
|
+
})
|
|
41
|
+
);
|
|
42
|
+
if (!roles.length && !attachRoles.length) {
|
|
43
|
+
ctx.state.currentRole = void 0;
|
|
44
|
+
return ctx.throw(401, {
|
|
45
|
+
code: "USER_HAS_NO_ROLES_ERR",
|
|
46
|
+
message: ctx.t("The current user has no roles. Please try another account.", { ns: "acl" })
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
const rolesMap = /* @__PURE__ */ new Map();
|
|
50
|
+
attachRoles.forEach((role) => rolesMap.set(role.name, role));
|
|
51
|
+
roles.forEach((role) => rolesMap.set(role.name, role));
|
|
52
|
+
const userRoles = Array.from(rolesMap.values());
|
|
53
|
+
ctx.state.currentUser.roles = userRoles;
|
|
54
|
+
if (currentRole) {
|
|
55
|
+
ctx.state.currentRole = (_a = userRoles.find((role) => role.name === currentRole)) == null ? void 0 : _a.name;
|
|
56
|
+
} else {
|
|
57
|
+
const defaultRole = userRoles.find((role) => {
|
|
58
|
+
var _a2;
|
|
59
|
+
return (_a2 = role == null ? void 0 : role.rolesUsers) == null ? void 0 : _a2.default;
|
|
60
|
+
});
|
|
61
|
+
ctx.state.currentRole = (_b = defaultRole || userRoles[0]) == null ? void 0 : _b.name;
|
|
62
|
+
}
|
|
63
|
+
if (!ctx.state.currentRole) {
|
|
64
|
+
return ctx.throw(401, {
|
|
65
|
+
code: "ROLE_NOT_FOUND_ERR",
|
|
66
|
+
message: ctx.t("The user role does not exist. Please try signing in again", { ns: "acl" })
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
await next();
|
|
70
|
+
}
|
|
71
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
72
|
+
0 && (module.exports = {
|
|
73
|
+
setCurrentRole
|
|
74
|
+
});
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var with_acl_meta_exports = {};
|
|
29
|
+
__export(with_acl_meta_exports, {
|
|
30
|
+
createWithACLMetaMiddleware: () => createWithACLMetaMiddleware
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(with_acl_meta_exports);
|
|
33
|
+
var import_acl = require("@tachybase/acl");
|
|
34
|
+
var import_database = require("@tachybase/database");
|
|
35
|
+
var import_lodash = __toESM(require("lodash"));
|
|
36
|
+
function createWithACLMetaMiddleware() {
|
|
37
|
+
return async (ctx, next) => {
|
|
38
|
+
var _a, _b, _c;
|
|
39
|
+
await next();
|
|
40
|
+
const dataSourceKey = ctx.get("x-data-source");
|
|
41
|
+
const dataSource = ctx.app.dataSourceManager.dataSources.get(dataSourceKey);
|
|
42
|
+
const db = dataSource ? dataSource.collectionManager.db : ctx.db;
|
|
43
|
+
if (!db) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const acl = dataSource ? dataSource.acl : ctx.app.acl;
|
|
47
|
+
if (!ctx.action || !ctx.get("X-With-ACL-Meta") || ctx.status !== 200) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const { resourceName, actionName } = ctx.action;
|
|
51
|
+
if (!["list", "get"].includes(actionName)) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const collection = db.getCollection(resourceName);
|
|
55
|
+
if (!collection) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const Model = collection.model;
|
|
59
|
+
const primaryKeyField = Model.primaryKeyField || Model.primaryKeyAttribute;
|
|
60
|
+
const dataPath = ((_a = ctx.body) == null ? void 0 : _a.rows) ? "body.rows" : "body";
|
|
61
|
+
let listData = import_lodash.default.get(ctx, dataPath);
|
|
62
|
+
if (actionName === "get") {
|
|
63
|
+
listData = import_lodash.default.castArray(listData);
|
|
64
|
+
}
|
|
65
|
+
const inspectActions = ["view", "update", "destroy"];
|
|
66
|
+
const actionsParams = [];
|
|
67
|
+
for (const action of inspectActions) {
|
|
68
|
+
const actionCtx = {
|
|
69
|
+
db,
|
|
70
|
+
get: () => {
|
|
71
|
+
return void 0;
|
|
72
|
+
},
|
|
73
|
+
app: {
|
|
74
|
+
getDb() {
|
|
75
|
+
return db;
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
action: {
|
|
79
|
+
actionName: action,
|
|
80
|
+
name: action,
|
|
81
|
+
params: {},
|
|
82
|
+
resourceName: ctx.action.resourceName,
|
|
83
|
+
resourceOf: ctx.action.resourceOf,
|
|
84
|
+
mergeParams() {
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
state: {
|
|
88
|
+
currentRole: ctx.state.currentRole,
|
|
89
|
+
currentUser: (() => {
|
|
90
|
+
var _a2;
|
|
91
|
+
if (!ctx.state.currentUser) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
if (ctx.state.currentUser.toJSON) {
|
|
95
|
+
return (_a2 = ctx.state.currentUser) == null ? void 0 : _a2.toJSON();
|
|
96
|
+
}
|
|
97
|
+
return ctx.state.currentUser;
|
|
98
|
+
})()
|
|
99
|
+
},
|
|
100
|
+
permission: {},
|
|
101
|
+
throw(...args) {
|
|
102
|
+
throw new import_acl.NoPermissionError(...args);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
try {
|
|
106
|
+
await acl.getActionParams(actionCtx);
|
|
107
|
+
} catch (e) {
|
|
108
|
+
if (e instanceof import_acl.NoPermissionError) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
throw e;
|
|
112
|
+
}
|
|
113
|
+
actionsParams.push([
|
|
114
|
+
action,
|
|
115
|
+
((_b = actionCtx.permission) == null ? void 0 : _b.can) === null && !actionCtx.permission.skip ? null : ((_c = actionCtx.permission) == null ? void 0 : _c.parsedParams) || {},
|
|
116
|
+
actionCtx
|
|
117
|
+
]);
|
|
118
|
+
}
|
|
119
|
+
const ids = (() => {
|
|
120
|
+
if (collection.options.tree) {
|
|
121
|
+
if (listData.length === 0) return [];
|
|
122
|
+
const getAllNodeIds = (data) => [data[primaryKeyField], ...(data.children || []).flatMap(getAllNodeIds)];
|
|
123
|
+
return listData.map((tree) => getAllNodeIds(tree.toJSON())).flat();
|
|
124
|
+
}
|
|
125
|
+
return listData.filter(Boolean).map((item) => item[primaryKeyField]);
|
|
126
|
+
})();
|
|
127
|
+
const conditions = [];
|
|
128
|
+
const allAllowed = [];
|
|
129
|
+
for (const [action, params, actionCtx] of actionsParams) {
|
|
130
|
+
if (!params) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (import_lodash.default.isEmpty(params) || import_lodash.default.isEmpty(params.filter)) {
|
|
134
|
+
allAllowed.push(action);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const queryParams = collection.repository.buildQueryOptions({
|
|
138
|
+
...params,
|
|
139
|
+
context: actionCtx
|
|
140
|
+
});
|
|
141
|
+
const actionSql = ctx.db.sequelize.queryInterface.queryGenerator.selectQuery(
|
|
142
|
+
Model.getTableName(),
|
|
143
|
+
{
|
|
144
|
+
where: (() => {
|
|
145
|
+
const filterObj = queryParams.where;
|
|
146
|
+
if (!db.options.underscored) {
|
|
147
|
+
return filterObj;
|
|
148
|
+
}
|
|
149
|
+
const isAssociationKey = (key) => {
|
|
150
|
+
return key.startsWith("$") && key.endsWith("$");
|
|
151
|
+
};
|
|
152
|
+
const iterate = (rootObj, path = []) => {
|
|
153
|
+
const obj = path.length === 0 ? rootObj : import_lodash.default.get(rootObj, path);
|
|
154
|
+
if (Array.isArray(obj)) {
|
|
155
|
+
for (let i = 0; i < obj.length; i++) {
|
|
156
|
+
if (obj[i] === null) {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (typeof obj[i] === "object") {
|
|
160
|
+
iterate(rootObj, [...path, i]);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
Reflect.ownKeys(obj).forEach((key) => {
|
|
166
|
+
if (Array.isArray(obj) && key === "length") {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (typeof obj[key] === "object" && obj[key] !== null || typeof obj[key] === "symbol") {
|
|
170
|
+
iterate(rootObj, [...path, key]);
|
|
171
|
+
}
|
|
172
|
+
if (typeof key === "string" && key !== (0, import_database.snakeCase)(key)) {
|
|
173
|
+
const setKey = isAssociationKey(key) ? (() => {
|
|
174
|
+
const parts = key.split(".");
|
|
175
|
+
parts[parts.length - 1] = import_lodash.default.snakeCase(parts[parts.length - 1]);
|
|
176
|
+
const result = parts.join(".");
|
|
177
|
+
return result.endsWith("$") ? result : `${result}$`;
|
|
178
|
+
})() : (0, import_database.snakeCase)(key);
|
|
179
|
+
const setValue = import_lodash.default.cloneDeep(obj[key]);
|
|
180
|
+
import_lodash.default.unset(rootObj, [...path, key]);
|
|
181
|
+
import_lodash.default.set(rootObj, [...path, setKey], setValue);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
iterate(filterObj);
|
|
186
|
+
return filterObj;
|
|
187
|
+
})(),
|
|
188
|
+
attributes: [primaryKeyField],
|
|
189
|
+
includeIgnoreAttributes: false
|
|
190
|
+
},
|
|
191
|
+
Model
|
|
192
|
+
);
|
|
193
|
+
const whereCase = actionSql.match(/WHERE (.*?);/)[1];
|
|
194
|
+
conditions.push({
|
|
195
|
+
whereCase,
|
|
196
|
+
action,
|
|
197
|
+
include: queryParams.include
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
let include = conditions.map((condition) => condition.include).flat();
|
|
201
|
+
const whereCases = conditions.map((condition) => condition.whereCase);
|
|
202
|
+
include = include.filter((inc) => {
|
|
203
|
+
return conditions.some((condition) => {
|
|
204
|
+
return whereCases.some((whereCase) => whereCase.includes(inc.association));
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
const results = await collection.model.findAll({
|
|
208
|
+
where: {
|
|
209
|
+
[primaryKeyField]: ids
|
|
210
|
+
},
|
|
211
|
+
attributes: [
|
|
212
|
+
primaryKeyField,
|
|
213
|
+
...conditions.map((condition) => {
|
|
214
|
+
return [ctx.db.sequelize.literal(`CASE WHEN ${condition.whereCase} THEN 1 ELSE 0 END`), condition.action];
|
|
215
|
+
})
|
|
216
|
+
],
|
|
217
|
+
include
|
|
218
|
+
});
|
|
219
|
+
const allowedActions = inspectActions.map((action) => {
|
|
220
|
+
if (allAllowed.includes(action)) {
|
|
221
|
+
return [action, ids];
|
|
222
|
+
}
|
|
223
|
+
return [action, results.filter((item) => Boolean(item.get(action))).map((item) => item.get(primaryKeyField))];
|
|
224
|
+
}).reduce((acc, [action, ids2]) => {
|
|
225
|
+
acc[action] = ids2;
|
|
226
|
+
return acc;
|
|
227
|
+
}, {});
|
|
228
|
+
if (actionName === "get") {
|
|
229
|
+
ctx.bodyMeta = {
|
|
230
|
+
...ctx.bodyMeta,
|
|
231
|
+
allowedActions
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
if (actionName === "list") {
|
|
235
|
+
ctx.body.allowedActions = allowedActions;
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
240
|
+
0 && (module.exports = {
|
|
241
|
+
createWithACLMetaMiddleware
|
|
242
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var set_role_snippets_exports = {};
|
|
19
|
+
__export(set_role_snippets_exports, {
|
|
20
|
+
default: () => set_role_snippets_default
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(set_role_snippets_exports);
|
|
23
|
+
var import_server = require("@tachybase/server");
|
|
24
|
+
class set_role_snippets_default extends import_server.Migration {
|
|
25
|
+
appVersion = "<0.9.0-alpha.1";
|
|
26
|
+
async up() {
|
|
27
|
+
const result = await this.app.version.satisfies("<0.9.0-alpha.1");
|
|
28
|
+
if (!result) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
await this.app.db.getRepository("roles").update({
|
|
32
|
+
filter: {
|
|
33
|
+
$or: [{ allowConfigure: true }, { name: "root" }]
|
|
34
|
+
},
|
|
35
|
+
values: {
|
|
36
|
+
snippets: ["ui.*", "pm", "pm.*"],
|
|
37
|
+
allowConfigure: false
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
async down() {
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var RoleModel_exports = {};
|
|
19
|
+
__export(RoleModel_exports, {
|
|
20
|
+
RoleModel: () => RoleModel
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(RoleModel_exports);
|
|
23
|
+
var import_database = require("@tachybase/database");
|
|
24
|
+
class RoleModel extends import_database.Model {
|
|
25
|
+
writeToAcl(options) {
|
|
26
|
+
const { acl } = options;
|
|
27
|
+
const roleName = this.get("name");
|
|
28
|
+
let role = acl.getRole(roleName);
|
|
29
|
+
if (!role) {
|
|
30
|
+
role = acl.define({
|
|
31
|
+
role: roleName
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
if (options.withOutStrategy !== true) {
|
|
35
|
+
role.setStrategy({
|
|
36
|
+
...this.get("strategy") || {},
|
|
37
|
+
allowConfigure: this.get("allowConfigure")
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
role.snippets = new Set(this.get("snippets"));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
44
|
+
0 && (module.exports = {
|
|
45
|
+
RoleModel
|
|
46
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ACL, ACLRole } from '@tachybase/acl';
|
|
2
|
+
import { Model } from '@tachybase/database';
|
|
3
|
+
import { AssociationFieldsActions, GrantHelper } from '../server';
|
|
4
|
+
export declare class RoleResourceActionModel extends Model {
|
|
5
|
+
writeToACL(options: {
|
|
6
|
+
acl: ACL;
|
|
7
|
+
role: ACLRole;
|
|
8
|
+
resourceName: string;
|
|
9
|
+
associationFieldsActions: AssociationFieldsActions;
|
|
10
|
+
grantHelper: GrantHelper;
|
|
11
|
+
}): Promise<void>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var RoleResourceActionModel_exports = {};
|
|
19
|
+
__export(RoleResourceActionModel_exports, {
|
|
20
|
+
RoleResourceActionModel: () => RoleResourceActionModel
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(RoleResourceActionModel_exports);
|
|
23
|
+
var import_database = require("@tachybase/database");
|
|
24
|
+
class RoleResourceActionModel extends import_database.Model {
|
|
25
|
+
async writeToACL(options) {
|
|
26
|
+
var _a;
|
|
27
|
+
const db = this.constructor.database;
|
|
28
|
+
const { resourceName, role, acl, associationFieldsActions, grantHelper } = options;
|
|
29
|
+
const actionName = this.get("name");
|
|
30
|
+
const fields = this.get("fields");
|
|
31
|
+
const actionPath = `${resourceName}:${actionName}`;
|
|
32
|
+
const actionParams = {
|
|
33
|
+
fields
|
|
34
|
+
};
|
|
35
|
+
const scope = await this.getScope();
|
|
36
|
+
if (scope) {
|
|
37
|
+
actionParams["own"] = scope.get("key") === "own";
|
|
38
|
+
actionParams["filter"] = scope.get("scope");
|
|
39
|
+
}
|
|
40
|
+
role.grantAction(actionPath, actionParams);
|
|
41
|
+
const collection = db.getCollection(resourceName);
|
|
42
|
+
if (!collection) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const availableAction = acl.resolveActionAlias(actionName);
|
|
46
|
+
for (const field of fields) {
|
|
47
|
+
const collectionField = collection.getField(field);
|
|
48
|
+
if (!collectionField) {
|
|
49
|
+
console.log(`field ${field} does not exist at ${collection.name}`);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const fieldType = collectionField.get("type");
|
|
53
|
+
const fieldActions = (_a = associationFieldsActions == null ? void 0 : associationFieldsActions[fieldType]) == null ? void 0 : _a[availableAction];
|
|
54
|
+
const fieldTarget = collectionField.get("target");
|
|
55
|
+
if (fieldActions) {
|
|
56
|
+
const associationActions = fieldActions.associationActions || [];
|
|
57
|
+
associationActions.forEach((associationAction) => {
|
|
58
|
+
const actionName2 = `${resourceName}.${collectionField.get("name")}:${associationAction}`;
|
|
59
|
+
role.grantAction(actionName2);
|
|
60
|
+
});
|
|
61
|
+
const targetActions = fieldActions.targetActions || [];
|
|
62
|
+
targetActions.forEach((targetAction) => {
|
|
63
|
+
const targetActionPath = `${fieldTarget}:${targetAction}`;
|
|
64
|
+
const existsAction = role.getActionParams(targetActionPath);
|
|
65
|
+
if (existsAction) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
grantHelper.resourceTargetActionMap.set(`${role.name}.${resourceName}`, [
|
|
69
|
+
...grantHelper.resourceTargetActionMap.get(resourceName) || [],
|
|
70
|
+
targetActionPath
|
|
71
|
+
]);
|
|
72
|
+
grantHelper.targetActionResourceMap.set(targetActionPath, [
|
|
73
|
+
...grantHelper.targetActionResourceMap.get(targetActionPath) || [],
|
|
74
|
+
`${role.name}.${resourceName}`
|
|
75
|
+
]);
|
|
76
|
+
role.grantAction(targetActionPath);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
83
|
+
0 && (module.exports = {
|
|
84
|
+
RoleResourceActionModel
|
|
85
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ACL, ACLRole } from '@tachybase/acl';
|
|
2
|
+
import { Model } from '@tachybase/database';
|
|
3
|
+
import Application from '@tachybase/server';
|
|
4
|
+
import { AssociationFieldsActions, GrantHelper } from '../server';
|
|
5
|
+
export declare class RoleResourceModel extends Model {
|
|
6
|
+
revoke(options: {
|
|
7
|
+
role: ACLRole;
|
|
8
|
+
resourceName: string;
|
|
9
|
+
grantHelper: GrantHelper;
|
|
10
|
+
}): Promise<void>;
|
|
11
|
+
writeToACL(options: {
|
|
12
|
+
acl: ACL;
|
|
13
|
+
associationFieldsActions: AssociationFieldsActions;
|
|
14
|
+
grantHelper: GrantHelper;
|
|
15
|
+
transaction: any;
|
|
16
|
+
app?: Application;
|
|
17
|
+
}): Promise<void>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var RoleResourceModel_exports = {};
|
|
19
|
+
__export(RoleResourceModel_exports, {
|
|
20
|
+
RoleResourceModel: () => RoleResourceModel
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(RoleResourceModel_exports);
|
|
23
|
+
var import_acl = require("@tachybase/acl");
|
|
24
|
+
var import_database = require("@tachybase/database");
|
|
25
|
+
class RoleResourceModel extends import_database.Model {
|
|
26
|
+
async revoke(options) {
|
|
27
|
+
const { role, resourceName, grantHelper } = options;
|
|
28
|
+
role.revokeResource(resourceName);
|
|
29
|
+
const targetActions = grantHelper.resourceTargetActionMap.get(`${role.name}.${resourceName}`) || [];
|
|
30
|
+
for (const targetAction of targetActions) {
|
|
31
|
+
const targetActionResource = (grantHelper.targetActionResourceMap.get(targetAction) || []).filter(
|
|
32
|
+
(item) => `${role.name}.${resourceName}` !== item
|
|
33
|
+
);
|
|
34
|
+
grantHelper.targetActionResourceMap.set(targetAction, targetActionResource);
|
|
35
|
+
if (targetActionResource.length === 0) {
|
|
36
|
+
role.revokeAction(targetAction);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
grantHelper.resourceTargetActionMap.set(`${role.name}.${resourceName}`, []);
|
|
40
|
+
}
|
|
41
|
+
async writeToACL(options) {
|
|
42
|
+
const { acl, associationFieldsActions, grantHelper } = options;
|
|
43
|
+
const resourceName = this.get("name");
|
|
44
|
+
const roleName = this.get("roleName");
|
|
45
|
+
const role = acl.getRole(roleName);
|
|
46
|
+
if (!role) {
|
|
47
|
+
console.log(`${roleName} role does not exist`);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
await this.revoke({ role, resourceName, grantHelper });
|
|
51
|
+
if (this.usingActionsConfig === false) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const resource = new import_acl.ACLResource({
|
|
55
|
+
role,
|
|
56
|
+
name: resourceName
|
|
57
|
+
});
|
|
58
|
+
role.resources.set(resourceName, resource);
|
|
59
|
+
const actions = await this.getActions({
|
|
60
|
+
transaction: options.transaction
|
|
61
|
+
});
|
|
62
|
+
for (const action of actions) {
|
|
63
|
+
await action.writeToACL({
|
|
64
|
+
acl,
|
|
65
|
+
role,
|
|
66
|
+
resourceName,
|
|
67
|
+
associationFieldsActions,
|
|
68
|
+
grantHelper: options.grantHelper
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
if (options.app) {
|
|
72
|
+
await options.app.emitAsync("dataSource:writeToAcl", { roleName, transaction: options.transaction });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
77
|
+
0 && (module.exports = {
|
|
78
|
+
RoleResourceModel
|
|
79
|
+
});
|