chore(release): 5.14.0-beta.4 [skip ci]

# [5.14.0-beta.4](https://github.com/casbin/node-casbin/compare/v5.14.0-beta.3...v5.14.0-beta.4) (2022-04-15)

### Features

* trigger new release ([7f19769](https://github.com/casbin/node-casbin/commit/7f19769fd5de806b6e30886c6b0e0a6a1035935a))
2 files changed
tree: 8c00b836a22772e6b8b1a31864c11cd7aaabbc51
  1. .github/
  2. examples/
  3. src/
  4. test/
  5. .eslintignore
  6. .eslintrc
  7. .gitattributes
  8. .gitignore
  9. .prettierrc
  10. .releaserc.json
  11. casbin-logo.png
  12. CHANGELOG.md
  13. jest.config.js
  14. LICENSE
  15. package.json
  16. README.md
  17. rollup.config.js
  18. tsconfig.cjs.json
  19. tsconfig.esm.json
  20. tsconfig.json
  21. yarn.lock
README.md

Node-Casbin

NPM version NPM download install size codebeat badge GitHub Actions Coverage Status Release Gitter

💖 Looking for an open-source identity and access management solution like Okta, Auth0, Keycloak ? Learn more about: Casdoor

News: still worry about how to write the correct node-casbin policy? Casbin online editor is coming to help!

casbin Logo

node-casbin is a powerful and efficient open-source access control library for Node.JS projects. It provides support for enforcing authorization based on various access control models.

All the languages supported by Casbin:

golangjavanodejsphp
CasbinjCasbinnode-CasbinPHP-Casbin
production-readyproduction-readyproduction-readyproduction-ready
pythondotnetc++rust
PyCasbinCasbin.NETCasbin-CPPCasbin-RS
production-readyproduction-readybeta-testproduction-ready

Documentation

https://casbin.org/docs/en/overview

Feature

  • 😎 Written in TypeScript to provide the type definitions
  • 🎯 Support multiple access model such as ACL, RBAC, ABAC
  • 🎮 Run everywhere on JavaScript platforms such as WEB, Node.js, React-Native, Electron, etc.

Installation

# NPM
npm install casbin@beta --save

# Yarn
yarn add casbin@beta

Get started

New an enforcer with a model string and a memory policy, see Model section for details:

import { newEnforcer, newModel, MemoryAdapter } from 'casbin';

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, node-casbin 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.

Policy management

Casbin provides two sets of APIs to manage permissions:

  • Management API: the primitive API that provides full support for Casbin policy management.
  • RBAC API: a more friendly API for RBAC. This API is a subset of Management API. The RBAC users could use this API to simplify the code.

Official Model

https://casbin.org/docs/en/supported-models

Policy persistence

https://casbin.org/docs/en/adapters

Policy consistence between multiple nodes

https://casbin.org/docs/en/watchers

Role manager

https://casbin.org/docs/en/role-managers

Contributors

This project exists thanks to all the people who contribute.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

License

This project is licensed under the Apache 2.0 license.

Contact

If you have any issues or feature requests, please contact us. PR is welcomed.