GET /Users always returns 401 "Authorization Required"
Hi,
I think I'm missing something. I'm using Loopback 2.0, and on a clean install (using yeoman generator-loopback) I can't seem to get a list of Users. It always says I need authorization, is this normal? Can't I use the User model to build my collection of users?
I can successfully create, login and logout a user. But I want to get a list of user records and I can't seem to do that. Can someone please point me in the right direction?
By default, the User model has ACLs to prevent methods such as find to be called from the REST API. You should relax it by configuring ACLs for the model. See http://docs.strongloop.com/display/LB/Authentication+and+authorization.
Thanks Raymond! I will try relaxing the ACLs for the model.
Solved it! I was trying to override the User acls through the model json file, for some reason this doesn't work. The solution was to do it programmatically in the server.js file.
@jcsmesquita - Can you please show me how you did this programmatically?
Hi Shankar, sorry for the delay.
In server.js, you can add something like this:
app.models.user.settings.acls = require('./user-acls.json');
and the json file looks something like this:
[ { "principalType": "ROLE", "principalId": "$everyone", "permission": "DENY" }, { "principalType": "ROLE", "principalId": "$everyone", "permission": "ALLOW", "property": "create" }, { "principalType": "ROLE", "principalId": "admin", "permission": "ALLOW", "property": "deleteById" }, { "principalType": "ROLE", "principalId": "$everyone", "permission": "ALLOW", "property": "login" }, { "principalType": "ROLE", "principalId": "admin", "permission": "ALLOW", "property": "exists" }, { "principalType": "ROLE", "principalId": "$everyone", "permission": "ALLOW", "property": "logout" }, { "principalType": "ROLE", "principalId": "admin", "permission": "ALLOW", "property": "find" }, { "principalType": "ROLE", "principalId": "admin", "permission": "ALLOW", "property": "findById" }, { "principalType": "ROLE", "principalId": "$owner", "permission": "ALLOW", "property": "updateAttributes" }, { "principalType": "ROLE", "principalId": "$everyone", "permission": "ALLOW", "property": "confirm" }, { "principalType": "ROLE", "principalId": "admin", "permission": "ALLOW", "property": "upsert" }, { "principalType": "ROLE", "principalId": "admin", "permission": "ALLOW", "property": "updateAll" } ]
"admin" is a custom user role we have in our app.
Hope that helps...
Good, thanks
We accomplish this by using the following utility function to clear the base model ACLs:
exports.clearBaseACLs = function (ModelType, ModelConfig) {
ModelType.settings.acls.length = 0;
ModelConfig.acls.forEach(function (r) {
ModelType.settings.acls.push(r);
});
};
Usage being like this (from user.js, for example):
modelUtils.clearBaseACLs(user, require('./user.json'));
This will clear the built-in ACLs and replace them with whatever is configured in the model JSON.
@doublemarked thanks.
@gustavomick no problem. At this time I would suggest converting this into a simple mixin. The logic may remain the same.
@doublemarked could you give me an example?
@gustavomick sure, why not - it's not flawless, but it gets the job done.
1: Add this to your model.json:
"mixins": {
"ClearBaseAcls": true
}
2: Drop this in common/mixins/clear-base-acls.js:
var appRoot = require('app-root-path');
module.exports = function (Model, options) {
var configFile = options.config;
if (!configFile) {
// Works for 99% of cases. For others, set explicit path via options.
configFile = '/common/models/' + slugify(Model.modelName) + '.json';
}
var config = appRoot.require(configFile);
if (!config || !config.settings || !config.settings.acls) {
console.error('ClearBaseAcls: Failed to load model config from',configFile);
return;
}
Model.settings.acls.length = 0;
config.acls.forEach(function (r) {
Model.settings.acls.push(r);
});
};
function slugify(name) {
name = name.replace(/^[A-Z]+/, function (s) { return s.toLowerCase(); });
return name.replace(/[A-Z]/g, function (s) { return '-' + s.toLowerCase(); });
}
nice. why is a mixin preferable?, instead of may be a boot script, its because migrations aspects? thank you.
Because it's config driven instead of code driven. The configuration of the acls stays near the configuration of the clearing of the acls.
And generally a mixin is cleanly reusable, but it is perhaps unlikely that this example would be reused across many models.
Hey I am not able to understand the implementation @doublemarked . I have put the code "mixins": { "ClearBaseAcls": true } in model-config.json and it gives "Error: Model not found: mixins" since I dont have any model.json. Should I create a new Model named mixins?. Any leads would be awesome as I am new to loopback. Thanks.
This belongs in your common/models/whatever-model.json, not in model-config.json.
Ohkay and after putting it there it gives ClearBaseAcls: Failed to load model config from /common/models/user.json error
Do you have any ACLs defined in your user.json file? Is your file located at
var config = appRoot.require(configFile);
if (!config || !config.settings || !config.settings.acls) {
console.error('ClearBaseAcls: Failed to load model config from',configFile);
return;
}
Is it possible to have a clear solution to this problem, maybe with a detailed tutorial? It is not very clear how to get all Users... Thanks
That mixin wasn't working for me, so I tweaked it and converted it to use the ES6 features now supported in node without any CLI flags.
'use strict';
const path = require('path');
const appRoot = require('app-root-path');
function slugify(name) {
name = name.replace(/^[A-Z]+/, s => s.toLowerCase());
return name.replace(/[A-Z]/g, s => '-' + s.toLowerCase());
}
module.exports = (Model) => {
const configFile = path.join('./common/models/', slugify(Model.modelName) + '.json');
const config = appRoot.require(configFile);
if (!config || !config.acls) {
console.error('ClearBaseAcls: Failed to load model config from', configFile);
return;
}
Model.settings.acls.length = 0;
config.acls.forEach(r => Model.settings.acls.push(r));
};