Django user has_perm returning false even though group has permission

一曲冷凌霜 提交于 2020-01-05 05:30:11

问题


I'm running into a problem where I've created a Group with certain permissions, and successfully added a user to that group, but when I check user.has_perm with that permission, I'm getting False.

I've even tried things like saving the user and re-fetching them from the database to avoid caching issues, but it makes no difference.

The terminal output below should give an idea of what's happening

# Get a group from the database and check its permissions
> special_group = Group.objects.get(name="special_group")
> a_perm = special_group.permissions.all()[0]
> a_perm.codename
'some.permission'

# Get a user from that group and check if they have those permissions
> a_user = special_group.user_set.all()[0]
> a_user.has_perm(a_perm.codename)
False
> a_perm.codename in a_user.get_group_permissions()
False

# Curiously, get_group_permission sort of returns what we need
> a_user.get_group_permissions()
{'ContentType object.some.permission'}

# If we essentially coax the group_permissions into a list and pull an item out of there, has_perm returns true, but this string is different to a_perm.codename
> a_user.has_perm(list(a_user.get_group_permissions())[0])
True

Does anyone know why has_perm isn't behaving as I'm expecting? We are using Django 1.10.3.

来源:https://stackoverflow.com/questions/43106704/django-user-has-perm-returning-false-even-though-group-has-permission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!