dl-commons 0.0.2-testing.22 → 0.0.2-testing.24

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,7 +1,7 @@
1
1
  {
2
2
  "name": "dl-commons",
3
3
  "type": "commonjs",
4
- "version": "0.0.2-testing.22",
4
+ "version": "0.0.2-testing.24",
5
5
  "description": "distance learning commons",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -2,12 +2,14 @@ import { Injectable, CanActivate, ExecutionContext, UnauthorizedException, Forbi
2
2
  import { Reflector } from '@nestjs/core';
3
3
  import { SessionsService } from '../services/sessions.service';
4
4
 
5
+ export const ALLOW_PERMISSIONS_KEY = 'AllowPermissions';
6
+
5
7
  export class PermissionGuardArgs {
6
8
  keywords?: string[] | null | undefined;
7
9
  verificationLevels?: string[] | null | undefined;
8
10
  }
9
11
 
10
- export const AllowPermissions = Reflector.createDecorator<PermissionGuardArgs>();
12
+ export const AllowPermissions = Reflector.createDecorator<PermissionGuardArgs>({ key: 'AllowPermissions' });
11
13
 
12
14
  @Injectable()
13
15
  export class PermissionGuard implements CanActivate {
package/src/index.ts CHANGED
@@ -115,11 +115,13 @@ import { StorageService } from './services/storage.service';
115
115
  import { IDParams } from './models/dtos/commons/id.param';
116
116
  import { UserType } from './models/enums/user-type.enum';
117
117
  import { IAMService } from './services/iam.service';
118
+ import { PermissionsScannerService } from './services/permissions-scanner.service';
118
119
 
119
120
  export {
120
121
  /**
121
122
  * Services
122
123
  */
124
+ PermissionsScannerService,
123
125
  SessionsService,
124
126
  DictionariesService,
125
127
  SMTPService,
@@ -0,0 +1,46 @@
1
+ import { Injectable, OnModuleInit } from '@nestjs/common';
2
+ import { DiscoveryService, Reflector as CoreReflector } from '@nestjs/core';
3
+ import { ALLOW_PERMISSIONS_KEY } from '../guards/permission.guard';
4
+
5
+ @Injectable()
6
+ export class PermissionsScannerService implements OnModuleInit {
7
+ private readonly _permissions: Set<string> = new Set<string>();
8
+
9
+ //#region Constructor
10
+ constructor(private readonly _discoveryService: DiscoveryService, private readonly _reflector: CoreReflector) {}
11
+ //#endregion
12
+
13
+ //#region Getters
14
+ public get permissions(): string[] {
15
+ return Array.from(this._permissions);
16
+ }
17
+ //#endregion
18
+
19
+ //#region Public APIs
20
+ async onModuleInit() {
21
+ const controllers = this._discoveryService.getControllers();
22
+
23
+ // Scan controller methods
24
+ for (const wrapper of controllers) {
25
+ const { instance } = wrapper;
26
+ if (!instance) continue;
27
+
28
+ const prototype = Object.getPrototypeOf(instance);
29
+ const methodNames = Object.getOwnPropertyNames(prototype);
30
+
31
+ for (const methodName of methodNames) {
32
+ const method = prototype[methodName];
33
+
34
+ if (typeof method !== 'function') continue;
35
+ const args: { keywords: string[] } = this._reflector.get(ALLOW_PERMISSIONS_KEY, method);
36
+
37
+ if (args && args.keywords) {
38
+ args.keywords.forEach((k) => this._permissions.add(k));
39
+ }
40
+ }
41
+ }
42
+
43
+ console.log(this._permissions);
44
+ }
45
+ //#endregion
46
+ }