-
Notifications
You must be signed in to change notification settings - Fork 568
/
enforcer.service.ts
59 lines (49 loc) 路 1.4 KB
/
enforcer.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { newEnforcer, Enforcer } from 'casbin';
import {
ApplicationContext,
Config,
delegateTargetAllPrototypeMethod,
IMidwayContainer,
Init,
Provide,
Scope,
ScopeEnum,
} from '@midwayjs/core';
import { CasbinConfigOptions } from './interface';
@Provide()
@Scope(ScopeEnum.Singleton)
export class CasbinEnforcerService {
private instance: Enforcer;
@Config('casbin')
protected casbinConfig: CasbinConfigOptions;
@ApplicationContext()
applicationContext: IMidwayContainer;
@Init()
protected async init() {
if (typeof this.casbinConfig.policyAdapter === 'function') {
this.casbinConfig.policyAdapter = await this.casbinConfig.policyAdapter(
this.applicationContext
);
}
if (typeof this.casbinConfig.policyWatcher === 'function') {
this.casbinConfig.policyWatcher = await this.casbinConfig.policyWatcher(
this.applicationContext
);
}
this.instance = await newEnforcer(
this.casbinConfig.modelPath,
this.casbinConfig.policyAdapter
);
if (this.casbinConfig.policyWatcher) {
this.instance.setWatcher(this.casbinConfig.policyWatcher);
}
}
public getEnforcer(): Enforcer {
return this.instance;
}
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface CasbinEnforcerService extends Enforcer {
// empty
}
delegateTargetAllPrototypeMethod(CasbinEnforcerService, Enforcer);