Skip to content

Commit 554bbc4

Browse files
Merge pull request #1687 from stalniy/patch-1
docs(authorization): updates casl example in accordance to the changes in v5
2 parents 9206fb2 + b08f584 commit 554bbc4

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

content/security/authorization.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,21 @@ export class CaslAbilityFactory {
250250
can(Action.Update, Article, { authorId: user.id });
251251
cannot(Action.Delete, Article, { isPublished: true });
252252

253-
return build();
253+
return build({
254+
// Read https://casl.js.org/v5/en/guide/subject-type-detection#use-classes-as-subject-types for details
255+
detectSubjectType: item => item.constructor as ExtractSubjectType<Subjects>
256+
});
254257
}
255258
}
256259
```
257260

258261
> warning **Notice** `all` is a special keyword in CASL that represents "any subject".
259262
260-
> info **Hint** `Ability`, `AbilityBuilder`, and `AbilityClass` classes are exported from the `@casl/ability` package.
263+
> info **Hint** `Ability`, `AbilityBuilder`, `AbilityClass`, and `ExtractSubjectType` classes are exported from the `@casl/ability` package.
261264
262-
In the example above, we created the `Ability` instance using the `AbilityBuilder` class. As you probably guessed, `can` and `cannot` accept the same arguments but has different meanings, `can` allows to do an action on the specified subject and `cannot` forbids. Both may accept up to 4 arguments. To learn more about these functions, visit the official [CASL documentation](https://casl.js.org/v4/en/guide/intro).
265+
> info **Hint** `detectSubjectType` option let CASL understand how to get subject type out of an object. For more information read [CASL documentation](https://casl.js.org/v5/en/guide/subject-type-detection#use-classes-as-subject-types) for details.
266+
267+
In the example above, we created the `Ability` instance using the `AbilityBuilder` class. As you probably guessed, `can` and `cannot` accept the same arguments but has different meanings, `can` allows to do an action on the specified subject and `cannot` forbids. Both may accept up to 4 arguments. To learn more about these functions, visit the official [CASL documentation](https://casl.js.org/v5/en/guide/intro).
263268

264269
Lastly, make sure to add the `CaslAbilityFactory` to the `providers` and `exports` arrays in the `CaslModule` module definition:
265270

@@ -289,7 +294,7 @@ if (ability.can(Action.Read, 'all')) {
289294
}
290295
```
291296

292-
> info **Hint** Learn more about the `Ability` class in the official [CASL documentation](https://casl.js.org/v4/en/guide/intro).
297+
> info **Hint** Learn more about the `Ability` class in the official [CASL documentation](https://casl.js.org/v5/en/guide/intro).
293298
294299
For example, let's say we have a user who is not an admin. In this case, the user should be able to read articles, but creating new ones or removing the existing articles should be prohibited.
295300

0 commit comments

Comments
 (0)