From 623be9ac834baeca6b53adf0a4e06fa947195b0b Mon Sep 17 00:00:00 2001 From: Charlie Jacomme Date: Fri, 31 Aug 2018 18:11:17 +0200 Subject: [PATCH] Specify range for autocapture --- machines/models.py | 5 ++- ...051_optionalmachine_autocapture_iprange.py | 21 +++++++++ preferences/models.py | 7 +++ .../preferences/display_preferences.html | 5 +++ users/views.py | 45 +++++++++++-------- 5 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 preferences/migrations/0051_optionalmachine_autocapture_iprange.py diff --git a/machines/models.py b/machines/models.py index 4de0b012..f6168d37 100644 --- a/machines/models.py +++ b/machines/models.py @@ -397,7 +397,10 @@ class IpType(RevMixin, AclMixin, models.Model): @cached_property def complete_prefixv6(self): """Return the complete prefix v6 as cidr""" - return str(self.prefix_v6) + "/" + str(self.prefix_v6_length) + if self.prefix_v6: + return str(self.prefix_v6) + "/" + str(self.prefix_v6_length) + else: + return None def ip_objects(self): """ Renvoie tous les objets ipv4 relié à ce type""" diff --git a/preferences/migrations/0051_optionalmachine_autocapture_iprange.py b/preferences/migrations/0051_optionalmachine_autocapture_iprange.py new file mode 100644 index 00000000..df104d9b --- /dev/null +++ b/preferences/migrations/0051_optionalmachine_autocapture_iprange.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-08-31 14:37 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0094_auto_20180815_1918'), + ('preferences', '0050_auto_20180818_1329'), + ] + + operations = [ + migrations.AddField( + model_name='optionalmachine', + name='autocapture_iprange', + field=models.ManyToManyField(blank=True, help_text="Définit la plage d'ip depuis laquel un utilisateur peut enregistrer une machine filaire", to='machines.IpType'), + ), + ] diff --git a/preferences/models.py b/preferences/models.py index 3199dd6c..32de668e 100644 --- a/preferences/models.py +++ b/preferences/models.py @@ -149,6 +149,13 @@ class OptionalMachine(AclMixin, PreferencesModel): default=True ) + autocapture_iprange = models.ManyToManyField( + 'machines.IpType', + help_text=_("Définit la plage d'ip depuis laquel un utilisateur peut enregistrer une machine filaire"), + blank=True, + ) + + @cached_property def ipv6(self): """ Check if the IPv6 option is activated """ diff --git a/preferences/templates/preferences/display_preferences.html b/preferences/templates/preferences/display_preferences.html index 6a499969..6cb4c3a7 100644 --- a/preferences/templates/preferences/display_preferences.html +++ b/preferences/templates/preferences/display_preferences.html @@ -96,6 +96,11 @@ with this program; if not, write to the Free Software Foundation, Inc., {% trans "Creation of machines" %} {{ machineoptions.create_machine|tick }} + {% trans "IPs range d'autoregister" %} + {% for type in machineoptions.autocapture_iprange.all %} + {{type.type}} + {% endfor %} +

{% trans "Topology preferences" %}

diff --git a/users/views.py b/users/views.py index 360e256b..b221f170 100644 --- a/users/views.py +++ b/users/views.py @@ -47,13 +47,14 @@ from django.http import HttpResponse from django.http import HttpResponseRedirect from django.views.decorators.csrf import csrf_exempt from django.utils.translation import ugettext as _ +from django.core.exceptions import PermissionDenied from rest_framework.renderers import JSONRenderer from reversion import revisions as reversion from cotisations.models import Facture, Paiement from machines.models import Machine -from preferences.models import OptionalUser, GeneralOption, AssoOption +from preferences.models import OptionalUser, OptionalMachine, GeneralOption, AssoOption from re2o.views import form from re2o.utils import ( all_has_access, @@ -71,6 +72,8 @@ from re2o.acl import ( ) from cotisations.utils import find_payment_method +from netaddr import IPAddress, IPNetwork + from .serializers import MailingSerializer, MailingMemberSerializer from .models import ( User, @@ -1084,27 +1087,31 @@ def process_passwd(request, req): @login_required def initial_register(request): - u_form = InitialRegisterForm(request.POST or None, user=request.user, switch_ip=request.GET.get('switch_ip', None), switch_port=request.GET.get('switch_port', None), client_mac=request.GET.get('client_mac', None)) - if not u_form.fields: - messages.error(request, _("Incorrect URL, or already registered device")) - return redirect(reverse( - 'users:profil', - kwargs={'userid': str(request.user.id)} - )) - if u_form.is_valid(): - messages.success(request, _("Successful registration! Please" - " disconnect and reconnect your Ethernet" - " cable to get Internet access.")) + options, created = OptionalMachine.objects.get_or_create() + if any(IPAddress(request.META['REMOTE_ADDR']) in t.ip_set for t in options.autocapture_iprange.all()) or any(IPAddress(request.META['REMOTE_ADDR']) in IPNetwork(t.complete_prefixv6) for t in options.autocapture_iprange.all() if t.complete_prefixv6 ): + u_form = InitialRegisterForm(request.POST or None, user=request.user, switch_ip=request.GET.get('switch_ip', None), switch_port=request.GET.get('switch_port', None), client_mac=request.GET.get('client_mac', None)) + if not u_form.fields: + messages.error(request, _("Incorrect URL, or already registered device")) + return redirect(reverse( + 'users:profil', + kwargs={'userid': str(request.user.id)} + )) + if u_form.is_valid(): + messages.success(request, _("Successful registration! Please" + " disconnect and reconnect your Ethernet" + " cable to get Internet access.")) + return form( + {}, + 'users/plugin_out.html', + request + ) return form( - {}, - 'users/plugin_out.html', + {'userform': u_form, 'action_name': _("Register device or room")}, + 'users/user.html', request ) - return form( - {'userform': u_form, 'action_name': _("Register device or room")}, - 'users/user.html', - request - ) + else: + raise PermissionDenied class JSONResponse(HttpResponse):