Loopback: Extend Role and RoleMapping Models

Created on 17 Mar 2016  路  9Comments  路  Source: strongloop/loopback

Hi

Using loopback CLI capability, I created two models 'role' and 'roleMapping' with their base class pointed to loopback built-in models Role and RoleMapping respectively. Added ACL to 'role' model programatically as follows :
var Role = app.models.role;
var RoleMapping = app.models.roleMapping;
var aclToAdd = {};
aclToAdd.accessType = "*";
aclToAdd.principalId = "$everyone";
aclToAdd.principalType = "ROLE";
aclToAdd.permission = "DENY";
Role.settings.acls.push(aclToAdd);

created a role 'superadmin' and principals subsequently :
Role.create({
name: 'superadmin'
}, function(err, myrole) {
if (err) throw err;

  console.log('Created role:', myrole);

  //make bob an admin
  myrole.principals.create({
    principalType: RoleMapping.USER,
    principalId: users[0].id
  }, function(err, principal) {
    if (err) throw err;

    console.log('Created principal:', principal);

aclToAdd = {};
aclToAdd.accessType = "READ";
aclToAdd.principalId = "superadmin";
aclToAdd.principalType = "ROLE";
aclToAdd.permission = "ALLOW";
Role.settings.acls.push(aclToAdd);

  });
});

getting error that myrole.principals is undefined and cannot invoke create on it..

role.json
{
"name": "role",
"base": "Role",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}

role-mapping.json
{
"name": "roleMapping",
"base": "RoleMapping",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}

why is myrole.principals undefined even when the base class of model 'role' is the loopback built-in model 'Role' ? Been stuck with this problem for hours .. :(

Thanks

Most helpful comment

I don't think that relations get extended, just the properties and methods.
Checkout the two JSON files for the built-in models:
https://github.com/strongloop/loopback/blob/master/common/models/role.json
https://github.com/strongloop/loopback/blob/master/common/models/role-mapping.json

All 9 comments

I don't think that relations get extended, just the properties and methods.
Checkout the two JSON files for the built-in models:
https://github.com/strongloop/loopback/blob/master/common/models/role.json
https://github.com/strongloop/loopback/blob/master/common/models/role-mapping.json

+1 I have the exact same problem while generating a memberRole and memberRoleMapping model. I am unable to invoke the create method on the role object I get from :

memberRole.create({
                name: 'admin'
            }, function(err, role) {
                if (err) throw err;

                console.log('Created role:', role);

                role.principals.create({ // TypeError: Cannot read property 'create' of undefined

Do we have a solution for this problem or do I have to stick with the built-in models? Is role and mapping extendable or not?

Ok, well the solution was of course in front of my eyes, thank you @richardpringle. I created the relations in the extended role and mapping json model files and the problem is solved.

@kwuite haha glad to have helped!

Closing the issue, please re-open if it persists.

@richardpringle I'm having this problem too but I'm using built in models. It appears the guys above solved it basically by redefining the models with extended models therefore creating their own. I've tried getting the Role by id too as below. This is in an angular controller as part of a role manager I need to build.

Regards.

    function createTestRole() {
      Role.create({
        name: 'superUser'
      },
      function(role) {
        console.log('role', role);
        Role.findById(
          {
            id: role.id
          },
          function(res) {
            console.log('res', res);
            // Make User id 1 a superUser
            res.principals.create({ //TypeError: Cannot read property 'create' of undefined
              principalType: RoleMapping.USER,
              principalId: 1
            }, function(principal) {
              console.log('principal', principal);
            }, function(err) {
              console.log('error1', err);
            });
          },
          function(err) {
            console.log('error2', err);
          });
      },
      function(err) {
        console.log('error3', err);
      });
    }

Hey @louisl, I'm not actually working on LoopBack anymore but I could still try to help.

Are you just trying to create a role instance? Extending the Role model is a little more complicated.

@richardpringle Apologies, I was unaware you've moved on. Thank you for your reply. Yes I'm just trying to create roles in the database for persistent use, Super User, Admin, Sales Team etc. But as explained above it's failing with TypeError: Cannot read property 'create' of undefined

checkout https://github.com/strongloop/loopback-example-access-control and https://loopback.io/doc/en/lb2/Defining-and-using-roles.html. If you've seen those already, my apologies.

I think the reason that you might be seeing that error is because of where you're calling Role.create. I'm guessing that the script is running before all the models are finished initializing.

for some reason i needed to add both models in mode-config.json

"RoleMapping": {
"dataSource": "accountDS",
"public": false
},
"roleMapping": {
"dataSource": "accountDS",
"public": false
},

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rkmax picture rkmax  路  3Comments

Overdrivr picture Overdrivr  路  4Comments

ian-lewis-cs picture ian-lewis-cs  路  4Comments

daankets picture daankets  路  4Comments

crandmck picture crandmck  路  3Comments