From d411718fb194a643e02f26d15c5d69b8c9e9423f Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sun, 23 Feb 2025 13:25:18 -0800 Subject: [PATCH] dynamicdns: Handle addition of duplicate domains Tests: - Adding a new domain works. Adding a new domain with old name results in error message. - Editing a old domain works. Updating the domain name works. Updating configuration parameters without domain name change works. - Adding a domain with capital letters results in domain with lower cased letters. Reviewed-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/dynamicdns/forms.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/plinth/modules/dynamicdns/forms.py b/plinth/modules/dynamicdns/forms.py index a7165661b..a870ed28a 100644 --- a/plinth/modules/dynamicdns/forms.py +++ b/plinth/modules/dynamicdns/forms.py @@ -5,10 +5,12 @@ Forms for the dynamicsdns module. from django import forms from django.core import validators +from django.core.exceptions import ValidationError from django.utils.translation import gettext as _ from django.utils.translation import gettext_lazy from plinth import cfg +from plinth.modules.dynamicdns import get_config from plinth.utils import format_lazy @@ -94,10 +96,6 @@ class DomainForm(forms.Form): """Further validate and transform field data.""" cleaned_data = super().clean() - # Domain name is not case sensitive, but Let's Encrypt - # certificate paths use lower-case domain name. - cleaned_data['domain'] = cleaned_data['domain'].lower() - update_url = cleaned_data.get('update_url') password = cleaned_data.get('password') service_type = cleaned_data.get('service_type') @@ -129,3 +127,14 @@ class DomainForm(forms.Form): del cleaned_data['show_password'] return cleaned_data + + def clean_domain(self): + """Validate the domain field.""" + # Domain name is not case sensitive, but Let's Encrypt + # certificate paths use lower-case domain name. + domain = self.cleaned_data['domain'].lower() + initial_domain = self.initial.get('domain') + if initial_domain != domain and domain in get_config()['domains']: + raise ValidationError(_('Domain already exists.')) + + return domain