@autofleet/zehut 3.1.2-beta.15 → 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.
@@ -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,29 +102,13 @@ class ApiUser {
82
102
  return Object.keys(this.privatePermissions[key] || {});
83
103
  }
84
104
  get elevatedPermissions() {
85
- const permissions = {
86
- fleets: {},
87
- businessModels: {},
88
- demandSources: {},
89
- };
90
- [...this.privateElevatedPermissionsHash.values()].forEach((elevatedPermissions) => {
91
- (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
- const permissions = {
100
- ...this.privatePermissions,
101
- fleets: { ...this.privatePermissions.fleets },
102
- businessModels: { ...this.privatePermissions.businessModels },
103
- demandSources: { ...this.privatePermissions.demandSources },
104
- // Clone other nested objects as needed
105
- };
106
- (0, utils_1.mergePermissions)(permissions, this.elevatedPermissions);
107
- return permissions;
111
+ return mergePermissions(this.privatePermissions, this.privateElevatedPermissionsHash.values());
108
112
  }
109
113
  elevatePermissions(addedPermissions) {
110
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.mergePermissions = exports.getContextAttributes = exports.getEntitiesFromContext = exports.parsePermissions = exports.decodeBearer = exports.getAuthFromBearer = void 0;
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,15 +97,3 @@ const getContextAttributes = (contextId, decodedToken) => {
97
97
  return attributes;
98
98
  };
99
99
  exports.getContextAttributes = getContextAttributes;
100
- const mergePermissions = (target, sources) => {
101
- Object.keys(sources).forEach((topLevelKey) => {
102
- // eslint-disable-next-line no-param-reassign
103
- target[topLevelKey] ?? (target[topLevelKey] = {});
104
- Object.entries(sources[topLevelKey]).forEach(([entityId, perms]) => {
105
- // eslint-disable-next-line no-param-reassign
106
- target[topLevelKey][entityId] = (target[topLevelKey][entityId] || []).concat(perms);
107
- });
108
- });
109
- return target;
110
- };
111
- exports.mergePermissions = mergePermissions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/zehut",
3
- "version": "3.1.2-beta.15",
3
+ "version": "3.1.2-beta.16",
4
4
  "description": "manage user's identity",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",