Monday, 3 April 2017

LoopBack ACL not working

I created a model MyUser inherited from LoopBack User model. In boot script I created a user in MyUser model, custom role and mapped that role to it. But the ACL is not working after assigning in MyUser.json file.

Bootscript.js

'use strict';

module.exports = function(app) {

var User = app.models.MyUser;
var Role = app.models.Role;
var RoleMapping = app.models.RoleMapping;
User.findOrCreate({ name: "Administrator", email: "admin@user.com", username: "admin", password: "12345" }, function(err, succ) {
    if (err) {
        console.log(err);
    } else {
        Role.create({ name: "superUser" }, function(err, role) {
            if (err) {
                console.log(err);
            } else {
                console.log("Role created successfuly");
                role.principals.create({ principalType: RoleMapping.USER, principalId: succ.id }, function(err, success) {
                    if (err) {
                        console.log(err);
                    } else {
                        console.log("role mapped successfuly");
                    }
                });
            }
        });
    }
});

};

MyUser.json

"acls": [
{
  "accessType": "*",
  "principalType": "ROLE",
  "principalId": "superUser",
  "permission": "ALLOW"
}
]

But when I access MyUser.find (get all users) method in api explorer it says

{
"error": {
"statusCode": 401,
"name": "Error",
"message": "Authorization Required",
"code": "AUTHORIZATION_REQUIRED",
"stack": "Error: Authorization Required\n    at ...
}
}

What am I doing wrong?



via SHAHRUKH SHAHID

No comments:

Post a Comment