django-orchestra/orchestra/permissions/auth.py

48 lines
1.4 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
from django.contrib.auth.backends import ModelBackend
2015-05-01 17:23:22 +00:00
from django.apps import apps
2014-05-08 16:59:35 +00:00
class OrchestraPermissionBackend(ModelBackend):
supports_object_permissions = True
supports_anonymous_user = False
supports_inactive_user = False
def has_perm(self, user, perm, obj=None):
""" perm 'app.action_model' """
if not user.is_active:
return False
perm_type = perm.split('.')[1].split('_')[0]
if obj is None:
app_label = perm.split('.')[0]
model_label = perm.split('_')[1]
2015-05-01 17:23:22 +00:00
model = apps.get_model(app_label, model_label)
2014-05-08 16:59:35 +00:00
perm_manager = model
else:
perm_manager = obj
try:
is_authorized = perm_manager.has_permission(user, perm_type)
except AttributeError:
is_authorized = False
return is_authorized
def has_module_perms(self, user, app_label):
"""
Returns True if user_obj has any permissions in the given app_label.
"""
if not user.is_active:
return False
2015-05-01 17:23:22 +00:00
app = apps.get_app_config(app_label)
for model in apps.get_models(app):
2014-05-08 16:59:35 +00:00
try:
has_perm = model.has_permission.view(user)
except AttributeError:
pass
else:
if has_perm:
return True
return False