node_acl icon indicating copy to clipboard operation
node_acl copied to clipboard

Ability to get list of roles

Open Niro opened this issue 9 years ago • 9 comments

Hello, I'm working on writing simple forum system and got issue with roles. The forum has to has ability to edit existing roles and setup new ones. I looked up through source code and didn't find any function or storage of roles list. It seems to roles are storing only in backend.

acl.getRoles(function(err, roles){
...
})

Sorry for my English

Niro avatar Jul 27 '15 17:07 Niro

Same question over here, would be a nice feature!

EDIT:

I think this should fix it in the mongob-backend.js:

Changing this:

/**
     Gets the contents at the bucket's key.
  */
  get : function(bucket, key, cb){
    contract(arguments)
        .params('string', 'string|number', 'function')
        .end();
    key = encodeText(key);
    var searchParams = (this.useSingle? {_bucketname: bucket, key:key} : {key:key});
    var collName = (this.useSingle? aclCollectionName : bucket);

    this.db.collection(this.prefix + collName,function(err,collection){
      if(err instanceof Error) return cb(err);
      // Excluding bucket field from search result
      collection.findOne(searchParams, {_bucketname: 0},function(err, doc){
        if(err) return cb(err);
        if(! _.isObject(doc) ) return cb(undefined,[]);
        doc = fixKeys(doc);
        cb(undefined,_.without(_.keys(doc),"key","_id"));
      });
    });
  },

To this:

/**
     Gets the contents at the bucket's key.
  */
  get : function(bucket, key, cb){
    contract(arguments)
        .params('string', 'string|number', 'function')
        .end();

    if(key){
      key = encodeText(key);
      var searchParams = (this.useSingle? {_bucketname: bucket, key:key} : {key:key});
      var collName = (this.useSingle? aclCollectionName : bucket);
    }else{
      var searchParams = (this.useSingle? {_bucketname: bucket} : {});
      var collName = (this.useSingle? aclCollectionName : bucket);
    }

    this.db.collection(this.prefix + collName,function(err,collection){
      if(err instanceof Error) return cb(err);
      // Excluding bucket field from search result
      collection.findOne(searchParams, {_bucketname: 0},function(err, doc){
        if(err) return cb(err);
        if(! _.isObject(doc) ) return cb(undefined,[]);
        doc = fixKeys(doc);
        cb(undefined,_.without(_.keys(doc),"key","_id"));
      });
    });
  },

So the key property is optional. If the key property is empty, it just returns everything.

ErikvdVen avatar Nov 25 '15 10:11 ErikvdVen

IMO, I don't think it was expected to work like that. Since all the users data is not to be kept into ACL database, roles should be the same. You can always write a plugin over node-acl that implement that kind of functionality.

jonmassot avatar Nov 25 '15 13:11 jonmassot

I understand. What you are saying is that users and roles should be saved in a different collection? Like, for example, we already have a users collection which contains all users, and ACL is using those ID's to link them to specific roles and permissions. And roles should be the same thing, that's what you are saying? Cause that does definitely make sense.

ErikvdVen avatar Nov 25 '15 13:11 ErikvdVen

@ErikvdVen Yes, that's what I'm saying. I pretty much understand the use case, I'm just arguing as to why you want to have roles (without users) in the acl database that should be more like a collection of authorization and not a collection of roles and authorization.

jonmassot avatar Nov 25 '15 13:11 jonmassot

Alright! Thanks for thinking along! We just started creating a separated collection with all roles :+1:

ErikvdVen avatar Nov 25 '15 14:11 ErikvdVen

Cool. Glad I could be of any help!

jonmassot avatar Nov 25 '15 14:11 jonmassot

I just ran into this issue. say I am creating an admin section that defines role permissions. I need to be able to list all the roles in the acl_roles collection. Currently that is not possible without queying the collection independently... which means I will probably need a mongoose schema. Not sure why there is not basic CRUD functionality on this module.

trainerbill avatar Apr 11 '16 02:04 trainerbill

Hey trainerbill any updates of how to do it without mongoose schema?

anandka avatar May 14 '16 08:05 anandka

@trainerbill : You can use roles same as users: just ids of them, not whole objects.

Store Roles, Users outside of ACL storage, and use ACL only for defining relationships between permissions/roles/users.

deksden avatar Jul 27 '17 09:07 deksden