django-casbin-auth


Namedjango-casbin-auth JSON
Version 1.5.0 PyPI version JSON
download
home_pageNone
SummaryAn authorization library that supports access control models like ACL, RBAC, ABAC in Django
upload_time2024-10-26 16:16:31
maintainerNone
docs_urlNone
authorNone
requires_python>=3.6
licenseApache 2.0
keywords casbin django acl rbac abac auth authz authorization access control permission
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Django Casbin Auth

[![tests](https://github.com/officialpycasbin/django-casbin-auth/actions/workflows/release.yml/badge.svg)](https://github.com/officialpycasbin/django-casbin-auth/actions/workflows/release.yml)
[![Coverage Status](https://coveralls.io/repos/github/officialpycasbin/django-casbin-auth/badge.svg?branch=master)](https://coveralls.io/github/officialpycasbin/django-casbin-auth?branch=master)
[![Version](https://img.shields.io/pypi/v/django-casbin-auth.svg)](https://pypi.org/project/django-casbin-auth/)
[![Download](https://img.shields.io/pypi/dm/django-casbin-auth.svg)](https://pypi.org/project/django-casbin-auth/)
[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN)

django-casbin-auth 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 Casbin Auth](#django-casbin-auth)
  * [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-casbin-auth
```

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-casbin-auth, we made a django-app based on django-casbin-auth, you can see it in [django-casbin-auth-example](https://github.com/officialpycasbin/django-casbin-auth-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-casbin-auth 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-casbin-auth",
    "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": "Casbin <admin@casbin.org>",
    "download_url": "https://files.pythonhosted.org/packages/54/e9/8a168324e07c114c1a9c25cf1516dc39fd9b104279a070895b4631109b3b/django_casbin_auth-1.5.0.tar.gz",
    "platform": null,
    "description": "# Django Casbin Auth\n\n[![tests](https://github.com/officialpycasbin/django-casbin-auth/actions/workflows/release.yml/badge.svg)](https://github.com/officialpycasbin/django-casbin-auth/actions/workflows/release.yml)\n[![Coverage Status](https://coveralls.io/repos/github/officialpycasbin/django-casbin-auth/badge.svg?branch=master)](https://coveralls.io/github/officialpycasbin/django-casbin-auth?branch=master)\n[![Version](https://img.shields.io/pypi/v/django-casbin-auth.svg)](https://pypi.org/project/django-casbin-auth/)\n[![Download](https://img.shields.io/pypi/dm/django-casbin-auth.svg)](https://pypi.org/project/django-casbin-auth/)\n[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN)\n\ndjango-casbin-auth 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 Casbin Auth](#django-casbin-auth)\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-casbin-auth\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-casbin-auth, we made a django-app based on django-casbin-auth, you can see it in [django-casbin-auth-example](https://github.com/officialpycasbin/django-casbin-auth-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-casbin-auth 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.5.0",
    "project_urls": {
        "Home-page": "https://github.com/officialpycasbin/django-casbin-auth"
    },
    "split_keywords": [
        "casbin",
        " django",
        " acl",
        " rbac",
        " abac",
        " auth",
        " authz",
        " authorization",
        " access control",
        " permission"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "584fa77e42489d27a8f9d042ecec7855a3b381be176028e041346fe9b0c56478",
                "md5": "1c3984f642b0500ab2b8a692efa8fb25",
                "sha256": "90e0db0abd4c9e92a42200e0146f76216425ddc609e3f8f1e5ebed1527d936d4"
            },
            "downloads": -1,
            "filename": "django_casbin_auth-1.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1c3984f642b0500ab2b8a692efa8fb25",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 409121,
            "upload_time": "2024-10-26T16:16:29",
            "upload_time_iso_8601": "2024-10-26T16:16:29.033878Z",
            "url": "https://files.pythonhosted.org/packages/58/4f/a77e42489d27a8f9d042ecec7855a3b381be176028e041346fe9b0c56478/django_casbin_auth-1.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "54e98a168324e07c114c1a9c25cf1516dc39fd9b104279a070895b4631109b3b",
                "md5": "0960673d2951dc4e13ad8dc2a4873690",
                "sha256": "eab37931ce42a1837f34af6f5e88d41f9d13226caca9e182b83784531691ea01"
            },
            "downloads": -1,
            "filename": "django_casbin_auth-1.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "0960673d2951dc4e13ad8dc2a4873690",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 373393,
            "upload_time": "2024-10-26T16:16:31",
            "upload_time_iso_8601": "2024-10-26T16:16:31.577112Z",
            "url": "https://files.pythonhosted.org/packages/54/e9/8a168324e07c114c1a9c25cf1516dc39fd9b104279a070895b4631109b3b/django_casbin_auth-1.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-26 16:16:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "officialpycasbin",
    "github_project": "django-casbin-auth",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "django-casbin-auth"
}
        
Elapsed time: 0.66173s