【问题标题】:Restrict access to only owned content django限制仅访问拥有的内容 django
【发布时间】:2013-03-18 13:03:23
【问题描述】:

我正在使用 django-tastypie 编写 API。我有两个自定义权限问题,希望 django-guardian 可以解决。

我有两个用户组临床医生和患者。临床医生应该能够访问仅属于他们的患者的对象,而患者应该只能访问自己创建的对象。

我的代码如下:

class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        resource_name = 'auth/user'
        excludes = ['email', 'password', 'is_superuser']


class BlogPostResource(ModelResource):
    author = fields.ToOneField(UserResource, 'author', full=True)

    class Meta:
        queryset = BlogPost.objects.all()
        resource_name = 'posts'
        allowed_methods = ["get", "post"]
        # Add it here.
        authentication = BasicAuthentication()
        authorization = DjangoAuthorization()
        filtering = {
            'author': ALL_WITH_RELATIONS,
        }

如何使用权限来限制对此BlogPostResource 的访问?

【问题讨论】:

    标签: python django tastypie django-guardian


    【解决方案1】:

    您可以通过自定义 Authorization 类来实现这一点,例如:

    class CustomAuthorization(Authorization):
        def apply_limits(self, request, object_list):     
            ...
            clin_group = Group.objects.get(name='YOUR GROUP')
            if request and hasattr(request, 'user'):
                if clin_group in request.user.groups.all(): 
                     object_list = object_list.filter(user__in=request.user.patients.all()) # or however you stop clinician>patient relation
                else:
                     object_list = object_list.filter(user=request.user)
            return object_list 
    

    【讨论】:

    • 谢谢,这是我考虑过的一种方法。唯一的事情是(我忘记在我的问题中提到)我们还有一个正常的 django Web 界面来访问需要创建完全相同权限的内容,我不想分割访问策略代码......
    • @Prydie - 好吧,我明白了。您可以在设置访问控制的位置创建管理器,然后在您的核心应用程序/管理员和美味派应用程序中调用管理器?将代码保存在一个地方。
    • managers 允许您使用 BlogPost.objects.patient_posts(clinician_user) 之类的东西,请参阅 docs.djangoproject.com/en/dev/topics/db/managers/#managers 可能会有所帮助!
    【解决方案2】:

    我的最终解决方案基于来自@JamesOanswer。他的答案的问题是它是在重写Authorization 类之前为旧版本的 django-tastypie 编写的。这是我的代码供将来参考:

    from tastypie.authorization import Authorization
    from django.contrib.auth.models import Group
    from extendedusers.models import ExtendedUser
    
    
    class CustomAuthorization(Authorization):
        def read_list(self, object_list, bundle):
            clinician_group = Group.objects.get(name='clinician')
            if bundle.request and hasattr(bundle.request, 'user'):
                if clinician_group in bundle.request.user.groups.all():
                    patients = ExtendedUser.objects.filter(clinician_id=bundle.request.user.id)
                    object_list = object_list.filter(author__id__in=patients)
                else:
                    object_list = object_list.filter(author=bundle.request.user)
                return object_list
            else:
                return object_list.none()
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-23
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多