Giter Site home page Giter Site logo

Comments (4)

nodece avatar nodece commented on August 19, 2024 1

Should use single pattern :) Yan can run it and is working.

const appBoot = {
  enforcer: null,
  newCasbin: async () => {
    if(!this.enforcer){
      const dbConnect = await TypeORMAdapter.newAdapter({
        "type": "mysql",
        "host": "localhost",
        "port": 3306,
        "username": "root",
        "password": "123456",
        "database": "casbin"
      });
      this.enforcer = await casbin.newEnforcer('authz_model.conf', dbConnect);
    }
    return this.enforcer
  }
}

module.exports = {
  middleware: [ 'user', 'authz' ],
  authz: {
    enable: true,
    newEnforcer: async () => {
      const e = await appBoot.newCasbin()
      return e        
    }
  }
}

from typeorm-adapter.

nodece avatar nodece commented on August 19, 2024

@Shaun-Li typeorm-adapter v1.0.3 has been published and fixed where this errror.

from typeorm-adapter.

hsluoyz avatar hsluoyz commented on August 19, 2024

Well, I'm happy to see it's fixed, but I just wonder why this issue is not in: https://github.com/node-casbin/typeorm-adapter ?

from typeorm-adapter.

thakurakhilesh avatar thakurakhilesh commented on August 19, 2024

I am using nestjs and install casbin and typeorm-adapter v1.2.0. I am facing below issue.
Error: AlreadyHasActiveConnectionError: Cannot create a new connection named "node-casbin-official", because connection with such name already exist and it now has an active connection session

Node version: 12.0.0
My code is:
Filename: main.ts

import { authz } from './auth-module/authorization.middleware';
async function bootstrap(): Promise<void> {
app.use(authz(async () => {
          const adapter = await TypeORMAdapter.newAdapter(configService.getTypeOrmAdapter());
          const e = await newEnforcer('/var/www/html/avantha-audit-pro-api/src/casbin_conf/rbac_model.conf', adapter);
        // Load the policy from DB.
        await e.loadPolicy();
        return e;
    }));
}
bootstrap();

Filename:authorization.middleware.ts

import { Enforcer, newEnforcer } from 'casbin';
import TypeORMAdapter from 'typeorm-adapter';


export class BasicAuthorizer {
  private req: any;
  private enforcer: any;

  constructor(req, enforcer) {
    this.req = req;
    this.enforcer = enforcer;
  }

  getUserRole() {
    const { user } = this.req;
    if(typeof user !== 'undefined'){
      const { role } = user;
      return role;
    }
    return true;

  }

  checkPermission() {
    const { req, enforcer } = this;
    const { originalUrl: path, method } = req;
    const userRole = this.getUserRole();
    if (userRole){
      return enforcer.enforce(userRole, path, method);
    }
    return true;
  }
}

// the authorizer middleware
export function authz(newEnforcer: () => Promise<Enforcer>) {
  return async (req, res, next) => {
    const enforcer = await newEnforcer();

    // user sample
    //req.user = { role: 'admin' };

    if (!(enforcer instanceof Enforcer)) {
      res.status(500).json({ 500: 'Invalid enforcer' });
      return;
    }

    const authorizer = new BasicAuthorizer(req, enforcer);
    if (!authorizer.checkPermission()) {
      res.status(403).json({ 403: 'Forbidden' });
      return;
    }

    next();
  }
};

Please help me to solve this issue.

from typeorm-adapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.