1
votes

I have kind of multi-tenant application. I have many groups, users belongsTo one group. Group has many different models e.g. news, files etc. Users have access only to elements of their group. It is similar to the 'teamMember' example on the Loopback docs, in my case I have `groupMember', so when user is trying to access, I check if User.groupId === Model.groupId

The issue which I have, is that I need to have roles for those groups. So, e.g. basic, privileged, admin. Basic can access some of methods, privileged more than basic but not all of them and admin can access all methods, but ONLY FOR ELEMENTS of his group. So, in my understanding I have to combine - groupMember and e.g. admin, but I have no idea how to do that.

Anyone can help? I think it's quite common thing.

1
You could get some inspiration in loopback-component-access-groups package. - Ivan Schwarz
to be honest it is complete solution for this issue. Thank you. I may just forked it to improve the access levels solution. - Marek Urbanowicz

1 Answers

0
votes
module.exports = function (app) {
  //get User model from the express app
  var loopback = require('loopback');
  var User = app.models.User;
  let { Role, RoleMapping } = require('../../server/server').models;

  //Role Mapping
  User.observe('after save', function setRoleMapping(ctx, next) {
    console.log("Current User : "+currentUser);
    if (ctx.instance) {
      if(ctx.isNewInstance) {
        let rol_name=[ctx.instance.type];
        // look up role based on type
        Role.find({where: {name:{inq:rol_name} }}, function(err, role) {
          console.log('Role: '+role)
          if (err) {return console.log(err);}

          RoleMapping.create({
            principalType: rol_name,
            principalId: ctx.instance.id,
            roleId: role[0].id
          }, function(err, roleMapping) {

            if (err) {return console.log(err);}
            console.log("Created Role :"+JSON. parse(JSON. stringify(role)));
            console.log('User assigned RoleID ' + role[0].id + ' (' + ctx.instance.type + ')');

          });

        });

      }
    }
    next();
  });

}

Post json Data Format

{
"email":"[email protected]",
"password":"123456",
"type":"storeAdmin"
}

My Role Table: enter image description here