From fc7f98539281750f5d7df43de5495c4a947cc592 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 27 Mar 2018 01:26:17 +0200 Subject: [PATCH] =?UTF-8?q?Adapte=20auth.py=20au=20nouveau=20syst=C3=A8me?= =?UTF-8?q?=20de=20switchs=20+=20optimisations=20forms?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- freeradius_utils/auth.py | 18 ++++++++++-------- topologie/forms.py | 15 +++++++++------ topologie/views.py | 21 +++++++++++++++------ 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/freeradius_utils/auth.py b/freeradius_utils/auth.py index 4ab45b25..c20764e9 100644 --- a/freeradius_utils/auth.py +++ b/freeradius_utils/auth.py @@ -192,19 +192,21 @@ def post_auth(data): mac = data.get('Calling-Station-Id', None) + # Switch et bornes héritent de machine et peuvent avoir plusieurs interfaces filles + nas_machine = nas_instance.machine # Si il s'agit d'un switch - if hasattr(nas_instance, 'switch'): + if hasattr(nas_machine, 'switch'): port = data.get('NAS-Port-Id', data.get('NAS-Port', None)) #Pour les infrastructures possédant des switchs Juniper : #On vérifie si le switch fait partie d'un stack Juniper - instance_stack = nas_instance.switch.stack + instance_stack = nas_machine.switch.stack if instance_stack: # Si c'est le cas, on resélectionne le bon switch dans la stack id_stack_member = port.split("-")[1].split('/')[0] - nas_instance = Switch.objects.filter(stack=instance_stack).filter(stack_member_id=id_stack_member).select_related('domain__extension').first() + nas_machine = Switch.objects.filter(stack=instance_stack).filter(stack_member_id=id_stack_member).prefetch_related('interface_set__domain__extension').first() # On récupère le numéro du port sur l'output de freeradius. La ligne suivante fonctionne pour cisco, HP et Juniper port = port.split(".")[0].split('/')[-1][-2:] - out = decide_vlan_and_register_switch(nas_instance, nas_type, port, mac) + out = decide_vlan_and_register_switch(nas_machine, nas_type, port, mac) sw_name, room, reason, vlan_id = out log_message = '(fil) %s -> %s [%s%s]' % \ @@ -271,7 +273,7 @@ def check_user_machine_and_register(nas_type, username, mac_address): return (False, u"Machine inconnue", '') -def decide_vlan_and_register_switch(nas, nas_type, port_number, mac_address): +def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, mac_address): """Fonction de placement vlan pour un switch en radius filaire auth par mac. Plusieurs modes : - nas inconnu, port inconnu : on place sur le vlan par defaut VLAN_OK @@ -296,12 +298,12 @@ def decide_vlan_and_register_switch(nas, nas_type, port_number, mac_address): # Get port from switch and port number extra_log = "" # Si le NAS est inconnu, on place sur le vlan defaut - if not nas: + if not nas_machine: return ('?', u'Chambre inconnue', u'Nas inconnu', VLAN_OK) - sw_name = str(nas) + sw_name = str(nas_machine) - port = Port.objects.filter(switch=Switch.objects.filter(interface_ptr=nas), port=port_number).first() + port = Port.objects.filter(switch=Switch.objects.filter(machine_ptr=nas_machine), port=port_number).first() #Si le port est inconnu, on place sur le vlan defaut if not port: return (sw_name, "Chambre inconnue", u'Port inconnu', VLAN_OK) diff --git a/topologie/forms.py b/topologie/forms.py index 653cd0c5..7b8f0955 100644 --- a/topologie/forms.py +++ b/topologie/forms.py @@ -40,6 +40,7 @@ from machines.forms import ( ) from django import forms from django.forms import ModelForm, Form +from django.db.models import Prefetch from .models import ( Port, Switch, @@ -82,9 +83,10 @@ class EditPortForm(ModelForm): self.fields['machine_interface'].queryset = Interface.objects.all()\ .select_related('domain__extension') self.fields['related'].queryset = Port.objects.all()\ - .select_related('switch')\ - .prefetch_related('switch__interface_set__domain__extension')\ - .order_by('switch', 'port') + .prefetch_related(Prefetch( + 'switch__interface_set', + queryset=Interface.objects.select_related('ipv4__ip_type__extension').select_related('domain__extension') + )) class AddPortForm(ModelForm): @@ -100,9 +102,10 @@ class AddPortForm(ModelForm): self.fields['machine_interface'].queryset = Interface.objects.all()\ .select_related('domain__extension') self.fields['related'].queryset = Port.objects.all()\ - .select_related('switch')\ - .prefetch_related('switch__interface_set__domain__extension')\ - .order_by('switch', 'port') + .prefetch_related(Prefetch( + 'switch__interface_set', + queryset=Interface.objects.select_related('ipv4__ip_type__extension').select_related('domain__extension') + )) class StackForm(ModelForm): diff --git a/topologie/views.py b/topologie/views.py index 68febece..bc53c77b 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -41,7 +41,7 @@ from django.contrib import messages from django.contrib.auth.decorators import login_required, permission_required from django.db import IntegrityError from django.db import transaction -from django.db.models import ProtectedError +from django.db.models import ProtectedError, Prefetch from django.core.exceptions import ValidationError from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from reversion import revisions as reversion @@ -84,6 +84,7 @@ from machines.forms import ( AddInterfaceForm ) from machines.views import generate_ipv4_mbf_param +from machines.models import Interface from preferences.models import AssoOption, GeneralOption @@ -92,9 +93,10 @@ from preferences.models import AssoOption, GeneralOption def index(request): """ Vue d'affichage de tous les swicthes""" switch_list = Switch.objects\ - .prefetch_related('interface_set__domain__extension')\ - .prefetch_related('interface_set__ipv4__ip_type')\ - .prefetch_related('interface_set__type__ip_type__extension')\ + .prefetch_related(Prefetch( + 'interface_set', + queryset=Interface.objects.select_related('ipv4__ip_type__extension').select_related('domain__extension') + ))\ .select_related('stack') switch_list = SortTable.sort( switch_list, @@ -128,7 +130,10 @@ def index_port(request, switch, switch_id): .select_related('machine_interface__domain__extension')\ .select_related('machine_interface__machine__user')\ .select_related('related__switch')\ - .prefetch_related('related__switch__interface_set__domain__extension')\ + .prefetch_related(Prefetch( + 'related__switch__interface_set', + queryset=Interface.objects.select_related('domain__extension') + ))\ .select_related('switch') port_list = SortTable.sort( port_list, @@ -174,7 +179,11 @@ def index_room(request): @can_view_all(AccessPoint) def index_ap(request): """ Affichage de l'ensemble des bornes""" - ap_list = AccessPoint.objects + ap_list = AccessPoint.objects\ + .prefetch_related(Prefetch( + 'interface_set', + queryset=Interface.objects.select_related('ipv4__ip_type__extension').select_related('domain__extension') + )) ap_list = SortTable.sort( ap_list, request.GET.get('col'),