CASL is an isomorphic authorization JavaScript library which restricts what resources a given user is allowed to access
CASL is an isomorphic authorization JavaScript library which restricts what resources a given user is allowed to access
CASL (pronounced /ˈkæsəl/, like castle) is an isomorphic authorization JavaScript library which restricts what resources a given user is allowed to access. It's designed to be incrementally adoptable and can easily scale between a simple claim based and fully featured subject and attribute based authorization. It makes it easy to manage and share permissions across UI components, API services, and database queries.
Heavily inspired by cancan.
| Project | Status | Description | Supported environments |
|---|---|---|---|
| @casl/ability | CASL's core package | Node.js 18+, browsers IE 11+ (uses Map/Set) |
|
| @casl/mongoose | integration with Mongoose | Node.js 20 | |
| @casl/prisma | integration with Prisma | Node.js 20+ | |
| @casl/angular | integration with Angular | Node.js 20+, browsers IE 11+ | |
| @casl/react | integration with React | Node.js 20+, browsers IE 11+ | |
| @casl/vue | integration with Vue | Node.js 20+, browsers with Symbol support |
A lot of detailed information about CASL, integrations and examples can be found in documentation.
Ask it in support chat or on stackoverflow. Please don't ask questions in issues, the issue list of this repo is exclusively for bug reports and feature requests. Questions in the issue list may be closed immediately without answers.
CASL operates on the abilities level, that is what a user can actually do in the application. An ability itself depends on the 4 parameters (last 3 are optional):
prolong, read). Very often it will be a list of words from CRUD - create, read, update and delete.Subscription, BlogPost, User).BlogPost)hidden field of BlogPost but not update description or title)Using CASL you can describe abilities using regular and inverted rules. Let's see how
Note: all the examples below will be written in TypeScript but CASL can be used in similar way in ES6+ and Nodejs environments.
Lets define Ability for a blog website where visitors:
…Do you see how easily business requirements were translated into CASL's rules?
Note: you can use class instead of string as a subject type (e.g., can('read', BlogPost))
And yes, Ability class allow you to use some MongoDB operators to define conditions. Don't worry if you don't know MongoDB, it's not required and explained in details in Defining Abilities
Later on you can check abilities by using can and cannot methods of Ability instance.
…Of course, you are not restricted to use only class instances in order to check permissions on objects. See Introduction for the detailed explanation.
CASL has a complementary package @casl/mongoose which provides easy integration with MongoDB and mongoose.
…See Database integration for details.
CASL is incrementally adoptable, that means you can start your project with simple claim (or action) based authorization and evolve it later, when your app functionality evolves.
CASL is composable, that means you can implement alternative conditions matching (e.g., based on joi, ajv or pure functions) and field matching (e.g., to support alternative syntax in fields like addresses.*.street or addresses[0].street) logic.
See Advanced usage for details.
Looking for examples? Check CASL examples repository.
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
This project exists thanks to all the people who contribute. [Contribute].
Become a financial contributor and help us sustain our community. [Contribute]
Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]
Copyright (c) 2017-present, Sergii Stotskyi
Dependency Dashboard
Support Prisma 8 in `@cals/prisma`
CASL's `$exists` behaves inconsistently with Mongo's `$exists`
Move `Subjects` helper to `@casl/ability` from `@casl/prisma`
Casl/Vue + Storybook unable to access $can global
Missing type-checking in classes with protected constructor
sql & sequelize support
CASL NestJS integration & CASL TypeORM integration
[Bug] Invalid union type for subject when subjects have different actions
[prisma] Using extensions to remove the need for `subject` helper with prisma