@aptly-as/express-scope 1.1.3 → 1.2.0
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/Scope.js +18 -14
- package/lib/types.d.ts +1 -1
- package/package.json +1 -1
package/lib/Scope.js
CHANGED
|
@@ -69,8 +69,8 @@ export class Scope {
|
|
|
69
69
|
get(action, key) {
|
|
70
70
|
return (req, res, next) => {
|
|
71
71
|
try {
|
|
72
|
-
const { user, organization, modules, override,
|
|
73
|
-
if (override
|
|
72
|
+
const { user, organization, modules, override, extend } = this._scopeAccess(req);
|
|
73
|
+
if (override) {
|
|
74
74
|
return res.json(override);
|
|
75
75
|
}
|
|
76
76
|
let json = this._scopes.reduce((obj, modelKey) => {
|
|
@@ -86,8 +86,15 @@ export class Scope {
|
|
|
86
86
|
return obj;
|
|
87
87
|
}, {});
|
|
88
88
|
}
|
|
89
|
-
if (
|
|
90
|
-
|
|
89
|
+
if (extend) {
|
|
90
|
+
for (const extendKey in extend) {
|
|
91
|
+
if (!json[extendKey]) {
|
|
92
|
+
json[extendKey] = extend[extendKey];
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
json[extendKey] += extend[extendKey];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
91
98
|
}
|
|
92
99
|
if (action === 'return') {
|
|
93
100
|
return json;
|
|
@@ -127,19 +134,13 @@ export class Scope {
|
|
|
127
134
|
return [...new Set([...arr1, ...arr2])].join('');
|
|
128
135
|
}
|
|
129
136
|
_checkScope(req, scopes, access) {
|
|
130
|
-
const { user, organization, modules, override,
|
|
137
|
+
const { user, organization, modules, override, extend } = this._scopeAccess(req);
|
|
131
138
|
if (modules &&
|
|
132
139
|
!modules.some((module) => scopes.some((scope) => this._modules[module]?.includes(scope)))) {
|
|
133
140
|
return false;
|
|
134
141
|
}
|
|
135
142
|
if (override) {
|
|
136
|
-
|
|
137
|
-
if (hasAccess) {
|
|
138
|
-
return true;
|
|
139
|
-
}
|
|
140
|
-
if (onlyOverride) {
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
143
|
+
return scopes.some((mod) => override[mod] && override[mod].includes(access));
|
|
143
144
|
}
|
|
144
145
|
if (user?.some((glob) => scopes.some((mod) => this._userRoles[glob]?.[mod]?.includes(access)))) {
|
|
145
146
|
return true;
|
|
@@ -147,6 +148,9 @@ export class Scope {
|
|
|
147
148
|
if (organization?.some((org) => scopes.some((mod) => this._organizationRoles[org]?.[mod]?.includes(access)))) {
|
|
148
149
|
return true;
|
|
149
150
|
}
|
|
151
|
+
if (extend) {
|
|
152
|
+
return scopes.some((mod) => extend[mod] && extend[mod].includes(access));
|
|
153
|
+
}
|
|
150
154
|
return false;
|
|
151
155
|
}
|
|
152
156
|
_getObjects(obj) {
|
|
@@ -156,7 +160,7 @@ export class Scope {
|
|
|
156
160
|
return Array.isArray(scope) ? scope : [scope];
|
|
157
161
|
}
|
|
158
162
|
_scopeAccess(req) {
|
|
159
|
-
const { user, organization, modules, override,
|
|
163
|
+
const { user, organization, modules, override, extend } = this._request(req);
|
|
160
164
|
return {
|
|
161
165
|
user: Array.isArray(user) ? user : user ? [user] : [],
|
|
162
166
|
organization: Array.isArray(organization)
|
|
@@ -170,7 +174,7 @@ export class Scope {
|
|
|
170
174
|
? [modules]
|
|
171
175
|
: undefined,
|
|
172
176
|
override,
|
|
173
|
-
|
|
177
|
+
extend,
|
|
174
178
|
};
|
|
175
179
|
}
|
|
176
180
|
}
|
package/lib/types.d.ts
CHANGED
|
@@ -14,9 +14,9 @@ export type ScopeFn<R extends Request, SCOPE extends string, MODULE extends Scop
|
|
|
14
14
|
export interface ScopeOptions<SCOPE extends string, MODULE extends ScopeModuleSchema<string, SCOPE>, USER extends ScopeRolesSchema<string, SCOPE>, ORG extends ScopeRolesSchema<string, SCOPE>> {
|
|
15
15
|
modules?: Array<keyof MODULE>;
|
|
16
16
|
override?: ScopeAccessSchema<any>;
|
|
17
|
+
extend?: ScopeAccessSchema<any>;
|
|
17
18
|
organization?: Array<keyof ORG>;
|
|
18
19
|
user?: Array<keyof USER>;
|
|
19
|
-
onlyOverride?: boolean;
|
|
20
20
|
}
|
|
21
21
|
export type ScopeRequestValidate<R extends Request> = (req: R) => boolean;
|
|
22
22
|
export type ScopeRequestItem<R extends Request, SCOPE extends string> = [
|