@mondart/nestjs-common-module-iam 1.0.0 → 3.0.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.
Files changed (71) hide show
  1. package/dist/constants/iam-decorator-key.const.d.ts +7 -4
  2. package/dist/constants/iam-decorator-key.const.d.ts.map +1 -1
  3. package/dist/constants/iam-decorator-key.const.js +8 -5
  4. package/dist/constants/iam-decorator-key.const.js.map +1 -1
  5. package/dist/decorators/allow-iam.decorator.d.ts +3 -0
  6. package/dist/decorators/allow-iam.decorator.d.ts.map +1 -0
  7. package/dist/decorators/allow-iam.decorator.js +8 -0
  8. package/dist/decorators/allow-iam.decorator.js.map +1 -0
  9. package/dist/decorators/iam-entity-access.decorator.d.ts +4 -0
  10. package/dist/decorators/iam-entity-access.decorator.d.ts.map +1 -0
  11. package/dist/decorators/iam-entity-access.decorator.js +10 -0
  12. package/dist/decorators/iam-entity-access.decorator.js.map +1 -0
  13. package/dist/decorators/public-iam.decorators.d.ts +2 -0
  14. package/dist/decorators/public-iam.decorators.d.ts.map +1 -0
  15. package/dist/decorators/public-iam.decorators.js +8 -0
  16. package/dist/decorators/public-iam.decorators.js.map +1 -0
  17. package/dist/guards/iam-context.guard.d.ts +3 -4
  18. package/dist/guards/iam-context.guard.d.ts.map +1 -1
  19. package/dist/guards/iam-context.guard.js +22 -71
  20. package/dist/guards/iam-context.guard.js.map +1 -1
  21. package/dist/helpers/user-owner.helper.d.ts +12 -0
  22. package/dist/helpers/user-owner.helper.d.ts.map +1 -0
  23. package/dist/helpers/user-owner.helper.js +55 -0
  24. package/dist/helpers/user-owner.helper.js.map +1 -0
  25. package/dist/iam-context.module.d.ts.map +1 -1
  26. package/dist/iam-context.module.js +2 -0
  27. package/dist/iam-context.module.js.map +1 -1
  28. package/dist/index.d.ts +9 -5
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +9 -5
  31. package/dist/index.js.map +1 -1
  32. package/dist/interfaces/iam-entity-access.interface.d.ts +38 -0
  33. package/dist/interfaces/iam-entity-access.interface.d.ts.map +1 -0
  34. package/dist/interfaces/iam-entity-access.interface.js +3 -0
  35. package/dist/interfaces/iam-entity-access.interface.js.map +1 -0
  36. package/dist/pipes/iam-find-one-query-scope.pipe.d.ts +10 -0
  37. package/dist/pipes/iam-find-one-query-scope.pipe.d.ts.map +1 -0
  38. package/dist/pipes/{iam-paginate-query.pipe.js → iam-find-one-query-scope.pipe.js} +22 -24
  39. package/dist/pipes/iam-find-one-query-scope.pipe.js.map +1 -0
  40. package/dist/pipes/iam-paginate-query-scope.pipe.d.ts +11 -0
  41. package/dist/pipes/iam-paginate-query-scope.pipe.d.ts.map +1 -0
  42. package/dist/pipes/iam-paginate-query-scope.pipe.js +48 -0
  43. package/dist/pipes/iam-paginate-query-scope.pipe.js.map +1 -0
  44. package/dist/services/iam-entity-access.service.d.ts +22 -0
  45. package/dist/services/iam-entity-access.service.d.ts.map +1 -0
  46. package/dist/services/iam-entity-access.service.js +250 -0
  47. package/dist/services/iam-entity-access.service.js.map +1 -0
  48. package/dist/types/iam-entity-access.type.d.ts +19 -0
  49. package/dist/types/iam-entity-access.type.d.ts.map +1 -0
  50. package/dist/types/iam-entity-access.type.js +4 -0
  51. package/dist/types/iam-entity-access.type.js.map +1 -0
  52. package/package.json +16 -3
  53. package/dist/decorators/get-owner-id.decorator.d.ts +0 -2
  54. package/dist/decorators/get-owner-id.decorator.d.ts.map +0 -1
  55. package/dist/decorators/get-owner-id.decorator.js +0 -13
  56. package/dist/decorators/get-owner-id.decorator.js.map +0 -1
  57. package/dist/decorators/iam-public.decorator.d.ts +0 -2
  58. package/dist/decorators/iam-public.decorator.d.ts.map +0 -1
  59. package/dist/decorators/iam-public.decorator.js +0 -8
  60. package/dist/decorators/iam-public.decorator.js.map +0 -1
  61. package/dist/decorators/owns-entity.decorator.d.ts +0 -10
  62. package/dist/decorators/owns-entity.decorator.d.ts.map +0 -1
  63. package/dist/decorators/owns-entity.decorator.js +0 -8
  64. package/dist/decorators/owns-entity.decorator.js.map +0 -1
  65. package/dist/decorators/set-iam-route-user-type.decorator.d.ts +0 -3
  66. package/dist/decorators/set-iam-route-user-type.decorator.d.ts.map +0 -1
  67. package/dist/decorators/set-iam-route-user-type.decorator.js +0 -8
  68. package/dist/decorators/set-iam-route-user-type.decorator.js.map +0 -1
  69. package/dist/pipes/iam-paginate-query.pipe.d.ts +0 -8
  70. package/dist/pipes/iam-paginate-query.pipe.d.ts.map +0 -1
  71. package/dist/pipes/iam-paginate-query.pipe.js.map +0 -1
@@ -1,5 +1,8 @@
1
- export declare const IAM_PUBLIC_ROUTE = "iamPublicRoute";
2
- export declare const IAM_ROUTE_USER_TYPE = "iamRouteUserType";
3
- export declare const IAM_PRIVATE_ROUTE = "iamPrivateRoute";
4
- export declare const IAM_OWN_ROUTE = "iamOwnRoute";
1
+ export declare const IAM_PUBLIC_ROUTE = "iam_public_route";
2
+ export declare const IAM_ROUTE_USER_TYPE = "iam_route_user_type";
3
+ export declare const IAM_ENTITY_ACCESS_KEY = "iam_entity_access";
4
+ export declare const IAM_ENTITY_SCOPE_KEY = "iam_entity_scope";
5
+ export declare const IAM_REQUEST_USER_TYPE_KEY = "__iam_user_type";
6
+ export declare const IAM_REQUEST_ENTITY_SCOPE_OPTIONS_KEY = "__iam_entity_scope_options";
7
+ export declare const IAM_QUERY_SCOPE_WHERE_KEY = "iamWhere";
5
8
  //# sourceMappingURL=iam-decorator-key.const.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"iam-decorator-key.const.d.ts","sourceRoot":"","sources":["../../src/constants/iam-decorator-key.const.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,mBAAmB,CAAC;AACjD,eAAO,MAAM,mBAAmB,qBAAqB,CAAC;AACtD,eAAO,MAAM,iBAAiB,oBAAoB,CAAC;AACnD,eAAO,MAAM,aAAa,gBAAgB,CAAC"}
1
+ {"version":3,"file":"iam-decorator-key.const.d.ts","sourceRoot":"","sources":["../../src/constants/iam-decorator-key.const.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,qBAAqB,CAAC;AACnD,eAAO,MAAM,mBAAmB,wBAAwB,CAAC;AAEzD,eAAO,MAAM,qBAAqB,sBAAsB,CAAC;AACzD,eAAO,MAAM,oBAAoB,qBAAqB,CAAC;AAEvD,eAAO,MAAM,yBAAyB,oBAAoB,CAAC;AAC3D,eAAO,MAAM,oCAAoC,+BACnB,CAAC;AAE/B,eAAO,MAAM,yBAAyB,aAAa,CAAC"}
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.IAM_OWN_ROUTE = exports.IAM_PRIVATE_ROUTE = exports.IAM_ROUTE_USER_TYPE = exports.IAM_PUBLIC_ROUTE = void 0;
4
- exports.IAM_PUBLIC_ROUTE = 'iamPublicRoute';
5
- exports.IAM_ROUTE_USER_TYPE = 'iamRouteUserType';
6
- exports.IAM_PRIVATE_ROUTE = 'iamPrivateRoute';
7
- exports.IAM_OWN_ROUTE = 'iamOwnRoute';
3
+ exports.IAM_QUERY_SCOPE_WHERE_KEY = exports.IAM_REQUEST_ENTITY_SCOPE_OPTIONS_KEY = exports.IAM_REQUEST_USER_TYPE_KEY = exports.IAM_ENTITY_SCOPE_KEY = exports.IAM_ENTITY_ACCESS_KEY = exports.IAM_ROUTE_USER_TYPE = exports.IAM_PUBLIC_ROUTE = void 0;
4
+ exports.IAM_PUBLIC_ROUTE = 'iam_public_route';
5
+ exports.IAM_ROUTE_USER_TYPE = 'iam_route_user_type';
6
+ exports.IAM_ENTITY_ACCESS_KEY = 'iam_entity_access';
7
+ exports.IAM_ENTITY_SCOPE_KEY = 'iam_entity_scope';
8
+ exports.IAM_REQUEST_USER_TYPE_KEY = '__iam_user_type';
9
+ exports.IAM_REQUEST_ENTITY_SCOPE_OPTIONS_KEY = '__iam_entity_scope_options';
10
+ exports.IAM_QUERY_SCOPE_WHERE_KEY = 'iamWhere';
8
11
  //# sourceMappingURL=iam-decorator-key.const.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"iam-decorator-key.const.js","sourceRoot":"","sources":["../../src/constants/iam-decorator-key.const.ts"],"names":[],"mappings":";;;AAAa,QAAA,gBAAgB,GAAG,gBAAgB,CAAC;AACpC,QAAA,mBAAmB,GAAG,kBAAkB,CAAC;AACzC,QAAA,iBAAiB,GAAG,iBAAiB,CAAC;AACtC,QAAA,aAAa,GAAG,aAAa,CAAC"}
1
+ {"version":3,"file":"iam-decorator-key.const.js","sourceRoot":"","sources":["../../src/constants/iam-decorator-key.const.ts"],"names":[],"mappings":";;;AAAa,QAAA,gBAAgB,GAAG,kBAAkB,CAAC;AACtC,QAAA,mBAAmB,GAAG,qBAAqB,CAAC;AAE5C,QAAA,qBAAqB,GAAG,mBAAmB,CAAC;AAC5C,QAAA,oBAAoB,GAAG,kBAAkB,CAAC;AAE1C,QAAA,yBAAyB,GAAG,iBAAiB,CAAC;AAC9C,QAAA,oCAAoC,GAC/C,4BAA4B,CAAC;AAElB,QAAA,yBAAyB,GAAG,UAAU,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { IamUserTypeEnum } from '../enums/iam-user-type.enum';
2
+ export declare const AllowIam: (...userTypes: IamUserTypeEnum[]) => import("@nestjs/common").CustomDecorator<string>;
3
+ //# sourceMappingURL=allow-iam.decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"allow-iam.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/allow-iam.decorator.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAE9D,eAAO,MAAM,QAAQ,GAAI,GAAG,WAAW,eAAe,EAAE,qDACX,CAAC"}
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AllowIam = void 0;
4
+ const iam_decorator_key_const_1 = require("../constants/iam-decorator-key.const");
5
+ const common_1 = require("@nestjs/common");
6
+ const AllowIam = (...userTypes) => (0, common_1.SetMetadata)(iam_decorator_key_const_1.IAM_ROUTE_USER_TYPE, userTypes);
7
+ exports.AllowIam = AllowIam;
8
+ //# sourceMappingURL=allow-iam.decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"allow-iam.decorator.js","sourceRoot":"","sources":["../../src/decorators/allow-iam.decorator.ts"],"names":[],"mappings":";;;AAAA,kFAA2E;AAC3E,2CAA6C;AAGtC,MAAM,QAAQ,GAAG,CAAC,GAAG,SAA4B,EAAE,EAAE,CAC1D,IAAA,oBAAW,EAAC,6CAAmB,EAAE,SAAS,CAAC,CAAC;AADjC,QAAA,QAAQ,YACyB"}
@@ -0,0 +1,4 @@
1
+ import { ApplyIamEntityScopeOptions, RequireIamEntityAccessOptions } from '../interfaces/iam-entity-access.interface';
2
+ export declare const RequireIamEntityAccess: <T = any>(options: RequireIamEntityAccessOptions<T>) => import("@nestjs/common").CustomDecorator<string>;
3
+ export declare const ApplyIamEntityScope: <T = any>(options: ApplyIamEntityScopeOptions<T>) => import("@nestjs/common").CustomDecorator<string>;
4
+ //# sourceMappingURL=iam-entity-access.decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iam-entity-access.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/iam-entity-access.decorator.ts"],"names":[],"mappings":"AACA,OAAO,EACL,0BAA0B,EAC1B,6BAA6B,EAC9B,MAAM,2CAA2C,CAAC;AAMnD,eAAO,MAAM,sBAAsB,GAAI,CAAC,GAAG,GAAG,EAC5C,SAAS,6BAA6B,CAAC,CAAC,CAAC,qDACK,CAAC;AAEjD,eAAO,MAAM,mBAAmB,GAAI,CAAC,GAAG,GAAG,EACzC,SAAS,0BAA0B,CAAC,CAAC,CAAC,qDACO,CAAC"}
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApplyIamEntityScope = exports.RequireIamEntityAccess = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ const iam_decorator_key_const_1 = require("../constants/iam-decorator-key.const");
6
+ const RequireIamEntityAccess = (options) => (0, common_1.SetMetadata)(iam_decorator_key_const_1.IAM_ENTITY_ACCESS_KEY, options);
7
+ exports.RequireIamEntityAccess = RequireIamEntityAccess;
8
+ const ApplyIamEntityScope = (options) => (0, common_1.SetMetadata)(iam_decorator_key_const_1.IAM_ENTITY_SCOPE_KEY, options);
9
+ exports.ApplyIamEntityScope = ApplyIamEntityScope;
10
+ //# sourceMappingURL=iam-entity-access.decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iam-entity-access.decorator.js","sourceRoot":"","sources":["../../src/decorators/iam-entity-access.decorator.ts"],"names":[],"mappings":";;;AAAA,2CAA6C;AAK7C,kFAG8C;AAEvC,MAAM,sBAAsB,GAAG,CACpC,OAAyC,EACzC,EAAE,CAAC,IAAA,oBAAW,EAAC,+CAAqB,EAAE,OAAO,CAAC,CAAC;AAFpC,QAAA,sBAAsB,0BAEc;AAE1C,MAAM,mBAAmB,GAAG,CACjC,OAAsC,EACtC,EAAE,CAAC,IAAA,oBAAW,EAAC,8CAAoB,EAAE,OAAO,CAAC,CAAC;AAFnC,QAAA,mBAAmB,uBAEgB"}
@@ -0,0 +1,2 @@
1
+ export declare const PublicIam: () => import("@nestjs/common").CustomDecorator<string>;
2
+ //# sourceMappingURL=public-iam.decorators.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"public-iam.decorators.d.ts","sourceRoot":"","sources":["../../src/decorators/public-iam.decorators.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,SAAS,wDAA4C,CAAC"}
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PublicIam = void 0;
4
+ const iam_decorator_key_const_1 = require("../constants/iam-decorator-key.const");
5
+ const common_1 = require("@nestjs/common");
6
+ const PublicIam = () => (0, common_1.SetMetadata)(iam_decorator_key_const_1.IAM_PUBLIC_ROUTE, true);
7
+ exports.PublicIam = PublicIam;
8
+ //# sourceMappingURL=public-iam.decorators.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"public-iam.decorators.js","sourceRoot":"","sources":["../../src/decorators/public-iam.decorators.ts"],"names":[],"mappings":";;;AAAA,kFAAwE;AACxE,2CAA6C;AAEtC,MAAM,SAAS,GAAG,GAAG,EAAE,CAAC,IAAA,oBAAW,EAAC,0CAAgB,EAAE,IAAI,CAAC,CAAC;AAAtD,QAAA,SAAS,aAA6C"}
@@ -1,11 +1,10 @@
1
1
  import { CanActivate, ExecutionContext } from '@nestjs/common';
2
2
  import { Reflector } from '@nestjs/core';
3
- import { DataSource } from 'typeorm';
3
+ import { IamEntityAccessService } from '../services/iam-entity-access.service';
4
4
  export declare class IamContextGuard implements CanActivate {
5
5
  private readonly reflector;
6
- private readonly dataSource;
7
- constructor(reflector: Reflector, dataSource: DataSource);
6
+ private readonly iamEntityAccessService;
7
+ constructor(reflector: Reflector, iamEntityAccessService: IamEntityAccessService);
8
8
  canActivate(context: ExecutionContext): Promise<boolean>;
9
- private checkOwnership;
10
9
  }
11
10
  //# sourceMappingURL=iam-context.guard.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"iam-context.guard.d.ts","sourceRoot":"","sources":["../../src/guards/iam-context.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,gBAAgB,EAKjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAiBzC,OAAO,EAAE,UAAU,EAAU,MAAM,SAAS,CAAC;AAE7C,qBACa,eAAgB,YAAW,WAAW;IAE/C,OAAO,CAAC,QAAQ,CAAC,SAAS;IAE1B,OAAO,CAAC,QAAQ,CAAC,UAAU;gBAFV,SAAS,EAAE,SAAS,EAEpB,UAAU,EAAE,UAAU;IAGnC,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;YAgEhD,cAAc;CA6C7B"}
1
+ {"version":3,"file":"iam-context.guard.d.ts","sourceRoot":"","sources":["../../src/guards/iam-context.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,gBAAgB,EAGjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAWzC,OAAO,EAAE,sBAAsB,EAAE,MAAM,uCAAuC,CAAC;AAM/E,qBACa,eAAgB,YAAW,WAAW;IAE/C,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,sBAAsB;gBADtB,SAAS,EAAE,SAAS,EACpB,sBAAsB,EAAE,sBAAsB;IAG3D,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;CAoE/D"}
@@ -8,102 +8,53 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
8
8
  var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
- var __param = (this && this.__param) || function (paramIndex, decorator) {
12
- return function (target, key) { decorator(target, key, paramIndex); }
13
- };
14
11
  Object.defineProperty(exports, "__esModule", { value: true });
15
12
  exports.IamContextGuard = void 0;
16
13
  const common_1 = require("@nestjs/common");
17
14
  const core_1 = require("@nestjs/core");
18
15
  const nestjs_common_module_core_1 = require("@mondart/nestjs-common-module-core");
19
16
  const iam_decorator_key_const_1 = require("../constants/iam-decorator-key.const");
20
- const iam_user_type_enum_1 = require("../enums/iam-user-type.enum");
21
- const owns_entity_decorator_1 = require("../decorators/owns-entity.decorator");
22
- const constants_1 = require("@nestjs/common/constants");
23
- const typeorm_1 = require("@nestjs/typeorm");
24
- const typeorm_2 = require("typeorm");
17
+ const iam_entity_access_service_1 = require("../services/iam-entity-access.service");
25
18
  let IamContextGuard = class IamContextGuard {
26
- constructor(reflector, dataSource) {
19
+ constructor(reflector, iamEntityAccessService) {
27
20
  this.reflector = reflector;
28
- this.dataSource = dataSource;
21
+ this.iamEntityAccessService = iamEntityAccessService;
29
22
  }
30
23
  async canActivate(context) {
31
24
  const request = context.switchToHttp().getRequest();
32
- const iamContext = (0, nestjs_common_module_core_1.getIamContext)(request);
33
- const iaPublicRoute = this.reflector.getAllAndOverride(iam_decorator_key_const_1.IAM_PUBLIC_ROUTE, [context.getHandler(), context.getClass()]);
34
- if (iaPublicRoute)
25
+ const isPublicRoute = this.reflector.getAllAndOverride(iam_decorator_key_const_1.IAM_PUBLIC_ROUTE, [context.getHandler(), context.getClass()]);
26
+ if (isPublicRoute) {
35
27
  return true;
36
- const userType = this.reflector.getAllAndOverride(iam_decorator_key_const_1.IAM_ROUTE_USER_TYPE, [
28
+ }
29
+ const iamContext = (0, nestjs_common_module_core_1.getIamContext)(request);
30
+ const allowedUserTypes = this.reflector.getAllAndOverride(iam_decorator_key_const_1.IAM_ROUTE_USER_TYPE, [
37
31
  context.getHandler(),
38
32
  context.getClass(),
39
33
  ]) || [];
40
- if (iamContext.agentId) {
41
- if (userType.includes(iam_user_type_enum_1.IamUserTypeEnum.AGENT)) {
42
- return true;
43
- }
44
- else {
45
- throw new common_1.ForbiddenException();
46
- }
47
- }
48
- if (userType.includes(iam_user_type_enum_1.IamUserTypeEnum.USER) ||
49
- userType.includes(iam_user_type_enum_1.IamUserTypeEnum.GUEST)) {
50
- const options = this.reflector.getAllAndOverride(owns_entity_decorator_1.OWNS_ENTITY_KEY, [context.getHandler(), context.getClass()]);
51
- if (options) {
52
- const isOwner = await this.checkOwnership(options.entity.name, options.entityPropertyKey, options.requestEntityId(request), options.ownerPropertyKey, iamContext?.user?.contactId);
53
- const controllerPath = this.reflector.getAllAndOverride(constants_1.PATH_METADATA, [
54
- context.getHandler(),
55
- context.getClass(),
56
- ]) ?? '';
57
- if (!isOwner) {
58
- throw new common_1.ForbiddenException(`You do not own this ${controllerPath}`);
59
- }
60
- }
61
- return true;
62
- }
63
- else {
34
+ const currentUserType = this.iamEntityAccessService.resolveUserType(iamContext, allowedUserTypes);
35
+ request[iam_decorator_key_const_1.IAM_REQUEST_USER_TYPE_KEY] = currentUserType;
36
+ if (!allowedUserTypes.includes(currentUserType)) {
64
37
  throw new common_1.ForbiddenException();
65
38
  }
66
- throw new common_1.UnauthorizedException();
67
- }
68
- async checkOwnership(entity, entityPropertyKey, entityId, ownerPropertyKey, ownerId) {
69
- if (!entityId)
70
- return true;
71
- const queryRunner = this.dataSource.createQueryRunner();
72
- await queryRunner.connect();
73
- await queryRunner.startTransaction('READ UNCOMMITTED');
74
- try {
75
- const entityManager = queryRunner.manager;
76
- const isExists = await entityManager.getRepository(entity).exists({
77
- where: {
78
- [entityPropertyKey]: entityId,
79
- },
80
- });
81
- if (!isExists) {
82
- throw new common_1.NotFoundException(nestjs_common_module_core_1.MessageFormatter.replace(nestjs_common_module_core_1.SharedMessages.RESOURCE_NOT_FOUND, 'Cart'));
83
- }
84
- const isBlongeToOwner = await entityManager.getRepository(entity).exists({
85
- where: {
86
- [entityPropertyKey]: entityId,
87
- [ownerPropertyKey]: ownerId || (0, typeorm_2.IsNull)(),
88
- },
89
- });
90
- await queryRunner.commitTransaction();
91
- return isBlongeToOwner;
39
+ const scopeOptions = this.reflector.getAllAndOverride(iam_decorator_key_const_1.IAM_ENTITY_SCOPE_KEY, [context.getHandler(), context.getClass()]);
40
+ if (scopeOptions) {
41
+ request[iam_decorator_key_const_1.IAM_REQUEST_ENTITY_SCOPE_OPTIONS_KEY] = scopeOptions;
92
42
  }
93
- catch (err) {
94
- await queryRunner.rollbackTransaction();
95
- throw err;
43
+ const entityAccessOptions = this.reflector.getAllAndOverride(iam_decorator_key_const_1.IAM_ENTITY_ACCESS_KEY, [context.getHandler(), context.getClass()]);
44
+ if (!entityAccessOptions) {
45
+ return true;
96
46
  }
97
- finally {
98
- await queryRunner.release();
47
+ const hasAccess = await this.iamEntityAccessService.hasEntityAccess(entityAccessOptions, request, iamContext, currentUserType);
48
+ if (!hasAccess) {
49
+ throw new common_1.ForbiddenException(`You do not have access to this ${entityAccessOptions.resourceName ?? 'resource'}`);
99
50
  }
51
+ return true;
100
52
  }
101
53
  };
102
54
  exports.IamContextGuard = IamContextGuard;
103
55
  exports.IamContextGuard = IamContextGuard = __decorate([
104
56
  (0, common_1.Injectable)(),
105
- __param(1, (0, typeorm_1.InjectDataSource)()),
106
57
  __metadata("design:paramtypes", [core_1.Reflector,
107
- typeorm_2.DataSource])
58
+ iam_entity_access_service_1.IamEntityAccessService])
108
59
  ], IamContextGuard);
109
60
  //# sourceMappingURL=iam-context.guard.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"iam-context.guard.js","sourceRoot":"","sources":["../../src/guards/iam-context.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAOwB;AACxB,uCAAyC;AACzC,kFAI4C;AAC5C,kFAG8C;AAC9C,oEAA8D;AAC9D,+EAG6C;AAC7C,wDAAyD;AACzD,6CAAmD;AACnD,qCAA6C;AAGtC,IAAM,eAAe,GAArB,MAAM,eAAe;IAC1B,YACmB,SAAoB,EAEpB,UAAsB;QAFtB,cAAS,GAAT,SAAS,CAAW;QAEpB,eAAU,GAAV,UAAU,CAAY;IACtC,CAAC;IAEJ,KAAK,CAAC,WAAW,CAAC,OAAyB;QACzC,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;QAEpD,MAAM,UAAU,GAAG,IAAA,yCAAa,EAAC,OAAO,CAAC,CAAC;QAE1C,MAAM,aAAa,GAAG,IAAI,CAAC,SAAS,CAAC,iBAAiB,CACpD,0CAAgB,EAChB,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,OAAO,CAAC,QAAQ,EAAE,CAAC,CAC3C,CAAC;QAEF,IAAI,aAAa;YAAE,OAAO,IAAI,CAAC;QAE/B,MAAM,QAAQ,GACZ,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAoB,6CAAmB,EAAE;YACvE,OAA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;QAErD,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC;YAChD,MAAM,IAAI,2BAAkB,EAAE,CAAC;QACjC,CAAC;QAED,MAAM,YAAY,GAChB,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAC9B,8CAAoB,EACpB,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,OAAO,CAAC,QAAQ,EAAE,CAAC,CAC3C,CAAC;QAEJ,IAAI,YAAY,EAAE,CAAC;YACjB,OAAO,CAAC,8DAAoC,CAAC,GAAG,YAAY,CAAC;QAC/D,CAAC;QAED,MAAM,mBAAmB,GACvB,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAC9B,+CAAqB,EACrB,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,OAAO,CAAC,QAAQ,EAAE,CAAC,CAC3C,CAAC;QAEJ,IAAI,CAAC,mBAAmB,EAAE,CAAC;YACzB,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,sBAAsB,CAAC,eAAe,CACjE,mBAAmB,EACnB,OAAO,EACP,UAAU,EACV,eAAe,CAChB,CAAC;QAEF,IAAI,CAAC,SAAS,EAAE,CAAC;YACf,MAAM,IAAI,2BAAkB,CAC1B,kCACE,mBAAmB,CAAC,YAAY,IAAI,UACtC,EAAE,CACH,CAAC;QACJ,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAA;AA1EY,0CAAe;0BAAf,eAAe;IAD3B,IAAA,mBAAU,GAAE;qCAGmB,gBAAS;QACI,kDAAsB;GAHtD,eAAe,CA0E3B"}
@@ -0,0 +1,12 @@
1
+ import { AccessValueCast, EntityAccessBranch } from '../types/iam-entity-access.type';
2
+ import { EntityFieldMatch } from '../interfaces/iam-entity-access.interface';
3
+ export declare const byIam: <T = any>(field: keyof T & string, iamPath: string, cast?: AccessValueCast) => EntityAccessBranch<T>;
4
+ export declare const byReq: <T = any>(field: keyof T & string, reqPath: string, cast?: AccessValueCast) => EntityAccessBranch<T>;
5
+ export declare const matchIam: <T = any>(field: keyof T & string, iamPath: string, cast?: AccessValueCast) => EntityFieldMatch<T>;
6
+ export declare const matchReq: <T = any>(field: keyof T & string, reqPath: string, cast?: AccessValueCast) => EntityFieldMatch<T>;
7
+ export declare const allOf: <T = any>(...matches: EntityFieldMatch<T>[]) => EntityAccessBranch<T>;
8
+ export declare const nullFields: <T = any>(...fields: (keyof T & string)[]) => EntityAccessBranch<T>;
9
+ export declare const unowned: <T = any>(...fields: (keyof T & string)[]) => EntityAccessBranch<T>;
10
+ export declare const userAuditAccess: <T = any>(createdField?: keyof T & string, updatedField?: keyof T & string) => EntityAccessBranch<T>[];
11
+ export declare const agentAuditAccess: <T = any>(createdField?: keyof T & string, updatedField?: keyof T & string) => EntityAccessBranch<T>[];
12
+ //# sourceMappingURL=user-owner.helper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user-owner.helper.d.ts","sourceRoot":"","sources":["../../src/helpers/user-owner.helper.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,kBAAkB,EACnB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAK7E,eAAO,MAAM,KAAK,GAAI,CAAC,GAAG,GAAG,EAC3B,OAAO,MAAM,CAAC,GAAG,MAAM,EACvB,SAAS,MAAM,EACf,OAAO,eAAe,KACrB,kBAAkB,CAAC,CAAC,CAQrB,CAAC;AAKH,eAAO,MAAM,KAAK,GAAI,CAAC,GAAG,GAAG,EAC3B,OAAO,MAAM,CAAC,GAAG,MAAM,EACvB,SAAS,MAAM,EACf,OAAO,eAAe,KACrB,kBAAkB,CAAC,CAAC,CAQrB,CAAC;AAKH,eAAO,MAAM,QAAQ,GAAI,CAAC,GAAG,GAAG,EAC9B,OAAO,MAAM,CAAC,GAAG,MAAM,EACvB,SAAS,MAAM,EACf,OAAO,eAAe,KACrB,gBAAgB,CAAC,CAAC,CAInB,CAAC;AAEH,eAAO,MAAM,QAAQ,GAAI,CAAC,GAAG,GAAG,EAC9B,OAAO,MAAM,CAAC,GAAG,MAAM,EACvB,SAAS,MAAM,EACf,OAAO,eAAe,KACrB,gBAAgB,CAAC,CAAC,CAInB,CAAC;AAEH,eAAO,MAAM,KAAK,GAAI,CAAC,GAAG,GAAG,EAC3B,GAAG,SAAS,gBAAgB,CAAC,CAAC,CAAC,EAAE,KAChC,kBAAkB,CAAC,CAAC,CAErB,CAAC;AAKH,eAAO,MAAM,UAAU,GAAI,CAAC,GAAG,GAAG,EAChC,GAAG,QAAQ,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,EAAE,KAC9B,kBAAkB,CAAC,CAAC,CAErB,CAAC;AAKH,eAAO,MAAM,OAAO,GATO,CAAC,mBACf,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,EAAE,KAC9B,kBAAkB,CAAC,CAAC,CAOU,CAAC;AAQlC,eAAO,MAAM,eAAe,GAAI,CAAC,GAAG,GAAG,EACrC,eAAc,MAAM,CAAC,GAAG,MAA4C,EACpE,eAAc,MAAM,CAAC,GAAG,MAA4C,KACnE,kBAAkB,CAAC,CAAC,CAAC,EAGvB,CAAC;AAKF,eAAO,MAAM,gBAAgB,GAAI,CAAC,GAAG,GAAG,EACtC,eAAc,MAAM,CAAC,GAAG,MAA6C,EACrE,eAAc,MAAM,CAAC,GAAG,MAA6C,KACpE,kBAAkB,CAAC,CAAC,CAAC,EAGvB,CAAC"}
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.agentAuditAccess = exports.userAuditAccess = exports.unowned = exports.nullFields = exports.allOf = exports.matchReq = exports.matchIam = exports.byReq = exports.byIam = void 0;
4
+ const byIam = (field, iamPath, cast) => ({
5
+ all: [
6
+ {
7
+ field,
8
+ valueFrom: `iam.${iamPath}`,
9
+ cast,
10
+ },
11
+ ],
12
+ });
13
+ exports.byIam = byIam;
14
+ const byReq = (field, reqPath, cast) => ({
15
+ all: [
16
+ {
17
+ field,
18
+ valueFrom: `req.${reqPath}`,
19
+ cast,
20
+ },
21
+ ],
22
+ });
23
+ exports.byReq = byReq;
24
+ const matchIam = (field, iamPath, cast) => ({
25
+ field,
26
+ valueFrom: `iam.${iamPath}`,
27
+ cast,
28
+ });
29
+ exports.matchIam = matchIam;
30
+ const matchReq = (field, reqPath, cast) => ({
31
+ field,
32
+ valueFrom: `req.${reqPath}`,
33
+ cast,
34
+ });
35
+ exports.matchReq = matchReq;
36
+ const allOf = (...matches) => ({
37
+ all: matches,
38
+ });
39
+ exports.allOf = allOf;
40
+ const nullFields = (...fields) => ({
41
+ nullFields: fields,
42
+ });
43
+ exports.nullFields = nullFields;
44
+ exports.unowned = exports.nullFields;
45
+ const userAuditAccess = (createdField = 'createdByUser', updatedField = 'updatedByUser') => [
46
+ (0, exports.byIam)(createdField, 'userId', 'string'),
47
+ (0, exports.byIam)(updatedField, 'userId', 'string'),
48
+ ];
49
+ exports.userAuditAccess = userAuditAccess;
50
+ const agentAuditAccess = (createdField = 'createdByAdmin', updatedField = 'updatedByAdmin') => [
51
+ (0, exports.byIam)(createdField, 'agentId', 'string'),
52
+ (0, exports.byIam)(updatedField, 'agentId', 'string'),
53
+ ];
54
+ exports.agentAuditAccess = agentAuditAccess;
55
+ //# sourceMappingURL=user-owner.helper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user-owner.helper.js","sourceRoot":"","sources":["../../src/helpers/user-owner.helper.ts"],"names":[],"mappings":";;;AASO,MAAM,KAAK,GAAG,CACnB,KAAuB,EACvB,OAAe,EACf,IAAsB,EACC,EAAE,CAAC,CAAC;IAC3B,GAAG,EAAE;QACH;YACE,KAAK;YACL,SAAS,EAAE,OAAO,OAAO,EAAqB;YAC9C,IAAI;SACL;KACF;CACF,CAAC,CAAC;AAZU,QAAA,KAAK,SAYf;AAKI,MAAM,KAAK,GAAG,CACnB,KAAuB,EACvB,O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EAAE,SAAS,EAAE,QAAQ,CAAC;IAC3C,IAAA,aAAK,EAAI,YAAY,EAAE,SAAS,EAAE,QAAQ,CAAC;CAC5C,CAAC;AANW,QAAA,gBAAgB,oBAM3B"}
@@ -1 +1 @@
1
- {"version":3,"file":"iam-context.module.d.ts","sourceRoot":"","sources":["../src/iam-context.module.ts"],"names":[],"mappings":"AAIA,qBAQa,gBAAgB;CAAG"}
1
+ {"version":3,"file":"iam-context.module.d.ts","sourceRoot":"","sources":["../src/iam-context.module.ts"],"names":[],"mappings":"AAKA,qBASa,gBAAgB;CAAG"}
@@ -10,12 +10,14 @@ exports.IamContextModule = void 0;
10
10
  const common_1 = require("@nestjs/common");
11
11
  const iam_context_guard_1 = require("./guards/iam-context.guard");
12
12
  const core_1 = require("@nestjs/core");
13
+ const iam_entity_access_service_1 = require("./services/iam-entity-access.service");
13
14
  let IamContextModule = class IamContextModule {
14
15
  };
15
16
  exports.IamContextModule = IamContextModule;
16
17
  exports.IamContextModule = IamContextModule = __decorate([
17
18
  (0, common_1.Module)({
18
19
  providers: [
20
+ iam_entity_access_service_1.IamEntityAccessService,
19
21
  {
20
22
  provide: core_1.APP_GUARD,
21
23
  useClass: iam_context_guard_1.IamContextGuard,
@@ -1 +1 @@
1
- {"version":3,"file":"iam-context.module.js","sourceRoot":"","sources":["../src/iam-context.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAwC;AACxC,kEAA6D;AAC7D,uCAAyC;AAUlC,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;CAAG,CAAA;AAAnB,4CAAgB;2BAAhB,gBAAgB;IAR5B,IAAA,eAAM,EAAC;QACN,SAAS,EAAE;YACT;gBACE,OAAO,EAAE,gBAAS;gBAClB,QAAQ,EAAE,mCAAe;aAC1B;SACF;KACF,CAAC;GACW,gBAAgB,CAAG"}
1
+ {"version":3,"file":"iam-context.module.js","sourceRoot":"","sources":["../src/iam-context.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAwC;AACxC,kEAA6D;AAC7D,uCAAyC;AACzC,oFAA8E;AAWvE,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;CAAG,CAAA;AAAnB,4CAAgB;2BAAhB,gBAAgB;IAT5B,IAAA,eAAM,EAAC;QACN,SAAS,EAAE;YACT,kDAAsB;YACtB;gBACE,OAAO,EAAE,gBAAS;gBAClB,QAAQ,EAAE,mCAAe;aAC1B;SACF;KACF,CAAC;GACW,gBAAgB,CAAG"}
package/dist/index.d.ts CHANGED
@@ -1,9 +1,13 @@
1
- export * from './decorators/get-owner-id.decorator';
2
- export * from './decorators/iam-public.decorator';
3
- export * from './decorators/owns-entity.decorator';
4
- export * from './decorators/set-iam-route-user-type.decorator';
1
+ export * from './decorators/public-iam.decorators';
2
+ export * from './decorators/iam-entity-access.decorator';
3
+ export * from './decorators/allow-iam.decorator';
5
4
  export * from './enums/iam-user-type.enum';
6
5
  export * from './guards/iam-context.guard';
7
- export * from './pipes/iam-paginate-query.pipe';
6
+ export * from './helpers/user-owner.helper';
7
+ export * from './interfaces/iam-entity-access.interface';
8
+ export * from './types/iam-entity-access.type';
9
+ export * from './pipes/iam-paginate-query-scope.pipe';
10
+ export * from './pipes/iam-find-one-query-scope.pipe';
11
+ export * from './services/iam-entity-access.service';
8
12
  export * from './iam-context.module';
9
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qCAAqC,CAAC;AACpD,cAAc,mCAAmC,CAAC;AAClD,cAAc,oCAAoC,CAAC;AACnD,cAAc,gDAAgD,CAAC;AAC/D,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,iCAAiC,CAAC;AAChD,cAAc,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oCAAoC,CAAC;AACnD,cAAc,0CAA0C,CAAC;AACzD,cAAc,kCAAkC,CAAC;AACjD,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0CAA0C,CAAC;AACzD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,uCAAuC,CAAC;AACtD,cAAc,uCAAuC,CAAC;AACtD,cAAc,sCAAsC,CAAC;AACrD,cAAc,sBAAsB,CAAC"}
package/dist/index.js CHANGED
@@ -14,12 +14,16 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./decorators/get-owner-id.decorator"), exports);
18
- __exportStar(require("./decorators/iam-public.decorator"), exports);
19
- __exportStar(require("./decorators/owns-entity.decorator"), exports);
20
- __exportStar(require("./decorators/set-iam-route-user-type.decorator"), exports);
17
+ __exportStar(require("./decorators/public-iam.decorators"), exports);
18
+ __exportStar(require("./decorators/iam-entity-access.decorator"), exports);
19
+ __exportStar(require("./decorators/allow-iam.decorator"), exports);
21
20
  __exportStar(require("./enums/iam-user-type.enum"), exports);
22
21
  __exportStar(require("./guards/iam-context.guard"), exports);
23
- __exportStar(require("./pipes/iam-paginate-query.pipe"), exports);
22
+ __exportStar(require("./helpers/user-owner.helper"), exports);
23
+ __exportStar(require("./interfaces/iam-entity-access.interface"), exports);
24
+ __exportStar(require("./types/iam-entity-access.type"), exports);
25
+ __exportStar(require("./pipes/iam-paginate-query-scope.pipe"), exports);
26
+ __exportStar(require("./pipes/iam-find-one-query-scope.pipe"), exports);
27
+ __exportStar(require("./services/iam-entity-access.service"), exports);
24
28
  __exportStar(require("./iam-context.module"), exports);
25
29
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sEAAoD;AACpD,oEAAkD;AAClD,qEAAmD;AACnD,iFAA+D;AAC/D,6DAA2C;AAC3C,6DAA2C;AAC3C,kEAAgD;AAChD,uDAAqC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qEAAmD;AACnD,2EAAyD;AACzD,mEAAiD;AACjD,6DAA2C;AAC3C,6DAA2C;AAC3C,8DAA4C;AAC5C,2EAAyD;AACzD,iEAA+C;AAC/C,wEAAsD;AACtD,wEAAsD;AACtD,uEAAqD;AACrD,uDAAqC"}
@@ -0,0 +1,38 @@
1
+ import { IamUserTypeEnum } from '../enums/iam-user-type.enum';
2
+ import { AccessValueCast, AccessValueSource, EntityAccessBranch } from '../types/iam-entity-access.type';
3
+ import { EntityTarget } from 'typeorm';
4
+ export interface IamAccessValueContext {
5
+ req: any;
6
+ iamContext: any;
7
+ userType: IamUserTypeEnum;
8
+ }
9
+ export interface EntityFieldMatch<T = any> {
10
+ field: keyof T & string;
11
+ valueFrom: AccessValueSource;
12
+ cast?: AccessValueCast;
13
+ }
14
+ export interface RoleEntityAccess<T = any> {
15
+ anyOf: EntityAccessBranch<T>[];
16
+ }
17
+ export interface EntityAccessByRole<T = any> {
18
+ agent?: {
19
+ bypass?: boolean;
20
+ anyOf?: EntityAccessBranch<T>[];
21
+ };
22
+ user?: RoleEntityAccess<T>;
23
+ guest?: RoleEntityAccess<T>;
24
+ }
25
+ export interface RequireIamEntityAccessOptions<T = any> {
26
+ entity: EntityTarget<T>;
27
+ lookup: {
28
+ field: keyof T & string;
29
+ valueFrom: AccessValueSource;
30
+ };
31
+ access: EntityAccessByRole<T>;
32
+ resourceName?: string;
33
+ }
34
+ export interface ApplyIamEntityScopeOptions<T = any> {
35
+ access: EntityAccessByRole<T>;
36
+ withDeleted?: boolean;
37
+ }
38
+ //# sourceMappingURL=iam-entity-access.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iam-entity-access.interface.d.ts","sourceRoot":"","sources":["../../src/interfaces/iam-entity-access.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EACL,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEvC,MAAM,WAAW,qBAAqB;IACpC,GAAG,EAAE,GAAG,CAAC;IACT,UAAU,EAAE,GAAG,CAAC;IAChB,QAAQ,EAAE,eAAe,CAAC;CAC3B;AAED,MAAM,WAAW,gBAAgB,CAAC,CAAC,GAAG,GAAG;IACvC,KAAK,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC;IACxB,SAAS,EAAE,iBAAiB,CAAC;IAC7B,IAAI,CAAC,EAAE,eAAe,CAAC;CACxB;AAED,MAAM,WAAW,gBAAgB,CAAC,CAAC,GAAG,GAAG;IACvC,KAAK,EAAE,kBAAkB,CAAC,CAAC,CAAC,EAAE,CAAC;CAChC;AAED,MAAM,WAAW,kBAAkB,CAAC,CAAC,GAAG,GAAG;IACzC,KAAK,CAAC,EAAE;QAKN,MAAM,CAAC,EAAE,OAAO,CAAC;QAKjB,KAAK,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAAC,EAAE,CAAC;KACjC,CAAC;IAEF,IAAI,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC;IAE3B,KAAK,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC;CAC7B;AAQD,MAAM,WAAW,6BAA6B,CAAC,CAAC,GAAG,GAAG;IACpD,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;IAExB,MAAM,EAAE;QACN,KAAK,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC;QACxB,SAAS,EAAE,iBAAiB,CAAC;KAC9B,CAAC;IAEF,MAAM,EAAE,kBAAkB,CAAC,CAAC,CAAC,CAAC;IAE9B,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AASD,MAAM,WAAW,0BAA0B,CAAC,CAAC,GAAG,GAAG;IACjD,MAAM,EAAE,kBAAkB,CAAC,CAAC,CAAC,CAAC;IAK9B,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=iam-entity-access.interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iam-entity-access.interface.js","sourceRoot":"","sources":["../../src/interfaces/iam-entity-access.interface.ts"],"names":[],"mappings":""}
@@ -0,0 +1,10 @@
1
+ import { PipeTransform } from '@nestjs/common';
2
+ import { IamEntityAccessService } from '../services/iam-entity-access.service';
3
+ import { IamScopedFindOneQuery } from '../types/iam-entity-access.type';
4
+ export declare class IamFindOneQueryScopePipe<T = any> implements PipeTransform<Record<string, any>, IamScopedFindOneQuery<T>> {
5
+ private readonly request;
6
+ private readonly iamEntityAccessService;
7
+ constructor(request: any, iamEntityAccessService: IamEntityAccessService);
8
+ transform(value: Record<string, any>): IamScopedFindOneQuery<T>;
9
+ }
10
+ //# sourceMappingURL=iam-find-one-query-scope.pipe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iam-find-one-query-scope.pipe.d.ts","sourceRoot":"","sources":["../../src/pipes/iam-find-one-query-scope.pipe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,aAAa,EAAS,MAAM,gBAAgB,CAAC;AAI1E,OAAO,EAAE,sBAAsB,EAAE,MAAM,uCAAuC,CAAC;AAE/E,OAAO,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAC;AAQxE,qBACa,wBAAwB,CAAC,CAAC,GAAG,GAAG,CAAE,YAAW,aAAa,CACrE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACnB,qBAAqB,CAAC,CAAC,CAAC,CACzB;IAGG,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,sBAAsB;gBADtB,OAAO,EAAE,GAAG,EACZ,sBAAsB,EAAE,sBAAsB;IAGjE,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,qBAAqB,CAAC,CAAC,CAAC;CA4BhE"}
@@ -12,36 +12,34 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
12
  return function (target, key) { decorator(target, key, paramIndex); }
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.IamPaginateQueryPipe = void 0;
15
+ exports.IamFindOneQueryScopePipe = void 0;
16
16
  const common_1 = require("@nestjs/common");
17
17
  const core_1 = require("@nestjs/core");
18
18
  const nestjs_common_module_core_1 = require("@mondart/nestjs-common-module-core");
19
- let IamPaginateQueryPipe = class IamPaginateQueryPipe {
20
- constructor(request) {
19
+ const iam_entity_access_service_1 = require("../services/iam-entity-access.service");
20
+ const iam_decorator_key_const_1 = require("../constants/iam-decorator-key.const");
21
+ let IamFindOneQueryScopePipe = class IamFindOneQueryScopePipe {
22
+ constructor(request, iamEntityAccessService) {
21
23
  this.request = request;
24
+ this.iamEntityAccessService = iamEntityAccessService;
22
25
  }
23
- transform(value, metadata) {
24
- const iamContext = (0, nestjs_common_module_core_1.getIamContext)(this.request);
25
- if (!iamContext?.isAgent && iamContext?.user) {
26
- const { storeId, contactId } = iamContext.user;
27
- value.filter = {
28
- contactId: `$eq:${contactId}`,
29
- storeId: `$eq:${storeId}`,
30
- };
31
- }
32
- if (!iamContext?.isAgent && !iamContext?.user) {
33
- value.filter = {
34
- contactId: `$null`,
35
- };
26
+ transform(value) {
27
+ const query = (value ?? {});
28
+ const scopeOptions = this.request[iam_decorator_key_const_1.IAM_REQUEST_ENTITY_SCOPE_OPTIONS_KEY];
29
+ if (!scopeOptions) {
30
+ return query;
36
31
  }
37
- value.withDeleted = true;
38
- return value;
32
+ const iamContext = (0, nestjs_common_module_core_1.getIamContext)(this.request);
33
+ const currentUserType = this.request[iam_decorator_key_const_1.IAM_REQUEST_USER_TYPE_KEY];
34
+ const scopeWhere = this.iamEntityAccessService.buildScopeWhere(scopeOptions, this.request, iamContext, currentUserType);
35
+ query[iam_decorator_key_const_1.IAM_QUERY_SCOPE_WHERE_KEY] = scopeWhere;
36
+ return query;
39
37
  }
40
38
  };
41
- exports.IamPaginateQueryPipe = IamPaginateQueryPipe;
42
- exports.IamPaginateQueryPipe = IamPaginateQueryPipe = __decorate([
43
- (0, common_1.Injectable)(),
39
+ exports.IamFindOneQueryScopePipe = IamFindOneQueryScopePipe;
40
+ exports.IamFindOneQueryScopePipe = IamFindOneQueryScopePipe = __decorate([
41
+ (0, common_1.Injectable)({ scope: common_1.Scope.REQUEST }),
44
42
  __param(0, (0, common_1.Inject)(core_1.REQUEST)),
45
- __metadata("design:paramtypes", [Request])
46
- ], IamPaginateQueryPipe);
47
- //# sourceMappingURL=iam-paginate-query.pipe.js.map
43
+ __metadata("design:paramtypes", [Object, iam_entity_access_service_1.IamEntityAccessService])
44
+ ], IamFindOneQueryScopePipe);
45
+ //# sourceMappingURL=iam-find-one-query-scope.pipe.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iam-find-one-query-scope.pipe.js","sourceRoot":"","sources":["../../src/pipes/iam-find-one-query-scope.pipe.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA0E;AAC1E,uCAAuC;AACvC,kFAAmE;AAEnE,qFAA+E;AAG/E,kFAI8C;AAIvC,IAAM,wBAAwB,GAA9B,MAAM,wBAAwB;IAInC,YAEmB,OAAY,EACZ,sBAA8C;QAD9C,YAAO,GAAP,OAAO,CAAK;QACZ,2BAAsB,GAAtB,sBAAsB,CAAwB;IAC9D,CAAC;IAEJ,SAAS,CAAC,KAA0B;QAClC,MAAM,KAAK,GAAG,CAAC,KAAK,IAAI,EAAE,CAA6B,CAAC;QAExD,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,8DAAoC,CAEzD,CAAC;QAEd,IAAI,CAAC,YAAY,EAAE,CAAC;YAClB,OAAO,KAAK,CAAC;QACf,CAAC;QAED,MAAM,UAAU,GAAG,IAAA,yCAAa,EAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAE/C,MAAM,eAAe,GAAG,IAAI,CAAC,OAAO,CAClC,mDAAyB,CACP,CAAC;QAErB,MAAM,UAAU,GAAG,IAAI,CAAC,sBAAsB,CAAC,eAAe,CAC5D,YAAY,EACZ,IAAI,CAAC,OAAO,EACZ,UAAU,EACV,eAAe,CAChB,CAAC;QAEF,KAAK,CAAC,mDAAyB,CAAC,GAAG,UAAU,CAAC;QAE9C,OAAO,KAAK,CAAC;IACf,CAAC;CACF,CAAA;AAtCY,4DAAwB;mCAAxB,wBAAwB;IADpC,IAAA,mBAAU,EAAC,EAAE,KAAK,EAAE,cAAK,CAAC,OAAO,EAAE,CAAC;IAMhC,WAAA,IAAA,eAAM,EAAC,cAAO,CAAC,CAAA;6CAEyB,kDAAsB;GAPtD,wBAAwB,CAsCpC"}
@@ -0,0 +1,11 @@
1
+ import { PipeTransform } from '@nestjs/common';
2
+ import { PaginateQuery } from 'nestjs-paginate';
3
+ import { IamEntityAccessService } from '../services/iam-entity-access.service';
4
+ import { IamScopedPaginateQuery } from '../types/iam-entity-access.type';
5
+ export declare class IamPaginateQueryScopePipe<T = any> implements PipeTransform<PaginateQuery, IamScopedPaginateQuery<T>> {
6
+ private readonly request;
7
+ private readonly iamEntityAccessService;
8
+ constructor(request: any, iamEntityAccessService: IamEntityAccessService);
9
+ transform(value: PaginateQuery): IamScopedPaginateQuery<T>;
10
+ }
11
+ //# sourceMappingURL=iam-paginate-query-scope.pipe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iam-paginate-query-scope.pipe.d.ts","sourceRoot":"","sources":["../../src/pipes/iam-paginate-query-scope.pipe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,aAAa,EAAS,MAAM,gBAAgB,CAAC;AAE1E,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGhD,OAAO,EAAE,sBAAsB,EAAE,MAAM,uCAAuC,CAAC;AAE/E,OAAO,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AAQzE,qBACa,yBAAyB,CAAC,CAAC,GAAG,GAAG,CAAE,YAAW,aAAa,CACtE,aAAa,EACb,sBAAsB,CAAC,CAAC,CAAC,CAC1B;IAGG,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,sBAAsB;gBADtB,OAAO,EAAE,GAAG,EACZ,sBAAsB,EAAE,sBAAsB;IAGjE,SAAS,CAAC,KAAK,EAAE,aAAa,GAAG,sBAAsB,CAAC,CAAC,CAAC;CA+B3D"}