loopback icon indicating copy to clipboard operation
loopback copied to clipboard

GET /Users always returns 401 "Authorization Required"

Open LightningK0ala opened this issue 12 years ago • 47 comments

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?

LightningK0ala avatar Sep 12 '14 18:09 LightningK0ala

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.

raymondfeng avatar Sep 12 '14 19:09 raymondfeng

Thanks Raymond! I will try relaxing the ACLs for the model.

LightningK0ala avatar Sep 13 '14 12:09 LightningK0ala

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.

LightningK0ala avatar Sep 15 '14 16:09 LightningK0ala

@jcsmesquita - Can you please show me how you did this programmatically?

ShankarSangoli avatar Nov 17 '14 18:11 ShankarSangoli

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...

LightningK0ala avatar Nov 24 '14 15:11 LightningK0ala

Good, thanks

melochale avatar Jun 17 '15 02:06 melochale

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 avatar Jul 21 '15 18:07 doublemarked

@doublemarked thanks.

gustavomick avatar Nov 11 '15 23:11 gustavomick

@gustavomick no problem. At this time I would suggest converting this into a simple mixin. The logic may remain the same.

doublemarked avatar Nov 12 '15 12:11 doublemarked

@doublemarked could you give me an example?

gustavomick avatar Nov 12 '15 13:11 gustavomick

@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(); });
}

doublemarked avatar Nov 12 '15 16:11 doublemarked

nice. why is a mixin preferable?, instead of may be a boot script, its because migrations aspects? thank you.

gustavomick avatar Nov 12 '15 19:11 gustavomick

Because it's config driven instead of code driven. The configuration of the acls stays near the configuration of the clearing of the acls.

doublemarked avatar Nov 12 '15 20:11 doublemarked

And generally a mixin is cleanly reusable, but it is perhaps unlikely that this example would be reused across many models.

doublemarked avatar Nov 12 '15 20:11 doublemarked

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.

ghost avatar Nov 24 '15 09:11 ghost

This belongs in your common/models/whatever-model.json, not in model-config.json.

doublemarked avatar Nov 24 '15 09:11 doublemarked

Ohkay and after putting it there it gives ClearBaseAcls: Failed to load model config from /common/models/user.json error

ghost avatar Nov 24 '15 09:11 ghost

Do you have any ACLs defined in your user.json file? Is your file located at /common/models/user.json? Note the code above is very straight forward:

  var config = appRoot.require(configFile);
  if (!config || !config.settings || !config.settings.acls) {
    console.error('ClearBaseAcls: Failed to load model config from',configFile);
    return;
  }

doublemarked avatar Nov 24 '15 11:11 doublemarked

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

Torone avatar Jan 06 '16 12:01 Torone

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));
};

noderat avatar Jan 07 '16 17:01 noderat