3

我决定不使用django-guardian,因为如果我的客户删除/创建Group,我必须增加更多开销ModelManager才能将现有权限转移到新组。因此,让filter_queryset通过 Django REST 过滤的实例对我来说很好。

失败测试案例:

from django.contrib.auth.models import User

from poinkbackend.apps.companies.models import Company
from poinkbackend.apps.roles.models import Role
from poinkbackend.apps.userroles.models import UserRole
from poinkbackend.apps.commons.tests import companies, userprofiles, branches

def test_support_view_userprofile(companies, userprofiles):
    company = Company.objects.get(name='Singh')
    support = Role.objects.add_support('firstsupport', company)
    user = User.objects.get(username='notty')
    UserRole.objects.create(user=user, role=support)
    user.refresh_from_db()
    import pdb; pdb.set_trace()
    assert True is user.has_perm('view_userprofile')
-> assert True is user.has_perm('view_userprofile')
(Pdb) user.groups.first()
<Group: firstsupport-Singh>
(Pdb) g1 = user.groups.first()
(Pdb) g1.permissions.all()
<QuerySet [<Permission: userprofiles | user profile | View UserProfile>]>
(Pdb) user.has_perm('view_userprofile')
False

问:
我哪里错了?

更新 2017 年 11 月 16 日 10:30 GMT+7 添加第 4 个网址 要点: 公司:https : //gist.github.com/elcolie/684ba34391d0a94df7ca98855cea765b 角色:https ://gist.github.com/elcolie/64a3a3daf22240b2072e113eb10164e2 用户角色 :https:/ /gist.github.com/elcolie/d28e7fcf54334a9f13df5fff1b7d9fe0 商业许可:https ://gist.github.com/elcolie/bbeb00f41db0c7884cee34c6bccaf5f9




参考:
Django 用户 has_perm 返回 false 即使组具有权限
Django 用户没有获得分配组的权限
django 组权限

4

1 回答 1

0

虽然我找到了错误的根本原因。我根据@Adam Dobrawy 的评论更新了我的问题。

问题是我混淆了守护者的命令。django-guardign语法可以在没有app_label前面的情况下使用。就我而言,它是userprofiles

我必须user.has_perm('view_userprofile')user.has_perm('userprofiles.view_userprofile')

再次。非常感谢亚当花时间和精力阅读我的问题。

于 2017-11-16T03:38:52.237 回答