airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From machris...@apache.org
Subject [airavata-django-portal] 03/05: AIRAVATA-2888 Create account form validation
Date Thu, 11 Oct 2018 21:11:20 GMT
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 5750d9be3b6a206383f85e2edafdc7db1d787e1c
Author: Marcus Christie <machrist@iu.edu>
AuthorDate: Thu Oct 11 12:57:34 2018 -0400

    AIRAVATA-2888 Create account form validation
---
 django_airavata/apps/auth/forms.py                 | 71 ++++++++++++++++++++++
 django_airavata/apps/auth/iam_admin_client.py      | 13 ++++
 .../django_airavata_auth/create_account.html       | 48 ++++++---------
 django_airavata/apps/auth/urls.py                  |  2 -
 django_airavata/apps/auth/utils.py                 |  6 +-
 django_airavata/apps/auth/views.py                 | 18 ++++--
 6 files changed, 115 insertions(+), 43 deletions(-)

diff --git a/django_airavata/apps/auth/forms.py b/django_airavata/apps/auth/forms.py
new file mode 100644
index 0000000..007d05f
--- /dev/null
+++ b/django_airavata/apps/auth/forms.py
@@ -0,0 +1,71 @@
+from django import forms
+
+from . import iam_admin_client
+
+
+class CreateAccountForm(forms.Form):
+    error_css_class = "is-invalid"
+    username = forms.CharField(
+        label='Username',
+        widget=forms.TextInput(attrs={'class': 'form-control',
+                                      'placeholder': 'Username'}))
+    password = forms.CharField(
+        label='Password',
+        widget=forms.PasswordInput(attrs={'class': 'form-control',
+                                          'placeholder': 'Password'}))
+    password_again = forms.CharField(
+        label='Password (again)',
+        widget=forms.PasswordInput(attrs={'class': 'form-control',
+                                          'placeholder': 'Password (again)'}))
+    email = forms.EmailField(
+        label='E-mail',
+        widget=forms.EmailInput(attrs={'class': 'form-control',
+                                       'placeholder': 'email@example.com'}))
+    email_again = forms.EmailField(
+        label='E-mail (again)',
+        widget=forms.EmailInput(
+            attrs={
+                'class': 'form-control',
+                'placeholder': 'email@example.com (again)'}))
+    first_name = forms.CharField(
+        label='First Name',
+        widget=forms.TextInput(attrs={'class': 'form-control',
+                                      'placeholder': 'First Name'}))
+    last_name = forms.CharField(
+        label='Last Name',
+        widget=forms.TextInput(attrs={'class': 'form-control',
+                                      'placeholder': 'Last Name'}))
+
+    def clean(self):
+        cleaned_data = super().clean()
+        password = cleaned_data.get('password')
+        password_again = cleaned_data.get('password_again')
+
+        if password != password_again:
+            self.add_error(
+                'password',
+                forms.ValidationError("Passwords do not match"))
+            self.add_error(
+                'password_again',
+                forms.ValidationError("Passwords do not match"))
+
+        email = cleaned_data.get('email')
+        email_again = cleaned_data.get('email_again')
+        if email != email_again:
+            self.add_error(
+                'email',
+                forms.ValidationError("E-mail addresses do not match")
+            )
+            self.add_error(
+                'email_again',
+                forms.ValidationError("E-mail addresses do not match")
+            )
+
+        username = cleaned_data.get('username')
+        if not iam_admin_client.is_username_available(username):
+            self.add_error(
+                'username',
+                forms.ValidationError("That username is not available")
+            )
+
+        return cleaned_data
diff --git a/django_airavata/apps/auth/iam_admin_client.py b/django_airavata/apps/auth/iam_admin_client.py
new file mode 100644
index 0000000..5e619fe
--- /dev/null
+++ b/django_airavata/apps/auth/iam_admin_client.py
@@ -0,0 +1,13 @@
+"""
+Wrapper around the IAM Admin Services client.
+"""
+
+from django_airavata.utils import get_iam_admin_client
+
+from . import utils
+
+
+def is_username_available(username):
+    with get_iam_admin_client() as iam_admin_client:
+        authz_token = utils.get_service_account_authz_token()
+        return iam_admin_client.isUsernameAvailable(authz_token, username)
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 8c063f2..1da2a23 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
@@ -30,40 +30,26 @@
           {% else %}
           <h5 class="card-title">Create a {{ options.password.name }} account</h5>
           {% endif %}
-          <form action="{% url 'django_airavata_auth:handle_create_account' %}" method="post">
+          <form action="{% url 'django_airavata_auth:create_account' %}" method="post">
+            {{ form.non_field_errors }}
             {% csrf_token %}
+
+            {% for field in form %}
             <div class="form-group">
-              <label for="username">Username</label>
-              <input type="text" class="form-control" id="username" name="username" placeholder="Username"
value="{{ username }}"
-                required>
-            </div>
-            <div class="form-group">
-              <label for="password">Password</label>
-              <input type="password" class="form-control" id="password" name="password"
placeholder="Password" required>
-            </div>
-            <div class="form-group">
-              <label for="password_again">Password (again)</label>
-              <input type="password" class="form-control" id="password_again" name="password_again"
placeholder="Password (again)"
-                required>
-            </div>
-            <div class="form-group">
-              <label for="email">E-mail</label>
-              <input type="email" class="form-control" id="email" name="email" placeholder="email@example.com"
required>
-            </div>
-            <div class="form-group">
-              <label for="email_again">E-mail (again)</label>
-              <input type="email" class="form-control" id="email_again" name="email_again"
placeholder="email@example.com (again)"
-                required>
-            </div>
-            <div class="form-group">
-              <label for="first_name">First Name</label>
-              <input type="text" class="form-control" id="first_name" name="first_name"
placeholder="First Name"
-                required>
-            </div>
-            <div class="form-group">
-              <label for="last_name">Last Name</label>
-              <input type="text" class="form-control" id="last_name" name="last_name"
placeholder="Last Name" required>
+              <label for="{{ field.id_for_label }}">{{ field.label }}</label>
+              <input id="{{ field.id_for_label }}" type="{{ field.field.widget.input_type
}}"
+                class="form-control{% if field.errors %} is-invalid{% endif %}" name="{{
field.name }}"
+                placeholder="{{ field.field.widget.attrs.placeholder }}"
+                {% 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 %}
+              </div>
             </div>
+            {% endfor %}
+
             <button type="submit" class="btn btn-primary btn-block">
               Create
             </button>
diff --git a/django_airavata/apps/auth/urls.py b/django_airavata/apps/auth/urls.py
index 33e341e..b605d03 100644
--- a/django_airavata/apps/auth/urls.py
+++ b/django_airavata/apps/auth/urls.py
@@ -13,6 +13,4 @@ urlpatterns = [
     url(r'^callback', views.callback, name='callback'),
     url(r'^error', views.auth_error, name='error'),
     url(r'^create-account$', views.create_account, name='create_account'),
-    url(r'^handle-create-account$', views.handle_create_account,
-        name='handle_create_account'),
 ]
diff --git a/django_airavata/apps/auth/utils.py b/django_airavata/apps/auth/utils.py
index e5d39ea..4e949f1 100644
--- a/django_airavata/apps/auth/utils.py
+++ b/django_airavata/apps/auth/utils.py
@@ -41,10 +41,8 @@ def get_service_account_authz_token():
     access_token = token.get('access_token')
     return AuthzToken(
         accessToken=access_token,
-        claimsMap={
-            'gatewayID': settings.GATEWAY_ID,
-            # This is a service account, so leaving userName blank for now
-            'userName': None})
+        # This is a service account, so leaving out userName for now
+        claimsMap={'gatewayID': settings.GATEWAY_ID})
 
 
 def _create_authz_token(request):
diff --git a/django_airavata/apps/auth/views.py b/django_airavata/apps/auth/views.py
index 524586f..612022c 100644
--- a/django_airavata/apps/auth/views.py
+++ b/django_airavata/apps/auth/views.py
@@ -7,6 +7,8 @@ from django.shortcuts import redirect, render, resolve_url
 from django.urls import reverse
 from requests_oauthlib import OAuth2Session
 
+from . import forms
+
 logger = logging.getLogger(__name__)
 
 
@@ -91,12 +93,16 @@ def auth_error(request):
 
 
 def create_account(request):
+    if request.method == 'POST':
+        form = forms.CreateAccountForm(request.POST)
+        if form.is_valid():
+            # TODO: IAM registerUser
+            # TODO: send email account verification email
+            # TODO: success message
+            return redirect(reverse('django_airavata_auth:login'))
+    else:
+        form = forms.CreateAccountForm()
     return render(request, 'django_airavata_auth/create_account.html', {
         'options': settings.AUTHENTICATION_OPTIONS,
-    })
-
-
-def handle_create_account(request):
-    return render(request, 'django_airavata_auth/create_account.html', {
-        'options': settings.AUTHENTICATION_OPTIONS,
+        'form': form
     })


Mime
View raw message