Package: django-permission2
AuthorMalte Gerth <mail@malte-gerth.de>Original AuthorAlisue <lambdalisue@hashnote.net>Supported python versionsPython 3.8, 3.9, 3.10, 3.11Supported django versionsDjango 2.2, 3.2, 4.0, 4.1, 4.2An enhanced permission library which enables alogic-based permission systemto handle complex permissions in Django.Documentationhttp://django-permission2.readthedocs.org/InstallationUsepiplike:$ pip install django-permission2UsageThe following might help you to understand as well.Basic strategy or so on,Issue #28Advanced usage and examples,Issue #26ConfigurationAddpermissionto theINSTALLED_APPSin your settings
moduleINSTALLED_APPS=(# ...'permission',)Add our extra authorization/authentication backendAUTHENTICATION_BACKENDS=('django.contrib.auth.backends.ModelBackend',# default'permission.backends.PermissionBackend',)Follow the instructions below to apply logical permissions to django modelsQuick tutorialLet’s assume you wrote an article model which has anauthorattribute to store the creator of the article, and you want to give that author full control permissions
(e.g. add, change and delete permissions).Addimport permission; permission.autodiscover()to yoururls.pylike:fromdjango.conf.urlsimportpatterns,includefromdjango.urlsimportpathfromdjango.contribimportadminadmin.autodiscover()# only add the following lineimportpermission;permission.autodiscover()urlpatterns=[path('admin/',include(admin.site.urls)),# ...]Writeperms.pyin your application directory like:frompermission.logicsimportAuthorPermissionLogicfrompermission.logicsimportCollaboratorsPermissionLogicPERMISSION_LOGICS=(('your_app.Article',AuthorPermissionLogic()),('your_app.Article',CollaboratorsPermissionLogic()),)What you need to do is just applyingpermission.logics.AuthorPermissionLogicto theArticlemodel likefromdjango.dbimportmodelsfromdjango.contrib.auth.modelsimportUserclassArticle(models.Model):title=models.CharField('title',max_length=120)body=models.TextField('body')author=models.ForeignKey(User)# this is just required for easy explanationclassMeta:app_label='permission'# apply AuthorPermissionLogicfrompermissionimportadd_permission_logicfrompermission.logicsimportAuthorPermissionLogicadd_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.frompermission.utils.permissionsimportperm_to_permissionuser1.user_permissions.add(perm_to_permission('permission.add_article'))assertuser1.has_perm('permission.add_article')==Trueassertuser1.has_perm('permission.change_article')==Falseassertuser1.has_perm('permission.change_article',art1)==Trueassertuser1.has_perm('permission.change_article',art2)==Falseassertuser2.has_perm('permission.add_article')==Falseassertuser2.has_perm('permission.delete_article')==Falseassertuser2.has_perm('permission.delete_article',art1)==Falseassertuser2.has_perm('permission.delete_article',art2)==TrueLicenseThe 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.