delete.py 3.6 KB

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