Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

nestjs CASL

type Subjects = InferSubjects<typeof Article | typeof User> | 'all';

export type AppAbility = Ability<[Action, Subjects]>;

@Injectable()
export class CaslAbilityFactory {
  createForUser(user: User) {
    const { can, cannot, build } = new AbilityBuilder<
      Ability<[Action, Subjects]>
    >(Ability as AbilityClass<AppAbility>);

    if (user.isAdmin) {
      can(Action.Manage, 'all'); // read-write access to everything
    } else {
      can(Action.Read, 'all'); // read-only access to everything
    }

    can(Action.Update, Article, { authorId: user.id });
    cannot(Action.Delete, Article, { isPublished: true });

    return build({
      // Read https://casl.js.org/v5/en/guide/subject-type-detection#use-classes-as-subject-types for details
      detectSubjectType: (item) =>
        item.constructor as ExtractSubjectType<Subjects>,
    });
  }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: angular mailto on button click 
Typescript :: promise.all does not wait 
Typescript :: transport unknown socket.io 
Typescript :: type script encode url 
Typescript :: check all elements in list are false python 
Typescript :: how to update typescript in global 
Typescript :: failed prop type: the prop `startdateid` is marked as required in `withstyles(daterangepicker)`, but its value is `undefined`. 
Typescript :: difference between statistical learning and machine learning 
Typescript :: typescript enum to string 
Typescript :: install snowpack 
Typescript :: how to remove last 2 elements from list in python 
Typescript :: mysqli_real_escape_string() expects parameter 1 to be mysqli 
Typescript :: nodejs exec exit code 
Typescript :: change url param angular 
Typescript :: typescript comments 
Typescript :: woocommerce change related products tect 
Typescript :: Error: Missing "key" prop for element in iterator 
Typescript :: android studio loop through all objects in layout 
Typescript :: clone a list typescript 
Typescript :: typescript check type of variable 
Typescript :: multer s3 
Typescript :: abosulute cell refrence in google sheet 
Typescript :: activate jquery in typescript 
Typescript :: common mistakes 
Typescript :: whats $_.FullName in powershell 
Typescript :: react-excel-renderer nextjs error 
Typescript :: basic variable types typescript 
Typescript :: check if file exists on s3 python 
Typescript :: pagination in typescript 
Typescript :: Custom validation for phone-number using class-validator package 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =