.. image:: https://img.shields.io/coveralls/grey0ne/django-protector.svg
.. image:: https://img.shields.io/travis/grey0ne/django-protector/master.svg
Protector is used for managing object level permissions in performance efficient way.
It supports queryset filtering by permission and user.
Also it allow every object in your project to behave as a user group. i.e. adding permissions and users with roles.
1. Install from pip (it will also install required django-mptt)::
pip install django-protector
2. Add "protector" to your INSTALLED_APPS setting::
INSTALLED_APPS = (
3. Add protector authentication backend. Also you should remove default auth backend::
AUTHENTICATION_BACKENDS = (
4. Make some model your default group::
PROTECTOR_GENERIC_GROUP = 'users.group'
5. Run `python manage.py migrate` to create the protector models and copy existing user permissions
Now you can check permissions on objects like this::
Or filter any queryset by permission::
from protector.models import filter_queryset_by_permission
filtered_qset = filter_queryset_by_permission(some_qset, user, 'some_app.some_perm')
1. Add GenericPermsMixin to your User model to conviniently add permissions and groups::
class User(UserGenericPermsMixin, AbstractBaseUser)
2. Create some group models. Every model in your project could now behave like a group. You could inherit your models from abstract group to have convinient fields for users and permissions::
4. Inherit your Querysets from PermissionQuerySet to conviniently filter by permission::
Now you can manipulate permissions and groups::
Restricting access to objects
This is somewhat different from just filtering queryset by permission
1. Inherit your model from Restricted::
2. Inherit model manager from RestrictedManager::
3. Restricted contains some additional fields so you need to run makemigration for your app
4. Now you can restrict instances of your model::
To enable user view one or all restricted objects::
To filter model objects visible by user::
You could define Template-like permissions. For example you want all group moderators to have edit_permission on their group.
Such templates could be created In Global Group Permissions admin interface. You should choose ContentType of your group, roles and, of course, permission those roles should have.
No further actions required::
would return true if user is moderator in somegroup
Permission on Foreign Key to User
Every so often you would like owners of your objects to have some permissions of their objects.
You should inherit you object, for example TestPost from AbstractGenericGroup
SUBSCRIBER = 1
AUTHOR = 2
ROLES = (
author = models.ForeignKey(to=TestUser)
MEMBER_FOREIGN_KEY_FIELDS = (
permissions = (
('manage_post', 'Manage Post'),
MEMBER_FOREIGN_KEY_FIELDS defines which foreign key gets which role.
Notice: This is accomplished via some denormalization and works through create, save and update model and manager methods overloading
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.