All Projects → lambdalisue → Django Permission

lambdalisue / Django Permission

Licence: mit
[Not maintained] An enhanced permission system which support object permission in Django

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Django Permission

Best Of Web Python
🏆 A ranked list of awesome python libraries for web development. Updated weekly.
Stars: ✭ 1,118 (+266.56%)
Mutual labels:  framework, django
Php Casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in PHP .
Stars: ✭ 865 (+183.61%)
Mutual labels:  framework, permission
Cancan
cancan is a tiny permission controller base on ruby cancan library.
Stars: ✭ 244 (-20%)
Mutual labels:  django, permission
Django
The Web framework for perfectionists with deadlines.
Stars: ✭ 61,277 (+19990.82%)
Mutual labels:  framework, django
Django Salesman
Headless e-commerce framework for Django.
Stars: ✭ 157 (-48.52%)
Mutual labels:  framework, django
Django Arctic
Django Arctic is a framework that simplifies the creation of custom content management systems.
Stars: ✭ 68 (-77.7%)
Mutual labels:  framework, django
Hzdtf.foundation.framework
基础框架系统,支持.NET和.NET Core平台,语言:C#,DB支持MySql和SqlServer,主要功能有抽象持久化、服务层,将业务基本的增删改查抽离复用;提供代码生成器从DB生成实体、持久化、服务以及MVC控制器,每层依赖接口,并需要在客户端将对应实现层用Autofac程序集依赖注入,用AOP提供日志跟踪、事务、模型验证等。对Autofac、Redis、RabbitMQ封装扩展;DB访问提供自动主从访问,Redis客户端分区。特别适合管理系统。
Stars: ✭ 22 (-92.79%)
Mutual labels:  framework, permission
Django Ra Erp
A Django based framework to create diverse business solutions, equipped with a reporting engine and a responsive dashboard.
Stars: ✭ 108 (-64.59%)
Mutual labels:  framework, django
Wq
📱🌐📋 wq: a modular framework supporting web / native geographic data collection apps for mobile surveys and citizen science. Powered by Django REST Framework, Redux, React, and Material UI.
Stars: ✭ 182 (-40.33%)
Mutual labels:  framework, django
Cms
Club Management System of amFOSS, powered by CMS
Stars: ✭ 263 (-13.77%)
Mutual labels:  framework, django
Core
Our Front-end baseplate, from mobile to desktop
Stars: ✭ 298 (-2.3%)
Mutual labels:  framework
Horse
Fast, opinionated, minimalist web framework for Delphi
Stars: ✭ 295 (-3.28%)
Mutual labels:  framework
Djaodjin Saas
Django application for software-as-service and subscription businesses
Stars: ✭ 297 (-2.62%)
Mutual labels:  django
Openvpnadapter
Objective-C wrapper for OpenVPN library. Compatible with iOS and macOS.
Stars: ✭ 302 (-0.98%)
Mutual labels:  framework
Django Cassandra Engine
Django Cassandra Engine - the Cassandra backend for Django
Stars: ✭ 298 (-2.3%)
Mutual labels:  django
Waliki
A wiki engine powered by Django and Git
Stars: ✭ 300 (-1.64%)
Mutual labels:  django
Django Rest Framework Jwt
JSON Web Token Authentication support for Django REST Framework
Stars: ✭ 3,105 (+918.03%)
Mutual labels:  django
Pink
A high performance network library
Stars: ✭ 297 (-2.62%)
Mutual labels:  framework
Turkce Python Kaynaklari
Türkçe olarak hazırlanmış Python programlama dili ile ilgili içeriklerin derlendiği sayfa.
Stars: ✭ 295 (-3.28%)
Mutual labels:  django
Routing Controllers
Create structured, declarative and beautifully organized class-based controllers with heavy decorators usage in Express / Koa using TypeScript and Routing Controllers Framework.
Stars: ✭ 3,557 (+1066.23%)
Mutual labels:  framework

django-permission

.. image:: https://img.shields.io/travis/lambdalisue/django-permission/master.svg?style=flat-square :target: http://travis-ci.org/lambdalisue/django-permission :alt: Build status .. image:: https://img.shields.io/coveralls/lambdalisue/django-permission/master.svg?style=flat-square :target: https://coveralls.io/github/lambdalisue/django-permission?branch=master :alt: Coverage .. image:: https://img.shields.io/requires/github/lambdalisue/django-permission/master.svg?style=flat-square :target: https://requires.io/github/lambdalisue/django-permission/requirements/?branch=master :alt: Requirements Status .. image:: https://img.shields.io/scrutinizer/g/lambdalisue/django-permission/master.svg?style=flat-square :target: https://scrutinizer-ci.com/g/lambdalisue/django-permission/inspections :alt: Inspection .. image:: https://img.shields.io/pypi/v/django-permission.svg?style=flat-square :target: https://github.com/lambdalisue/django-permission/blob/master/setup.py :alt: Version .. image:: https://img.shields.io/pypi/l/django-permission.svg?style=flat-square :target: https://github.com/lambdalisue/django-permission/blob/master/LICENSE :alt: License .. image:: https://img.shields.io/pypi/format/django-permission.svg?style=flat-square :target: https://pypi.python.org/pypi/django-permission/ :alt: Format .. image:: https://img.shields.io/pypi/pyversions/django-permission.svg?style=flat-square :target: https://pypi.python.org/pypi/django-permission/ :alt: Supported python versions .. image:: https://img.shields.io/pypi/status/django-permission.svg?style=flat-square :target: https://pypi.python.org/pypi/django-permission/ :alt: Status

Author Alisue [email protected] Supported python versions Python 2.7, 3.3, 3.4, 3.5, 3.6 Supported django versions Django 1.8 - 1.11b

An enhanced permission library which enables a logic-based permission system to handle complex permissions in Django.

Documentation

http://django-permission.readthedocs.org/en/latest/

Installation

Use pip_ like::

$ pip install django-permission

.. _pip: https://pypi.python.org/pypi/pip

Usage

The following might help you to understand as well.

  • Basic strategy or so on, Issue #28 <https://github.com/lambdalisue/django-permission/issues/28>_
  • Advanced usage and examples, Issue #26 <https://github.com/lambdalisue/django-permission/issues/26>_

Configuration

1.  Add ``permission`` to the ``INSTALLED_APPS`` in your settings
    module

    .. code:: python

        INSTALLED_APPS = (
            # ...
            'permission',
        )

2.  Add our extra authorization/authentication backend

    .. code:: python

        AUTHENTICATION_BACKENDS = (
            'django.contrib.auth.backends.ModelBackend', # default
            'permission.backends.PermissionBackend',
        )

3.  Follow the instructions below to apply logical permissions to django models

Autodiscovery

Like django's admin package, django-permission automatically discovers the perms.py in your application directory by running permission.autodiscover(). Additionally, if the perms.py module has a PERMISSION_LOGICS variable, django-permission automatically run the following functions to apply the permission logics.

.. code:: python

for model, permission_logic_instance in PERMISSION_LOGICS:
    if isinstance(model, str):
        model = get_model(*model.split(".", 1))
    add_permission_logic(model, permission_logic_instance)

.. note::

Autodiscover feature is automatically called if you are using django higher than 1.7 so no need to follow the tutorial below.
To disable, use `PERMISSION_AUTODISCOVER_ENABLE` setting.

Quick tutorial

  1. Add import permission; permission.autodiscover() to your urls.py like:

    .. code:: python

    from django.conf.urls import patterns, include, url
    from django.contrib import admin
    
    admin.autodiscover()
    # add this line
    import permission; permission.autodiscover()
    
    urlpatterns = patterns('',
        url(r'^admin/', include(admin.site.urls)),
        # ...
    )
    
  2. Write perms.py in your application directory like:

    .. code:: python

    from permission.logics import AuthorPermissionLogic
    from permission.logics import CollaboratorsPermissionLogic
    
    PERMISSION_LOGICS = (
        ('your_app.Article', AuthorPermissionLogic()),
        ('your_app.Article', CollaboratorsPermissionLogic()),
    )
    

You can specify a different module or variable name, with PERMISSION_AUTODISCOVER_MODULE_NAME or PERMISSION_AUTODISCOVER_VARIABLE_NAME respectively.

Apply permission logic

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).

What you need to do is just applying ``permission.logics.AuthorPermissionLogic``
to the ``Article`` model like

.. code:: python

    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())


.. note::
    From django-permission version 0.8.0, you can specify related object with
    `field__name` attribute like
    `django queryset lookup <https://docs.djangoproject.com/en/1.10/topics/db/queries/#field-lookups>`_.
    See the working example below:

.. code:: python

    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')
        project = models.ForeignKey('permission.Project')

        # this is just required for easy explanation
        class Meta:
            app_label='permission'

    class Project(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 to Article
    from permission import add_permission_logic
    from permission.logics import AuthorPermissionLogic
    add_permission_logic(Article, AuthorPermissionLogic(
        field_name='project__author',
    ))


That's it.
Now the following codes will work as expected:


.. code:: python

    user1 = User.objects.create_user(
        username='john',
        email='[email protected]',
        password='password',
    )
    user2 = User.objects.create_user(
        username='alice',
        email='[email protected]',
        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

    #
    # You may also be interested in django signals to apply 'add' permissions to the
    # newly created users.
    # https://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.signals.post_save
    #
    from django.db.models.signals.post_save
    from django.dispatch import receiver
    from permission.utils.permissions import perm_to_permission

    @receiver(post_save, sender=User)
    def apply_permissions_to_new_user(sender, instance, created, **kwargs):
        if not created:
            return
        #
        # permissions you want to apply to the newly created user
        # YOU SHOULD NOT APPLY PERMISSIONS EXCEPT PERMISSIONS FOR 'ADD'
        # in this way, the applied permissions are not object permission so
        # if you apply 'permission.change_article' then the user can change
        # any article object.
        #
        permissions = [
            'permission.add_article',
        ]
        for permission in permissions:
            # apply permission
            # perm_to_permission is a utility to convert string permission
            # to permission instance.
            instance.user_permissions.add(perm_to_permission(permission))


See http://django-permission.readthedocs.org/en/latest/_modules/permission/logics/author.html#AuthorPermissionLogic
to learn how this logic works.

Now, assume you add ``collaborators`` attribute to store collaborators
of the article and you want to give them a change permission.

What you need to do is quite simple.
Apply ``permission.logics.CollaboratorsPermissionLogic``
to the ``Article`` model as follows


.. code:: python

    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)
        collaborators = models.ManyToManyField(User)

        # this is just required for easy explanation
        class Meta:
            app_label='permission'

    # apply AuthorPermissionLogic and CollaboratorsPermissionLogic
    from permission import add_permission_logic
    from permission.logics import AuthorPermissionLogic
    from permission.logics import CollaboratorsPermissionLogic
    add_permission_logic(Article, AuthorPermissionLogic())
    add_permission_logic(Article, CollaboratorsPermissionLogic(
        field_name='collaborators',
        any_permission=False,
        change_permission=True,
        delete_permission=False,
    ))


.. note::
    From django-permission version 0.8.0, you can specify related object with
    `field_name` attribute like
    `django queryset lookup <https://docs.djangoproject.com/en/1.10/topics/db/queries/#field-lookups>`_.
    See the working example below:


.. code:: python

    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')
        project = models.ForeignKey('permission.Project')

        # this is just required for easy explanation
        class Meta:
            app_label='permission'

    class Project(models.Model):
        title = models.CharField('title', max_length=120)
        body = models.TextField('body')
        collaborators = models.ManyToManyField(User)

        # this is just required for easy explanation
        class Meta:
            app_label='permission'

    # apply AuthorPermissionLogic to Article
    from permission import add_permission_logic
    from permission.logics import CollaboratorsPermissionLogic
    add_permission_logic(Article, CollaboratorsPermissionLogic(
        field_name='project__collaborators',
    ))


That's it.
Now the following codes will work as expected:


.. code:: python

    user1 = User.objects.create_user(
        username='john',
        email='[email protected]',
        password='password',
    )
    user2 = User.objects.create_user(
        username='alice',
        email='[email protected]',
        password='password',
    )

    art1 = Article.objects.create(
        title="Article 1",
        body="foobar hogehoge",
        author=user1
    )
    art1.collaborators.add(user2)

    assert user1.has_perm('permission.change_article') == False
    assert user1.has_perm('permission.change_article', art1) == True
    assert user1.has_perm('permission.delete_article', art1) == True

    assert user2.has_perm('permission.change_article') == False
    assert user2.has_perm('permission.change_article', art1) == True
    assert user2.has_perm('permission.delete_article', art1) == False


See http://django-permission.readthedocs.org/en/latest/_modules/permission/logics/collaborators.html#CollaboratorsPermissionLogic
to learn how this logic works.

There are `StaffPermissionLogic <http://django-permission.readthedocs.org/en/latest/_modules/permission/logics/staff.html#StaffPermissionLogic>`_
and `GroupInPermissionLogic <http://django-permission.readthedocs.org/en/latest/_modules/permission/logics/groupin.html#GroupInPermissionLogic>`_ 
for ``is_staff`` or ``group`` based permission logic as well.

Customize permission logic
............................
Your own permission logic class must be a subclass of
``permission.logics.PermissionLogic`` and must override
``has_perm(user_obj, perm, obj=None)`` method which return boolean value.

Class, method, or function decorator
-------------------------------------
Like Django's ``permission_required`` but it can be used for object permissions
and as a class, method, or function decorator.
Also, you don't need to specify a object to this decorator for object permission.
This decorator automatically determined the object from request
(so you cannnot use this decorator for non view class/method/function but you
anyway use ``user.has_perm`` in that case).


.. code:: python

    >>> from permission.decorators import permission_required
    >>> # As class decorator
    >>> @permission_required('auth.change_user')
    >>> class UpdateAuthUserView(UpdateView):
    ...     pass
    >>> # As method decorator
    >>> class UpdateAuthUserView(UpdateView):
    ...     @permission_required('auth.change_user')
    ...     def dispatch(self, request, *args, **kwargs):
    ...         pass
    >>> # As function decorator
    >>> @permission_required('auth.change_user')
    >>> def update_auth_user(request, *args, **kwargs):
    ...     pass


Override the builtin ``if`` template tag
----------------------------------------
django-permission overrides the builtin ``if`` tag, adding two operators to handle
permissions in templates.
You can write a permission test by using ``has`` keyword, and a target object with ``of`` as below.


.. code:: html

    {% if user has 'blogs.add_article' %}
        <p>This user have 'blogs.add_article' permission</p>
    {% elif user has 'blog.change_article' of object %}
        <p>This user have 'blogs.change_article' permission of {{object}}</p>
    {% endif %}

    {# If you set 'PERMISSION_REPLACE_BUILTIN_IF = False' in settings #}
    {% permission user has 'blogs.add_article' %}
        <p>This user have 'blogs.add_article' permission</p>
    {% elpermission user has 'blog.change_article' of object %}
        <p>This user have 'blogs.change_article' permission of {{object}}</p>
    {% endpermission %}

.. note::
    From Django 1.9, users require to add `'permission.templatetags.permissionif'` to `'builtins'` option manually.
    See
    - https://docs.djangoproject.com/en/1.9/releases/1.9/#django-template-base-add-to-builtins-is-removed
    - https://docs.djangoproject.com/en/1.9/topics/templates/#module-django.template.backends.django
    Or following example:

    .. code:: python

        TEMPLATES = [
            {
                'BACKEND': 'django.template.backends.django.DjangoTemplates',
                'OPTIONS': {
                    'builtins': ['permission.templatetags.permissionif'],
                },
            },
        ]

License
-------------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) 2015 Alisue, hashnote.net

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].