Setup
I've just started working with django-guardian and have straight away run into some obstacles. I'm using custom users by extending the AbstractBaseUser class. I followed this example to setup my models.py and admin.py. I also followed the developers guide configuration guide to setup guardian.
Problem
django-guardian throws a AttributeError: type object 'MyCustomUser' has no attribute 'groups'
error (MyCustomUser is my custom user class) whenever I try to get permissions pertaining to a user i.e. when I add a permission, it goes straight into the guardian_userobjectpermission
table, like it should. However, calls to get_perms
throw the mentioned error.
The same error appears while trying to edit permissions via the admin page. (progmatically added permissions don't show up here. :/) I wrote a small manage.py task to test it:
class Command(BaseCommand):
def handle(self, *args, **options):
user1 = MyCustomUser.objects.filter(username='pankaj')[0]
checker = ObjectPermissionChecker(user1)
# model on which permissions are applied
stream = Stream.objects.filter(uuid='001')[0]
# works on the database level, doesn't show up on admin page
assign_perm('read_stream', user1, stream)
# error
print 'read_stream' in get_perms(user1, stream)
# error
print checker.has_perm('read_stream', stream)
# error
print checker.get_perms(stream)
# works on the database level, doesn't show up on admin page
remove_perm('read_stream', user, stream)
# ALWAYS returns True, irrespective of whether permission granted or not
print user.has_perm('read_stream', stream)
Possible Solution
There might be a problem with setting up the authentication backend. I currently have it set to:
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend', 'guardian.backends.ObjectPermissionBackend',
)
Maybe implementing a custom user => changing/implementing custom groups? Currently I have admin.site.unregister(Group) in admin.py, but changing it doesn't help.
As the developer has warned, guardian might not be compatible with custom users?
According to django-guardian documentations here. Gaurdian was depending heavily on the old fashion of django user model. But you can get through it, if you extend
AbstractUser
model, or defined ManyToMany relation with auth.Group could groups in your authentication model.