Browse Source

anonymize_old_ips Fixes #1000 (#1002)

Add removeoldips management command.
Devarakonda Veera Venkata Avinash 7 years ago
parent
commit
a964886dbc

+ 7 - 0
misago/conf/defaults.py

@@ -344,3 +344,10 @@ MISAGO_MOMENT_JS_LOCALES = [
     'vi',
     'zh-cn', 'zh-hans', 'zh-tw',
 ]
+
+
+# Specifies the number of days that IP addresses are stored in the database before removing.
+# Change this setting to None to never remove old IP addresses.
+
+MISAGO_IP_STORE_TIME = None
+

+ 7 - 0
misago/project_template/project_name/settings.py

@@ -409,3 +409,10 @@ MISAGO_PROFILE_FIELDS = [
         ],
     },
 ]
+
+
+# Specifies the number of days that IP addresses are stored in the database before removing.
+# Change this setting to None to never remove old IP addresses.
+
+MISAGO_IP_STORE_TIME = 50
+

+ 19 - 0
misago/users/management/commands/removeoldips.py

@@ -0,0 +1,19 @@
+from django.contrib.auth import get_user_model
+from django.core.management import BaseCommand
+
+from misago.conf import settings
+from misago.core.utils import ANONYMOUS_IP
+from misago.users.signals import remove_old_ips
+
+
+class Command(BaseCommand):
+    help =  "Removes users IPs stored for longer than set in MISAGO_IP_STORE_TIME."
+    
+    def handle(self, *args, **options):
+      if not settings.MISAGO_IP_STORE_TIME:
+        self.stdout.write("Old IP removal is disabled.")
+        return
+      
+      remove_old_ips.send(sender=self)
+      self.stdout.write("IP addresses older than {} days have been removed.".format(settings.MISAGO_IP_STORE_TIME))
+

+ 22 - 0
misago/users/signals.py

@@ -1,11 +1,33 @@
+from datetime import timedelta
+
+from django.contrib.auth import get_user_model
+from django.db.models import Q
 from django.dispatch import Signal, receiver
+from django.utils import timezone
+
+from misago.conf import settings
+from misago.core.utils import ANONYMOUS_IP
+
 
+UserModel = get_user_model()
 
 anonymize_user_content = Signal()
 delete_user_content = Signal()
+remove_old_ips = Signal()
 username_changed = Signal()
 
 
 @receiver(username_changed)
 def handle_name_change(sender, **kwargs):
     sender.user_renames.update(changed_by_username=sender.username)
+
+
+@receiver(remove_old_ips)
+def anonymize_old_registrations_ips(sender, **kwargs):
+    anonymization_cutoff = timezone.now() - timedelta(days=settings.MISAGO_IP_STORE_TIME)
+    ip_is_too_new = Q(joined_on__gt=anonymization_cutoff)
+    ip_is_already_anonymized = Q(joined_from_ip=ANONYMOUS_IP)
+    
+    queryset = UserModel.objects.exclude(ip_is_too_new | ip_is_already_anonymized)
+    queryset.update(joined_from_ip=ANONYMOUS_IP)
+

+ 49 - 0
misago/users/tests/test_removeoldips.py

@@ -0,0 +1,49 @@
+from datetime import timedelta
+
+from django.contrib.auth import get_user_model
+from django.core.management import call_command
+from django.test import TestCase, override_settings
+from django.utils import timezone
+from django.utils.six import StringIO
+
+from misago.core.utils import ANONYMOUS_IP
+from misago.users.management.commands import removeoldips
+
+
+UserModel = get_user_model()
+
+
+class RemoveOldIpsTests(TestCase):
+    def test_removeoldips_recent_user(self):
+        """command is not removing user's IP if its recent"""
+        user = UserModel.objects.create_user('Bob', 'bob@bob.com')
+        call_command(removeoldips.Command())
+        user_joined_from_ip = UserModel.objects.get(pk=user.pk).joined_from_ip
+        
+        self.assertNotEqual(user_joined_from_ip, ANONYMOUS_IP)
+    
+    def test_removeoldips_old_user(self):
+        """command removes user's IP if its old"""
+        joined_on_past = timezone.now() - timedelta(days=50)
+        user = UserModel.objects.create_user('Bob1', 'bob1@bob.com')
+        user.joined_on = joined_on_past
+        user.save()
+
+        call_command(removeoldips.Command())
+        user_joined_from_ip = UserModel.objects.get(pk=user.pk).joined_from_ip
+        self.assertEqual(user_joined_from_ip, ANONYMOUS_IP)
+
+    @override_settings(MISAGO_IP_STORE_TIME=None)
+    def test_not_removing_user_ip(self):
+        """command is not removing user's IP if removing is disabled"""
+        user = UserModel.objects.create_user('Bob1', 'bob1@bob.com')
+        
+        out = StringIO()
+        call_command(removeoldips.Command(), stdout=out)
+        command_output = out.getvalue().splitlines()[0].strip()
+        
+        self.assertEqual(command_output, "Old IP removal is disabled.")
+        
+        user_joined_from_ip = UserModel.objects.get(pk=user.pk).joined_from_ip
+        self.assertNotEqual(user_joined_from_ip, ANONYMOUS_IP)
+