Skip to content

Commit bec8ee8

Browse files
committed
Add README.
1 parent a413550 commit bec8ee8

1 file changed

Lines changed: 66 additions & 1 deletion

File tree

README.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,66 @@
1-
# django-casbin
1+
# django-casbin
2+
3+
[![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin)
4+
5+
django-casbin is an authorization middleware for [Django](https://www.djangoproject.com/), it's based on [PyCasbin](https://github.com/casbin/pycasbin).
6+
7+
## Installation
8+
9+
```
10+
pip install django-casbin
11+
```
12+
13+
## Simple Example
14+
15+
- Add the middleware to your Django app's ``settings.py``:
16+
17+
```python
18+
MIDDLEWARE = [
19+
'django.middleware.security.SecurityMiddleware',
20+
'django.contrib.sessions.middleware.SessionMiddleware',
21+
'django.middleware.common.CommonMiddleware',
22+
'django.middleware.csrf.CsrfViewMiddleware',
23+
'django.contrib.auth.middleware.AuthenticationMiddleware',
24+
'django.contrib.messages.middleware.MessageMiddleware',
25+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
26+
'casbin_middleware.middleware.CasbinMiddleware', # Add this line, must after AuthenticationMiddleware.
27+
]
28+
```
29+
30+
- In ``casbin_middleware/middleware.py``:
31+
32+
```python
33+
import casbin
34+
35+
def __init__(self, get_response):
36+
self.get_response = get_response
37+
# load the casbin model and policy from files, database is also supported.
38+
self.enforcer = casbin.Enforcer("casbin_middleware/authz_model.conf", "casbin_middleware/authz_policy.csv")
39+
40+
def check_permission(self, request):
41+
# check the permission.
42+
user = request.user.username
43+
if request.user.is_anonymous:
44+
user = 'anonymous'
45+
path = request.path
46+
method = request.method
47+
return self.enforcer.enforce(user, path, method)
48+
```
49+
50+
## Documentation
51+
52+
The authorization determines a request based on ``{subject, object, action}``, which means what ``subject`` can perform what ``action`` on what ``object``. In this plugin, the meanings are:
53+
54+
1. ``subject``: the logged-in user name
55+
2. ``object``: the URL path for the web resource like "dataset1/item1"
56+
3. ``action``: HTTP method like GET, POST, PUT, DELETE, or the high-level actions you defined like "read-file", "write-blog"
57+
58+
For how to write authorization policy and other details, please refer to [the Casbin's documentation](https://casbin.org).
59+
60+
## Getting Help
61+
62+
- [Casbin](https://casbin.org)
63+
64+
## License
65+
66+
This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.

0 commit comments

Comments
 (0)