permissions.py 3.7 KB

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