8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-27 01:13:46 +00:00

Fix erreurs de validation du dns

This commit is contained in:
Gabriel Detraz 2016-07-11 00:52:36 +02:00
parent 9465ea5d21
commit d75911794f
6 changed files with 51 additions and 55 deletions

View file

@ -15,6 +15,6 @@ class Migration(migrations.Migration):
migrations.AlterField( migrations.AlterField(
model_name='interface', model_name='interface',
name='dns', name='dns',
field=models.CharField(unique=True, validators=[machines.models.full_domain_validator], max_length=255, help_text="Obligatoire et unique, doit se terminer en .rez et ne pas comporter d'autres points"), field=models.CharField(unique=True, max_length=255, help_text="Obligatoire et unique, doit se terminer en .rez et ne pas comporter d'autres points"),
), ),
] ]

View file

@ -15,6 +15,6 @@ class Migration(migrations.Migration):
migrations.AlterField( migrations.AlterField(
model_name='interface', model_name='interface',
name='dns', name='dns',
field=models.CharField(unique=True, validators=[machines.models.full_domain_validator], help_text="Obligatoire et unique, doit se terminer en .example et ne pas comporter d'autres points", max_length=255), field=models.CharField(unique=True, help_text="Obligatoire et unique, doit se terminer en .example et ne pas comporter d'autres points", max_length=255),
), ),
] ]

View file

@ -23,7 +23,7 @@ class Migration(migrations.Migration):
migrations.AlterField( migrations.AlterField(
model_name='interface', model_name='interface',
name='dns', name='dns',
field=models.CharField(unique=True, max_length=255, validators=[machines.models.full_domain_validator], help_text="Obligatoire et unique, doit se terminer en .rez et ne pas comporter d'autres points"), field=models.CharField(unique=True, max_length=255, help_text="Obligatoire et unique, doit se terminer en .rez et ne pas comporter d'autres points"),
), ),
migrations.AddField( migrations.AddField(
model_name='machinetype', model_name='machinetype',

View file

@ -2,31 +2,6 @@ from django.db import models
from django.forms import ValidationError from django.forms import ValidationError
from macaddress.fields import MACAddressField from macaddress.fields import MACAddressField
import re
def full_domain_validator(interface):
""" Validation du nom de domaine, extensions dans type de machine, prefixe pas plus long que 63 caractères """
HOSTNAME_LABEL_PATTERN = re.compile("(?!-)[A-Z\d-]+(?<!-)$", re.IGNORECASE)
dns = interface.dns.lower()
allowed_extension = interface.machine.type.extension.name
if not dns.endswith(allowed_extension):
raise ValidationError(
"Le nom de domaine '%(label)s' doit comporter une extension valide en '%(extension)s'",
params={'label': dns, 'extension': allowed_extension},
)
dns_short=re.sub('%s$' % allowed_extension, '', dns)
if len(dns_short) > 63:
raise ValidationError(
"Le nom de domaine '%(label)s' est trop long (maximum de 63 caractères).",
params={'label': dns},
)
if not HOSTNAME_LABEL_PATTERN.match(dns_short):
raise ValidationError(
"Ce nom de domaine '%(label)s' contient des carractères interdits.",
params={'label': dns},
)
return dns
class Machine(models.Model): class Machine(models.Model):
user = models.ForeignKey('users.User', on_delete=models.PROTECT) user = models.ForeignKey('users.User', on_delete=models.PROTECT)
type = models.ForeignKey('MachineType', on_delete=models.PROTECT) type = models.ForeignKey('MachineType', on_delete=models.PROTECT)
@ -66,9 +41,6 @@ class Interface(models.Model):
def __str__(self): def __str__(self):
return self.dns return self.dns
def clean(self):
self.dns=full_domain_validator(self)
class IpList(models.Model): class IpList(models.Model):
ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True) ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True)

View file

@ -8,11 +8,33 @@ from django.template import Context, RequestContext, loader
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required from django.contrib.auth.decorators import login_required, permission_required
from django.db.models import ProtectedError from django.db.models import ProtectedError
from django.forms import ValidationError
import re
from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, NewInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, NewInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm
from .models import Machine, Interface, IpList, MachineType, Extension from .models import Machine, Interface, IpList, MachineType, Extension
from users.models import User from users.models import User
def full_domain_validator(request, interface, machine):
""" Validation du nom de domaine, extensions dans type de machine, prefixe pas plus long que 63 caractères """
HOSTNAME_LABEL_PATTERN = re.compile("(?!-)[A-Z\d-]+(?<!-)$", re.IGNORECASE)
dns = interface.dns.lower()
allowed_extension = machine.type.extension.name
if not dns.endswith(allowed_extension):
messages.error(request,
"Le nom de domaine %s doit comporter une extension valide en %s" % (dns, allowed_extension) )
return False
dns_short=re.sub('%s$' % allowed_extension, '', dns)
if len(dns_short) > 63:
messages.error(request,
"Le nom de domaine %s est trop long (maximum de 63 caractères)." % dns)
return False
if not HOSTNAME_LABEL_PATTERN.match(dns_short):
messages.error(request,
"Ce nom de domaine %s contient des carractères interdits." % dns)
return False
return True
def unassign_ips(user): def unassign_ips(user):
machines = Interface.objects.filter(machine=Machine.objects.filter(user=user)) machines = Interface.objects.filter(machine=Machine.objects.filter(user=user))
for machine in machines: for machine in machines:
@ -63,8 +85,9 @@ def new_machine(request, userid):
if machine.is_valid() and interface.is_valid(): if machine.is_valid() and interface.is_valid():
new_machine = machine.save(commit=False) new_machine = machine.save(commit=False)
new_machine.user = user new_machine.user = user
new_machine.save()
new_interface = interface.save(commit=False) new_interface = interface.save(commit=False)
if full_domain_validator(request, new_interface, new_machine):
new_machine.save()
new_interface.machine = new_machine new_interface.machine = new_machine
if free_ip() and not new_interface.ipv4: if free_ip() and not new_interface.ipv4:
new_interface = assign_ipv4(new_interface) new_interface = assign_ipv4(new_interface)
@ -92,8 +115,11 @@ def edit_machine(request, interfaceid):
machine_form = EditMachineForm(request.POST or None, instance=interface.machine) machine_form = EditMachineForm(request.POST or None, instance=interface.machine)
interface_form = EditInterfaceForm(request.POST or None, instance=interface) interface_form = EditInterfaceForm(request.POST or None, instance=interface)
if machine_form.is_valid() and interface_form.is_valid(): if machine_form.is_valid() and interface_form.is_valid():
machine_form.save() new_interface = interface_form.save(commit=False)
interface_form.save() new_machine = machine_form.save(commit=False)
if full_domain_validator(request, new_interface, new_machine):
new_machine.save()
new_interface.save()
messages.success(request, "La machine a été modifiée") messages.success(request, "La machine a été modifiée")
return redirect("/users/profil/" + str(interface.machine.user.id)) return redirect("/users/profil/" + str(interface.machine.user.id))
return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request) return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request)
@ -117,6 +143,7 @@ def new_interface(request, machineid):
machine_form.save() machine_form.save()
new_interface = interface_form.save(commit=False) new_interface = interface_form.save(commit=False)
new_interface.machine = machine new_interface.machine = machine
if full_domain_validator(request, new_interface, machine):
if free_ip() and not new_interface.ipv4: if free_ip() and not new_interface.ipv4:
new_interface = assign_ipv4(new_interface) new_interface = assign_ipv4(new_interface)
elif not new_interface.ipv4: elif not new_interface.ipv4:

View file

@ -76,16 +76,13 @@ TEMPLATES = [
'django.template.context_processors.request', 'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth', 'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages', 'django.contrib.messages.context_processors.messages',
'django.core.context_processors.request',
're2o.context_processors.context_user', 're2o.context_processors.context_user',
], ],
}, },
}, },
] ]
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
)
WSGI_APPLICATION = 're2o.wsgi.application' WSGI_APPLICATION = 're2o.wsgi.application'