
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
An authorization library that supports access control models like ACL, RBAC, ABAC in Typescript
News: still worry about how to write the correct casbin.js
policy? Casbin online editor is coming to help!
casbin.js
is a powerful and efficient open-source access control library for TypeScript/JavaScript projects. It provides support for enforcing authorization based on various access control models.
![]() | ![]() | ![]() | ![]() |
---|---|---|---|
Casbin | jCasbin | node-Casbin | PHP-Casbin |
production-ready | production-ready | production-ready | production-ready |
![]() | ![]() | ![]() | ![]() |
---|---|---|---|
PyCasbin | Casbin.NET | Casbin-CPP | Casbin-RS |
production-ready | production-ready | beta-test | production-ready |
https://casbin.org/docs/en/overview
# NPM
npm install casbin.js@next --save
# Yarn
yarn add casbin.js@next
New a casbin.js
enforcer with a model string and a memory policy, see Model section for details:
import { newEnforcer, newModel, MemoryAdapter } from 'casbin.js';
const model = newModel(`
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[role_definition]
g = _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
`);
const adapter = new MemoryAdapter(`
p, alice, data1, read
p, bob, data2, write
p, data2_admin, data2, read
p, data2_admin, data2, write
g, alice, data2_admin
`);
const enforcer = await newEnforcer(model, adapter);
Note: you can also initialize an enforcer with policy in DB instead of file, see Persistence section for details.
Add an enforcement hook into your code right before the access happens:
const sub = 'alice'; // the user that wants to access a resource.
const obj = 'data1'; // the resource that is going to be accessed.
const act = 'read'; // the operation that the user performs on the resource.
// Async:
const res = await enforcer.enforce(sub, obj, act);
// Sync:
// const res = enforcer.enforceSync(sub, obj, act);
if (res) {
// permit alice to read data1
} else {
// deny the request, show an error
}
Besides the static policy file, casbin.js
also provides API for permission management at run-time.
For example, You can get all the roles assigned to a user as below:
const roles = await enforcer.getRolesForUser('alice');
See Policy management APIs for more usage.
Casbin provides two sets of APIs to manage permissions:
https://casbin.org/docs/en/supported-models
https://casbin.org/docs/en/adapters
https://casbin.org/docs/en/watchers
https://casbin.org/docs/en/role-managers
This project exists thanks to all the people who contribute.
Thank you to all our backers! 🙏 [Become a backer]
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
This project is licensed under the Apache 2.0 license.
If you have any issues or feature requests, please contact us. PR is welcomed.
FAQs
[](https://www.npmjs.com/package/casbin.js) [](https://github.com/casbin/casbin.js/action
The npm package casbin.js receives a total of 2,497 weekly downloads. As such, casbin.js popularity was classified as popular.
We found that casbin.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.