Last active
July 28, 2017 15:07
-
-
Save stalniy/34f26877f7782737c1e7507950b99e6b to your computer and use it in GitHub Desktop.
CASL feathers service name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { Ability, AbilityBuilder, toMongoQuery } = require('casl') | |
| const { Forbidden } = require('feathers-errors') | |
| const TYPE_KEY = Symbol.for('type') // <--- added | |
| // the rest of the logic | |
| function subjectName(subject) { // <--- added | |
| if (!subject || typeof subject === 'string') { | |
| return subject | |
| } | |
| return subject[TYPE_KEY] | |
| } | |
| function defineAbilitiesFor(user) { | |
| const { rules, can } = AbilityBuilder.extract() | |
| // rules definition | |
| return new Ability(rules, { subjectName }) // <--- passed subjectName option | |
| } | |
| module.exports = function authorize(name = null) { | |
| return async function(hook) { | |
| // logic to check request type and retrieve result for item request | |
| result[TYPE_KEY] = serviceName // <--- added | |
| if (hook.params.ability.cannot(action, result)) { | |
| throw new Forbidden(`You are not allowed to ${action} ${serviceName}`) | |
| } | |
| if (action === 'get') { | |
| hook.result = result | |
| } | |
| return hook | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment