UNPKG

8.99 kBMarkdownView Raw
1# Node-Casbin
2
3[![NPM version][npm-image]][npm-url]
4[![NPM download][download-image]][download-url]
5[![install size](https://packagephobia.now.sh/badge?p=casbin)](https://packagephobia.now.sh/result?p=casbin)
6[![codebeat badge](https://codebeat.co/badges/c17c9ee1-da42-4db3-8047-9574ad2b23b1)](https://codebeat.co/projects/github-com-casbin-node-casbin-master)
7[![Build Status](https://travis-ci.org/casbin/node-casbin.svg?branch=master)](https://travis-ci.org/casbin/node-casbin)
8[![Coverage Status](https://coveralls.io/repos/github/casbin/node-casbin/badge.svg?branch=master)](https://coveralls.io/github/casbin/node-casbin?branch=master)
9[![Release](https://img.shields.io/github/release/casbin/node-casbin.svg)](https://github.com/casbin/node-casbin/releases/latest)
10[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/casbin/lobby)
11
12[npm-image]: https://img.shields.io/npm/v/casbin.svg?style=flat-square
13[npm-url]: https://npmjs.org/package/casbin
14[download-image]: https://img.shields.io/npm/dm/casbin.svg?style=flat-square
15[download-url]: https://npmjs.org/package/casbin
16
17**News**: still worry about how to write the correct `node-casbin` policy? [Casbin online editor](http://casbin.org/en/editor) is coming to help!
18
19![casbin Logo](casbin-logo.png)
20
21`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](https://en.wikipedia.org/wiki/Computer_security_model).
22
23## All the languages supported by Casbin:
24
25| [![golang](https://casbin.org/img/langs/golang.png)](https://github.com/casbin/casbin) | [![java](https://casbin.org/img/langs/java.png)](https://github.com/casbin/jcasbin) | [![nodejs](https://casbin.org/img/langs/nodejs.png)](https://github.com/casbin/node-casbin) | [![php](https://casbin.org/img/langs/php.png)](https://github.com/php-casbin/php-casbin) |
26| -------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
27| [Casbin](https://github.com/casbin/casbin) | [jCasbin](https://github.com/casbin/jcasbin) | [node-Casbin](https://github.com/casbin/node-casbin) | [PHP-Casbin](https://github.com/php-casbin/php-casbin) |
28| production-ready | production-ready | production-ready | production-ready |
29
30| [![python](https://casbin.org/img/langs/python.png)](https://github.com/casbin/pycasbin) | [![dotnet](https://casbin.org/img/langs/dotnet.png)](https://github.com/casbin-net/Casbin.NET) | [![c++](https://casbin.org/img/langs/cpp.png)](https://github.com/casbin/casbin-cpp) | [![rust](https://casbin.org/img/langs/rust.png)](https://github.com/casbin/casbin-rs) |
31| ---------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------- |
32| [PyCasbin](https://github.com/casbin/pycasbin) | [Casbin.NET](https://github.com/casbin-net/Casbin.NET) | [Casbin-CPP](https://github.com/casbin/casbin-cpp) | [Casbin-RS](https://github.com/casbin/casbin-rs) |
33| production-ready | production-ready | beta-test | production-ready |
34
35## Documentation
36
37https://casbin.org/docs/en/overview
38
39## Installation
40
41```shell script
42# NPM
43npm install casbin --save
44
45# Yarn
46yarn add casbin
47```
48
49## Get started
50
51New a `node-casbin` enforcer with a model file and a policy file, see [Model](#official-model) section for details:
52
53```typescript
54import { newEnforcer } from 'casbin';
55const enforcer = await newEnforcer('basic_model.conf', 'basic_policy.csv');
56```
57
58> **Note**: you can also initialize an enforcer with policy in DB instead of file, see [Persistence](#policy-persistence) section for details.
59
60Add an enforcement hook into your code right before the access happens:
61
62```typescript
63const sub = 'alice'; // the user that wants to access a resource.
64const obj = 'data1'; // the resource that is going to be accessed.
65const act = 'read'; // the operation that the user performs on the resource.
66
67const res = await enforcer.enforce(sub, obj, act);
68if (res) {
69 // permit alice to read data1
70} else {
71 // deny the request, show an error
72}
73```
74
75Besides the static policy file, `node-casbin` also provides API for permission management at run-time.
76For example, You can get all the roles assigned to a user as below:
77
78```typescript
79const roles = await enforcer.getRolesForUser('alice');
80```
81
82See [Policy management APIs](#policy-management) for more usage.
83
84## Policy management
85
86Casbin provides two sets of APIs to manage permissions:
87
88- [Management API](https://casbin.org/docs/en/management-api): the primitive API that provides full support for Casbin policy management.
89- [RBAC API](https://casbin.org/docs/en/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.
90
91## Official Model
92
93https://casbin.org/docs/en/supported-models
94
95## Policy persistence
96
97https://casbin.org/docs/en/adapters
98
99## Policy consistence between multiple nodes
100
101https://casbin.org/docs/en/watchers
102
103## Role manager
104
105https://casbin.org/docs/en/role-managers
106
107## Contributors
108
109This project exists thanks to all the people who contribute.
110<a href="https://github.com/casbin/node-casbin/graphs/contributors"><img src="https://opencollective.com/node-casbin/contributors.svg?width=890&button=false" /></a>
111
112## Backers
113
114Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/casbin#backer)]
115
116<a href="https://opencollective.com/casbin#backers" target="_blank"><img src="https://opencollective.com/casbin/backers.svg?width=890"></a>
117
118## Sponsors
119
120Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/casbin#sponsor)]
121
122<a href="https://opencollective.com/casbin/sponsor/0/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/0/avatar.svg"></a>
123<a href="https://opencollective.com/casbin/sponsor/1/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/1/avatar.svg"></a>
124<a href="https://opencollective.com/casbin/sponsor/2/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/2/avatar.svg"></a>
125<a href="https://opencollective.com/casbin/sponsor/3/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/3/avatar.svg"></a>
126<a href="https://opencollective.com/casbin/sponsor/4/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/4/avatar.svg"></a>
127<a href="https://opencollective.com/casbin/sponsor/5/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/5/avatar.svg"></a>
128<a href="https://opencollective.com/casbin/sponsor/6/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/6/avatar.svg"></a>
129<a href="https://opencollective.com/casbin/sponsor/7/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/7/avatar.svg"></a>
130<a href="https://opencollective.com/casbin/sponsor/8/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/8/avatar.svg"></a>
131<a href="https://opencollective.com/casbin/sponsor/9/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/9/avatar.svg"></a>
132
133## License
134
135This project is licensed under the [Apache 2.0 license](LICENSE).
136
137## Contact
138
139If you have any issues or feature requests, please contact us. PR is welcomed.
140
141- https://github.com/casbin/node-casbin/issues
142- hsluoyz@gmail.com
143- Tencent QQ group: [546057381](//shang.qq.com/wpa/qunwpa?idkey=8ac8b91fc97ace3d383d0035f7aa06f7d670fd8e8d4837347354a31c18fac885)