permissions.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # coding=utf-8
  2. from __future__ import absolute_import
  3. from __future__ import division
  4. from __future__ import print_function
  5. from __future__ import unicode_literals
  6. from django.core.exceptions import ObjectDoesNotExist
  7. from rapid.models import Application, Profile
  8. __author__ = 'marcos.medeiros'
  9. # noinspection PyProtectedMember
  10. from registry import _caller_urls_module
  11. class Permission(object):
  12. """
  13. A permission for an registry entry.
  14. """
  15. def __init__(self, model, instances):
  16. self.model = model
  17. self.instances = instances
  18. def all_instances(model):
  19. """
  20. Shortcut function for granting permission over all instances of a model.
  21. For that use, create a permission with this function at its "instances" attribute.
  22. """
  23. def i(request):
  24. if model(request):
  25. return {}
  26. else:
  27. return None
  28. return i
  29. def apply_instances_permission(model, perm):
  30. """
  31. Returns the set of objects that a resolved permission has access to.
  32. :param model: ModelData of the model that'll be filtered
  33. :param perm: Resolved permission (that is, the result of evaluating permission.instances(request))
  34. """
  35. if perm is None:
  36. return []
  37. if hasattr(perm, 'keys'):
  38. return model.default_manager().filter(**perm)
  39. if hasattr(perm, '__iter__'):
  40. return perm
  41. if hasattr(perm, 'all'):
  42. return perm
  43. return []
  44. def has_instance(model, perm, instance):
  45. """
  46. Verifies if an object instance access is permitted
  47. :param model: ModelData of the desired model
  48. :param perm: Resolved permission (that is, the result of evaluating permission.instances(request))
  49. :param instance: Instance that'll be verified.
  50. """
  51. p = apply_instances_permission(model, perm)
  52. if hasattr(p, 'filter'):
  53. return p.filter(pk=instance.pk).exists()
  54. if hasattr(p, '__iter__'):
  55. return bool([f for f in p if f.pk == instance.pk])
  56. return False
  57. def to_profile(profile_name):
  58. """
  59. Grants permission over the model and all instances to the given profile(s)
  60. of the application the model is being registered.
  61. :param profile_name: Name or list of names of profiles that'll receive the permission.
  62. """
  63. app = Application.objects.get(python_name=_caller_urls_module()) # Should never fail
  64. if hasattr(profile_name, "__iter__"):
  65. profiles = [pf.pk for pf in Profile.objects.filter(name__in=profile_name, application=app).all()]
  66. def m(request):
  67. if not request.user.is_authenticated():
  68. return False
  69. up = [p.pk for p in request.user.profile_set]
  70. for p in up:
  71. if p in profiles:
  72. return True
  73. return False
  74. else:
  75. profile = None
  76. try:
  77. profile = Profile.objects.get(name=profile_name, application=app)
  78. except ObjectDoesNotExist:
  79. # Profile is not registered yet. May happen after a deploy.
  80. return False
  81. def m(request):
  82. if not request.user.is_authenticated():
  83. return False
  84. up = [p.pk for p in request.user.profile_set.all()]
  85. if profile in up:
  86. return True
  87. return False
  88. return Permission(m, all_instances(m))
  89. def to_staff():
  90. """
  91. Grants permission over the model and all instances to every user with is_staff set.
  92. """
  93. def m(request):
  94. if request.user.is_authenticated() and request.user.is_staff:
  95. return True
  96. return False
  97. return Permission(m, all_instances(m))
  98. def to_all():
  99. """
  100. Grants permission over the model and all instances to all users.
  101. """
  102. # noinspection PyUnusedLocal
  103. def m(request):
  104. return True
  105. return Permission(m, all_instances(m))
  106. def to_superusers():
  107. """
  108. Grants permission over the model and all instances to every user with superuser set.
  109. """
  110. def m(request):
  111. if request.user.is_authenticated() and request.user.is_superuser:
  112. return True
  113. return False
  114. return Permission(m, all_instances(m))
  115. def to_application_managers(python_name):
  116. """
  117. Grants permission over the model and all instances to the manager of the given application
  118. :param python_name: Name of the desired application main module
  119. """
  120. app = [ap.pk for ap in Application.objects.filter(python_name=python_name).all()]
  121. def m(request):
  122. if not request.user.is_authenticated():
  123. return False
  124. up = [a.pk for a in request.user.managed_applications.all()]
  125. if app in up:
  126. return True
  127. return False
  128. return Permission(m, all_instances(m))