Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hehehai opened this issue Jul 12, 2020 · 1 comment
Open

Comments

@hehehai
Copy link

hehehai commented Jul 12, 2020

@juicycleff Hello there! This library is great

Support more interfaces
like

  • addGroupingPolicy
  • AddPolicies
    ...
@hehehai
Copy link
Author

hehehai commented Jul 12, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant