destroyusers.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from django.utils import timezone
  2. from django.utils.translation import ugettext_lazy as _
  3. import floppyforms as forms
  4. from misago.acl.builder import BaseACL
  5. from misago.forms import YesNoSwitch
  6. from misago.acl.exceptions import ACLError403, ACLError404
  7. def make_form(request, role, form):
  8. if role.special != 'guest':
  9. form.base_fields['can_destroy_user_newer_than'] = forms.IntegerField(label=_("Maximum Age of destroyed account (in days)"),
  10. help_text=_("Enter zero to disable this check."),
  11. initial=0, min_value=0, required=False)
  12. form.base_fields['can_destroy_users_with_less_posts_than'] = forms.IntegerField(label=_("Maximum number of posts on destroyed account"),
  13. help_text=_("Enter zero to disable this check."),
  14. initial=0, min_value=0, required=False)
  15. form.fieldsets.append((
  16. _("Destroying User Accounts"),
  17. ('can_destroy_user_newer_than',
  18. 'can_destroy_users_with_less_posts_than')
  19. ))
  20. class DestroyUserACL(BaseACL):
  21. def allow_destroy_user(self, user):
  22. if not (self.acl['can_destroy_user_newer_than']
  23. or self.acl['can_destroy_users_with_less_posts_than']):
  24. raise ACLError403(_("You can't destroy user accounts."))
  25. if user.is_god() or user.is_team:
  26. raise ACLError403(_("This user account is protected and cannot be destroyed."))
  27. if self.acl['can_destroy_user_newer_than']:
  28. user_age = timezone.now() - user.join_date
  29. if user_age.days > self.acl['can_destroy_user_newer_than']:
  30. raise ACLError403(_("You can't destroy this user account. It's too old."))
  31. if (self.acl['can_destroy_users_with_less_posts_than']
  32. and user.posts > self.acl['can_destroy_users_with_less_posts_than']):
  33. raise ACLError403(_("You can't destroy this user account. Too many messages were posted from it."))
  34. def can_destroy_user(self, user):
  35. try:
  36. self.allow_destroy_user(user)
  37. except ACLError403:
  38. return False
  39. return True
  40. def build(acl, roles):
  41. acl.destroy_users = DestroyUserACL()
  42. acl.destroy_users.acl['can_destroy_user_newer_than'] = 0
  43. acl.destroy_users.acl['can_destroy_users_with_less_posts_than'] = 0
  44. for role in roles:
  45. try:
  46. if (role['can_destroy_user_newer_than']
  47. and role['can_destroy_user_newer_than'] > acl.destroy_users.acl['can_destroy_user_newer_than']):
  48. acl.destroy_users.acl['can_destroy_user_newer_than'] = role['can_destroy_user_newer_than']
  49. if (role['can_destroy_users_with_less_posts_than']
  50. and role['can_destroy_users_with_less_posts_than'] > acl.destroy_users.acl['can_destroy_users_with_less_posts_than']):
  51. acl.destroy_users.acl['can_destroy_users_with_less_posts_than'] = role['can_destroy_users_with_less_posts_than']
  52. except KeyError:
  53. pass