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

Using iff with restrictToOwner #140

Closed
beevelop opened this issue Mar 10, 2017 · 10 comments
Closed

Using iff with restrictToOwner #140

beevelop opened this issue Mar 10, 2017 · 10 comments

Comments

@beevelop
Copy link

beevelop commented Mar 10, 2017

Steps to reproduce

const auth = require('feathers-authentication').hooks
const iff = require('feathers-hooks-common').iff
const isNot = require('feathers-hooks-common').isNot
const isCortex = require('../../hooks/cortex').isCortex
const isUser = require('../../hooks/auth').isUser

exports.before = {
  [...]
  patch: [ isUser, iff(isNot(isCortex()), auth.restrictToOwner({ ownerField: 'id' })) ]
  [...]
}

Send a patch request to the service as user ("Cortex" is the equivalent of an admin user).

Actual behavior

TypeError: Cannot read property 'get' of undefined
  File "/[...]/node_modules/feathers-authentication/lib/hooks/restrict-to-owner.js", line 47, col 19, in exports.default
    return _this.get(hook.id, params).then(function (data) {
  File "/[...]/node_modules/feathers-authentication/lib/hooks/restrict-to-owner.js", line 42, col 12, in exports.default
    return new Promise(function (resolve, reject) {

Module versions (especially the part that's not working):

feathers: "2.1.1",
feathers-authentication: "0.7.11",
feathers-hooks: "1.8.1",
feathers-hooks-common: "2.0.3" or "3.0.0-pre"

NodeJS version:

Node v6.9.4

Unfortunately I could not find any reference if this kind of hook combination should properly work this way.

@eddyystop
Copy link
Collaborator

eddyystop commented Mar 10, 2017 via email

@ekryski
Copy link
Member

ekryski commented Mar 10, 2017

hmm... is likely an issue with the restrictToOwner hook but they were never designed with this in mind because they were created before feathers-hooks-common came into play. Likely it's because they are doing checks on usage inside the hook. We have new permissions ones coming soon that play nicer and should work better.

@beevelop
Copy link
Author

@ekryski Well, that's unhandy. As a temporary work-around (until feathers-permissions, etc.) are ready-to-rock, is there a simple way to replicate iff's behaviour without loosing the context (this)? – restrictToOwner does in fact work seamless on its own, I just want to apply the conditional logic to the relevant hook.

@ekryski
Copy link
Member

ekryski commented Mar 10, 2017

Actually @eddyystop I did notice this when using hooks. That the this context was no longer set to the service. I wonder if it is this line? https://github.com/feathersjs/feathers-hooks-common/blob/master/src/common/iff.js#L10

@beevelop so I take back my previous comment, those hooks should work with the common hooks.

@eddyystop
Copy link
Collaborator

eddyystop commented Mar 10, 2017 via email

@beevelop
Copy link
Author

@eddyystop @ekryski Do you mean something like this 👇 in common/iff.js:

iffWithoutElse.else = (...falseHooks) => _iffElse(predicate, trueHooks, falseHooks).call(that, hook);

@eddyystop
Copy link
Collaborator

eddyystop commented Mar 22, 2017 via email

@ekryski
Copy link
Member

ekryski commented Mar 22, 2017

I think so. @beevelop I'm also tied up with Feathers docs right now but if you are able to write a test and create a PR I'll ❤️ you forever. 😄

@eddyystop
Copy link
Collaborator

eddyystop commented May 4, 2017

I have spent way too long on this without success. The expected context isn't being passed to functions with call. Instead they seemingly get the export of src/index.js. (Babel?)

I've changed the docs to say this !== service in predicates and hooks used within iff, else and iffElse , and that hook.service should be used instead. feathersjs-ecosystem/docs#580

BTW we've run into other issues with Babel. #116

@eddyystop
Copy link
Collaborator

Reopen this issue if you want to take a shot at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants