Really simple permission backend for django
Class based, No database, Object level
Inspired by django-permission
Tested with Django 3.2 to 5.1 - python 3.8 to 3.13.
For older django versions, use django-simple_perms 0.2.8
Introduction
The app autodiscover perms.py module in your project's apps.
This modules should register PermissionLogic based class.
When calling django's has_perm method, it will run the corresponding method name in your PermissionLogic class.
See usage section below for comprehensive example.
Usage
Install from pypi :
pip install django-simple_perms
or
poetry add django-simple_perms
settings.py
INSTALLED_APPS = (
# ...
'simple_perms', # Add simple_perms app to your INSTALLED_APPS
# ...
)
AUTHENTICATION_BACKENDS = (
'simple_perms.backend.PermissionBackend', # Add permission backend before django's one
'django.contrib.auth.backends.ModelBackend',
)
project_app/perms.py
from simple_perms import register, PermissionLogic
@register('project_app')
class ProjectLogic(PermissionLogic):
def add_project(self, user, project, perm):
return True
def change_project(self, user, project, perm):
return user.is_admin() or project.owner == user
delete_project = change_project
def default_permission(self, user, project, perm):
# Optional, default to global default permission, which default to False
return user.is_admin()
It is also possible to register using function instead of decorator:
from simple_perms import register, PermissionLogic
class ProjectLogic(PermissionLogic):
...
register('project_app', ProjectLogic)
user1.has_perm('project_app.add_project') # True
user1.has_perm('project_app.change_project', user1_project) # True
user1.has_perm('project_app.delete_project', user1_project) # True
user2.has_perm('project_app.change_project', user1_project) # False
admin.has_perm('project_app.change_project', user1_project) # True
Default permission
If a checked permission doesn't exists in registered PermissionLogic based classe, the backend will run the default_permission method of this class. If no default_permission defined, it default to the global default permission which default to False.
Change global default permission
settings.py
SIMPLE_PERMS_GLOBAL_DEFAULT_PERMISSION = 'path.to.custom_global_default_permission'
path/to.py
def custom_global_default_permission(user, obj, perm):
return user.is_admin()
global_default_permission and default_permission have the same arguments as others permissions : (user, obj, perm)
Change autodiscovered module name
simple_perms autodiscover perms.py modules in every django's apps. You can change the module name to autodiscover using the SIMPLE_PERMS_MODULE_NAME setting :
SIMPLE_PERMS_MODULE_NAME = 'permission'
Run tests
python runtests.py
Helper for your tests
from django.test import TestCase
from simple_perms.helpers import AssertPermissions
class TestContractPermission(AssertPermissions, TestCase):
def setUp(self):
self.admin = UserFactory(role="admin")
self.contract = ContractFactory()
def test_permissions_for_admin(self):
permissions = [
{ 'usr': 'admin', 'perm': 'contracts.add', 'args': (None,), 'result': True, },
{ 'usr': 'admin', 'perm': 'contracts.view', 'args': (self.contract, ), 'result': True, },
{ 'usr': 'admin', 'perm': 'contracts.change', 'args': (self.contract, ), 'result': True, },
]
self.assertPerms(permissions)
Which fails:
======================================================================
FAIL: test_permissions_for_admin (contracts.tests.perms.TestContractPermission)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/app/django/contracts/tests/perms.py", line 48, in test_permissions_of_admin
self.assertPerms(permissions)
File "/app/django/django-simple_perms/simple_perms/helpers.py", line 37, in assertPerms
raise e
File "/app/django/django-simple_perms/simple_perms/helpers.py", line 66, in _test_permission_
getattr(self, permission['usr']).has_perm(permission['perm'], *permission['args'])
AssertionError: ('PERM ERROR admin contracts.add: False is not true', 'PERM ERROR admin contracts.view: False is not true', 'PERM ERROR admin contracts.change: False is not true')
----------------------------------------------------------------------
Setup dev environnement
# install dev dependencies
poetry install --no-root
# install git pre-commit
pre-commit install
Build package and publish on PyPI
Change version number in pyproject.toml
poetry build
poetry publish
Metadata
Release files for django-simple_perms 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_simple_perms-1.2.0.tar.gz | 6.3 kB | Without provenance | Details for django_simple_perms-1.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_simple_perms-1.2.0-py3-none-any.whl | Python 3 | none | any | Details for django_simple_perms-1.2.0-py3-none-any.whl |
Total release size: 13.5 kB
Release files / django_simple_perms-1.2.0.tar.gz
| Download URL | django_simple_perms-1.2.0.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b180d935089cc068ef615fa62af9039e7f31c33677a50780ff89ef4100f1a274
|
|
BLAKE2b-256 checksum How to use checksums |
86c70a5c7a2aac8206b30e63c94c7abca6c90fa7d4972d292ff3678aa0434429
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.7 Linux/6.11.7-arch1-1
|
Release files / django_simple_perms-1.2.0-py3-none-any.whl
| Download URL | django_simple_perms-1.2.0-py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
753efe3ebc9b42f0c2a86ec5979586a98f2af012fb1acca178cdbf7e14068afa
|
|
BLAKE2b-256 checksum How to use checksums |
e093290759c92daa843d9b35b1ae93f79202511895232648da058aaa81336ec8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.7 Linux/6.11.7-arch1-1
|
Security for django-simple_perms 1.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.