@abtnode/rbac 1.16.44 → 1.16.45-beta-20250609-082716-b6b0592b
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/store/sequelize.js +27 -52
- package/package.json +6 -5
package/lib/store/sequelize.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
const pick = require('lodash/pick');
|
|
2
|
-
const SingleFlightLRUCache = require('@abtnode/util/lib/single-flight-lru-cache');
|
|
3
|
-
const { RBAC_CACHE_TTL } = require('@abtnode/constant');
|
|
4
2
|
|
|
5
3
|
const Storage = require('../core/storage');
|
|
6
4
|
const Permission = require('../core/permission');
|
|
@@ -17,11 +15,6 @@ class SequelizeStorage extends Storage {
|
|
|
17
15
|
this.db = db;
|
|
18
16
|
}
|
|
19
17
|
|
|
20
|
-
cache = new SingleFlightLRUCache({
|
|
21
|
-
max: 1000,
|
|
22
|
-
ttl: RBAC_CACHE_TTL,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
18
|
async add(item) {
|
|
26
19
|
const { name } = item;
|
|
27
20
|
|
|
@@ -46,7 +39,6 @@ class SequelizeStorage extends Storage {
|
|
|
46
39
|
}
|
|
47
40
|
|
|
48
41
|
await this.db.insert(doc);
|
|
49
|
-
this.cache.clear();
|
|
50
42
|
|
|
51
43
|
return true;
|
|
52
44
|
}
|
|
@@ -63,7 +55,6 @@ class SequelizeStorage extends Storage {
|
|
|
63
55
|
await Promise.all(
|
|
64
56
|
docs.map((doc) => this.db.update({ id: doc.id }, { $set: { grants: doc.grants.filter((x) => x !== name) } }))
|
|
65
57
|
);
|
|
66
|
-
this.cache.clear();
|
|
67
58
|
return true;
|
|
68
59
|
}
|
|
69
60
|
|
|
@@ -75,7 +66,6 @@ class SequelizeStorage extends Storage {
|
|
|
75
66
|
}
|
|
76
67
|
|
|
77
68
|
await this.db.update({ name }, pick({ title, description, extra }, 'title', 'description', 'extra'));
|
|
78
|
-
this.cache.clear();
|
|
79
69
|
return this.get(name);
|
|
80
70
|
}
|
|
81
71
|
|
|
@@ -106,8 +96,6 @@ class SequelizeStorage extends Storage {
|
|
|
106
96
|
)
|
|
107
97
|
);
|
|
108
98
|
|
|
109
|
-
this.cache.clear();
|
|
110
|
-
|
|
111
99
|
return true;
|
|
112
100
|
}
|
|
113
101
|
|
|
@@ -124,60 +112,48 @@ class SequelizeStorage extends Storage {
|
|
|
124
112
|
docs.map((doc) => this.db.update({ id: doc.id }, { grants: doc.grants.filter((x) => x !== childName) }))
|
|
125
113
|
);
|
|
126
114
|
|
|
127
|
-
this.cache.clear();
|
|
128
|
-
|
|
129
115
|
return true;
|
|
130
116
|
}
|
|
131
117
|
|
|
132
|
-
get(name) {
|
|
133
|
-
const
|
|
134
|
-
return this.cache.autoCache(cacheKey, async () => {
|
|
135
|
-
const item = await this.db.findOne({ name });
|
|
118
|
+
async get(name) {
|
|
119
|
+
const item = await this.db.findOne({ name });
|
|
136
120
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
121
|
+
if (!item) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
140
124
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
125
|
+
if (item.type === ItemTypes.Role) {
|
|
126
|
+
return this.resolveRole(item);
|
|
127
|
+
}
|
|
144
128
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
129
|
+
if (item.type === ItemTypes.Permission) {
|
|
130
|
+
return this.resolvePermission(item);
|
|
131
|
+
}
|
|
148
132
|
|
|
149
|
-
|
|
150
|
-
});
|
|
133
|
+
return undefined;
|
|
151
134
|
}
|
|
152
135
|
|
|
153
|
-
getRoles() {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
return roles.map((item) => this.resolveRole(item));
|
|
158
|
-
});
|
|
136
|
+
async getRoles() {
|
|
137
|
+
// 因为对象 JSON 有循环引用, 所以无法用原来的缓存
|
|
138
|
+
const roles = await this.db.find({ type: ItemTypes.Role }, {}, { createdAt: -1 });
|
|
139
|
+
return roles.map((item) => this.resolveRole(item));
|
|
159
140
|
}
|
|
160
141
|
|
|
161
|
-
getPermissions() {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
return permissions.map((item) => this.resolvePermission(item));
|
|
166
|
-
});
|
|
142
|
+
async getPermissions() {
|
|
143
|
+
// 因为对象 JSON 有循环引用, 所以无法用原来的缓存
|
|
144
|
+
const permissions = await this.db.find({ type: ItemTypes.Permission }, {}, { createdAt: -1 });
|
|
145
|
+
return permissions.map((item) => this.resolvePermission(item));
|
|
167
146
|
}
|
|
168
147
|
|
|
169
|
-
getGrants(name) {
|
|
170
|
-
const
|
|
171
|
-
return this.cache.autoCache(cacheKey, async () => {
|
|
172
|
-
const item = await this.db.findOne({ name });
|
|
148
|
+
async getGrants(name) {
|
|
149
|
+
const item = await this.db.findOne({ name });
|
|
173
150
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
151
|
+
if (item) {
|
|
152
|
+
const grants = await Promise.all((item.grants || []).map((x) => this.get(x)));
|
|
153
|
+
return grants;
|
|
154
|
+
}
|
|
178
155
|
|
|
179
|
-
|
|
180
|
-
});
|
|
156
|
+
return [];
|
|
181
157
|
}
|
|
182
158
|
// custom
|
|
183
159
|
|
|
@@ -209,7 +185,6 @@ class SequelizeStorage extends Storage {
|
|
|
209
185
|
});
|
|
210
186
|
|
|
211
187
|
await this.db.update({ name: roleName }, { grants: grantNames });
|
|
212
|
-
this.cache.clear();
|
|
213
188
|
return this.get(roleName);
|
|
214
189
|
}
|
|
215
190
|
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.
|
|
6
|
+
"version": "1.16.45-beta-20250609-082716-b6b0592b",
|
|
7
7
|
"description": "Simple lib to manage access controls in ABT Node",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -19,14 +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.
|
|
23
|
-
"@abtnode/db": "1.16.
|
|
24
|
-
"@abtnode/
|
|
22
|
+
"@abtnode/constant": "1.16.45-beta-20250609-082716-b6b0592b",
|
|
23
|
+
"@abtnode/db": "1.16.45-beta-20250609-082716-b6b0592b",
|
|
24
|
+
"@abtnode/db-cache": "1.16.45-beta-20250609-082716-b6b0592b",
|
|
25
|
+
"@abtnode/util": "1.16.45-beta-20250609-082716-b6b0592b",
|
|
25
26
|
"fs-extra": "^11.2.0",
|
|
26
27
|
"lodash": "^4.17.21"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
30
|
"jest": "^29.7.0"
|
|
30
31
|
},
|
|
31
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "adeceb28a9b5c91d2eb35d33880782048ae629cc"
|
|
32
33
|
}
|