A simple class based permission backend for django
Project description
Really simple permission backend for django
Class based, No database
Inspired by [django-permission](https://github.com/lambdalisue/django-permission)
Tested with Django 1.10 - python 3.5
# 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
*settings.py*
```python
INSTALLED_APPS = (
# ...
'simple_perms', # Add simple_perms app to your INSTALLED_APPS
# ...
)
AUTHENTICATION_BACKENDS = (
'simple_perms.PermissionBackend', # Add permission backend before django's one
'django.contrib.auth.backends.ModelBackend',
)
```
*project_app/perms.py*
```python
from simple_perms import register, PermissionLogic
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()
register('project_app', ProjectLogic)
```
```python
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*
```python
SIMPLE_PERMS_GLOBAL_DEFAULT_PERMISSION = 'path.to.custom_global_default_permission'
```
*path/to.py*
```python
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 :
```python
SIMPLE_PERMS_MODULE_NAME = 'permission'
```
# Run tests
```bash
python runtests.py
```
# Helper for your tests
```python
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:
``` text
======================================================================
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')
----------------------------------------------------------------------
```
Class based, No database
Inspired by [django-permission](https://github.com/lambdalisue/django-permission)
Tested with Django 1.10 - python 3.5
# 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
*settings.py*
```python
INSTALLED_APPS = (
# ...
'simple_perms', # Add simple_perms app to your INSTALLED_APPS
# ...
)
AUTHENTICATION_BACKENDS = (
'simple_perms.PermissionBackend', # Add permission backend before django's one
'django.contrib.auth.backends.ModelBackend',
)
```
*project_app/perms.py*
```python
from simple_perms import register, PermissionLogic
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()
register('project_app', ProjectLogic)
```
```python
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*
```python
SIMPLE_PERMS_GLOBAL_DEFAULT_PERMISSION = 'path.to.custom_global_default_permission'
```
*path/to.py*
```python
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 :
```python
SIMPLE_PERMS_MODULE_NAME = 'permission'
```
# Run tests
```bash
python runtests.py
```
# Helper for your tests
```python
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:
``` text
======================================================================
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')
----------------------------------------------------------------------
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-simple_perms-0.2.8.tar.gz
.
File metadata
- Download URL: django-simple_perms-0.2.8.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.23.3 CPython/3.6.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 771deb81b2440bd072a41ada7b1bacdeae0422dd2bf34f035738deef74b177ab |
|
MD5 | 7b5a03c35f45dfab03e9fc808b4e852d |
|
BLAKE2b-256 | 001d8f9b30b3f30b4c7a938669d15f0401654e5781829718055bcd0731eb00e2 |
Provenance
File details
Details for the file django_simple_perms-0.2.8-py3-none-any.whl
.
File metadata
- Download URL: django_simple_perms-0.2.8-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.23.3 CPython/3.6.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed47a03afb7a8a362b91323a4b81cc1793f5cea7705f194198aa8260f446f295 |
|
MD5 | a0bbdef3d936c535a949728b2f5eccda |
|
BLAKE2b-256 | b5c9c9e4364974d628f28150239eb02a77376af03b4d1465cbd1d3ec1b6e2e08 |