Skip to content

Commit

Permalink
Merge pull request #54 from dbvcode/patch-1
Browse files Browse the repository at this point in the history
Appened typescript version of code
  • Loading branch information
fratzinger authored Jan 19, 2022
2 parents fc3ff04 + 0adfa65 commit f6871b5
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,52 @@ module.exports = {
defineAbilitiesFor
};

```
Typescript version of the code:

```js
// src/services/authentication/authentication.abilities.ts
import { createAliasResolver, makeAbilityFromRules } from 'feathers-casl';
import { AbilityBuilder, Ability } from '@casl/ability';

// don't forget this, as `read` is used internally
const resolveAction = createAliasResolver({
update: 'patch', // define the same rules for update & patch
read: ['get', 'find'], // use 'read' as a equivalent for 'get' & 'find'
delete: 'remove' // use 'delete' or 'remove'
});

export const defineRulesFor = (user: any) => {
// also see https://casl.js.org/v5/en/guide/define-rules
const { can, cannot, rules } = new AbilityBuilder(Ability);

if (user.role && user.role.name === 'SuperAdmin') {
// SuperAdmin can do evil
can('manage', 'all');
return rules;
}

if (user.role && user.role.name === 'Admin') {
can('create', 'users');
}

can('read', 'users');
can('update', 'users', { id: user.id });
cannot('update', 'users', ['roleId'], { id: user.id });
cannot('delete', 'users', { id: user.id });

can('manage', 'tasks', { userId: user.id });
can('create-multi', 'posts', { userId: user.id });

return rules;
};

export const defineAbilitiesFor = (user: any) => {
const rules = defineRulesFor(user);

return makeAbilityFromRules(rules, { resolveAction });
};

```

### Add abilities to hooks context
Expand Down

0 comments on commit f6871b5

Please sign in to comment.