Browse Source

Destroy User Account ACL provider

Rafał Pitoń 11 years ago
parent
commit
7560a4ae1b

+ 60 - 0
misago/acl/permissions/destroyusers.py

@@ -0,0 +1,60 @@
+from django.utils import timezone
+from django.utils.translation import ugettext_lazy as _
+import floppyforms as forms
+from misago.acl.builder import BaseACL
+from misago.forms import YesNoSwitch
+from misago.acl.exceptions import ACLError403, ACLError404
+
+def make_form(request, role, form):
+    form.base_fields['can_destroy_user_older_than'] = forms.IntegerField(label=_("Maximum Age of destroyed account (in days)"),
+                                                                         help_text=_("Enter zero to disable this check."),
+                                                                         initial=0, min_value=0, required=False)
+    form.base_fields['can_destroy_user_with_more_posts_than'] = forms.IntegerField(label=_("Maximum number of posts on destroyed account"),
+                                                                                   help_text=_("Enter zero to disable this check."),
+                                                                                   initial=0, min_value=0, required=False)
+
+    form.fieldsets.append((
+                           _("Destroying User Accounts"),
+                           ('can_destroy_user_older_than',
+                            'can_destroy_user_with_more_posts_than')
+                          ))
+
+
+class DestroyUserACL(BaseACL):
+    def allow_destroy_user(self, user):
+        if not (self.acl['can_destroy_user_older_than']
+                or self.acl['can_destroy_user_with_more_posts_than']):
+            raise ACLError403(_("You can't destroy user accounts."))
+
+        if self.acl['can_destroy_user_older_than']:
+            user_age = timezone.now() - user.join_date
+            if user_age.days > self.acl['can_destroy_user_older_than']:
+                raise ACLError403(_("You can't destroy this user account. It's too old."))
+
+        if (self.acl['can_destroy_user_with_more_posts_than']
+                and user.posts > self.acl['can_destroy_user_with_more_posts_than']):
+            raise ACLError403(_("You can't destroy this user account. Too many messages were posted from it."))
+
+    def can_destroy_user(self, user):
+        try:
+            self.allow_destroy_user(user)
+        except ACLError403:
+            return False
+        return True
+
+
+def build(acl, roles):
+    acl.destroy_user = DestroyUserACL()
+    acl.destroy_user.acl['can_destroy_user_older_than'] = 0
+    acl.destroy_user.acl['can_destroy_user_with_more_posts_than'] = 0
+
+    for role in roles:
+        try:
+            if (role['can_destroy_user_older_than']
+                    and role['can_destroy_user_older_than'] > acl.destroy_user.acl['can_destroy_user_older_than']):
+                acl.special.acl['can_destroy_user_older_than'] = role['can_destroy_user_older_than']
+            if (role['can_destroy_user_with_more_posts_than']
+                    and role['can_destroy_user_with_more_posts_than'] > acl.destroy_user.acl['can_destroy_user_with_more_posts_than']):
+                acl.special.acl['can_destroy_user_with_more_posts_than'] = role['can_destroy_user_with_more_posts_than']
+        except KeyError:
+            pass

+ 1 - 1
misago/acl/permissions/special.py

@@ -11,7 +11,7 @@ def make_form(request, role, form):
         form.base_fields['can_use_acp'] = forms.BooleanField(label=_("Can use Admin Control Panel"),
         form.base_fields['can_use_acp'] = forms.BooleanField(label=_("Can use Admin Control Panel"),
                                                              help_text=_("Change this permission to yes to grant admin access for users with this role."),
                                                              help_text=_("Change this permission to yes to grant admin access for users with this role."),
                                                              widget=YesNoSwitch, initial=False, required=False)
                                                              widget=YesNoSwitch, initial=False, required=False)
-        
+
         form.fieldsets.append((
         form.fieldsets.append((
                                _("Special Access"),
                                _("Special Access"),
                                ('can_use_mcp', 'can_use_acp')
                                ('can_use_mcp', 'can_use_acp')

+ 1 - 0
misago/settings_base.py

@@ -145,6 +145,7 @@ PERMISSION_PROVIDERS = (
     'misago.acl.permissions.threads',
     'misago.acl.permissions.threads',
     'misago.acl.permissions.privatethreads',
     'misago.acl.permissions.privatethreads',
     'misago.acl.permissions.reports',
     'misago.acl.permissions.reports',
+    'misago.acl.permissions.destroyusers',
     'misago.acl.permissions.special',
     'misago.acl.permissions.special',
 )
 )