Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object level cascading permission in Django

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?

like image 258
Tony Avatar asked Jun 11 '12 06:06

Tony


1 Answers

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.

like image 70
jpic Avatar answered Nov 20 '22 00:11

jpic