Package: django-authorization
Django AuthorizationEnglish |中文Django-authorization is an authorization library for Django framework.Based onCasbinandDjango-casbin(middleware, light weight of this plugin), an authorization library that that supports access control models like ACL, RBAC, ABAC.Django AuthorizationInstallation and ConfigureUsageSome Important Concepts:Middleware UsageDecorator UsageCommand Line UsageLicenseInstallation and Configurepip install django-authorizationWe recommend that you first configure the adapter for persistent storage of the policy, such as:django-orm-adapter, After integrating it into the project continue with the configuration of django-authrization# 1. Add the app to INSTALLED_APPSINSTALLED_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 dauthzDAUTHZ={# 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 indjango-authorization-exampleUsageSome Important Concepts:such as .conf file, policy, sub, obj, act, please refer to thecasbin websiteMiddleware Usage# Install middleware for django-authorization as requiredMIDDLEWARE=["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 UsageRequest decorator will check the authorization status of user, path, method# use request decorator@request_decoratordefsome_view(request):returnHttpResponse("Hello World")Enforcer decorator will check the authorization status of user, obj, edit. example:# use enforcer decorator# sub: user in request obj: "artical" act: "edit"@enforcer_decorator("artical","edit")defsome_view(request):returnHttpResponse("Hello World")Command Line UsageThe 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.Add/Getpolicy,usage:pythonmanage.pypolicy[opt:--enforcer=<enforcer_name>]add<sub><obj><act>
pythonmanage.pypolicy[opt:--enforcer=<enforcer_name>]get<sub><obj><act>

Add/Getroletouser,usage:pythonmanage.pyrole[opt:--enforcer=<enforcer_name>]add<user><role>
pythonmanage.pyrole[opt:--enforcer=<enforcer_name>]get<user>

Add/Getgrouppolicy,usage:
pythonmanage.pygroup[opt:--enforcer=<enforcer_name>]add<user><role>[opt:<domain>]pythonmanage.pygroup[opt:--enforcer=<enforcer_name>]get<user><role>[opt:<domain>]Backend UsageYou can integrate Pycasbin withDjango authentication system. For more usage, you can refer totests/test_backend.py. To enable the backend, you need to specify it insettings.py.AUTHENTICATION_BACKENDS=["dauthz.backends.CasbinBackend","django.contrib.auth.backends.ModelBackend",]Note that you still need to add permissions for users with pycasbinadd_policy()due to the mechanism of the django permission system.LicenseThis project is licensed under theApache 2.0 license.