Browse Source

Remove invalid VPN endpoints when the subnets change

Baptiste Jonglez 11 years ago
parent
commit
5b7e0af787
1 changed files with 20 additions and 7 deletions
  1. 20 7
      coin/vpn/models.py

+ 20 - 7
coin/vpn/models.py

@@ -31,10 +31,13 @@ class VPNSubscription(CoinLdapSyncModel):
 
 
     # These two methods are part of the general configuration interface.
     # These two methods are part of the general configuration interface.
     def save_subnet(self, subnet, creation):
     def save_subnet(self, subnet, creation):
-        self.sync_to_ldap(False)
+        self.check_endpoints(delete=True)
+        # We potentially changed the endpoints, so we need to save.
+        self.full_clean()
+        self.save()
 
 
     def delete_subnet(self, subnet):
     def delete_subnet(self, subnet):
-        self.sync_to_ldap(False)
+        self.save_subnet(subnet, False)
 
 
     def get_subnets(self, version):
     def get_subnets(self, version):
         subnets = self.administrative_subscription.ip_subnet.all()
         subnets = self.administrative_subscription.ip_subnet.all()
@@ -85,15 +88,25 @@ class VPNSubscription(CoinLdapSyncModel):
                 updated = True
                 updated = True
         return updated
         return updated
 
 
-    def check_endpoints(self):
+    def check_endpoints(self, delete=False):
         """Check that the IP endpoints are included in one of the attributed IP
         """Check that the IP endpoints are included in one of the attributed IP
         subnets.
         subnets.
+
+        If [delete] is True, then simply delete the faulty endpoints
+        instead of raising an exception.
         """
         """
         subnets = self.administrative_subscription.ip_subnet.all()
         subnets = self.administrative_subscription.ip_subnet.all()
-        for endpoint in [self.ipv4_endpoint, self.ipv6_endpoint]:
-            if endpoint:
-                if not any([endpoint in subnet.inet for subnet in subnets]):
-                    raise ValidationError("Endpoint {} is not in an attributed range".format(endpoint))
+        is_faulty = lambda endpoint : endpoint and not any([endpoint in subnet.inet for subnet in subnets])
+        if is_faulty(self.ipv4_endpoint):
+            if delete:
+                self.ipv4_endpoint = None
+            else:
+                raise ValidationError("Endpoint {} is not in an attributed range".format(self.ipv4_endpoint))
+        if is_faulty(self.ipv6_endpoint):
+            if delete:
+                self.ipv6_endpoint = None
+            else:
+                raise ValidationError("Endpoint {} is not in an attributed range".format(self.ipv6_endpoint))
 
 
     def clean(self):
     def clean(self):
         # Hash password if needed
         # Hash password if needed