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(
|
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"),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
|
@ -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),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
|
@ -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',
|
||||||
|
|
|
@ -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)
|
||||||
|
|
||||||
|
|
|
@ -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,16 +85,17 @@ 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)
|
||||||
new_interface.machine = new_machine
|
if full_domain_validator(request, new_interface, new_machine):
|
||||||
if free_ip() and not new_interface.ipv4:
|
new_machine.save()
|
||||||
new_interface = assign_ipv4(new_interface)
|
new_interface.machine = new_machine
|
||||||
elif not new_interface.ipv4:
|
if free_ip() and not new_interface.ipv4:
|
||||||
messages.error(request, u"Il n'y a plus d'ip disponibles")
|
new_interface = assign_ipv4(new_interface)
|
||||||
new_interface.save()
|
elif not new_interface.ipv4:
|
||||||
messages.success(request, "La machine a été crée")
|
messages.error(request, u"Il n'y a plus d'ip disponibles")
|
||||||
return redirect("/users/profil/" + userid)
|
new_interface.save()
|
||||||
|
messages.success(request, "La machine a été crée")
|
||||||
|
return redirect("/users/profil/" + userid)
|
||||||
return form({'machineform': machine, 'interfaceform': interface}, 'machines/machine.html', request)
|
return form({'machineform': machine, 'interfaceform': interface}, 'machines/machine.html', request)
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
@ -92,10 +115,13 @@ 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)
|
||||||
messages.success(request, "La machine a été modifiée")
|
if full_domain_validator(request, new_interface, new_machine):
|
||||||
return redirect("/users/profil/" + str(interface.machine.user.id))
|
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)
|
return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request)
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
@ -117,13 +143,14 @@ 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 free_ip() and not new_interface.ipv4:
|
if full_domain_validator(request, new_interface, machine):
|
||||||
new_interface = assign_ipv4(new_interface)
|
if free_ip() and not new_interface.ipv4:
|
||||||
elif not new_interface.ipv4:
|
new_interface = assign_ipv4(new_interface)
|
||||||
messages.error(request, u"Il n'y a plus d'ip disponibles")
|
elif not new_interface.ipv4:
|
||||||
new_interface.save()
|
messages.error(request, u"Il n'y a plus d'ip disponibles")
|
||||||
messages.success(request, "L'interface a été ajoutée")
|
new_interface.save()
|
||||||
return redirect("/machines/")
|
messages.success(request, "L'interface a été ajoutée")
|
||||||
|
return redirect("/machines/")
|
||||||
return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request)
|
return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request)
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
|
|
@ -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'
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue