permissions.py 3.7 KB

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