GithubHelp home page GithubHelp logo

Comments (4)

hsluoyz avatar hsluoyz commented on June 1, 2024

Please show your code.

from sequelize-adapter.

HighSoftWare96 avatar HighSoftWare96 commented on June 1, 2024

This is my schema model:

[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[role_definition]
g = _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && (regEqJollyMatch(r.act, p.act) || actionGroupMatch(r.obj, r.act, p.act))

This is the ACL Manager:

const casbin = require('casbin');
const { SequelizeAdapter } = require('casbin-sequelize-adapter');
const { conf } = require('shared/config');
const { acls } = conf;
const { stream: dbConfig } = conf.db;
const fs = require('fs');
const { resolve, join } = require('path');
const { loadMatchers } = require('./aclMatchers');

const loadAclEnforcer = async function loadAclEnforcer(ctx) {
  let caspinEnforcer;

  try {

    const sequelizeAdapter = await SequelizeAdapter.newAdapter({
      username: dbConfig.user,
      host: dbConfig.host,
      port: dbConfig.port,
      password: dbConfig.pw,
      database: dbConfig.name,
      schema: dbConfig.schema,
      dialect: 'postgres',
      logging: conf.logQuery
    });

    const confPath = resolve(process.cwd(), acls.confPath);
    const policiesDir = resolve(process.cwd(), acls.policiesDir);

    caspinEnforcer = await casbin.newEnforcer(
      confPath,
      sequelizeAdapter
    );

    loadMatchers(ctx, caspinEnforcer);

    let policiesNeedsRefresh = false;

    fs.readdirSync(policiesDir).forEach(filePath => {

      if (!filePath.match(/^(.*)\.acl\.json$/gi)) {
        return;
      }

      const policyPath = join(policiesDir, filePath);
      const policiesGroup = require(policyPath);
      if (policiesGroup && policiesGroup.length > 0) {
        policiesGroup.forEach(async (policy) => {
          let added;
          if (policy[0] === 'g') {
            added = await caspinEnforcer.addNamedGroupingPolicy(...policy);
          } else if (policy[0] === 'p') {
            added = await caspinEnforcer.addNamedPolicy(...policy);
          }
          policiesNeedsRefresh = policiesNeedsRefresh || added;
        });
      }
    });

    // here Casbin duplicates the policies
    await caspinEnforcer.savePolicy();

  } catch (error) {
    ctx.logger.error('An error occurred loading the ACLs.');
    throw error;
  }

  return caspinEnforcer;
};

module.exports = { loadAclEnforcer };

from sequelize-adapter.

nodece avatar nodece commented on June 1, 2024

Remove the await caspinEnforcer.savePolicy();

from sequelize-adapter.

nodece avatar nodece commented on June 1, 2024

Casbin can automatically save policies.

from sequelize-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.