【问题标题】:Django permission belongs to group, but user on that group has no group's permissionDjango 权限属于组,但该组上的用户没有组的权限
【发布时间】:2018-04-28 04:27:55
【问题描述】:

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

FailedTestCase:

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 user has_perm returning false even though group has permission
Django user not getting the assigned group's permissions
django group permission

【问题讨论】:

  • 对不起。糟糕的是,我刚刚更新了FailedTestCase gist 中的调试行。
  • 哦,我的错。 user.has_perm('userprofiles.view_userprofile')。我忘记了app_label
  • 我强烈建议更正这些示例。它们很难追踪。 add_support 方法是什么?她的密码在哪里?为什么是 pdb?为什么不使用控制台模式来防止示例后不必要的跳转?模型在哪里(CompanyRoleUserRole 是什么?)?

标签: python django permissions django-guardian


【解决方案1】:

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

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

我必须将user.has_perm('view_userprofile') 行替换为user.has_perm('userprofiles.view_userprofile')

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

【讨论】:

    猜你喜欢
    • 2020-11-04
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 2015-07-16
    • 2013-05-04
    • 2019-09-15
    • 2020-03-31
    • 2020-12-19
    相关资源
    最近更新 更多