@permission_required not working

191 views
Skip to first unread message

g...@hyprbrands.com

unread,
Aug 18, 2014, 5:15:53 AM8/18/14
to django...@googlegroups.com
Hey all,

I'm trying to use Django's permission functionalities but having some trouble.

I've created a custom permission in one of my models with this code:
class Meta:
        permissions
= (
           
("actions_for_both_user_type", "General permission for both user types"),
           
("actions_for_managers", "managers general permission"),
       
)

I synced the DB and I can see the permissions in auth_permission table.
I entered the Django admin panel and chose a test user and gave him the 2 new permissions.

I've added a view that should be served only for a user with one of these permissions with this header:
@permission_required('campaign.actions_for_managers')

After login in with the user with the permissions I still can't get into the view.
If I take the permission_required head off and print these three lines
print request.user
print request.user.has_perm('campaign.actions_for_managers')
print request.user.user_permissions.all()

I'll see that I get the correct logged in user.
False for the second line (has_perm)
and for the third line I get this result:
[<Permission: campaign | campaign | General permission for both user types>, <Permission: campaign | campaign | managers general permission>]

I'm not sure what I'm doing wrong. It seems like the user does have the permission but the "had_perm" isn't working..

Please help.
Best

Collin Anderson

unread,
Aug 18, 2014, 7:28:33 AM8/18/14
to django...@googlegroups.com
Is the user active? Are you using a custom user backend?

g...@hyprbrands.com

unread,
Aug 18, 2014, 7:41:46 AM8/18/14
to django...@googlegroups.com
I double checked. the user is active.
Not using a custom user backend but in the "AUTHENTICATION_BACKENDS" I have this set:

AUTHENTICATION_BACKENDS = (
   
'bla.models.CaseInsensitiveModelBackend',


)



class CaseInsensitiveModelBackend(object):
   
def authenticate(self, username=None, password=None):
       
try:
            user
= User.objects.get(username__iexact=username)
           
if user.check_password(password):
               
return user
       
except User.DoesNotExist:
           
return None


   
def get_user(self, user_id):
       
try:
           
return User.objects.get(pk=user_id)
       
except User.DoesNotExist:
           
return None

Collin Anderson

unread,
Aug 18, 2014, 7:46:05 AM8/18/14
to django...@googlegroups.com
Try subclassing ModelBackend.

from django.contrib.auth.backends import ModelBackend

class CaseInsensitiveModelBackend(ModelBackend):
# etc

g...@hyprbrands.com

unread,
Aug 18, 2014, 7:59:48 AM8/18/14
to django...@googlegroups.com
Yes, it worked :)
Thanks!
Reply all
Reply to author
Forward
0 new messages