Object level cascading permission in Django

核能气质少年 提交于 2019-12-10 14:22:30

问题


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.

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


回答1:


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

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



来源:https://stackoverflow.com/questions/10975486/object-level-cascading-permission-in-django

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