@autofleet/zehut 3.1.0-beta.0 → 3.1.0-beta.1

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.
@@ -3,6 +3,7 @@ interface EntityPermissions {
3
3
  [key: string]: string[];
4
4
  }
5
5
  export declare const ELEVATED_PERMISSIONS_HEADER = "x-af-elevated-permissions";
6
+ export declare const CONTEXTS_IDS_HEADER = "x-af-context-ids";
6
7
  export interface UserPayload {
7
8
  businessModels: EntityPermissions;
8
9
  fleets: EntityPermissions;
@@ -20,15 +21,9 @@ export interface PartialUserPayload {
20
21
  drivers?: EntityPermissions;
21
22
  businessAccounts?: EntityPermissions;
22
23
  }
23
- export interface AuthorizationFilters {
24
- businessModelId?: string;
25
- fleetId?: string;
26
- demandSourceId?: string;
27
- }
28
24
  export type CustomPermissionLoader = (string: any) => Promise<UserPayload>;
29
25
  export default class ApiUser {
30
26
  id: string | undefined;
31
- authorizationFilters: AuthorizationFilters;
32
27
  privatePermissions: UserPayload | undefined;
33
28
  privateElevatedPermissionsHash: Map<string, PartialUserPayload | undefined>;
34
29
  privatePermissionsLegacy: any;
@@ -37,7 +32,8 @@ export default class ApiUser {
37
32
  };
38
33
  emptyUser: boolean;
39
34
  accountType: AccountType | undefined;
40
- constructor(id?: string, accountType?: AccountType, elevatedPermissions?: PartialUserPayload, authorizationFilters?: AuthorizationFilters);
35
+ contextIds?: string[];
36
+ constructor(id?: string, accountType?: AccountType, elevatedPermissions?: PartialUserPayload, contextIds?: string[]);
41
37
  getUserPermissions(): Promise<UserPayload>;
42
38
  useCustomPermissionLoader(customPermissionLoader: any): Promise<UserPayload>;
43
39
  get businessModels(): string[] | undefined;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ELEVATED_PERMISSIONS_HEADER = void 0;
6
+ exports.CONTEXTS_IDS_HEADER = exports.ELEVATED_PERMISSIONS_HEADER = void 0;
7
7
  /* eslint-disable consistent-return */
8
8
  const node_cache_1 = __importDefault(require("node-cache"));
9
9
  const object_hash_1 = __importDefault(require("object-hash"));
@@ -12,14 +12,15 @@ const uuid_1 = require("uuid");
12
12
  const outbreak_1 = require("@autofleet/outbreak");
13
13
  const services_1 = require("../services");
14
14
  exports.ELEVATED_PERMISSIONS_HEADER = 'x-af-elevated-permissions';
15
+ exports.CONTEXTS_IDS_HEADER = 'x-af-context-ids';
15
16
  const userCache = new node_cache_1.default({ stdTTL: 10 });
16
17
  class ApiUser {
17
- constructor(id, accountType, elevatedPermissions, authorizationFilters = {}) {
18
+ constructor(id, accountType, elevatedPermissions, contextIds) {
18
19
  this.id = id;
19
- this.authorizationFilters = authorizationFilters;
20
20
  this.emptyUser = !!id;
21
21
  this.appPermission = {};
22
22
  this.privateElevatedPermissionsHash = new Map();
23
+ this.contextIds = contextIds;
23
24
  if (elevatedPermissions) {
24
25
  this.privateElevatedPermissionsHash.set('initial', elevatedPermissions);
25
26
  }
@@ -36,11 +37,11 @@ class ApiUser {
36
37
  }
37
38
  const cacheKey = (0, object_hash_1.default)({
38
39
  id: this.id,
39
- authorizationFilters: this.authorizationFilters,
40
+ contextIds: this.contextIds,
40
41
  });
41
42
  let data = userCache.get(cacheKey);
42
43
  if (!data) {
43
- ({ data } = await services_1.IdentityNetwork.get(`/api/v1/users/${this.id}/authorization-payload`, this.authorizationFilters));
44
+ ({ data } = await services_1.IdentityNetwork.get(`/api/v1/users/${this.id}/authorization-payload`, { contextIds: this.contextIds }));
44
45
  userCache.set(cacheKey, data);
45
46
  }
46
47
  this.accountType = data.accountType;
package/lib/user/index.js CHANGED
@@ -42,7 +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
+ const contextIds = req.headers?.['x-af-contexts-ids']?.split(',');
46
46
  const trace = (0, tracer_1.newTrace)('userPayload');
47
47
  if (!userId) {
48
48
  return next();
@@ -50,11 +50,7 @@ const middleware = (options = {}) => async (req, res, next) => {
50
50
  const elevatedPermissionsFromHeader = req.headers[ApiUser_1.ELEVATED_PERMISSIONS_HEADER] && req.headers[ApiUser_1.ELEVATED_PERMISSIONS_HEADER].length > 0
51
51
  ? JSON.parse(req.headers[ApiUser_1.ELEVATED_PERMISSIONS_HEADER])
52
52
  : {};
53
- const userObject = new ApiUser_1.default(userId, 'user', elevatedPermissionsFromHeader, {
54
- fleetId,
55
- businessModelId,
56
- demandSourceId,
57
- });
53
+ const userObject = new ApiUser_1.default(userId, 'user', elevatedPermissionsFromHeader, contextIds);
58
54
  if (eagerLoadUserPermissions) {
59
55
  if (customPermissionLoader) {
60
56
  await userObject.useCustomPermissionLoader(customPermissionLoader);
@@ -113,12 +109,8 @@ const middlewareWithDecode = (options = {}) => async (req, res, next) => {
113
109
  if (userId) {
114
110
  req.headers['X-AF-USER-ID'] = userId;
115
111
  }
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
+ const contextIds = req.headers?.['x-af-contexts-ids']?.split(',');
113
+ const userObject = new ApiUser_1.default(userId, decoded?.user?.accountType, undefined, contextIds);
122
114
  if (eagerLoadUserPermissions) {
123
115
  await userObject.getUserPermissions();
124
116
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/zehut",
3
- "version": "3.1.0-beta.0",
3
+ "version": "3.1.0-beta.1",
4
4
  "description": "manage user's identity",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",