Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

restrictToRoles hook: More complex determination of "owner". #205

Closed
lukasbuenger opened this issue May 17, 2016 · 2 comments
Closed

restrictToRoles hook: More complex determination of "owner". #205

lukasbuenger opened this issue May 17, 2016 · 2 comments
Labels
Milestone

Comments

@lukasbuenger
Copy link

lukasbuenger commented May 17, 2016

Problem:
In many cases, the determination of an "owner" relationship between a user and a record can not be just pinned down by checking on a foreign key on the user model (e.g. everything depending on n:m relations).

Suggestion:
Introduce something like an resolveOwner field, which references a function that returns a Promise that either gets resolved (permission granted) or rejected (permission denied).

I'd gladly send a PR if you're interested, but I just wanted to check if there's already some (established) way of achieving that kind of behavior and, more importantly, how you guys think about this kind of approach.

restrictToRoles({
    roles: ['admin', 'super-admin'],
    fieldName: 'permissions',
    idField: 'id',
    resolveOwner: (hook) => {
        // check whatever
    ),
    owner: true
})
@lukasbuenger lukasbuenger changed the title restrictToRoles hook: More complex determination of "owner". restrictToRoles hook: More complex determination of "owner". May 17, 2016
@ekryski
Copy link
Member

ekryski commented May 18, 2016

I agree. I've created an issue for you to pass a callback to do whatever you want (#210) however in the mean time you can also just write your own hook to do any custom resolving.

@ekryski ekryski added this to the 1.0 milestone May 21, 2016
@ekryski
Copy link
Member

ekryski commented Jun 30, 2016

We actually came up with a better solution. You can simply wrap any existing hooks in your own, like so:

import { hooks } from 'feathers-authentication';

exports.hashPassword = function(options) {
  // Add any custom options

  function(hook) {
    return new Promise((resolve, reject) => {
      if (myCondition !== true) {
        return resolve(hook);
      }

      // call the original hook
      hooks.hashPassword(options)(hook)
        .then(hook => {
          // do something custom
          resolve(hook);
        })
        .catch(error => {
          // do any custom error handling
          error.message = 'my custom message';
          reject(error);
        });
    });
  });
}

This provides the ultimate flexibility and doesn't require a change to the core hooks.

@ekryski ekryski closed this as completed Jun 30, 2016
@ekryski ekryski removed the Backlog label Jun 30, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants