warnings.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from django.contrib.auth import get_user_model
  2. from django.core.exceptions import PermissionDenied
  3. from django.utils.translation import ugettext_lazy as _
  4. from misago.acl import algebra
  5. from misago.acl.decorators import return_boolean
  6. from misago.acl.models import Role
  7. from misago.core import forms
  8. from misago.users.models import UserWarning
  9. """
  10. Admin Permissions Form
  11. """
  12. NO_OWNED_ALL = ((0, _("No")), (1, _("Owned")), (2, _("All")))
  13. class PermissionsForm(forms.Form):
  14. legend = _("Warnings")
  15. can_see_other_users_warnings = forms.YesNoSwitch(
  16. label=_("Can see other users warnings"))
  17. can_warn_users = forms.YesNoSwitch(label=_("Can warn users"))
  18. can_be_warned = forms.YesNoSwitch(label=_("Can be warned"), initial=False)
  19. can_cancel_warnings = forms.TypedChoiceField(
  20. label=_("Can cancel warnings"),
  21. coerce=int,
  22. choices=NO_OWNED_ALL,
  23. initial=0)
  24. can_delete_warnings = forms.TypedChoiceField(
  25. label=_("Can delete warnings"),
  26. coerce=int,
  27. choices=NO_OWNED_ALL,
  28. initial=0)
  29. def change_permissions_form(role):
  30. if isinstance(role, Role) and role.special_role != 'anonymous':
  31. return PermissionsForm
  32. else:
  33. return None
  34. """
  35. ACL Builder
  36. """
  37. def build_acl(acl, roles, key_name):
  38. new_acl = {
  39. 'can_see_other_users_warnings': 0,
  40. 'can_warn_users': 0,
  41. 'can_cancel_warnings': 0,
  42. 'can_delete_warnings': 0,
  43. 'can_be_warned': 1,
  44. }
  45. new_acl.update(acl)
  46. return algebra.sum_acls(
  47. new_acl, roles=roles, key=key_name,
  48. can_see_other_users_warnings=algebra.greater,
  49. can_warn_users=algebra.greater,
  50. can_cancel_warnings=algebra.greater,
  51. can_delete_warnings=algebra.greater,
  52. can_be_warned=algebra.lower
  53. )
  54. """
  55. ACL's for targets
  56. """
  57. def add_acl_to_target(user, target):
  58. if isinstance(target, get_user_model()):
  59. add_acl_to_user(user, target)
  60. elif isinstance(target, UserWarning):
  61. add_acl_to_warning(user, target)
  62. def add_acl_to_user(user, target):
  63. target_acl = target.acl_
  64. target_acl['can_see_warnings'] = can_see_warnings(user, target)
  65. target_acl['can_warn'] = can_warn_user(user, target)
  66. target_acl['can_cancel_warnings'] = False
  67. target_acl['can_delete_warnings'] = False
  68. mod_permissions = (
  69. 'can_warn',
  70. )
  71. if target_acl['can_warn']:
  72. target_acl['can_moderate'] = True
  73. def add_acl_to_warning(user, target):
  74. target.acl['can_cancel'] = can_cancel_warning(user, target)
  75. target.acl['can_delete'] = can_delete_warning(user, target)
  76. can_moderate = target.acl['can_cancel'] or target.acl['can_delete']
  77. target.acl['can_moderate'] = can_moderate
  78. """
  79. ACL tests
  80. """
  81. def allow_see_warnings(user, target):
  82. if user.is_authenticated() and user.pk == target.pk:
  83. return None
  84. if not user.acl['can_see_other_users_warnings']:
  85. raise PermissionDenied(_("You can't see other users warnings."))
  86. can_see_warnings = return_boolean(allow_see_warnings)
  87. def allow_warn_user(user, target):
  88. if not user.acl['can_warn_users']:
  89. raise PermissionDenied(_("You can't warn users."))
  90. if not user.is_superuser and (target.is_staff or target.is_superuser):
  91. raise PermissionDenied(_("You can't warn administrators."))
  92. if not target.acl['can_be_warned']:
  93. message = _("%(username)s can't be warned.")
  94. raise PermissionDenied(message % {'username': target.username})
  95. can_warn_user = return_boolean(allow_warn_user)
  96. def allow_cancel_warning(user, target):
  97. if user.is_anonymous() or not user.acl['can_cancel_warnings']:
  98. raise PermissionDenied(_("You can't cancel warnings."))
  99. if user.acl['can_cancel_warnings'] == 1:
  100. if target.giver_id != user.pk:
  101. message = _("You can't cancel warnings issued by other users.")
  102. raise PermissionDenied(message)
  103. if target.is_canceled:
  104. raise PermissionDenied(_("This warning is already canceled."))
  105. can_cancel_warning = return_boolean(allow_cancel_warning)
  106. def allow_delete_warning(user, target):
  107. if user.is_anonymous() or not user.acl['can_delete_warnings']:
  108. raise PermissionDenied(_("You can't delete warnings."))
  109. if user.acl['can_delete_warnings'] == 1:
  110. if target.giver_id != user.pk:
  111. message = _("You can't delete warnings issued by other users.")
  112. raise PermissionDenied(message)
  113. can_delete_warning = return_boolean(allow_delete_warning)