Skip to content

Commit 82b7a35

Browse files
Merge pull request #3256 from cuonglamphu/update-casl-integration-docs
fix(docs): Update deprecated 'Ability' class reference
2 parents de8fed5 + 5a4a5ba commit 82b7a35

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

content/security/authorization.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,12 @@ With this in place, we can define the `createForUser()` method on the `CaslAbili
234234
```typescript
235235
type Subjects = InferSubjects<typeof Article | typeof User> | 'all';
236236

237-
export type AppAbility = Ability<[Action, Subjects]>;
237+
export type AppAbility = MongoAbility<[Action, Subjects]>;
238238

239239
@Injectable()
240240
export class CaslAbilityFactory {
241241
createForUser(user: User) {
242-
const { can, cannot, build } = new AbilityBuilder<
243-
Ability<[Action, Subjects]>
244-
>(Ability as AbilityClass<AppAbility>);
242+
const { can, cannot, build } = new AbilityBuilder(createMongoAbility);
245243

246244
if (user.isAdmin) {
247245
can(Action.Manage, 'all'); // read-write access to everything
@@ -263,11 +261,13 @@ export class CaslAbilityFactory {
263261

264262
> warning **Notice** `all` is a special keyword in CASL that represents "any subject".
265263
266-
> info **Hint** `Ability`, `AbilityBuilder`, `AbilityClass`, and `ExtractSubjectType` classes are exported from the `@casl/ability` package.
264+
> info **Hint** Since CASL v6, `MongoAbility` serves as the default ability class, replacing the legacy `Ability` to better support condition-based permissions using MongoDB-like syntax. Despite the name, it is not tied to MongoDB — it works with any kind of data by simply comparing objects against conditions written in Mongo-like syntax.
265+
266+
> info **Hint** `MongoAbility`, `AbilityBuilder`, `AbilityClass`, and `ExtractSubjectType` classes are exported from the `@casl/ability` package.
267267
268268
> 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/v6/en/guide/subject-type-detection#use-classes-as-subject-types) for details.
269269
270-
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 have 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/v6/en/guide/intro).
270+
In the example above, we created the `MongoAbility` instance using the `AbilityBuilder` class. As you probably guessed, `can` and `cannot` accept the same arguments but have 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/v6/en/guide/intro).
271271

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

@@ -297,7 +297,7 @@ if (ability.can(Action.Read, 'all')) {
297297
}
298298
```
299299

300-
> info **Hint** Learn more about the `Ability` class in the official [CASL documentation](https://casl.js.org/v6/en/guide/intro).
300+
> info **Hint** Learn more about the `MongoAbility` class in the official [CASL documentation](https://casl.js.org/v6/en/guide/intro).
301301
302302
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.
303303

@@ -311,7 +311,7 @@ ability.can(Action.Delete, Article); // false
311311
ability.can(Action.Create, Article); // false
312312
```
313313

314-
> info **Hint** Although both `Ability` and `AbilityBuilder` classes provide `can` and `cannot` methods, they have different purposes and accept slightly different arguments.
314+
> info **Hint** Although both `MongoAbility` and `AbilityBuilder` classes provide `can` and `cannot` methods, they have different purposes and accept slightly different arguments.
315315
316316
Also, as we have specified in our requirements, the user should be able to update its articles:
317317

@@ -329,7 +329,7 @@ article.authorId = 2;
329329
ability.can(Action.Update, article); // false
330330
```
331331

332-
As you can see, `Ability` instance allows us to check permissions in pretty readable way. Likewise, `AbilityBuilder` allows us to define permissions (and specify various conditions) in a similar fashion. To find more examples, visit the official documentation.
332+
As you can see, `MongoAbility` instance allows us to check permissions in pretty readable way. Likewise, `AbilityBuilder` allows us to define permissions (and specify various conditions) in a similar fashion. To find more examples, visit the official documentation.
333333

334334
#### Advanced: Implementing a `PoliciesGuard`
335335

0 commit comments

Comments
 (0)