Django中的对象级别级联权限 [英] Object level cascading permission in Django

查看:220
本文介绍了Django中的对象级别级联权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

诸如Django-guardian和django-permissions之类的项目使您能够拥有对象级权限.但是,如果两个对象之间通过父子关系相互关联,则除非另有说明,否则子对象是否可以通过任何方式继承父对象的许可?例如,除非用户为子文件夹明确分配其他权限,否则subfolder应该继承parent folder的权限.

Projects such as Django-guardian and django-permissions enables you to have object level permissions. However, if two objects are related to each other by a parent-child relationship, is there any way for the child object to inherit permission from the parent object unless otherwise specified? For instance, a subfolder should inherit permission from parent folder unless a user explicitly assigns a different permission for the subfolder.

使用Django(尤其是Django-guardian模块)完成此操作的最佳方法是什么?

What's the best way to accomplish this using Django, in particular, the Django-guardian module?

推荐答案

当您检查用户是否对某个对象具有权限时,您可以检查该用户是否对其父对象具有权限.

When you check if a user has permissions on an object, and doesn't, then you can check if it has permission on its parent.

您甚至可能想要创建自己的功能,例如:

You might even want to make your own function, for example:

def your_has_perm(user, perm, obj):
    has = user.has_perm(perm, obj)

    if not has and hasattr(obj, 'parent'):
        return your_has_perm(user, perm, obj.parent)

    return has

这应该遍历父母,直到找到父母的许可或返回False.

This should traverse parents until it finds a permission for a parent or return False.

这篇关于Django中的对象级别级联权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆