delete.py 3.7 KB

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