delete.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from datetime import timedelta
  2. from django import forms
  3. from django.contrib.auth import get_user_model
  4. from django.core.exceptions import PermissionDenied
  5. from django.utils import timezone
  6. from django.utils.translation import ugettext_lazy as _
  7. from django.utils.translation import ungettext
  8. from misago.acl import algebra
  9. from misago.acl.decorators import return_boolean
  10. from misago.acl.models import Role
  11. from misago.conf import settings
  12. class PermissionsForm(forms.Form):
  13. legend = _("Deleting users")
  14. can_delete_users_newer_than = forms.IntegerField(
  15. label=_("Maximum age of deleted account (in days)"),
  16. help_text=_("Enter zero to disable this check."),
  17. min_value=0,
  18. initial=0,
  19. )
  20. can_delete_users_with_less_posts_than = forms.IntegerField(
  21. label=_("Maximum number of posts on deleted account"),
  22. help_text=_("Enter zero to disable this check."),
  23. min_value=0,
  24. initial=0,
  25. )
  26. def change_permissions_form(role):
  27. if isinstance(role, Role) and role.special_role != 'anonymous':
  28. return PermissionsForm
  29. else:
  30. return None
  31. def build_acl(acl, roles, key_name):
  32. new_acl = {
  33. 'can_delete_users_newer_than': 0,
  34. 'can_delete_users_with_less_posts_than': 0,
  35. }
  36. new_acl.update(acl)
  37. return algebra.sum_acls(
  38. new_acl,
  39. roles=roles,
  40. key=key_name,
  41. can_delete_users_newer_than=algebra.greater,
  42. can_delete_users_with_less_posts_than=algebra.greater,
  43. )
  44. def add_acl_to_user(user, target):
  45. target.acl['can_delete'] = can_delete_user(user, target)
  46. if target.acl['can_delete']:
  47. target.acl['can_moderate'] = True
  48. def register_with(registry):
  49. registry.acl_annotator(get_user_model(), add_acl_to_user)
  50. def allow_delete_user(user, target):
  51. newer_than = user.acl_cache['can_delete_users_newer_than']
  52. less_posts_than = user.acl_cache['can_delete_users_with_less_posts_than']
  53. if not newer_than and not less_posts_than:
  54. raise PermissionDenied(_("You can't delete users."))
  55. if user.pk == target.pk:
  56. raise PermissionDenied(_("You can't delete your account."))
  57. if target.is_staff or target.is_superuser:
  58. raise PermissionDenied(_("You can't delete administrators."))
  59. if newer_than:
  60. if target.joined_on < timezone.now() - timedelta(days=newer_than):
  61. message = ungettext(
  62. "You can't delete users that are members for more than %(days)s day.",
  63. "You can't delete users that are members for more than %(days)s days.",
  64. newer_than,
  65. )
  66. raise PermissionDenied(message % {'days': newer_than})
  67. if less_posts_than:
  68. if target.posts > less_posts_than:
  69. message = ungettext(
  70. "You can't delete users that made more than %(posts)s post.",
  71. "You can't delete users that made more than %(posts)s posts.",
  72. less_posts_than,
  73. )
  74. raise PermissionDenied(message % {'posts': less_posts_than})
  75. can_delete_user = return_boolean(allow_delete_user)
  76. def allow_delete_own_account(user, target):
  77. if not settings.MISAGO_ENABLE_DELETE_OWN_ACCOUNT:
  78. raise PermissionDenied(_("You can't delete your account."))
  79. if user.pk != target.pk:
  80. raise PermissionDenied(_("You can't delete other users accounts."))
  81. if user.is_staff or user.is_superuser:
  82. raise PermissionDenied(
  83. _("You can't delete your account because you are an administrator.")
  84. )
  85. can_delete_own_account = return_boolean(allow_delete_own_account)