@autofleet/zehut 3.1.2-beta.14 → 3.1.2-beta.16
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/user/ApiUser.js +23 -12
- package/lib/utils.d.ts +0 -1
- package/lib/utils.js +1 -12
- package/package.json +1 -1
package/lib/user/ApiUser.js
CHANGED
|
@@ -10,10 +10,30 @@ const object_hash_1 = __importDefault(require("object-hash"));
|
|
|
10
10
|
const uuid_1 = require("uuid");
|
|
11
11
|
const outbreak_1 = require("@autofleet/outbreak");
|
|
12
12
|
const services_1 = require("../services");
|
|
13
|
-
const utils_1 = require("../utils");
|
|
14
13
|
exports.ELEVATED_PERMISSIONS_HEADER = 'x-af-elevated-permissions';
|
|
15
14
|
exports.CONTEXTS_IDS_HEADER = 'x-af-context-ids';
|
|
16
15
|
const userCache = new node_cache_1.default({ stdTTL: 10 });
|
|
16
|
+
const mergePermissions = (target, sources) => {
|
|
17
|
+
const permissions = {
|
|
18
|
+
...(target || {}),
|
|
19
|
+
fleets: { ...target?.fleets },
|
|
20
|
+
businessModels: { ...target?.businessModels },
|
|
21
|
+
demandSources: { ...target?.demandSources },
|
|
22
|
+
// Clone other nested objects as needed
|
|
23
|
+
};
|
|
24
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
25
|
+
for (const source of sources) {
|
|
26
|
+
Object.keys(source).forEach((entityType) => {
|
|
27
|
+
// eslint-disable-next-line no-param-reassign
|
|
28
|
+
permissions[entityType] ?? (permissions[entityType] = {});
|
|
29
|
+
Object.entries(source[entityType]).forEach(([entityId, perms]) => {
|
|
30
|
+
// eslint-disable-next-line no-param-reassign
|
|
31
|
+
permissions[entityType][entityId] = (permissions[entityType][entityId] || []).concat(perms);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return permissions;
|
|
36
|
+
};
|
|
17
37
|
class ApiUser {
|
|
18
38
|
constructor(id, accountType, elevatedPermissions, contextIds) {
|
|
19
39
|
this.id = id;
|
|
@@ -82,22 +102,13 @@ class ApiUser {
|
|
|
82
102
|
return Object.keys(this.privatePermissions[key] || {});
|
|
83
103
|
}
|
|
84
104
|
get elevatedPermissions() {
|
|
85
|
-
|
|
86
|
-
fleets: {},
|
|
87
|
-
businessModels: {},
|
|
88
|
-
demandSources: {},
|
|
89
|
-
};
|
|
90
|
-
[...this.privateElevatedPermissionsHash.values()].forEach((elevatedPermissions) => {
|
|
91
|
-
permissions = (0, utils_1.mergePermissions)(permissions, elevatedPermissions);
|
|
92
|
-
});
|
|
93
|
-
return permissions;
|
|
105
|
+
return mergePermissions(undefined, this.privateElevatedPermissionsHash.values());
|
|
94
106
|
}
|
|
95
107
|
get permissions() {
|
|
96
108
|
if (!this.privatePermissions) {
|
|
97
109
|
throw new Error('Cannot get permissions without calling (async) getUserPermissions before');
|
|
98
110
|
}
|
|
99
|
-
|
|
100
|
-
return permissions;
|
|
111
|
+
return mergePermissions(this.privatePermissions, this.privateElevatedPermissionsHash.values());
|
|
101
112
|
}
|
|
102
113
|
elevatePermissions(addedPermissions) {
|
|
103
114
|
const elevationId = (0, uuid_1.v4)();
|
package/lib/utils.d.ts
CHANGED
|
@@ -3,4 +3,3 @@ export declare const decodeBearer: (bearer: string, appSecret?: string) => any;
|
|
|
3
3
|
export declare const parsePermissions: (contextId: any, decodedToken: any) => any;
|
|
4
4
|
export declare const getEntitiesFromContext: (contextId: string, decodedToken: any) => any;
|
|
5
5
|
export declare const getContextAttributes: (contextId: string, decodedToken: any) => any;
|
|
6
|
-
export declare const mergePermissions: (target: any, sources: any) => any;
|
package/lib/utils.js
CHANGED
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
26
|
+
exports.getContextAttributes = exports.getEntitiesFromContext = exports.parsePermissions = exports.decodeBearer = exports.getAuthFromBearer = void 0;
|
|
27
27
|
/* eslint-disable prefer-destructuring */
|
|
28
28
|
const jwt = __importStar(require("jsonwebtoken"));
|
|
29
29
|
const secret_getter_1 = require("./secret-getter");
|
|
@@ -97,14 +97,3 @@ const getContextAttributes = (contextId, decodedToken) => {
|
|
|
97
97
|
return attributes;
|
|
98
98
|
};
|
|
99
99
|
exports.getContextAttributes = getContextAttributes;
|
|
100
|
-
const mergePermissions = (target, sources) => {
|
|
101
|
-
const base = structuredClone(target);
|
|
102
|
-
Object.keys(sources).forEach((topLevelKey) => {
|
|
103
|
-
base[topLevelKey] ?? (base[topLevelKey] = {});
|
|
104
|
-
Object.entries(sources[topLevelKey]).forEach(([entityId, perms]) => {
|
|
105
|
-
base[topLevelKey][entityId] = (base[topLevelKey][entityId] || []).concat(perms);
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
return base;
|
|
109
|
-
};
|
|
110
|
-
exports.mergePermissions = mergePermissions;
|