juicycleff / nestjs-casbin

NestJS Casbin module with MongoDB Adapter
MIT License
26 stars 12 forks source link

Is this library still updated? Can more interfaces be supported? #5

Open hehehai opened 4 years ago

hehehai commented 4 years ago

@juicycleff Hello there! This library is great

Support more interfaces like

hehehai commented 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!

https://github.com/switchit-conseil/nestjs-casbin-module/blob/32e5d4d9ab2518a8a1cad6d323704962c56a71d8/lib/common/casbin.decorators.ts#L7