django-permission2
==========================
.. image:: https://img.shields.io/pypi/v/django-permission2.svg?style=flat-square
:target: https://github.com/janmalte/django-permission2/blob/master/setup.py
:alt: Version
.. image:: https://img.shields.io/pypi/l/django-permission2.svg?style=flat-square
:target: https://github.com/janmalte/django-permission2/blob/master/LICENSE
:alt: License
.. image:: https://img.shields.io/pypi/format/django-permission2.svg?style=flat-square
:target: https://pypi.python.org/pypi/django-permission2/
:alt: Format
.. image:: https://img.shields.io/pypi/pyversions/django-permission2.svg?style=flat-square
:target: https://pypi.python.org/pypi/django-permission2/
:alt: Supported python versions
.. image:: https://img.shields.io/pypi/status/django-permission2.svg?style=flat-square
:target: https://pypi.python.org/pypi/django-permission2/
:alt: Status
.. image:: https://readthedocs.org/projects/django-permission2/badge/?version=latest
:target: https://django-permission2.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status
.. image:: https://github.com/JanMalte/django-permission2/actions/workflows/run-tests.yml/badge.svg
:target: https://github.com/JanMalte/django-permission2/actions/workflows/run-tests.yml
:alt: 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
.. _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/jazzband/django-permission/issues/28>`_
- Advanced usage and examples, `Issue #26 <https://github.com/jazzband/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
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:
.. code:: python
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:
.. code:: python
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
.. 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())
That's it.
Now the following codes will work as expected:
.. code:: python
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.
Raw data
{
"_id": null,
"home_page": "https://django-permission2.readthedocs.io/",
"name": "django-permission2",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "django,object,logical,permission",
"author": "Malte Gerth",
"author_email": "mail@malte-gerth.de",
"download_url": "https://files.pythonhosted.org/packages/e0/f0/6aa68f3c9c2639ab14191d6e7130bb5b0632c4cdddafa317700e6d159a4e/django_permission2-2.1.0.tar.gz",
"platform": null,
"description": "django-permission2\n==========================\n.. image:: https://img.shields.io/pypi/v/django-permission2.svg?style=flat-square\n :target: https://github.com/janmalte/django-permission2/blob/master/setup.py\n :alt: Version\n.. image:: https://img.shields.io/pypi/l/django-permission2.svg?style=flat-square\n :target: https://github.com/janmalte/django-permission2/blob/master/LICENSE\n :alt: License\n.. image:: https://img.shields.io/pypi/format/django-permission2.svg?style=flat-square\n :target: https://pypi.python.org/pypi/django-permission2/\n :alt: Format\n.. image:: https://img.shields.io/pypi/pyversions/django-permission2.svg?style=flat-square\n :target: https://pypi.python.org/pypi/django-permission2/\n :alt: Supported python versions\n.. image:: https://img.shields.io/pypi/status/django-permission2.svg?style=flat-square\n :target: https://pypi.python.org/pypi/django-permission2/\n :alt: Status\n.. image:: https://readthedocs.org/projects/django-permission2/badge/?version=latest\n :target: https://django-permission2.readthedocs.io/en/latest/?badge=latest\n :alt: Documentation Status\n.. image:: https://github.com/JanMalte/django-permission2/actions/workflows/run-tests.yml/badge.svg\n :target: https://github.com/JanMalte/django-permission2/actions/workflows/run-tests.yml\n :alt: tests\n\nAuthor\n Malte Gerth <mail@malte-gerth.de>\nOriginal Author\n Alisue <lambdalisue@hashnote.net>\nSupported python versions\n Python 3.8, 3.9, 3.10, 3.11\nSupported django versions\n Django 2.2, 3.2, 4.0, 4.1, 4.2\n\nAn enhanced permission library which enables a *logic-based permission system*\nto handle complex permissions in Django.\n\n\nDocumentation\n-------------\nhttp://django-permission2.readthedocs.org/\n\nInstallation\n------------\nUse pip_ like::\n\n $ pip install django-permission2\n\n.. _pip: https://pypi.python.org/pypi/pip\n\n\nUsage\n-----\n\nThe following might help you to understand as well.\n\n- Basic strategy or so on, `Issue #28 <https://github.com/jazzband/django-permission/issues/28>`_\n- Advanced usage and examples, `Issue #26 <https://github.com/jazzband/django-permission/issues/26>`_\n\nConfiguration\n~~~~~~~~~~~~~\n1. Add ``permission`` to the ``INSTALLED_APPS`` in your settings\n module\n\n .. code:: python\n\n INSTALLED_APPS = (\n # ...\n 'permission',\n )\n\n2. Add our extra authorization/authentication backend\n\n .. code:: python\n\n AUTHENTICATION_BACKENDS = (\n 'django.contrib.auth.backends.ModelBackend', # default\n 'permission.backends.PermissionBackend',\n )\n\n3. Follow the instructions below to apply logical permissions to django models\n\nQuick tutorial\n~~~~~~~~~~~~~~\n\nLet'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\n(e.g. add, change and delete permissions).\n\n1. Add ``import permission; permission.autodiscover()`` to your ``urls.py`` like:\n\n .. code:: python\n\n from django.conf.urls import patterns, include\n from django.urls import path\n from django.contrib import admin\n\n admin.autodiscover()\n\n # only add the following line\n import permission; permission.autodiscover()\n\n urlpatterns = [\n path('admin/', include(admin.site.urls)),\n # ...\n ]\n\n2. Write ``perms.py`` in your application directory like:\n\n .. code:: python\n\n from permission.logics import AuthorPermissionLogic\n from permission.logics import CollaboratorsPermissionLogic\n\n PERMISSION_LOGICS = (\n ('your_app.Article', AuthorPermissionLogic()),\n ('your_app.Article', CollaboratorsPermissionLogic()),\n )\n\nWhat you need to do is just applying ``permission.logics.AuthorPermissionLogic``\nto the ``Article`` model like\n\n.. code:: python\n\n from django.db import models\n from django.contrib.auth.models import User\n\n\n class Article(models.Model):\n title = models.CharField('title', max_length=120)\n body = models.TextField('body')\n author = models.ForeignKey(User)\n\n # this is just required for easy explanation\n class Meta:\n app_label='permission'\n\n # apply AuthorPermissionLogic\n from permission import add_permission_logic\n from permission.logics import AuthorPermissionLogic\n add_permission_logic(Article, AuthorPermissionLogic())\n\n\nThat's it.\nNow the following codes will work as expected:\n\n\n.. code:: python\n\n user1 = User.objects.create_user(\n username='john',\n email='john@test.com',\n password='password',\n )\n user2 = User.objects.create_user(\n username='alice',\n email='alice@test.com',\n password='password',\n )\n\n art1 = Article.objects.create(\n title=\"Article 1\",\n body=\"foobar hogehoge\",\n author=user1\n )\n art2 = Article.objects.create(\n title=\"Article 2\",\n body=\"foobar hogehoge\",\n author=user2\n )\n\n # You have to apply 'permission.add_article' to users manually because it\n # is not an object permission.\n from permission.utils.permissions import perm_to_permission\n user1.user_permissions.add(perm_to_permission('permission.add_article'))\n\n assert user1.has_perm('permission.add_article') == True\n assert user1.has_perm('permission.change_article') == False\n assert user1.has_perm('permission.change_article', art1) == True\n assert user1.has_perm('permission.change_article', art2) == False\n\n assert user2.has_perm('permission.add_article') == False\n assert user2.has_perm('permission.delete_article') == False\n assert user2.has_perm('permission.delete_article', art1) == False\n assert user2.has_perm('permission.delete_article', art2) == True\n\nLicense\n-------------------------------------------------------------------------------\nThe MIT License (MIT)\n\nCopyright (c) 2022 Malte Gerth <mail@malte-gerth.de>\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A simple permission system which enable logical permission systems in Django",
"version": "2.1.0",
"project_urls": {
"Documentation": "https://django-permission2.readthedocs.io/",
"Homepage": "https://django-permission2.readthedocs.io/",
"Repository": "https://github.com/JanMalte/django-permission2/",
"Tracker": "https://github.com/JanMalte/django-permission2/issues/"
},
"split_keywords": [
"django",
"object",
"logical",
"permission"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4c593156c38bf5709f33087a2d715eda0fafafead9ae5815fd9314ac83932601",
"md5": "89d7700383afd30c95ada9c7c7b28ac6",
"sha256": "ba5d6327fa04b576aba9e59b2c071066944297a440554293a7971df3c5f77693"
},
"downloads": -1,
"filename": "django_permission2-2.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "89d7700383afd30c95ada9c7c7b28ac6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 52259,
"upload_time": "2023-07-31T13:53:11",
"upload_time_iso_8601": "2023-07-31T13:53:11.670220Z",
"url": "https://files.pythonhosted.org/packages/4c/59/3156c38bf5709f33087a2d715eda0fafafead9ae5815fd9314ac83932601/django_permission2-2.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e0f06aa68f3c9c2639ab14191d6e7130bb5b0632c4cdddafa317700e6d159a4e",
"md5": "85f4c5bc8f3a45dc37b5d52c96f956d9",
"sha256": "9d8c22c1cb753536b18b7cb4a322a7aef2688fde97d83d579e8e7807b27c738c"
},
"downloads": -1,
"filename": "django_permission2-2.1.0.tar.gz",
"has_sig": false,
"md5_digest": "85f4c5bc8f3a45dc37b5d52c96f956d9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 31871,
"upload_time": "2023-07-31T13:53:13",
"upload_time_iso_8601": "2023-07-31T13:53:13.527529Z",
"url": "https://files.pythonhosted.org/packages/e0/f0/6aa68f3c9c2639ab14191d6e7130bb5b0632c4cdddafa317700e6d159a4e/django_permission2-2.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-31 13:53:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "JanMalte",
"github_project": "django-permission2",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "app_version",
"specs": []
},
{
"name": "django-appconf",
"specs": []
}
],
"tox": true,
"lcname": "django-permission2"
}