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

SetPermissions is not working correctly when used as a before create hook #16

Closed
juanchristensen opened this issue Jan 17, 2017 · 0 comments

Comments

@juanchristensen
Copy link

juanchristensen commented Jan 17, 2017

Steps to reproduce

1- Add the setPermissions hook as a before create hook to the users service, with a template permission using the :id string.

create: [
    permissions.hooks.setPermissions({ permissions: ['users:*:id'] })
  ]

2- Create an user by calling the users service.

Expected behavior

Given a user id = 1.
The created user should have a permission equal to users:*:1.

Actual behavior

The created user has a permission equal to users:*:id.

Solution

The issue happens because as a before create hook, the setPermissions hook doesn't have access to the user id (the user hasn't been created yet), thus it cannot replace it on the template.
Using the setPermissions hook as an after create hook works correctly.

permissions = permissions.map(function (permission) {
      if (id !== undefined && permission.includes(':id')) {
        permission = permission.replace(':id', ':' + id);
      }

      return permission;
    });

The solution is to simply check if the hook is being used as a before create hook, and warn the user accordingly (as a before update or before patch hook it works correctly).

System configuration

Module versions:
I'm using the latest commit from the feathers-permissions filters branch (commit no. b33fcdb).

NodeJS version: 6.9.4

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

1 participant