@punks/backend-entity-manager 0.0.27 → 0.0.28

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 @@ export interface IAuthorizationResult {
3
3
  isAuthorized: boolean;
4
4
  }
5
5
  export interface IEntityAuthorizationMiddleware<TEntity, TAuthenticationContext extends IAuthenticationContext<TUserContext>, TUserContext> {
6
+ canSearch(context: TAuthenticationContext): Promise<IAuthorizationResult>;
6
7
  canRead(entity: Partial<TEntity>, context: TAuthenticationContext): Promise<IAuthorizationResult>;
7
8
  canCreate(entity: Partial<TEntity>, context: TAuthenticationContext): Promise<IAuthorizationResult>;
8
9
  canUpdate(entity: Partial<TEntity>, context: TAuthenticationContext): Promise<IAuthorizationResult>;
@@ -5,13 +5,17 @@ export declare enum EntityOperationType {
5
5
  Create = 0,
6
6
  Update = 1,
7
7
  Delete = 2,
8
- Read = 3
8
+ Read = 3,
9
+ Search = 4
9
10
  }
10
- export declare class EntityOperationUnauthorizedException<TEntity> extends EntityManagerException {
11
- private readonly entity;
11
+ export declare abstract class EntityManagerUnauthorizedException extends EntityManagerException {
12
+ constructor(message: string);
13
+ }
14
+ export declare class EntityOperationUnauthorizedException<TEntity> extends EntityManagerUnauthorizedException {
12
15
  private readonly operation;
13
- constructor(operationType: EntityOperationType, entity: Partial<TEntity>, entityName: string);
14
- get getEntity(): Partial<TEntity>;
16
+ private readonly entity?;
17
+ constructor(operationType: EntityOperationType, entityName: string, entity?: Partial<TEntity>);
18
+ get getEntity(): Partial<TEntity> | undefined;
15
19
  get getOperation(): EntityOperationType;
16
20
  }
17
21
  export declare class EntityNotFoundException<TEntityId> extends EntityManagerException {
@@ -3,6 +3,7 @@ import { NestEntityAuthorizationMiddleware } from "../../../../authentication";
3
3
  import { AppAuthContext, AppUserContext } from "../../authentication";
4
4
  import { FooEntity } from "./entity";
5
5
  export declare class FooAuthMiddleware extends NestEntityAuthorizationMiddleware<FooEntity, AppAuthContext, AppUserContext> {
6
+ canSearch(context: AppAuthContext): Promise<IAuthorizationResult>;
6
7
  canRead(entity: Partial<FooEntity>, context: AppAuthContext): Promise<IAuthorizationResult>;
7
8
  canCreate(entity: Partial<FooEntity>, context: AppAuthContext): Promise<IAuthorizationResult>;
8
9
  canUpdate(entity: Partial<FooEntity>, context: AppAuthContext): Promise<IAuthorizationResult>;
@@ -1,5 +1,6 @@
1
1
  import { IAuthenticationContext, IAuthorizationResult, IEntityAuthorizationMiddleware } from "../../../abstractions";
2
2
  export declare abstract class NestEntityAuthorizationMiddleware<TEntity, TAuthenticationContext extends IAuthenticationContext<TUserContext>, TUserContext> implements IEntityAuthorizationMiddleware<TEntity, TAuthenticationContext, TUserContext> {
3
+ abstract canSearch(context: TAuthenticationContext): Promise<IAuthorizationResult>;
3
4
  abstract canRead(entity: Partial<TEntity>, context: TAuthenticationContext): Promise<IAuthorizationResult>;
4
5
  abstract canCreate(entity: Partial<TEntity>, context: TAuthenticationContext): Promise<IAuthorizationResult>;
5
6
  abstract canUpdate(entity: Partial<TEntity>, context: TAuthenticationContext): Promise<IAuthorizationResult>;
@@ -9,4 +9,5 @@ export declare class EntitiesSearchQuery<TEntity, TEntityId, TEntitySearchParame
9
9
  execute(request: TEntitySearchParameters): Promise<IEntitiesSearchResults<TEntitySearchParameters, TEntity, TSorting, TCursor, TFacets>>;
10
10
  private getContext;
11
11
  private filterAllowedEntities;
12
+ private authorizeSearch;
12
13
  }
package/dist/esm/index.js CHANGED
@@ -19,9 +19,16 @@ var EntityOperationType;
19
19
  EntityOperationType[EntityOperationType["Update"] = 1] = "Update";
20
20
  EntityOperationType[EntityOperationType["Delete"] = 2] = "Delete";
21
21
  EntityOperationType[EntityOperationType["Read"] = 3] = "Read";
22
+ EntityOperationType[EntityOperationType["Search"] = 4] = "Search";
22
23
  })(EntityOperationType || (EntityOperationType = {}));
23
- class EntityOperationUnauthorizedException extends EntityManagerException {
24
- constructor(operationType, entity, entityName) {
24
+ class EntityManagerUnauthorizedException extends EntityManagerException {
25
+ constructor(message) {
26
+ super(message);
27
+ this.name = "EntityManagerUnauthorizedException";
28
+ }
29
+ }
30
+ class EntityOperationUnauthorizedException extends EntityManagerUnauthorizedException {
31
+ constructor(operationType, entityName, entity) {
25
32
  super(`The current user is not authorized to ${operationType} the entity of type ${entityName}.`);
26
33
  this.entity = entity;
27
34
  this.operation = operationType;
@@ -493,7 +500,7 @@ class EntityCreateCommand {
493
500
  const context = await contextService.getContext();
494
501
  const authorizationResult = await authorization.canCreate(entity, context);
495
502
  if (!authorizationResult.isAuthorized)
496
- throw new EntityOperationUnauthorizedException(EntityOperationType.Create, entity, this.services.getEntityName());
503
+ throw new EntityOperationUnauthorizedException(EntityOperationType.Create, this.services.getEntityName(), entity);
497
504
  }
498
505
  }
499
506
 
@@ -519,7 +526,7 @@ class EntityDeleteCommand {
519
526
  const context = await contextService.getContext();
520
527
  const authorizationResult = await authorization.canDelete(entity, context);
521
528
  if (!authorizationResult.isAuthorized)
522
- throw new EntityOperationUnauthorizedException(EntityOperationType.Delete, entity, this.services.getEntityName());
529
+ throw new EntityOperationUnauthorizedException(EntityOperationType.Delete, this.services.getEntityName(), entity);
523
530
  }
524
531
  }
525
532
 
@@ -557,7 +564,7 @@ class EntityUpdateCommand {
557
564
  const context = await contextService.getContext();
558
565
  const authorizationResult = await authorization.canUpdate(currentEntity, context);
559
566
  if (!authorizationResult.isAuthorized)
560
- throw new EntityOperationUnauthorizedException(EntityOperationType.Create, currentEntity, this.services.getEntityName());
567
+ throw new EntityOperationUnauthorizedException(EntityOperationType.Create, this.services.getEntityName(), currentEntity);
561
568
  }
562
569
  }
563
570
 
@@ -594,12 +601,12 @@ class EntityUpsertCommand {
594
601
  if (currentEntity) {
595
602
  const updateResult = await authorization.canUpdate(currentEntity, context);
596
603
  if (!updateResult.isAuthorized)
597
- throw new EntityOperationUnauthorizedException(EntityOperationType.Update, currentEntity, this.services.getEntityName());
604
+ throw new EntityOperationUnauthorizedException(EntityOperationType.Update, this.services.getEntityName(), currentEntity);
598
605
  return;
599
606
  }
600
607
  const authorizationResult = await authorization.canCreate(entity, context);
601
608
  if (!authorizationResult.isAuthorized)
602
- throw new EntityOperationUnauthorizedException(EntityOperationType.Create, entity, this.services.getEntityName());
609
+ throw new EntityOperationUnauthorizedException(EntityOperationType.Create, this.services.getEntityName(), entity);
603
610
  }
604
611
  }
605
612
 
@@ -1077,7 +1084,7 @@ class EntityGetQuery {
1077
1084
  const context = await contextService.getContext();
1078
1085
  const authorizationResult = await authorization.canRead(entity, context);
1079
1086
  if (!authorizationResult.isAuthorized)
1080
- throw new EntityOperationUnauthorizedException(EntityOperationType.Read, entity, this.services.getEntityName());
1087
+ throw new EntityOperationUnauthorizedException(EntityOperationType.Read, this.services.getEntityName(), entity);
1081
1088
  }
1082
1089
  }
1083
1090
 
@@ -1088,12 +1095,20 @@ class EntitiesSearchQuery {
1088
1095
  // @ts-ignore
1089
1096
  async execute(request) {
1090
1097
  const context = await this.getContext();
1098
+ await this.authorizeSearch(context);
1091
1099
  const result = await this.services
1092
1100
  .resolveQueryBuilder()
1093
1101
  .search(request, context);
1102
+ const filteredEntities = await this.filterAllowedEntities(result.items, context);
1094
1103
  return {
1095
1104
  ...result,
1096
- items: await this.filterAllowedEntities(result.items),
1105
+ items: filteredEntities,
1106
+ paging: result.paging
1107
+ ? {
1108
+ ...result.paging,
1109
+ totPageItems: filteredEntities.length,
1110
+ }
1111
+ : undefined,
1097
1112
  };
1098
1113
  }
1099
1114
  async getContext() {
@@ -1104,13 +1119,11 @@ class EntitiesSearchQuery {
1104
1119
  const contextService = this.services.resolveAuthenticationContextProvider();
1105
1120
  return await contextService.getContext();
1106
1121
  }
1107
- async filterAllowedEntities(entities) {
1122
+ async filterAllowedEntities(entities, context) {
1108
1123
  const authorization = this.services.resolveAuthorizationMiddleware();
1109
1124
  if (!authorization) {
1110
1125
  return entities;
1111
1126
  }
1112
- const contextService = this.services.resolveAuthenticationContextProvider();
1113
- const context = await contextService.getContext();
1114
1127
  const filteredEntities = await Promise.all(entities.map(async (entity) => {
1115
1128
  const authorizationResult = await authorization.canRead(entity, context);
1116
1129
  if (!authorizationResult.isAuthorized) {
@@ -1120,6 +1133,16 @@ class EntitiesSearchQuery {
1120
1133
  }));
1121
1134
  return filteredEntities.filter((entity) => entity !== null);
1122
1135
  }
1136
+ async authorizeSearch(context) {
1137
+ const authorization = this.services.resolveAuthorizationMiddleware();
1138
+ if (!authorization) {
1139
+ return;
1140
+ }
1141
+ const { isAuthorized } = await authorization.canSearch(context);
1142
+ if (!isAuthorized) {
1143
+ throw new EntityOperationUnauthorizedException(EntityOperationType.Search, this.services.getEntityName());
1144
+ }
1145
+ }
1123
1146
  }
1124
1147
 
1125
1148
  var ConnectorMode;
@@ -18924,6 +18947,9 @@ class AppExceptionsFilterBase {
18924
18947
  if (exception instanceof EntityOperationUnauthorizedException) {
18925
18948
  return HttpStatus.UNAUTHORIZED;
18926
18949
  }
18950
+ if (exception instanceof EntityNotFoundException) {
18951
+ return HttpStatus.NOT_FOUND;
18952
+ }
18927
18953
  if (exception instanceof HttpException) {
18928
18954
  return exception.getStatus();
18929
18955
  }
@@ -19121,5 +19147,5 @@ EntityManagerModule = __decorate([
19121
19147
 
19122
19148
  const newUuid = newUuid$1;
19123
19149
 
19124
- export { AppExceptionsFilterBase, AppSessionMiddleware, AppSessionService, AuthenticationModule, EntityManagerException, EntityManagerInitializer, EntityManagerModule, EntityManagerRegistry, EntityManagerSymbols, EntityNotFoundException, EntityOperationType, EntityOperationUnauthorizedException, ModulesContainerProvider, MultiTenancyModule, MultipleEntitiesFoundException, NestEntityActions, NestEntityAuthorizationMiddleware, NestEntityManager, NestTypeOrmQueryBuilder, NestTypeOrmRepository, QueryBuilderBase, ReplicationMode, SortDirection, WpEntity, WpEntityActions, WpEntityAdapter, WpEntityAuthMiddleware, WpEntityConnector, WpEntityConverter, WpEntityManager, WpEntityQueryBuilder, WpEntityRepository, newUuid };
19150
+ export { AppExceptionsFilterBase, AppSessionMiddleware, AppSessionService, AuthenticationModule, EntityManagerException, EntityManagerInitializer, EntityManagerModule, EntityManagerRegistry, EntityManagerSymbols, EntityManagerUnauthorizedException, EntityNotFoundException, EntityOperationType, EntityOperationUnauthorizedException, ModulesContainerProvider, MultiTenancyModule, MultipleEntitiesFoundException, NestEntityActions, NestEntityAuthorizationMiddleware, NestEntityManager, NestTypeOrmQueryBuilder, NestTypeOrmRepository, QueryBuilderBase, ReplicationMode, SortDirection, WpEntity, WpEntityActions, WpEntityAdapter, WpEntityAuthMiddleware, WpEntityConnector, WpEntityConverter, WpEntityManager, WpEntityQueryBuilder, WpEntityRepository, newUuid };
19125
19151
  //# sourceMappingURL=index.js.map