forked from stalniy/casl-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppAbility.ts
28 lines (22 loc) · 830 Bytes
/
AppAbility.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { AbilityBuilder, Ability, AbilityClass, InferSubjects } from '@casl/ability';
import { Todo } from '../models/Todo'
type Actions = 'manage' | 'create' | 'read' | 'update' | 'delete';
type Subjects = InferSubjects<Todo> | 'all';
export type AppAbility = Ability<[Actions, Subjects]>;
export const AppAbility = Ability as AbilityClass<AppAbility>;
export function defineAbilitiesFor(role: string) {
const { can, rules } = new AbilityBuilder(AppAbility);
if (role === 'admin') {
can('manage', 'all');
} else {
can('read', 'all');
can('manage', 'Todo', { assignee: 'me' });
}
return rules;
}
export function createAbility() {
return new AppAbility(defineAbilitiesFor('member'), {
// https://casl.js.org/v5/en/guide/subject-type-detection
detectSubjectType: object => object.kind,
});
}