Open hehehai opened 4 years ago
try
casbin.decorator.ts
import { Inject } from '@nestjs/common';
const CASBIN_ENFORCER = 'CASBIN_ENFORCER';
/**
* Used to inject the Enforcer in your services... It is very simple
*/
export const InjectEnforcer = (): any => Inject(CASBIN_ENFORCER);
xx.service.ts
import { Injectable } from '@nestjs/common';
import { InjectEnforcer } from '@app/decorators/casbin.decorator';
import { Enforcer } from 'casbin';
@Injectable()
export class InitService {
constructor(
@InjectEnforcer()
private readonly enforcer: Enforcer,
) {}
async initPolicy(): Promise<void> {
try {
await this.enforcer.loadPolicy();
const added = await this.enforcer.addPolicy('root', '/*', '*');
console.log(added);
if (added) {
await this.enforcer.savePolicy();
}
} catch (error) {
console.log('init policy error ', error);
}
}
}
that's working!
@juicycleff Hello there! This library is great
Support more interfaces like