npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@casl/angular

v8.2.8

Published

Angular module for CASL which makes it easy to add permissions in any Angular app

Downloads

39,391

Readme

CASL Angular

@casl/angular NPM version CASL Join the chat

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.

Installation

npm install @casl/angular @casl/ability
# or
yarn add @casl/angular @casl/ability
# or
pnpm add @casl/angular @casl/ability

Configure AppModule

To add pipes into your application's templates, you need to import AbilityModule in your AppModule and

import { NgModule } from '@angular/core';
import { AbilityModule } from '@casl/angular';
import { Ability, PureAbility } from '@casl/ability';

@NgModule({
  imports: [
    // other modules
    AbilityModule
  ],
  providers: [
    { provide: Ability, useValue: new Ability() },
    { provide: PureAbility, useExisting: Ability }
  ]
  // other properties
})
export class AppModule {}

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).

Read CASL and TypeScript to get more details about Ability type configuration.

Update Ability instance

Majority of applications that need permission checking support have something like AuthService or LoginService or Session service (name it as you wish) which is responsible for user login/logout functionality. Whenever user login (and logout), we need to update Ability instance with new rules.

Let's imagine that server returns user with a role on login:

import { Ability, AbilityBuilder } from '@casl/ability';
import { Injectable } from '@angular/core';

@Injectable({ provideIn: 'root' })
export class Session {
  private token: string

  constructor(private ability: Ability) {}

  login(details) {
    const params = { method: 'POST', body: JSON.stringify(details) };
    return fetch('path/to/api/login', params)
      .then(response => response.json())
      .then((session) => {
        this.updateAbility(session.user);
        this.token = session.token;
      });
  }

  private updateAbility(user) {
    const { can, rules } = new AbilityBuilder(Ability);

    if (user.role === 'admin') {
      can('manage', 'all');
    } else {
      can('read', 'all');
    }

    this.ability.update(rules);
  }

  logout() {
    this.token = null;
    this.ability.update([]);
  }
}

See Define rules to get more information of how to define Ability

Then use this Session service in LoginComponent:

import { Component } from '@angular/core';
import { Session } from '../services/Session';

@Component({
  selector: 'login-form',
  template: `
    <form (ngSubmit)="login()">
      <input type="email" [(ngModel)]="email" />
      <input type="password" [(ngModel)]="password" />
      <button type="submit">Login</button>
    </form>
  `
})
export class LoginForm {
  email: string;
  password: string;

  constructor(private session: Session) {}

  login() {
    const { email, password } = this;
    return this.session.login({ email, password });
  }
}

Check permissions in templates using AbilityService

AbilityService is a service that provides ability$ observable. This observable injects provided in DI PureAbility instance and emits it each time its rules are changed. This allows efficiently use permissions checks, especially in case we use ChangeDetectionStrategy.OnPush.

Let's first see how it can be used in any component:

@Component({
  selector: 'my-home',
  template: `
    <ng-container *ngIf="ability$ | async as ability">
      <h1>Home Page</h1>
      <button *ngIf="ability.can('create', 'Post')">Create Post</button>
    </ng-container>
  `
})
export class HomeComponent {
  readonly ability$: Observable<AppAbility>;

  constructor(abilityService: AbilityService<AppAbility>) {
    this.ability$ = abilityService.ability$;
  }
}

It also can be safely used inside *ngFor and other directives. If we use ChangeDetectionStrategy.OnPush, it will give us additional performance improvements because ability.can(...) won't be called without a need.

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.

Note: provide this service at root injector level as we need only 1 instance of it.

But let's also see how we can do permission checks using pipes and what are performance implications of that:

Check permissions in templates using pipe

To check permissions in any template you can use AblePipe:

<div *ngIf="'create' | able: 'Post'">
  <a (click)="createPost()">Add Post</a>
</div>

Why pipe and not directive?

Directive cannot be used to pass values into inputs of other components. For example, we need to enable or disable a button based on user's ability to create a post. With directive we cannot do this but we can do this with pipe:

<button [disabled]="!('create' | able: 'Post')">Add Post</button>

Performance considerations

There are 2 pipes in @casl/angular:

  • able - impure pipe
  • ablePure - pure pipe

So, when should we use which?

If you are in doubt, then use ablePure for action and subject type checks, and able for all others

According to Angular documentation pure pipes are called only if their arguments are changed. This means that you can't use mutable objects with pure pipes because changes in that objects don't trigger pure pipe re-evaluation. But a good thing is that Angular creates only single instance of a pure pipe for the whole app and reuses it across components, this way it safes component instantiation time and memory footprint.

Due to open feature in Angular, we need to pass the result of ablePure pipe to async pipe. So, instead of

<div *ngIf="'create' | ablePure: 'Todo'">...</div>

we need to write:

<div *ngIf="'create' | ablePure: 'Todo' | async">...</div>

ablePure pipe returns an Observable<boolean>, so async pipe can effectively unwrap it

For apps that mutate application state, we need to use impure able pipe as it can detect changes in object properties. Don't worry, checks by action and subject type are very fast and are done in O(1) time. The performance of checks by action and subject object are a bit slower and depend on the amount of rules for a particular subject type and used conditions but usually this won't become a bottle neck for the app.

TypeScript support

This package is written in TypeScript, so it will warn you about wrong usage.

It may be a bit tedious to use application specific abilities in Angular app because everywhere you inject Ability instance you will need to import its generic parameters:

import { Ability } from '@casl/ability';
import { Component } from '@angular/core';
import { AppAbilities } from '../services/AppAbility';

@Component({
  selector: 'todo-item'
})
export class TodoItem {
  constructor(
    private ability: Ability<AppAbilities>
  ) {}
}

To make the life easier, you can use AbilityClass<TAbility> class to utilize Companion object pattern:

import { Ability, AbilityClass } from '@casl/ability';

type Actions = 'create' | 'read' | 'update' | 'delete';
type Subjects = 'Article' | 'User';

export type AppAbility = Ability<[Actions, Subjects]>;
export const AppAbility = Ability as AbilityClass<AppAbility>;

And use AppAbility everywhere in your app:

import { NgModule } from '@angular/core';
import { AppAbility } from './services/AppAbility';

@NgModule({
  // other configuration
  providers: [
    { provide: AppAbility, useValue: new AppAbility() },
    { provide: PureAbility, useExisting: AppAbility },
  ]
})
export class AppModule {}

Want to help?

Want to file a bug, contribute some code, or improve documentation? Excellent! Read up on guidelines for contributing.

If you'd like to help us sustain our community and project, consider to become a financial contributor on Open Collective

See Support CASL for details

License

MIT License