Object level cascading permission in Django

2024/10/10 20:20:47

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?

Answer

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://en.xdnf.cn/q/69856.html

Related Q&A

How do I find out eigenvectors corresponding to a particular eigenvalue of a matrix?

How do I find out eigenvectors corresponding to a particular eigenvalue? I have a stochastic matrix(P), one of the eigenvalues of which is 1. I need to find the eigenvector corresponding to the eigen…

How to install my custom Python package with its custom dependencies?

I would like to find a way to install my own python package which depends on other custom python packages. I followed this guide to create my own python packages: https://python-packaging.readthedocs.i…

How to call a function only Once in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.The com…

Generating Compound Pie, or Pie of Pie Charts

Below is an example of a compound pie chart, also known as a pie of pie chart drawn using Excel. Is it possible to create a figure like this using python?

GitPython : git push - set upstream

Im using GitPython to clone a master branch and do a checkout of a feature branch, I do my local updates, commit and push back to git. The code snippet looks like below, Note : my branch name is featur…

How to access multi-level index in pandas data frame?

I would like to call those row with same index.so this is the example data frame, arrays = [np.array([bar, bar, baz, baz, foo, foo, qux, qux]), np.array([one, two, one, two, one, two, one, two])]df = p…

How to represent graphs with IPython

Recently I discovered IPython notebook which is a powerful tool. As an IT student, I was looking for a way to represent graphs in Python. For example, I would like to know if theres a library (like num…

How to check TypeVars Type at runtime

I have a generic class Graph[Generic[T], object]. My question, is there any function which returns type passed as generic to the class Graph>>> g = Graph[int]() >>> magic_func(g) <…

Can django-pagination do multiple paginations per page?

If it cant then are there any other alternatives (either Djangos native pagination or an alternate package) that allows multiple paginations per page?I would like to display a list of about 5 objects …

List directory file contents in a Django template

Im just learning Python & Django. (Thanks to everyone who contributes here -- its been an invaluable resource!)One seemingly basic thing that Im having trouble with is rendering a simple list of s…