mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-25 22:22:26 +00:00
Fix erreurs de validation du dns
This commit is contained in:
parent
6099d60147
commit
21c495f06c
6 changed files with 51 additions and 55 deletions
|
@ -15,6 +15,6 @@ class Migration(migrations.Migration):
|
|||
migrations.AlterField(
|
||||
model_name='interface',
|
||||
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"),
|
||||
),
|
||||
]
|
||||
|
|
|
@ -15,6 +15,6 @@ class Migration(migrations.Migration):
|
|||
migrations.AlterField(
|
||||
model_name='interface',
|
||||
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),
|
||||
),
|
||||
]
|
||||
|
|
|
@ -23,7 +23,7 @@ class Migration(migrations.Migration):
|
|||
migrations.AlterField(
|
||||
model_name='interface',
|
||||
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(
|
||||
model_name='machinetype',
|
||||
|
|
|
@ -2,31 +2,6 @@ from django.db import models
|
|||
from django.forms import ValidationError
|
||||
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):
|
||||
user = models.ForeignKey('users.User', on_delete=models.PROTECT)
|
||||
type = models.ForeignKey('MachineType', on_delete=models.PROTECT)
|
||||
|
@ -66,9 +41,6 @@ class Interface(models.Model):
|
|||
def __str__(self):
|
||||
return self.dns
|
||||
|
||||
def clean(self):
|
||||
self.dns=full_domain_validator(self)
|
||||
|
||||
class IpList(models.Model):
|
||||
ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True)
|
||||
|
||||
|
|
|
@ -8,11 +8,33 @@ from django.template import Context, RequestContext, loader
|
|||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required, permission_required
|
||||
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 .models import Machine, Interface, IpList, MachineType, Extension
|
||||
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):
|
||||
machines = Interface.objects.filter(machine=Machine.objects.filter(user=user))
|
||||
for machine in machines:
|
||||
|
@ -63,8 +85,9 @@ def new_machine(request, userid):
|
|||
if machine.is_valid() and interface.is_valid():
|
||||
new_machine = machine.save(commit=False)
|
||||
new_machine.user = user
|
||||
new_machine.save()
|
||||
new_interface = interface.save(commit=False)
|
||||
if full_domain_validator(request, new_interface, new_machine):
|
||||
new_machine.save()
|
||||
new_interface.machine = new_machine
|
||||
if free_ip() and not new_interface.ipv4:
|
||||
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)
|
||||
interface_form = EditInterfaceForm(request.POST or None, instance=interface)
|
||||
if machine_form.is_valid() and interface_form.is_valid():
|
||||
machine_form.save()
|
||||
interface_form.save()
|
||||
new_interface = interface_form.save(commit=False)
|
||||
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")
|
||||
return redirect("/users/profil/" + str(interface.machine.user.id))
|
||||
return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request)
|
||||
|
@ -117,6 +143,7 @@ def new_interface(request, machineid):
|
|||
machine_form.save()
|
||||
new_interface = interface_form.save(commit=False)
|
||||
new_interface.machine = machine
|
||||
if full_domain_validator(request, new_interface, machine):
|
||||
if free_ip() and not new_interface.ipv4:
|
||||
new_interface = assign_ipv4(new_interface)
|
||||
elif not new_interface.ipv4:
|
||||
|
|
|
@ -76,16 +76,13 @@ TEMPLATES = [
|
|||
'django.template.context_processors.request',
|
||||
'django.contrib.auth.context_processors.auth',
|
||||
'django.contrib.messages.context_processors.messages',
|
||||
'django.core.context_processors.request',
|
||||
're2o.context_processors.context_user',
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
TEMPLATE_CONTEXT_PROCESSORS = (
|
||||
'django.core.context_processors.request',
|
||||
)
|
||||
|
||||
WSGI_APPLICATION = 're2o.wsgi.application'
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue