views:

65

answers:

2

Hi,

I'm trying to setup some custom permissions for a Django application, but can't seem to get it working. The official documentation is a little scant, and doesn't mention (at least that I can find) how to actually set a permission? Based on a few 3rd party tutorials I found, I've extended the User class and it seems to work OK:

from django.db import models
from django.contrib.auth.models import User,UserManager

    class CustomUser(User):
        custom_field = models.CharField(max_length=250)
        objects = UserManager()

        class Meta:
            permissions = (
                ('is_custom','Has a Custom Permission'),
            )

When I try to set a permission however, it doesn't work:

>>> from project.custauth.models import CustomUser
>>> from django.contrib.auth.models import User, Permission
>>> user = CustomUser.objects.get(username='new.user')
>>> user
<CustomUser: new.user>
>>> custom_permission = Permission.objects.get(codename="is_custom")
>>> custom_permission
<Permission: custauth | custom user | Has a Custom Permission>
>>> custom_permission.save()
>>> user.user_permissions.add(custom_permission)
>>> user.save()
>>> user.has_perm(custom_permission)
False
>>> user.get_all_permissions()
set([])

Any ideas on what I'm doing wrong? I'm using Django 1.2.1 with Python 2.4.3. All input appreciated...

A: 

I tested your code (Python 2.6.2, Django 1.2.1) and everything worked as expected. Can you write a unit test to exercise the same code snippet?

On a side note user.has_perm(custom_permission) will not return True. Try user.has_perm('app.is_custom').

Update

Code snippet follows:

In [1]: from app.models import CustomUser

In [2]: from django.contrib.auth.models import User, Permission

In [3]: user = CustomUser.objects.get(username = 'new.user')

In [4]: custom_permission = Permission.objects.get(codename='is_custom')

In [5]: user.user_permissions.add(custom_permission)

In [6]: user.save()

In [7]: user.has_perm(custom_permission)
Out[7]: False

In [8]: user.has_perm('app.is_custom')
Out[8]: True

In [9]: user.get_all_permissions()
Out[9]: set([u'app.is_custom'])
Manoj Govindan
I'll definately keep the hint re: has_perm() in mind. Thanks.I'm yet to use the testing features of Django (it's a reasonably simple app, so I'm just testing as I go). Would there be any benefit to doing that over just using the manage.py shell?In any case, the issue is that user.get_all_permissions() returns an empty set. I noticed that you're running a newer version of Python so I tried again with 2.6.5, but still see the same behavior. Do you get an empty set? If not, would you mind pasting the code you used so I can see if there are any differences?Thanks.
Sparky
(1) I prefer to write unit tests for two reasons: no lingering database side effects as the test database is knocked off each time; useful for preventing regression later (2) I did not get an empty set. I'll update my answer to post the code I used.
Manoj Govindan
A: 

I did some more testing on a different platform and was able to replicate your results. After more digging I've found it's an issue with the authentication backend, rather than the permissions themselves.

Thanks for your help Manoj.

Sparky