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

Appened typescript version of code #54

Merged
merged 1 commit into from
Jan 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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