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