Skip to main content

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 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*

```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 perms 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
```

Project details


Download files

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

Source Distribution

django-simple_perms-0.1.tar.gz (4.5 kB view details)

Uploaded Source

File details

Details for the file django-simple_perms-0.1.tar.gz.

File metadata

File hashes

Hashes for django-simple_perms-0.1.tar.gz
Algorithm Hash digest
SHA256 5701b6bed5fa2c220b45f40b134a3714f5a80c2ed4ee58b6c094576e8a3a317c
MD5 857b7667380cb1cd875e575843b9ce08
BLAKE2b-256 29a0a39cdddc242001a79e8446f95061d7286576cd2b57da6cca619a9ad162f7

See more details on using hashes here.

Provenance

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