|
@@ -1,7 +1,11 @@
|
|
|
# coding=utf-8
|
|
|
+from django.core.exceptions import ObjectDoesNotExist
|
|
|
+from rapid.models import Application, Profile
|
|
|
|
|
|
__author__ = 'marcos.medeiros'
|
|
|
|
|
|
+from registry import _caller_urls_module
|
|
|
+
|
|
|
|
|
|
class Permission(object):
|
|
|
"""
|
|
@@ -57,21 +61,31 @@ def has_instance(model, perm, instance):
|
|
|
return False
|
|
|
|
|
|
|
|
|
-def to_profile(profile):
|
|
|
+def to_profile(profile_name):
|
|
|
"""
|
|
|
Grants permission over the model and all instances to the given profile(s)
|
|
|
- :param profile: A profile.id or an iterable of those.
|
|
|
+ :param profile_name: Name or list of names of profiles that'll receive the permission.
|
|
|
"""
|
|
|
- if hasattr(profile, "__iter__"):
|
|
|
+ app = Application.objects.get(python_name=_caller_urls_module()) # Should never fail
|
|
|
+ if hasattr(profile_name, "__iter__"):
|
|
|
+ profiles = [p.pk for p in Profile.objects.filter(name__in=profile_name, application=app).all()]
|
|
|
+
|
|
|
def m(request):
|
|
|
if not request.user.is_authenticated():
|
|
|
return False
|
|
|
up = [p.pk for p in request.user.profile_set]
|
|
|
for p in up:
|
|
|
- if p in profile:
|
|
|
+ if p in profiles:
|
|
|
return True
|
|
|
return False
|
|
|
else:
|
|
|
+ profile = None
|
|
|
+ try:
|
|
|
+ profile = Profile.objects.get(name=profile_name, application=app)
|
|
|
+ except ObjectDoesNotExist:
|
|
|
+ # Profile is not registered yet. May happen after a deploy.
|
|
|
+ return False
|
|
|
+
|
|
|
def m(request):
|
|
|
if not request.user.is_authenticated():
|
|
|
return False
|
|
@@ -114,11 +128,13 @@ def to_superusers():
|
|
|
return Permission(m, all_instances(m))
|
|
|
|
|
|
|
|
|
-def to_application_managers(app):
|
|
|
+def to_application_managers(python_name):
|
|
|
"""
|
|
|
Grants permission over the model and all instances to the manager of the given application
|
|
|
:param app: Application.id of the desired application
|
|
|
"""
|
|
|
+ app = [a.pk for a in Application.objects.filter(python_name=python_name).all()]
|
|
|
+
|
|
|
def m(request):
|
|
|
if not request.user.is_authenticated():
|
|
|
return False
|