permissions.py 4.4 KB

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