django-orchestra-test/orchestra/permissions/options.py

107 lines
3.4 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
import functools
import inspect
# WARNING: *MAGIC MODULE*
# This is not a safe place, lot of magic is happening here
class Permission(object):
"""
2014-05-08 16:59:35 +00:00
Base class used for defining class and instance permissions.
Enabling an ''intuitive'' interface for checking permissions:
2014-05-08 16:59:35 +00:00
# Define permissions
class NodePermission(Permission):
def change(self, obj, cls, user):
return obj.user == user
2014-05-08 16:59:35 +00:00
# Provide permissions
Node.has_permission = NodePermission()
2014-05-08 16:59:35 +00:00
# Check class permission by passing it as string
Node.has_permission(user, 'change')
2014-05-08 16:59:35 +00:00
# Check class permission by calling it
Node.has_permission.change(user)
2014-05-08 16:59:35 +00:00
# Check instance permissions
node = Node()
node.has_permission(user, 'change')
node.has_permission.change(user)
"""
def __get__(self, obj, cls):
""" Hacking object internals to provide means for the mentioned interface """
# call interface: has_permission(user, 'perm')
def call(user, perm):
return getattr(self, perm)(obj, cls, user)
2014-05-08 16:59:35 +00:00
# has_permission.perm(user)
for func in inspect.getmembers(type(self), predicate=inspect.ismethod):
2015-04-02 16:14:55 +00:00
if not isinstance(self, func[1].__self__.__class__):
2014-05-08 16:59:35 +00:00
# aggregated methods
setattr(call, func[0], functools.partial(func[1], obj, cls))
else:
# self methods
setattr(call, func[0], functools.partial(func[1], self, obj, cls))
return call
2014-05-08 16:59:35 +00:00
def _aggregate(self, obj, cls, perm):
""" Aggregates cls methods to self class"""
for method in inspect.getmembers(perm, predicate=inspect.ismethod):
if not method[0].startswith('_'):
setattr(type(self), method[0], method[1])
class ReadOnlyPermission(Permission):
""" Read only permissions """
def view(self, obj, cls, user):
return True
class AllowAllPermission(object):
""" All methods return True """
def __get__(self, obj, cls):
return self.AllowAllWrapper()
class AllowAllWrapper(object):
""" Fake object that always returns True """
def __call__(self, *args):
return True
2014-05-08 16:59:35 +00:00
def __getattr__(self, name):
return lambda n: True
class RelatedPermission(Permission):
"""
Inherit permissions of a related object
2014-05-08 16:59:35 +00:00
The following example will inherit permissions from sliver_iface.sliver.slice
SliverIfaces.has_permission = RelatedPermission('sliver.slices')
"""
def __init__(self, relation):
self.relation = relation
2014-05-08 16:59:35 +00:00
def __get__(self, obj, cls):
""" Hacking object internals to provide means for the mentioned interface """
# Walk through FK relations
relations = self.relation.split('.')
if obj is None:
parent = cls
for relation in relations:
parent = getattr(parent, relation).field.related_model
2014-05-08 16:59:35 +00:00
else:
2015-04-02 16:14:55 +00:00
parent = functools.reduce(getattr, relations, obj)
2014-05-08 16:59:35 +00:00
# call interface: has_permission(user, 'perm')
def call(user, perm):
return parent.has_permission(user, perm)
2014-05-08 16:59:35 +00:00
# method interface: has_permission.perm(user)
2015-04-02 16:14:55 +00:00
for name, func in parent.has_permission.__dict__.items():
2014-05-08 16:59:35 +00:00
if not name.startswith('_'):
setattr(call, name, func)
2014-05-08 16:59:35 +00:00
return call