I have implemented my own User model class as follows. Note that is it NOT customizing django's auth.User
model. I am new to this object permission knowledge and especially in this self-defined User model which is required in my project.
Could you give an example of adding per-object permission in this case? Much appreciated.
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=40, unique=True)
//.... other fields are omitted
class Article(models.Model):
title = models.CharField('title', max_length=120)
body = models.TextField('body')
author = models.ForeignKey(CustomUser)
Now, the object permission comes into play. Each user can create/update/delete/view their own article objects, but ONLY view others' articles without the permission to update/delete them.
From the django docs, the Model level permission does not apply here. If the Article is given model level update permission, then all users can update others' Article.
So, I found out the django-guardian. However, there seems to be no hope for this self-defined CustomUser model, as it relies heavily on Django's auth.User
model!
https://django-guardian.readthedocs.org/en/v1.2/userguide/custom-user-model.html
UPDATE:
- My case is subclassing AbstractBaseUser instead of AbstractUser;
- This is not for the admin but only for my backend code logic;
- I am not using Django REST API here, but if REST API is proper, please give an example.
Object-level permissions are not built into Django, even when using the standard auth.User
model. But the foundation is there in that Django's PermissionsMixin
defines the has_perm
method, which accepts a model instance. Django does nothing with it by default, but you can.
The has_perm
method effectively passes the hard work off onto the registered authentication backends. So you can create a custom authentication backend specifically for performing your object-level permission checks. It does not need to actually handle authentication. It can be as simple as a single method on a basic class. Something like the following (untested) is all you should need:
class ObjectPermissionsBackend(object):
def has_perm(self, user_obj, perm, obj=None):
if not obj:
return False # not dealing with non-object permissions
if perm == 'view':
return True # anyone can view
elif obj.author_id == user_obj.pk:
return True
else:
return False
Tell Django to use your custom backend using the AUTHENTICATION_BACKENDS
setting. In settings.py:
AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend', 'path.to.ObjectPermissionsBackend')
Then, in your code:
if user.has_perm('edit', article_instance):
# allow editing
See https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#custom-users-and-permissions and https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#specifying-authentication-backends
In the documentation page you posted, there is also stated:
Basically, if we subclass AbstractUser or define many-to-many relation
with auth.Group (and give reverse relate name groups) we should be
fine.
Since this is what you're doing, you should set AUTH_USER_MODEL
as written in the Django documentention (also see the ticket and the commit code for Django 1.5 compatibility).
I end up using logic based per-object permission so that it does not alter my database. It is django-rules which support my class based view. Remember to override the redirect_field_name, otherwise, you will end up with redirect loop if users are logged in.