Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service should provide a hook to loaded resource or respect filters in all methods #204

Closed
stalniy opened this issue Jul 26, 2017 · 4 comments

Comments

@stalniy
Copy link

stalniy commented Jul 26, 2017

Description

Currently I'm trying to find a way of how I can integrate casl with feathersjs application. In the current implementation of services it's impossible because I don't have access to loaded record by service and service ignores filter parameters for get(id), patch(id) and other similar methods (it just fetches an item by id).

Expected behavior

I need a way to check whether user can do something with record before service do this but after service loads record. Alterna

Actual behavior

There shoud be hook which is called when service loads record from database or service all service methods should respect filtering parameters

@stalniy stalniy changed the title Service should provide a hook to query or loaded resource Service should provide a hook to loaded resource or respect filters in all methods Jul 26, 2017
@daffl
Copy link
Member

daffl commented Jul 26, 2017

The current way to do this would be to retrieve the record in a hook, check the permissions and either throw an error or set hook.result if you are allowed:

const errors = require('feathers-errors');

app.service('myservice').hooks({
  before: {
    get(hook) {
      if(hook.id && hook.params.provider !== 'server') {
        const params = Object.assign({}, hook.params, {
          provider: 'server'
        });

        return hook.service.get(hook.id, params).then(result => {
          if(!checkPermissions(result)) {
            throw new errors.NotAuthorized('You are not allowed');
          }

          hook.result = result;

          return hook;
        });
      }
    }
  }
})

@stalniy
Copy link
Author

stalniy commented Jul 27, 2017

Aha. I thought about this scenario but I expected that service method would be called twice.

So, if I set hook.result in before hook the actual service method will not be called, correct?

@daffl
Copy link
Member

daffl commented Jul 27, 2017

Exactly. Setting hook.result in a before hook will skip the actual method call (but still run all after hooks). That way we can avoid duplicate calls in this case.

@stalniy
Copy link
Author

stalniy commented Jul 27, 2017

Cool! Thanks!

@stalniy stalniy closed this as completed Jul 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants