@boarteam/boar-pack-users-backend 6.6.0 → 6.7.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@boarteam/boar-pack-users-backend",
3
- "version": "6.6.0",
3
+ "version": "6.7.0",
4
4
  "description": "NestJS Users module including permissions system, authentication strategies etc",
5
5
  "main": "src/index",
6
6
  "files": [
@@ -64,5 +64,5 @@
64
64
  "yalc:push": "yalc push",
65
65
  "gen-types": "SWAGGER=true JWT_SECRET=swagger nest start"
66
66
  },
67
- "gitHead": "0c74a3f6b2b7e986124cac71a55467b9b8c25b63"
67
+ "gitHead": "a701f41e1d4c51394193a5bb7877e037cb8fab89"
68
68
  }
@@ -1,10 +1,13 @@
1
- import { Controller } from '@nestjs/common';
1
+ import { Controller, Req, UnauthorizedException } from '@nestjs/common';
2
2
  import { ApiTags } from '@nestjs/swagger';
3
- import { Crud, CrudController } from '@dataui/crud';
3
+ import { Crud, CrudController, CrudRequest, Override, ParsedRequest } from '@dataui/crud';
4
4
  import { CheckPolicies } from "@boarteam/boar-pack-users-backend";
5
5
  import { AuditLogsService } from './audit-logs.service';
6
6
  import { AuditLog } from './entities/audit-log.entity';
7
7
  import { ViewAuditLogsPolicy } from "./policies/view-audit-logs.policy";
8
+ import { Action, CaslAbilityFactory, Subjects } from "../casl";
9
+ import { Request } from "express";
10
+ import { TUser } from "../users";
8
11
 
9
12
  @Crud({
10
13
  model: {
@@ -35,5 +38,37 @@ import { ViewAuditLogsPolicy } from "./policies/view-audit-logs.policy";
35
38
  export class AuditLogsController implements CrudController<AuditLog>{
36
39
  constructor(
37
40
  readonly service: AuditLogsService,
41
+ private readonly caslAbilityFactory: CaslAbilityFactory,
38
42
  ) {}
43
+
44
+ get base(): CrudController<AuditLog> {
45
+ return this;
46
+ }
47
+
48
+ @Override('getManyBase')
49
+ async getMany(
50
+ @Req() request: Request,
51
+ @ParsedRequest() req: CrudRequest<TUser>,
52
+ ) {
53
+ const user = request.user;
54
+ if (!user) {
55
+ throw new UnauthorizedException();
56
+ }
57
+
58
+ const ability = await this.caslAbilityFactory.createForUser(user);
59
+ if (ability.cannot(Action.Manage, 'all')) {
60
+ const tablesNames = this.service.repo.manager.connection.entityMetadatas
61
+ .filter(meta => meta.target instanceof Function && ability.can(Action.Manage, meta.target as Subjects))
62
+ .map(meta => meta.tableName);
63
+
64
+ req.parsed.search = {
65
+ $and: [
66
+ req.parsed.search,
67
+ { tableName: { $in: tablesNames } },
68
+ ],
69
+ }
70
+ }
71
+
72
+ return this.base.getManyBase!(req);
73
+ }
39
74
  }
@@ -31,7 +31,7 @@ export type TTextSubjects = 'all';
31
31
  export type TSubjectsNames =
32
32
  | keyof TSubjects
33
33
  | TTextSubjects;
34
- type Subjects =
34
+ export type Subjects =
35
35
  | InferSubjects<TSubjects[keyof TSubjects]>
36
36
  | TTextSubjects;
37
37