@abtnode/rbac 1.16.52-beta-20251003-083412-fdfc4e36 → 1.16.52-beta-20251005-235515-42ad5caf
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/lib/core/base.js +2 -1
- package/lib/core/rbac.js +4 -4
- package/lib/core/role.js +2 -2
- package/lib/index.js +2 -2
- package/lib/store/memory.js +11 -1
- package/lib/store/nedb.js +9 -4
- package/lib/store/sequelize.js +9 -4
- package/package.json +6 -6
package/lib/core/base.js
CHANGED
|
@@ -8,7 +8,7 @@ module.exports = class Base {
|
|
|
8
8
|
* @param {String} opt.description Description of the grant
|
|
9
9
|
* @param {String} opt.extra Extra info of the grant
|
|
10
10
|
{ */
|
|
11
|
-
constructor(rbac, name, { title, description, extra } = {}) {
|
|
11
|
+
constructor(rbac, name, { title, description, extra, orgId } = {}) {
|
|
12
12
|
if (!rbac || !name) {
|
|
13
13
|
throw new Error('One of parameters is undefined');
|
|
14
14
|
}
|
|
@@ -17,6 +17,7 @@ module.exports = class Base {
|
|
|
17
17
|
this.title = title;
|
|
18
18
|
this.description = description;
|
|
19
19
|
this.extra = extra;
|
|
20
|
+
this.orgId = orgId;
|
|
20
21
|
this.rbac = rbac;
|
|
21
22
|
}
|
|
22
23
|
|
package/lib/core/rbac.js
CHANGED
|
@@ -185,8 +185,8 @@ module.exports = class RBAC {
|
|
|
185
185
|
* @param {String} opt.extra Extra info of new Role
|
|
186
186
|
* @return {Role} Instance of the Role
|
|
187
187
|
*/
|
|
188
|
-
async createRole(roleName, add, { title, description, extra } = {}) {
|
|
189
|
-
const role = new Role(this, roleName, { title, description, extra });
|
|
188
|
+
async createRole(roleName, add, { title, description, extra, orgId } = {}) {
|
|
189
|
+
const role = new Role(this, roleName, { title, description, extra, orgId });
|
|
190
190
|
if (add) {
|
|
191
191
|
await role.add();
|
|
192
192
|
}
|
|
@@ -256,8 +256,8 @@ module.exports = class RBAC {
|
|
|
256
256
|
* Return all instances of Role
|
|
257
257
|
* @method RBAC#getRoles
|
|
258
258
|
*/
|
|
259
|
-
getRoles() {
|
|
260
|
-
return this.storage.getRoles();
|
|
259
|
+
getRoles(orgId) {
|
|
260
|
+
return this.storage.getRoles(orgId);
|
|
261
261
|
}
|
|
262
262
|
|
|
263
263
|
/**
|
package/lib/core/role.js
CHANGED
|
@@ -12,12 +12,12 @@ module.exports = class Role extends Base {
|
|
|
12
12
|
* @param {String} opt.description Description of the role
|
|
13
13
|
* @param {String} opt.extra Extra info of the role
|
|
14
14
|
*/
|
|
15
|
-
constructor(rbac, name, { title, description, extra } = {}) {
|
|
15
|
+
constructor(rbac, name, { title, description, extra, orgId } = {}) {
|
|
16
16
|
if (!Permission.isValidName(name, rbac.options.delimiter)) {
|
|
17
17
|
throw new Error('Role has no valid name');
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
super(rbac, name, { title, description, extra });
|
|
20
|
+
super(rbac, name, { title, description, extra, orgId });
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
/**
|
package/lib/index.js
CHANGED
|
@@ -26,9 +26,9 @@ const createRBAC = async ({ storage, data = {} } = {}) => {
|
|
|
26
26
|
|
|
27
27
|
await rbac.init();
|
|
28
28
|
|
|
29
|
-
const createRole = async ({ name, title, description, extra, childName, permissions = [] } = {}) => {
|
|
29
|
+
const createRole = async ({ name, title, description, extra, childName, permissions = [], orgId } = {}) => {
|
|
30
30
|
// add role
|
|
31
|
-
await rbac.createRole(name, true, { title, description, extra });
|
|
31
|
+
await rbac.createRole(name, true, { title, description, extra, orgId });
|
|
32
32
|
|
|
33
33
|
// add grants
|
|
34
34
|
const grants = [childName, ...permissions].filter(Boolean);
|
package/lib/store/memory.js
CHANGED
|
@@ -129,8 +129,18 @@ class Memory extends Storage {
|
|
|
129
129
|
return undefined;
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
getRoles() {
|
|
132
|
+
getRoles(orgId) {
|
|
133
133
|
return Object.keys(this.items)
|
|
134
|
+
.filter((k) => {
|
|
135
|
+
const { instance } = this.items[k];
|
|
136
|
+
if (!orgId) {
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
if (instance instanceof Role) {
|
|
140
|
+
return instance.orgId === orgId;
|
|
141
|
+
}
|
|
142
|
+
return false;
|
|
143
|
+
})
|
|
134
144
|
.map((k) => this.items[k])
|
|
135
145
|
.reduce((filtered, item) => {
|
|
136
146
|
const { instance } = item;
|
package/lib/store/nedb.js
CHANGED
|
@@ -46,6 +46,7 @@ class Nedb extends Storage {
|
|
|
46
46
|
title: item.title,
|
|
47
47
|
description: item.description,
|
|
48
48
|
extra: item.extra,
|
|
49
|
+
orgId: item.orgId,
|
|
49
50
|
};
|
|
50
51
|
|
|
51
52
|
if (item instanceof Role) {
|
|
@@ -178,10 +179,14 @@ class Nedb extends Storage {
|
|
|
178
179
|
return undefined;
|
|
179
180
|
}
|
|
180
181
|
|
|
181
|
-
async getRoles() {
|
|
182
|
+
async getRoles(orgId) {
|
|
183
|
+
const where = { type: ItemTypes.Role };
|
|
184
|
+
if (orgId) {
|
|
185
|
+
where.orgId = orgId;
|
|
186
|
+
}
|
|
182
187
|
const roles = await new Promise((resolve, reject) => {
|
|
183
188
|
this.db
|
|
184
|
-
.cursor(
|
|
189
|
+
.cursor(where)
|
|
185
190
|
.sort({ createdAt: -1 })
|
|
186
191
|
.exec((err, docs) => {
|
|
187
192
|
if (err) {
|
|
@@ -280,8 +285,8 @@ class Nedb extends Storage {
|
|
|
280
285
|
* grants: string[]
|
|
281
286
|
*/
|
|
282
287
|
resolveRole(item) {
|
|
283
|
-
const { name, title, description, extra } = item;
|
|
284
|
-
const role = new Role(this.rbac, name, { title, description, extra });
|
|
288
|
+
const { name, title, description, extra, orgId } = item;
|
|
289
|
+
const role = new Role(this.rbac, name, { title, description, extra, orgId });
|
|
285
290
|
role.grants = item.grants;
|
|
286
291
|
return role;
|
|
287
292
|
}
|
package/lib/store/sequelize.js
CHANGED
|
@@ -27,6 +27,7 @@ class SequelizeStorage extends Storage {
|
|
|
27
27
|
title: item.title,
|
|
28
28
|
description: item.description,
|
|
29
29
|
extra: item.extra,
|
|
30
|
+
orgId: item.orgId,
|
|
30
31
|
};
|
|
31
32
|
|
|
32
33
|
if (item instanceof Role) {
|
|
@@ -133,9 +134,13 @@ class SequelizeStorage extends Storage {
|
|
|
133
134
|
return undefined;
|
|
134
135
|
}
|
|
135
136
|
|
|
136
|
-
async getRoles() {
|
|
137
|
+
async getRoles(orgId) {
|
|
137
138
|
// 因为对象 JSON 有循环引用, 所以无法用原来的缓存
|
|
138
|
-
const
|
|
139
|
+
const where = { type: ItemTypes.Role };
|
|
140
|
+
if (orgId) {
|
|
141
|
+
where.orgId = orgId;
|
|
142
|
+
}
|
|
143
|
+
const roles = await this.db.find(where, {}, { createdAt: -1 });
|
|
139
144
|
return roles.map((item) => this.resolveRole(item));
|
|
140
145
|
}
|
|
141
146
|
|
|
@@ -197,8 +202,8 @@ class SequelizeStorage extends Storage {
|
|
|
197
202
|
* grants: string[]
|
|
198
203
|
*/
|
|
199
204
|
resolveRole(item) {
|
|
200
|
-
const { name, title, description, extra } = item;
|
|
201
|
-
const role = new Role(this.rbac, name, { title, description, extra });
|
|
205
|
+
const { name, title, description, extra, orgId } = item;
|
|
206
|
+
const role = new Role(this.rbac, name, { title, description, extra, orgId });
|
|
202
207
|
role.grants = item.grants;
|
|
203
208
|
return role;
|
|
204
209
|
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.52-beta-
|
|
6
|
+
"version": "1.16.52-beta-20251005-235515-42ad5caf",
|
|
7
7
|
"description": "Simple lib to manage access controls in ABT Node",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -19,15 +19,15 @@
|
|
|
19
19
|
"author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
|
|
20
20
|
"license": "Apache-2.0",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@abtnode/constant": "1.16.52-beta-
|
|
23
|
-
"@abtnode/db": "1.16.52-beta-
|
|
24
|
-
"@abtnode/db-cache": "1.16.52-beta-
|
|
25
|
-
"@abtnode/util": "1.16.52-beta-
|
|
22
|
+
"@abtnode/constant": "1.16.52-beta-20251005-235515-42ad5caf",
|
|
23
|
+
"@abtnode/db": "1.16.52-beta-20251005-235515-42ad5caf",
|
|
24
|
+
"@abtnode/db-cache": "1.16.52-beta-20251005-235515-42ad5caf",
|
|
25
|
+
"@abtnode/util": "1.16.52-beta-20251005-235515-42ad5caf",
|
|
26
26
|
"fs-extra": "^11.2.0",
|
|
27
27
|
"lodash": "^4.17.21"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"jest": "^29.7.0"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "7b295929a123edac2cb292c43f2edda0d3e3e6b8"
|
|
33
33
|
}
|