node-casbin icon indicating copy to clipboard operation
node-casbin copied to clipboard

feat(defaultrolemanager.ts): add hierarchical domain support for rbac

Open Sefriol opened this issue 3 years ago • 9 comments

Work in progress. Initial idea from https://github.com/casbin/casbin/pull/1040

  • [x] add basic functionality to the roleManager
  • [x] Fix related helper functions
  • [ ] Add documentation?
  • [x] Rebase from master when https://github.com/casbin/node-casbin/pull/378 merged

g2 defitions are in "reverse" order for reason. Other way around it would not work.

Discussion welcome.

Sefriol avatar Aug 10 '22 19:08 Sefriol

@nodece @Zxilly @Shivansh-yadav13 please review

casbin-bot avatar Aug 10 '22 19:08 casbin-bot

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Aug 10 '22 19:08 CLAassistant

@Sefriol plz fix:

image

hsluoyz avatar Aug 11 '22 01:08 hsluoyz

Helper functions are now fixed and branch is rebased. What kind of documentation would you want @hsluoyz?

EDIT: Opinions about the testing set would be nice. Do we need more complicated test cases?

Sefriol avatar Aug 11 '22 20:08 Sefriol

@Sefriol good work! Plz add docs to v2 docs: https://casbin.io/docs/rbac-with-domains

hsluoyz avatar Aug 12 '22 06:08 hsluoyz

@Sefriol plz fix:

image

hsluoyz avatar Aug 12 '22 12:08 hsluoyz

I changed tests to use csv and conf files instead of programmatically generating the model.

Sefriol avatar Aug 14 '22 13:08 Sefriol

Thank @Sefriol for your contribution! Your approach is a good idea, but we have already added many magic methods to RoleManager, I think we should be able to extend the g() method, and pass a method/const to g() to support this feature like g(r.sub, p.sub, r.dom, inject("g2")), we will inject the roleManger of g2 to g(), then we check whether thearg includes the inject().

const name = "built-in-role-manager";
function inject(arg: string) {
  switch (arg) {
    case "g2":
      return {
        name:  name,
        value: rm2
      };
  }
}

function generateGFunction(rm: rbac.RoleManager): any {
  return async function (...args: any[]): Promise<boolean> {
    const g2 = args[args.length - 1];
    if (Object.prototype.toString.call(g2) === '[object Object]') {
      if (g2.name === builtInRoleManager) {
        // link the g2.value to the root role manager to build a new role link, then check the link
      }
    }
  };
}

What do you think about this approach? Please let me know if you have any ideas.

nodece avatar Aug 26 '22 16:08 nodece

I definitely would prefer a solution where hierarchy is defined by the model itself instead of requiring different calls on the RoleManager to setup the hierarchy.

Current test cases cover pretty much what I try to achieve. So minor details are not as important as others, but the test I have created deliver the perfect authorization solution for my use case. Not sure about the performance. I would ofcourse want a solution which is as elegant as possible while delivering the core functionality that I desire.

Sefriol avatar Aug 26 '22 19:08 Sefriol

@nodece Any update on the idea you proposed?

Sefriol avatar Oct 05 '22 14:10 Sefriol

I definitely would prefer a solution where hierarchy is defined by the model itself instead of requiring different calls on the RoleManager to setup the hierarchy.

I agreed with @Sefriol, it looks more standard.

What do you think of it? @hsluoyz

nodece avatar Dec 28 '22 17:12 nodece

lgtm, merged, plz add the docs

hsluoyz avatar Dec 29 '22 14:12 hsluoyz

:tada: This PR is included in version 5.20.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Dec 29 '22 14:12 github-actions[bot]

@nodece @hsluoyz You decided that this is a better option?

I was wondering since https://github.com/casbin/casbin/pull/1040 has not moved at all.

@sujit-baniya also raised some questions in the pull request, which might indicate that there is a problem with the implementation.

This was just an idea on how the implementation could be done, but @nodece idea could have been even better?

Just to confirm: Is this now the way how hierarchy will be defined in all casbin projects?

Sefriol avatar Jan 05 '23 14:01 Sefriol