@autofleet/zehut 3.0.10 → 3.1.0-beta.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.
@@ -20,9 +20,15 @@ export interface PartialUserPayload {
20
20
  drivers?: EntityPermissions;
21
21
  businessAccounts?: EntityPermissions;
22
22
  }
23
+ export interface AuthorizationFilters {
24
+ businessModelId?: string;
25
+ fleetId?: string;
26
+ demandSourceId?: string;
27
+ }
23
28
  export type CustomPermissionLoader = (string: any) => Promise<UserPayload>;
24
29
  export default class ApiUser {
25
30
  id: string | undefined;
31
+ authorizationFilters: AuthorizationFilters;
26
32
  privatePermissions: UserPayload | undefined;
27
33
  privateElevatedPermissionsHash: Map<string, PartialUserPayload | undefined>;
28
34
  privatePermissionsLegacy: any;
@@ -31,7 +37,7 @@ export default class ApiUser {
31
37
  };
32
38
  emptyUser: boolean;
33
39
  accountType: AccountType | undefined;
34
- constructor(id?: string, accountType?: AccountType, elevatedPermissions?: PartialUserPayload);
40
+ constructor(id?: string, accountType?: AccountType, elevatedPermissions?: PartialUserPayload, authorizationFilters?: AuthorizationFilters);
35
41
  getUserPermissions(): Promise<UserPayload>;
36
42
  useCustomPermissionLoader(customPermissionLoader: any): Promise<UserPayload>;
37
43
  get businessModels(): string[] | undefined;
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.ELEVATED_PERMISSIONS_HEADER = void 0;
7
7
  /* eslint-disable consistent-return */
8
8
  const node_cache_1 = __importDefault(require("node-cache"));
9
+ const object_hash_1 = __importDefault(require("object-hash"));
9
10
  const merge_deep_1 = __importDefault(require("merge-deep"));
10
11
  const uuid_1 = require("uuid");
11
12
  const outbreak_1 = require("@autofleet/outbreak");
@@ -13,8 +14,9 @@ const services_1 = require("../services");
13
14
  exports.ELEVATED_PERMISSIONS_HEADER = 'x-af-elevated-permissions';
14
15
  const userCache = new node_cache_1.default({ stdTTL: 10 });
15
16
  class ApiUser {
16
- constructor(id, accountType, elevatedPermissions) {
17
+ constructor(id, accountType, elevatedPermissions, authorizationFilters = {}) {
17
18
  this.id = id;
19
+ this.authorizationFilters = authorizationFilters;
18
20
  this.emptyUser = !!id;
19
21
  this.appPermission = {};
20
22
  this.privateElevatedPermissionsHash = new Map();
@@ -32,10 +34,14 @@ class ApiUser {
32
34
  if (this.privatePermissions) {
33
35
  return this.privatePermissions;
34
36
  }
35
- let data = userCache.get(this.id);
37
+ const cacheKey = (0, object_hash_1.default)({
38
+ id: this.id,
39
+ authorizationFilters: this.authorizationFilters,
40
+ });
41
+ let data = userCache.get(cacheKey);
36
42
  if (!data) {
37
- ({ data } = await services_1.IdentityNetwork.get(`/api/v1/users/${this.id}/authorization-payload`));
38
- userCache.set(this.id, data);
43
+ ({ data } = await services_1.IdentityNetwork.get(`/api/v1/users/${this.id}/authorization-payload`, this.authorizationFilters));
44
+ userCache.set(cacheKey, data);
39
45
  }
40
46
  this.accountType = data.accountType;
41
47
  this.privatePermissions = data;
@@ -48,13 +54,14 @@ class ApiUser {
48
54
  if (this.privatePermissions) {
49
55
  return this.privatePermissions;
50
56
  }
51
- const cachedResult = userCache.get(this.id);
57
+ const cacheKey = this.id;
58
+ const cachedResult = userCache.get(cacheKey);
52
59
  if (cachedResult) {
53
60
  this.privatePermissions = cachedResult;
54
61
  return cachedResult;
55
62
  }
56
63
  const data = await customPermissionLoader(this.id);
57
- userCache.set(this.id, data);
64
+ userCache.set(cacheKey, data);
58
65
  this.privatePermissions = data;
59
66
  return this.privatePermissions;
60
67
  }
@@ -139,7 +146,8 @@ class ApiUser {
139
146
  if (currentAppPermission) {
140
147
  return currentAppPermission;
141
148
  }
142
- const cachedResult = userCache.get(`${this.id}:${appId}`);
149
+ const cacheKey = `${this.id}:${appId}`;
150
+ const cachedResult = userCache.get(cacheKey);
143
151
  if (cachedResult) {
144
152
  this.appPermission[appId] = cachedResult;
145
153
  return cachedResult;
@@ -151,7 +159,7 @@ class ApiUser {
151
159
  'x-autofleet-apps-secret': clientSecret,
152
160
  },
153
161
  });
154
- userCache.set(this.id, data);
162
+ userCache.set(cacheKey, data);
155
163
  this.appPermission[appId] = data;
156
164
  return this.appPermission[appId];
157
165
  }
package/lib/user/index.js CHANGED
@@ -42,6 +42,7 @@ const middleware = (options = {}) => async (req, res, next) => {
42
42
  }
43
43
  const { eagerLoadUserPermissions, eagerLoadUserPermissionsLegacy, customPermissionLoader, } = options;
44
44
  const userId = req.headers['x-af-user-id'];
45
+ const { fleetId, businessModelId, demandSourceId, } = req.query;
45
46
  const trace = (0, tracer_1.newTrace)('userPayload');
46
47
  if (!userId) {
47
48
  return next();
@@ -49,7 +50,11 @@ const middleware = (options = {}) => async (req, res, next) => {
49
50
  const elevatedPermissionsFromHeader = req.headers[ApiUser_1.ELEVATED_PERMISSIONS_HEADER] && req.headers[ApiUser_1.ELEVATED_PERMISSIONS_HEADER].length > 0
50
51
  ? JSON.parse(req.headers[ApiUser_1.ELEVATED_PERMISSIONS_HEADER])
51
52
  : {};
52
- const userObject = new ApiUser_1.default(userId, 'user', elevatedPermissionsFromHeader);
53
+ const userObject = new ApiUser_1.default(userId, 'user', elevatedPermissionsFromHeader, {
54
+ fleetId,
55
+ businessModelId,
56
+ demandSourceId,
57
+ });
53
58
  if (eagerLoadUserPermissions) {
54
59
  if (customPermissionLoader) {
55
60
  await userObject.useCustomPermissionLoader(customPermissionLoader);
@@ -108,7 +113,12 @@ const middlewareWithDecode = (options = {}) => async (req, res, next) => {
108
113
  if (userId) {
109
114
  req.headers['X-AF-USER-ID'] = userId;
110
115
  }
111
- const userObject = new ApiUser_1.default(userId, decoded?.user?.accountType);
116
+ const { fleetId, businessModelId, demandSourceId, } = req.query;
117
+ const userObject = new ApiUser_1.default(userId, decoded?.user?.accountType, undefined, {
118
+ fleetId,
119
+ businessModelId,
120
+ demandSourceId,
121
+ });
112
122
  if (eagerLoadUserPermissions) {
113
123
  await userObject.getUserPermissions();
114
124
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/zehut",
3
- "version": "3.0.10",
3
+ "version": "3.1.0-beta.0",
4
4
  "description": "manage user's identity",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -33,6 +33,7 @@
33
33
  "moment": "^2.29.1",
34
34
  "nock": "^13.2.9",
35
35
  "node-cache": "^5.1.2",
36
+ "object-hash": "^3.0.0",
36
37
  "supertest": "^6.2.4",
37
38
  "uuid": "^8.3.2"
38
39
  },