Passed
Pull Request — master (#65)
by
unknown
02:01
created

src/role/role.rbac.ts   A

Complexity

Total Complexity 14
Complexity/F 14

Size

Lines of Code 72
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 52
mnd 13
bc 13
fnc 1
dl 0
loc 72
rs 10
bpm 13
cpm 14
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
F RoleRbac.can 0 54 14
1
import { Injectable } from '@nestjs/common';
2
import { IRoleRbac } from './interfaces/role.rbac.interface';
3
import { IFilterPermission } from '../permissions/interfaces/filter.permission.interface';
4
import { IParamsFilter } from '../params-filter/interfaces/params.filter.interface';
5
6
@Injectable()
7
export class RoleRbac implements IRoleRbac {
8
9
  constructor(
10
    private readonly role: string,
11
    private readonly grant: string[],
12
    private readonly filters: object,
13
    private readonly paramsFilter?: IParamsFilter,
14
  ) {
15
  }
16
17
  async can(...permissions: string[]): Promise<boolean> {
18
    if (!permissions.length) {
19
      return false;
20
    }
21
    // check grant
22
    for (const permission of permissions) {
23
      if (!this.grant.includes(permission)) {
24
        return false;
25
      }
26
    }
27
28
    // check custom filter
29
    for (const permission of permissions) {
30
      // check particular permission [permission@action]
31
      if (
32
        this.grant.includes(permission)
33
        && permission.includes('@')
34
      ) {
35
        const filter: string = permission.split('@')[1];
36
        const filterService: IFilterPermission = this.filters[filter];
37
        const isGranted = filterService ? await Promise.resolve(
38
          filterService.can(
39
            this.paramsFilter ? this.paramsFilter.getParam(filter) : null,
40
          )
41
        ) : false;
42
43
        if (filterService && !isGranted) {
44
          return false;
45
        }
46
      }
47
      // check particular permission [permission]
48
      if (this.grant.includes(permission)
49
        && !permission.includes('@')) {
50
51
        for (const filter in this.filters) {
52
          if (
53
            this.filters.hasOwnProperty(filter) &&
54
            this.grant.includes(`${permission}@${filter}`)
55
          ) {
56
            const isGranted = await Promise.resolve(
57
              this.filters[filter].can(
58
                this.paramsFilter ? this.paramsFilter.getParam(filter) : null,
59
              )
60
            );
61
            if (!isGranted) {
62
              return false;
63
            }
64
          }
65
        }
66
      }
67
    }
68
69
    return true;
70
  }
71
}
72