django-permission2
- Author
Malte Gerth <mail@malte-gerth.de>
- Original Author
Alisue <lambdalisue@hashnote.net>
- Supported python versions
Python 3.8, 3.9, 3.10, 3.11
- Supported django versions
Django 2.2, 3.2, 4.0, 4.1, 4.2
An enhanced permission library which enables a logic-based permission system to handle complex permissions in Django.
Documentation
Installation
Use pip like:
$ pip install django-permission2
Usage
The following might help you to understand as well.
Configuration
Add
permission
to theINSTALLED_APPS
in your settings moduleINSTALLED_APPS = ( # ... 'permission', )
Add our extra authorization/authentication backend
AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', # default 'permission.backends.PermissionBackend', )
Follow the instructions below to apply logical permissions to django models
Quick tutorial
Let’s assume you wrote an article model which has an author
attribute to store the creator of the article, and you want to give that author full control permissions
(e.g. add, change and delete permissions).
Add
import permission; permission.autodiscover()
to yoururls.py
like:from django.conf.urls import patterns, include from django.urls import path from django.contrib import admin admin.autodiscover() # only add the following line import permission; permission.autodiscover() urlpatterns = [ path('admin/', include(admin.site.urls)), # ... ]
Write
perms.py
in your application directory like:from permission.logics import AuthorPermissionLogic from permission.logics import CollaboratorsPermissionLogic PERMISSION_LOGICS = ( ('your_app.Article', AuthorPermissionLogic()), ('your_app.Article', CollaboratorsPermissionLogic()), )
What you need to do is just applying permission.logics.AuthorPermissionLogic
to the Article
model like
from django.db import models
from django.contrib.auth.models import User
class Article(models.Model):
title = models.CharField('title', max_length=120)
body = models.TextField('body')
author = models.ForeignKey(User)
# this is just required for easy explanation
class Meta:
app_label='permission'
# apply AuthorPermissionLogic
from permission import add_permission_logic
from permission.logics import AuthorPermissionLogic
add_permission_logic(Article, AuthorPermissionLogic())
That’s it. Now the following codes will work as expected:
user1 = User.objects.create_user(
username='john',
email='john@test.com',
password='password',
)
user2 = User.objects.create_user(
username='alice',
email='alice@test.com',
password='password',
)
art1 = Article.objects.create(
title="Article 1",
body="foobar hogehoge",
author=user1
)
art2 = Article.objects.create(
title="Article 2",
body="foobar hogehoge",
author=user2
)
# You have to apply 'permission.add_article' to users manually because it
# is not an object permission.
from permission.utils.permissions import perm_to_permission
user1.user_permissions.add(perm_to_permission('permission.add_article'))
assert user1.has_perm('permission.add_article') == True
assert user1.has_perm('permission.change_article') == False
assert user1.has_perm('permission.change_article', art1) == True
assert user1.has_perm('permission.change_article', art2) == False
assert user2.has_perm('permission.add_article') == False
assert user2.has_perm('permission.delete_article') == False
assert user2.has_perm('permission.delete_article', art1) == False
assert user2.has_perm('permission.delete_article', art2) == True
License
The MIT License (MIT)
Copyright (c) 2022 Malte Gerth <mail@malte-gerth.de>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.