Skip to main content

A simple permission system which enable logical permission systems in Django

Project description

Version License Format Supported python versions Status Documentation Status tests
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

http://django-permission2.readthedocs.org/

Installation

Use pip like:

$ pip install django-permission2

Usage

The following might help you to understand as well.

Configuration

  1. Add permission to the INSTALLED_APPS in your settings module

    INSTALLED_APPS = (
        # ...
        'permission',
    )
  2. Add our extra authorization/authentication backend

    AUTHENTICATION_BACKENDS = (
        'django.contrib.auth.backends.ModelBackend', # default
        'permission.backends.PermissionBackend',
    )
  3. 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).

  1. Add import permission; permission.autodiscover() to your urls.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)),
        # ...
    ]
  2. 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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_permission2-2.1.0.tar.gz (31.9 kB view details)

Uploaded Source

Built Distribution

django_permission2-2.1.0-py3-none-any.whl (52.3 kB view details)

Uploaded Python 3

File details

Details for the file django_permission2-2.1.0.tar.gz.

File metadata

  • Download URL: django_permission2-2.1.0.tar.gz
  • Upload date:
  • Size: 31.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/22.5.0

File hashes

Hashes for django_permission2-2.1.0.tar.gz
Algorithm Hash digest
SHA256 9d8c22c1cb753536b18b7cb4a322a7aef2688fde97d83d579e8e7807b27c738c
MD5 85f4c5bc8f3a45dc37b5d52c96f956d9
BLAKE2b-256 e0f06aa68f3c9c2639ab14191d6e7130bb5b0632c4cdddafa317700e6d159a4e

See more details on using hashes here.

File details

Details for the file django_permission2-2.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_permission2-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba5d6327fa04b576aba9e59b2c071066944297a440554293a7971df3c5f77693
MD5 89d7700383afd30c95ada9c7c7b28ac6
BLAKE2b-256 4c593156c38bf5709f33087a2d715eda0fafafead9ae5815fd9314ac83932601

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page