User .get_all_permissions() vs. .has_perm() inconsistency

1,102 views
Skip to first unread message

dslowik

unread,
Feb 10, 2012, 7:01:30 PM2/10/12
to Django users
Here is the code form my Python 2.6.6 session which is puzzling me:

>>> from django.contrib.auth.models import User
>>> u = User.objects.get(username='clint')
>>> u.get_all_permissions()
set([u'wb.delete_libraryitem', u'wb.change_libraryitem'])
>>> u.has_perm("wb.change_libraryitem")
True

...Shouldn't that be False?


I'm really trying to use permission checking as in:
{% if perms.foo.can_vote %}
as discussed in:
https://docs.djangoproject.com/en/1.2/topics/auth/#authentication-data-in-templates
, where I was expecting a False and got that True, but I've tracked
the problem down to the above puzzle.

I'm running
>>> django.VERSION
(1, 2, 3, 'final', 0)


Andy McKay

unread,
Feb 10, 2012, 10:49:03 PM2/10/12
to django...@googlegroups.com
On Fri, Feb 10, 2012 at 4:01 PM, dslowik <dsl...@tellink.net> wrote:
>>>> u.get_all_permissions()
> set([u'wb.delete_libraryitem', u'wb.change_libraryitem'])
>>>> u.has_perm("wb.change_libraryitem")
> True
>
> ...Shouldn't that be False?

Why would it be False? get_all_permissions: Returns a set of
permission strings that the user has.... has_perm: Returns True if the
user has the specified permission.

https://docs.djangoproject.com/en/1.2/topics/auth/#django.contrib.auth.models.User.get_all_permissions

dslowik

unread,
Feb 11, 2012, 10:38:03 AM2/11/12
to Django users
Yes, I have to agree entirely..
I bungled the OP. Here is what the code snippet should have been:
>>> from django.contrib.auth.models import User
>>> u = User.objects.get(username='clint')
>>> u.get_all_permissions()
set([u'wb.delete_libraryitem', u'wb.change_libraryitem'])
>>> u.has_perm("wb.add_libraryitem")
True

How can that be?

Your reply did prod me into realizing that I had made a similar typo
while trying to resolve this question last night. Anyway, here is the
answer:
>>> u.is_superuser
True

If the user is not superuser, it returns False. u.has_perm() returns
true for ANY permission if u is superuser.
>>> u.has_perm("wb.anything")
True

This solves the problem. Thanks for the help Andy. :)

Don Slowik

On Feb 10, 10:49 pm, Andy McKay <a...@clearwind.ca> wrote:
> On Fri, Feb 10, 2012 at 4:01 PM, dslowik <dslo...@tellink.net> wrote:
> >>>> u.get_all_permissions()
> > set([u'wb.delete_libraryitem', u'wb.change_libraryitem'])
> >>>> u.has_perm("wb.change_libraryitem")
> > True
>
> > ...Shouldn't that be False?
>
> Why would it be False? get_all_permissions: Returns a set of
> permission strings that the user has.... has_perm: Returns True if the
> user has the specified permission.
>
> https://docs.djangoproject.com/en/1.2/topics/auth/#django.contrib.aut...
Reply all
Reply to author
Forward
0 new messages