|
1
|
|
|
import { DynamicModule, Global, Module } from '@nestjs/common'; |
|
2
|
|
|
import { RbacService } from './services/rbac.service'; |
|
3
|
|
|
import { ModuleRef, Reflector } from '@nestjs/core'; |
|
4
|
|
|
import { StorageRbacService } from './services/storage.rbac.service'; |
|
5
|
|
|
import { IStorageRbac } from './interfaces/storage.rbac.interface'; |
|
6
|
|
|
import { IDynamicStorageRbac } from './interfaces/dynamic.storage.rbac.interface'; |
|
7
|
|
|
import { ICacheRBAC } from './interfaces/cache.rbac.interface'; |
|
8
|
|
|
|
|
9
|
|
|
@Global() |
|
10
|
|
|
@Module({ |
|
11
|
|
|
providers: [ |
|
12
|
|
|
RbacService, |
|
13
|
|
|
StorageRbacService, |
|
14
|
|
|
Reflector, |
|
15
|
|
|
], |
|
16
|
|
|
imports: [], |
|
17
|
|
|
exports: [ |
|
18
|
|
|
RbacService, |
|
19
|
|
|
], |
|
20
|
|
|
}) |
|
21
|
|
|
export class RBAcModule { |
|
22
|
|
|
private static cache?: any | ICacheRBAC; |
|
23
|
|
|
private static cacheOptions?: { KEY?: string, TTL?: number }; |
|
24
|
|
|
|
|
25
|
|
|
static useCache( |
|
26
|
|
|
cache: any | ICacheRBAC, |
|
27
|
|
|
options?: { |
|
28
|
|
|
KEY?: string, |
|
29
|
|
|
TTL?: number |
|
30
|
|
|
}, |
|
31
|
|
|
) { |
|
32
|
|
|
RBAcModule.cache = cache; |
|
33
|
|
|
RBAcModule.cacheOptions = options; |
|
34
|
|
|
return RBAcModule; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
static forRoot( |
|
38
|
|
|
rbac: IStorageRbac, |
|
39
|
|
|
providers?: any[], |
|
40
|
|
|
imports?: any[], |
|
41
|
|
|
): DynamicModule { |
|
42
|
|
|
|
|
43
|
|
|
return RBAcModule.forDynamic( |
|
44
|
|
|
/* tslint:disable */ |
|
45
|
|
|
class { |
|
46
|
|
|
async getRbac(): Promise<IStorageRbac> { |
|
47
|
|
|
return rbac; |
|
48
|
|
|
}; |
|
49
|
|
|
}, |
|
50
|
|
|
providers, |
|
51
|
|
|
imports, |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
static forDynamic( |
|
56
|
|
|
rbac: new () => IDynamicStorageRbac, |
|
57
|
|
|
providers?: any[], |
|
58
|
|
|
imports?: any[], |
|
59
|
|
|
): DynamicModule { |
|
60
|
|
|
const inject = [ModuleRef, rbac]; |
|
61
|
|
|
|
|
62
|
|
|
if (RBAcModule.cache) { |
|
63
|
|
|
inject.push(RBAcModule.cache); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
const commonProviders = [ |
|
67
|
|
|
...(providers || []), |
|
68
|
|
|
rbac, |
|
69
|
|
|
{ |
|
70
|
|
|
provide: StorageRbacService, |
|
71
|
|
|
useFactory: async (moduleRef: ModuleRef, rbacService: IDynamicStorageRbac, cache?: ICacheRBAC) => { |
|
72
|
|
|
if (cache && !RBAcModule.cacheOptions) { |
|
73
|
|
|
return cache; |
|
74
|
|
|
} |
|
75
|
|
|
if (cache && RBAcModule.cacheOptions.KEY) { |
|
76
|
|
|
cache.KEY = RBAcModule.cacheOptions.KEY; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (cache && RBAcModule.cacheOptions.TTL) { |
|
80
|
|
|
cache.TTL = RBAcModule.cacheOptions.TTL; |
|
81
|
|
|
} |
|
82
|
|
|
return new StorageRbacService(moduleRef, rbacService, cache); |
|
83
|
|
|
}, |
|
84
|
|
|
inject, |
|
85
|
|
|
}, |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
if (RBAcModule.cache) { |
|
89
|
|
|
commonProviders.push(RBAcModule.cache, { |
|
90
|
|
|
provide: 'ICacheRBAC', |
|
91
|
|
|
useFactory: (cache: ICacheRBAC): ICacheRBAC => { |
|
92
|
|
|
if (!RBAcModule.cacheOptions) { |
|
93
|
|
|
return cache; |
|
94
|
|
|
} |
|
95
|
|
|
if (RBAcModule.cacheOptions.KEY) { |
|
96
|
|
|
cache.KEY = RBAcModule.cacheOptions.KEY; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (RBAcModule.cacheOptions.TTL) { |
|
100
|
|
|
cache.TTL = RBAcModule.cacheOptions.TTL; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return cache; |
|
104
|
|
|
}, |
|
105
|
|
|
inject: [RBAcModule.cache], |
|
106
|
|
|
}); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return { |
|
110
|
|
|
module: RBAcModule, |
|
111
|
|
|
providers: commonProviders, |
|
112
|
|
|
imports: [ |
|
113
|
|
|
...(imports || []), |
|
114
|
|
|
], |
|
115
|
|
|
}; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|