Skip to content

pardjs/nest-casbin

Repository files navigation

nest-casbin

Make magic with Nest.js, Casbin and TypeORM.

Example

In YOUR_PROJECT_ROOT/src/app.module.ts file:

import { Module } from "@nestjs/common";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";
import { CasbinModule } from "@pardjs/nest-casbin";

@Module({
  imports: [
    CasbinModule.forRootAsync(
      {
        type: "mysql",
        host: "localhost",
        port: 3306,
        username: "root",
        password: "",
        database: "casbin"
      },
      "/path/to/the/casbin/model/file.conf"
    )
  ],
  controllers: [AppController],
  providers: [AppService]
})
export class AppModule {}

in ``YOUR_PROJECT_ROOT/src/app.service.ts` file:

import { Injectable, Inject } from "@nestjs/common";
import { Enforcer } from "casbin";
import { CASBIN_ENFORCER, CasbinService } from "@pardjs/nest-casbin";

@Injectable()
export class AppService {
  constructor(
    @Inject(CASBIN_ENFORCER) private readonly enforcer: Enforcer,
    private readonly casbinService: CasbinService
  ) {}

  getHello(): string {
    return this.casbinService.checkPermission("alice", "data1", "read");
  }
}

About

Make magic with Nest.js, Casbin and TypeORM.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published