# Django Authorization
[![tests](https://github.com/pycasbin/django-authorization/actions/workflows/release.yml/badge.svg)](https://github.com/pycasbin/django-authorization/actions/workflows/release.yml)
[![Coverage Status](https://coveralls.io/repos/github/pycasbin/django-authorization/badge.svg?branch=master)](https://coveralls.io/github/pycasbin/django-authorization?branch=master)
[![Version](https://img.shields.io/pypi/v/django-authorization.svg)](https://pypi.org/project/django-authorization/)
[![Download](https://img.shields.io/pypi/dm/django-authorization.svg)](https://pypi.org/project/django-authorization/)
[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN)
Django-authorization is an authorization library for Django framework.
Based on [Casbin](https://github.com/casbin/pycasbin) and [Django-casbin](https://github.com/pycasbin/django-casbin) (middleware, light weight of this plugin), an authorization library that that supports access control models like ACL, RBAC, ABAC.
![image](https://user-images.githubusercontent.com/75596353/188881538-a6a99cb1-c88b-4738-bf4f-452be4fb7c2d.png)
- [Django Authorization](#django-authorization)
* [Installation and Configure](#installation-and-configure)
* [Usage](#usage)
+ [Some Important Concepts:](#some-important-concepts-)
+ [Middleware Usage](#middleware-usage)
+ [Decorator Usage](#decorator-usage)
+ [Command Line Usage](#command-line-usage)
* [License](#license)
## Installation and Configure
```
pip install django-authorization
```
We recommend that you first configure the adapter for persistent storage of the policy, such as:
[django-orm-adapter](https://github.com/pycasbin/django-orm-adapter), After integrating it into the project continue with the configuration of django-authrization
```python
# 1. Add the app to INSTALLED_APPS
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"dauthz.apps.DauthzConfig", # add this app to INSTALLED_APPS
]
# 2. Add configure of dauthz
DAUTHZ = {
# DEFAULT Dauthz enforcer
"DEFAULT": {
# Casbin model setting.
"MODEL": {
# Available Settings: "file", "text"
"CONFIG_TYPE": "file",
"CONFIG_FILE_PATH": Path(__file__).parent.joinpath("dauthz-model.conf"),
"CONFIG_TEXT": "",
},
# Casbin adapter .
"ADAPTER": {
"NAME": "casbin_adapter.adapter.Adapter",
# 'OPTION_1': '',
},
"LOG": {
# Changes whether Dauthz will log messages to the Logger.
"ENABLED": False,
},
},
}
```
to better prompt the configure method of django-authorization, we made a django-app based on django-authorization, you can see it in [django-authorization-example](https://github.com/pycasbin/django-authorization-example)
## Usage
### Some Important Concepts:
such as .conf file, policy, sub, obj, act, please refer to the [casbin website](https://casbin.org/)
### Middleware Usage
```python
# Install middleware for django-authorization as required
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"dauthz.middlewares.request_middleware.RequestMiddleware", # add the middleware
]
```
You can freely set the casbin enforcer for the middleware via API: set_enforcer_for_request_middleware(enforcer_name) and set_enforcer_for_enforcer_middleware(enforcer_name)
### Decorator Usage
Request decorator will check the authorization status of user, path, method
```python
# use request decorator
@request_decorator
def some_view(request):
return HttpResponse("Hello World")
```
Enforcer decorator will check the authorization status of user, obj, edit. example:
```python
# use enforcer decorator
# sub: user in request obj: "artical" act: "edit"
@enforcer_decorator("artical", "edit")
def some_view(request):
return HttpResponse("Hello World")
```
### Command Line Usage
The command line operation allows you to operate directly on the enforcer's database. Three sets of commands are available: policy commands, group commands and role commands.
```shell
Add/Get policy, usage:
python manage.py policy [opt: --enforcer=<enforcer_name>] add <sub> <obj> <act>
python manage.py policy [opt: --enforcer=<enforcer_name>] get <sub> <obj> <act>
Add/Get role to user, usage:
python manage.py role [opt: --enforcer=<enforcer_name>] add <user> <role>
python manage.py role [opt: --enforcer=<enforcer_name>] get <user>
Add/Get group policy, usage:
python manage.py group [opt: --enforcer=<enforcer_name>] add <user> <role> [opt:<domain>]
python manage.py group [opt: --enforcer=<enforcer_name>] get <user> <role> [opt:<domain>]
```
### Backend Usage
You can integrate Pycasbin with [Django authentication system](https://docs.djangoproject.com/en/4.2/topics/auth/default/#permissions-and-authorization). For more usage, you can refer to `tests/test_backend.py`. To enable the backend, you need to specify it in `settings.py`.
```python
AUTHENTICATION_BACKENDS = [
"dauthz.backends.CasbinBackend",
"django.contrib.auth.backends.ModelBackend",
]
```
Note that you still need to add permissions for users with pycasbin `add_policy()` due to the mechanism of the django permission system.
## License
This project is licensed under the [Apache 2.0 license](https://github.com/php-casbin/laravel-authz/blob/master/LICENSE).
Raw data
{
"_id": null,
"home_page": null,
"name": "django-authorization",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "casbin, django, acl, rbac, abac, auth, authz, authorization, access control, permission",
"author": null,
"author_email": "JonLee <leeqvip@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/05/4f/f57283c370748bf3a2602aeb3a3b8d26bad2083c20be41f10eb3ce6f4274/django_authorization-1.4.0.tar.gz",
"platform": null,
"description": "# Django Authorization\n\n[![tests](https://github.com/pycasbin/django-authorization/actions/workflows/release.yml/badge.svg)](https://github.com/pycasbin/django-authorization/actions/workflows/release.yml)\n[![Coverage Status](https://coveralls.io/repos/github/pycasbin/django-authorization/badge.svg?branch=master)](https://coveralls.io/github/pycasbin/django-authorization?branch=master)\n[![Version](https://img.shields.io/pypi/v/django-authorization.svg)](https://pypi.org/project/django-authorization/)\n[![Download](https://img.shields.io/pypi/dm/django-authorization.svg)](https://pypi.org/project/django-authorization/)\n[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN)\n\nDjango-authorization is an authorization library for Django framework.\n\nBased on [Casbin](https://github.com/casbin/pycasbin) and [Django-casbin](https://github.com/pycasbin/django-casbin) (middleware, light weight of this plugin), an authorization library that that supports access control models like ACL, RBAC, ABAC.\n\n![image](https://user-images.githubusercontent.com/75596353/188881538-a6a99cb1-c88b-4738-bf4f-452be4fb7c2d.png)\n\n- [Django Authorization](#django-authorization)\n * [Installation and Configure](#installation-and-configure)\n * [Usage](#usage)\n + [Some Important Concepts:](#some-important-concepts-)\n + [Middleware Usage](#middleware-usage)\n + [Decorator Usage](#decorator-usage)\n + [Command Line Usage](#command-line-usage)\n * [License](#license)\n\n## Installation and Configure\n\n```\npip install django-authorization\n```\n\nWe recommend that you first configure the adapter for persistent storage of the policy, such as: \n\n[django-orm-adapter](https://github.com/pycasbin/django-orm-adapter), After integrating it into the project continue with the configuration of django-authrization\n\n```python\n# 1. Add the app to INSTALLED_APPS\nINSTALLED_APPS = [\n \"django.contrib.admin\",\n \"django.contrib.auth\",\n \"django.contrib.contenttypes\",\n \"django.contrib.sessions\",\n \"django.contrib.messages\",\n \"django.contrib.staticfiles\",\n \"dauthz.apps.DauthzConfig\",\t# add this app to INSTALLED_APPS\n]\n\n# 2. Add configure of dauthz\nDAUTHZ = {\n # DEFAULT Dauthz enforcer\n \"DEFAULT\": {\n # Casbin model setting.\n \"MODEL\": {\n # Available Settings: \"file\", \"text\"\n \"CONFIG_TYPE\": \"file\",\n \"CONFIG_FILE_PATH\": Path(__file__).parent.joinpath(\"dauthz-model.conf\"),\n \"CONFIG_TEXT\": \"\",\n },\n # Casbin adapter .\n \"ADAPTER\": {\n \"NAME\": \"casbin_adapter.adapter.Adapter\",\n # 'OPTION_1': '',\n },\n \"LOG\": {\n # Changes whether Dauthz will log messages to the Logger.\n \"ENABLED\": False,\n },\n },\n}\n```\n\nto better prompt the configure method of django-authorization, we made a django-app based on django-authorization, you can see it in [django-authorization-example](https://github.com/pycasbin/django-authorization-example)\n\n## Usage\n\n### Some Important Concepts:\n\nsuch as .conf file, policy, sub, obj, act, please refer to the [casbin website](https://casbin.org/)\n\n### Middleware Usage\n\n```python\n# Install middleware for django-authorization as required\nMIDDLEWARE = [\n \"django.middleware.security.SecurityMiddleware\",\n \"django.contrib.sessions.middleware.SessionMiddleware\",\n \"django.middleware.common.CommonMiddleware\",\n \"django.middleware.csrf.CsrfViewMiddleware\",\n \"django.contrib.auth.middleware.AuthenticationMiddleware\",\n \"django.contrib.messages.middleware.MessageMiddleware\",\n \"django.middleware.clickjacking.XFrameOptionsMiddleware\",\n \"dauthz.middlewares.request_middleware.RequestMiddleware\",\t# add the middleware \n]\n```\n\nYou can freely set the casbin enforcer for the middleware via API: set_enforcer_for_request_middleware(enforcer_name) and set_enforcer_for_enforcer_middleware(enforcer_name)\n\n### Decorator Usage\n\nRequest decorator will check the authorization status of user, path, method\n\n```python\n# use request decorator\n@request_decorator\ndef some_view(request):\n return HttpResponse(\"Hello World\")\n```\n\nEnforcer decorator will check the authorization status of user, obj, edit. example: \n\n```python\n# use enforcer decorator\n# sub: user in request obj: \"artical\" act: \"edit\"\n@enforcer_decorator(\"artical\", \"edit\")\ndef some_view(request):\n return HttpResponse(\"Hello World\")\n```\n\n### Command Line Usage\n\nThe command line operation allows you to operate directly on the enforcer's database. Three sets of commands are available: policy commands, group commands and role commands.\n\n```shell\nAdd/Get policy, usage: \npython manage.py policy [opt: --enforcer=<enforcer_name>] add <sub> <obj> <act>\npython manage.py policy [opt: --enforcer=<enforcer_name>] get <sub> <obj> <act>\n\nAdd/Get role to user, usage: \npython manage.py role [opt: --enforcer=<enforcer_name>] add <user> <role>\npython manage.py role [opt: --enforcer=<enforcer_name>] get <user>\n\nAdd/Get group policy, usage:\npython manage.py group [opt: --enforcer=<enforcer_name>] add <user> <role> [opt:<domain>]\npython manage.py group [opt: --enforcer=<enforcer_name>] get <user> <role> [opt:<domain>]\n```\n\n### Backend Usage\n\nYou can integrate Pycasbin with [Django authentication system](https://docs.djangoproject.com/en/4.2/topics/auth/default/#permissions-and-authorization). For more usage, you can refer to `tests/test_backend.py`. To enable the backend, you need to specify it in `settings.py`.\n\n```python\nAUTHENTICATION_BACKENDS = [\n \"dauthz.backends.CasbinBackend\",\n \"django.contrib.auth.backends.ModelBackend\", \n ]\n```\n\nNote that you still need to add permissions for users with pycasbin `add_policy()` due to the mechanism of the django permission system.\n\n## License\n\nThis project is licensed under the [Apache 2.0 license](https://github.com/php-casbin/laravel-authz/blob/master/LICENSE).\n",
"bugtrack_url": null,
"license": "Apache 2.0",
"summary": "An authorization library that supports access control models like ACL, RBAC, ABAC in Django",
"version": "1.4.0",
"project_urls": {
"Home-page": "https://github.com/pycasbin/django-authorization"
},
"split_keywords": [
"casbin",
" django",
" acl",
" rbac",
" abac",
" auth",
" authz",
" authorization",
" access control",
" permission"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b25d36a5070aef2e5848ed2c308467c98767f5b9e883220b2998130997786fd6",
"md5": "dc633cd2e0a6c8c87c57337529928243",
"sha256": "c1f1ff481d7b88306948e63ee79cc4257b82ab57ff5c5c1cc9c48f58112ad814"
},
"downloads": -1,
"filename": "django_authorization-1.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "dc633cd2e0a6c8c87c57337529928243",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 405957,
"upload_time": "2024-04-24T15:30:24",
"upload_time_iso_8601": "2024-04-24T15:30:24.623579Z",
"url": "https://files.pythonhosted.org/packages/b2/5d/36a5070aef2e5848ed2c308467c98767f5b9e883220b2998130997786fd6/django_authorization-1.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "054ff57283c370748bf3a2602aeb3a3b8d26bad2083c20be41f10eb3ce6f4274",
"md5": "eab49d5faa35191cb13ab66762e275d2",
"sha256": "7ab9bf0ac908d27ea9365fbc2151f013fe70497253df411a35902bb3f48ba551"
},
"downloads": -1,
"filename": "django_authorization-1.4.0.tar.gz",
"has_sig": false,
"md5_digest": "eab49d5faa35191cb13ab66762e275d2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 370630,
"upload_time": "2024-04-24T15:30:26",
"upload_time_iso_8601": "2024-04-24T15:30:26.346983Z",
"url": "https://files.pythonhosted.org/packages/05/4f/f57283c370748bf3a2602aeb3a3b8d26bad2083c20be41f10eb3ce6f4274/django_authorization-1.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-24 15:30:26",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "pycasbin",
"github_project": "django-authorization",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "casbin",
"specs": [
[
">=",
"1.17.0"
]
]
},
{
"name": "Django",
"specs": [
[
">=",
"3.0.0"
]
]
}
],
"lcname": "django-authorization"
}