@casl/angular 9.0.0 → 9.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +35 -20
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  [![](https://img.shields.io/npm/dm/%40casl%2Fangular.svg)](https://www.npmjs.com/package/%40casl%2Fangular)
5
5
  [![CASL Join the chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/stalniy-casl/casl)
6
6
 
7
- This package allows to integrate `@casl/ability` with [Angular] application. It provides `AblePipe` and `AblePurePipe` to Angular templates, so you can show or hide components, buttons, etc based on user ability to see them.
7
+ This package allows to integrate `@casl/ability` with [Angular] application. It provides deprecated `AblePipe`, `AblePurePipe` and new `AbilityService` and `AbilityServiceSignal` to Angular templates, so you can show or hide components, buttons, etc based on user ability to see them.
8
8
 
9
9
  ## Installation
10
10
 
@@ -18,30 +18,27 @@ pnpm add @casl/angular @casl/ability
18
18
 
19
19
  ## Configure AppModule
20
20
 
21
- To add pipes into your application's templates, you need to import `AbilityModule` in your `AppModule` and
21
+ To add pipes into your application's templates, you need to import the one you need
22
22
 
23
23
  ```ts @{data-filename="app.module.ts"}
24
24
  import { NgModule } from '@angular/core';
25
- import { AbilityModule } from '@casl/angular';
26
- import { Ability, PureAbility } from '@casl/ability';
25
+ import { AblePipe } from '@casl/angular';
26
+ import { createMongoAbility, PureAbility } from '@casl/ability';
27
27
 
28
28
  @NgModule({
29
29
  imports: [
30
30
  // other modules
31
- AbilityModule
31
+ AblePipe
32
32
  ],
33
33
  providers: [
34
- { provide: Ability, useValue: new Ability() },
35
- { provide: PureAbility, useExisting: Ability }
34
+ { provide: PureAbility, useValue: createMongoAbility() }
36
35
  ]
37
36
  // other properties
38
37
  })
39
38
  export class AppModule {}
40
39
  ```
41
40
 
42
- The 2nd provider provides instance of `PureAbility`, so pipes can inject it later. This pipes inject `PureAbility` (not `Ability`) because this allows an application developer to decide how to configure actions, subjects and conditions. Also this is the only way to get maximum from tree shaking (e.g., if you don't need conditions you can use `PureAbility` and shrink @casl/ability size).
43
-
44
- > Read [CASL and TypeScript](https://casl.js.org/v5/en/advanced/typescript) to get more details about `Ability` type configuration.
41
+ > Read [CASL and TypeScript](https://casl.js.org/v5/en/advanced/typescript) to get more details about `MongoAbility` type configuration.
45
42
 
46
43
  ## Update Ability instance
47
44
 
@@ -50,14 +47,14 @@ Majority of applications that need permission checking support have something li
50
47
  Let's imagine that server returns user with a role on login:
51
48
 
52
49
  ```ts @{data-filename="Session.ts"}
53
- import { Ability, AbilityBuilder } from '@casl/ability';
50
+ import { PureAbility, AbilityBuilder } from '@casl/ability';
54
51
  import { Injectable } from '@angular/core';
55
52
 
56
53
  @Injectable({ provideIn: 'root' })
57
54
  export class Session {
58
55
  private token: string
59
56
 
60
- constructor(private ability: Ability) {}
57
+ constructor(@Inject(PureAbility) private ability: MongoAbility) {}
61
58
 
62
59
  login(details) {
63
60
  const params = { method: 'POST', body: JSON.stringify(details) };
@@ -70,7 +67,7 @@ export class Session {
70
67
  }
71
68
 
72
69
  private updateAbility(user) {
73
- const { can, rules } = new AbilityBuilder(Ability);
70
+ const { can, rules } = new AbilityBuilder(createMongoAbility);
74
71
 
75
72
  if (user.role === 'admin') {
76
73
  can('manage', 'all');
@@ -148,11 +145,30 @@ It also can be safely used inside `*ngFor` and other directives. If we use `Chan
148
145
 
149
146
  This approach works good from performance point of view because it creates only single subscription per component (not per check as in case of `ablePure` pipe) and doesn't require our component to use `Default` or `OnPush` strategy.
150
147
 
151
- **Note**: provide this service at root injector level as we need only 1 instance of it.
152
-
153
148
  But let's also see how we can do permission checks using pipes and what are performance implications of that:
154
149
 
155
- ## Check permissions in templates using pipe
150
+ ## Signals support
151
+
152
+ The latest version of @casl/angular also supports new `signal`. To utilize it in your app, instead of `AbilityService` use `AbilityServiceSignal`:
153
+
154
+ ```ts
155
+ import { AbilityServiceSignal } from '@casl/angular';
156
+ import { AppAbility } from './AppAbility';
157
+
158
+ @Component({
159
+ selector: 'my-home',
160
+ template: `
161
+ <h1>Home Page</h1>
162
+ <button *ngIf="can('create', 'Post')">Create Post</button>
163
+ `
164
+ })
165
+ export class HomeComponent {
166
+ private readonly abilityService = inject<AbilityServiceSignal<AppAbility>>(AbilityServiceSignal);
167
+ protected readonly can = this.abilityService.can;
168
+ }
169
+ ```
170
+
171
+ ## Check permissions in templates using pipe (deprecated)
156
172
 
157
173
  To check permissions in any template you can use `AblePipe`:
158
174
 
@@ -228,8 +244,8 @@ import { Ability, AbilityClass } from '@casl/ability';
228
244
  type Actions = 'create' | 'read' | 'update' | 'delete';
229
245
  type Subjects = 'Article' | 'User';
230
246
 
231
- export type AppAbility = Ability<[Actions, Subjects]>;
232
- export const AppAbility = Ability as AbilityClass<AppAbility>;
247
+ export type AppAbility = MongoAbility<[Actions, Subjects]>;
248
+ export const AppAbility = PureAbility as AbilityClass<AppAbility>;
233
249
  ```
234
250
 
235
251
  And use `AppAbility` everywhere in your app:
@@ -241,8 +257,7 @@ import { AppAbility } from './services/AppAbility';
241
257
  @NgModule({
242
258
  // other configuration
243
259
  providers: [
244
- { provide: AppAbility, useValue: new AppAbility() },
245
- { provide: PureAbility, useExisting: AppAbility },
260
+ { provide: AppAbility, useValue: createMongoAbility() },
246
261
  ]
247
262
  })
248
263
  export class AppModule {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@casl/angular",
3
- "version": "9.0.0",
3
+ "version": "9.0.1",
4
4
  "description": "Angular module for CASL which makes it easy to add permissions in any Angular app",
5
5
  "main": "dist/umd/index.js",
6
6
  "module": "dist/es5m/index.js",