You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by ma...@apache.org on 2018/10/31 17:48:16 UTC
[airavata-django-portal] 02/09: AIRAVATA-2888 Username,
password validation
This is an automated email from the ASF dual-hosted git repository.
machristie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git
commit e93b56ec495e38b041bb61eedd5e4f16745b692a
Author: Marcus Christie <ma...@iu.edu>
AuthorDate: Wed Oct 24 11:20:17 2018 -0400
AIRAVATA-2888 Username, password validation
---
django_airavata/apps/auth/forms.py | 28 ++++++++++++++++++----
.../django_airavata_auth/create_account.html | 12 +++++++---
2 files changed, 32 insertions(+), 8 deletions(-)
diff --git a/django_airavata/apps/auth/forms.py b/django_airavata/apps/auth/forms.py
index 007d05f..1077e58 100644
--- a/django_airavata/apps/auth/forms.py
+++ b/django_airavata/apps/auth/forms.py
@@ -1,18 +1,36 @@
from django import forms
+from django.core import validators
from . import iam_admin_client
+USERNAME_VALIDATOR = validators.RegexValidator(
+ regex=r"^[a-z0-9_-]+$",
+ message="Username can only contain lowercase letters, numbers, "
+ "underscores and hyphens."
+)
+PASSWORD_VALIDATOR = validators.RegexValidator(
+ regex=r"^.*(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@!$#*&]).*$",
+ message="Password needs to contain at least (a) One lower case letter (b) "
+ "One Upper case letter and (c) One number (d) One of the following"
+ " special characters - !@#$&*"
+)
+
class CreateAccountForm(forms.Form):
error_css_class = "is-invalid"
username = forms.CharField(
label='Username',
widget=forms.TextInput(attrs={'class': 'form-control',
- 'placeholder': 'Username'}))
+ 'placeholder': 'Username'}),
+ min_length=6,
+ validators=[USERNAME_VALIDATOR])
password = forms.CharField(
label='Password',
widget=forms.PasswordInput(attrs={'class': 'form-control',
- 'placeholder': 'Password'}))
+ 'placeholder': 'Password'}),
+ min_length=8,
+ max_length=48,
+ validators=[PASSWORD_VALIDATOR])
password_again = forms.CharField(
label='Password (again)',
widget=forms.PasswordInput(attrs={'class': 'form-control',
@@ -41,7 +59,7 @@ class CreateAccountForm(forms.Form):
password = cleaned_data.get('password')
password_again = cleaned_data.get('password_again')
- if password != password_again:
+ if password and password_again and password != password_again:
self.add_error(
'password',
forms.ValidationError("Passwords do not match"))
@@ -51,7 +69,7 @@ class CreateAccountForm(forms.Form):
email = cleaned_data.get('email')
email_again = cleaned_data.get('email_again')
- if email != email_again:
+ if email and email_again and email != email_again:
self.add_error(
'email',
forms.ValidationError("E-mail addresses do not match")
@@ -62,7 +80,7 @@ class CreateAccountForm(forms.Form):
)
username = cleaned_data.get('username')
- if not iam_admin_client.is_username_available(username):
+ if username and not iam_admin_client.is_username_available(username):
self.add_error(
'username',
forms.ValidationError("That username is not available")
diff --git a/django_airavata/apps/auth/templates/django_airavata_auth/create_account.html b/django_airavata/apps/auth/templates/django_airavata_auth/create_account.html
index bd8a9e7..29ff91b 100644
--- a/django_airavata/apps/auth/templates/django_airavata_auth/create_account.html
+++ b/django_airavata/apps/auth/templates/django_airavata_auth/create_account.html
@@ -48,9 +48,15 @@
{% if field.value %} value="{{ field.value }}" {% endif %}
{% if field.field.required %} required {% endif %} />
<div class="invalid-feedback">
- {% for error in field.errors %}
- {{ error | escape }}
- {% endfor %}
+ {% if field.errors|length == 1 %}
+ {{ field.errors|first| escape }}
+ {% else %}
+ <ul>
+ {% for error in field.errors %}
+ <li>{{ error | escape }}</li>
+ {% endfor %}
+ </ul>
+ {% endif %}
</div>
</div>
{% endfor %}