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:19 UTC

[airavata-django-portal] 05/09: AIRAVATA-2888 Email verification email handler

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 21045f1b499d292ad8ed7ec4aaf8ad02b4c348ed
Author: Marcus Christie <ma...@iu.edu>
AuthorDate: Fri Oct 26 14:24:08 2018 -0400

    AIRAVATA-2888 Email verification email handler
---
 django_airavata/apps/auth/iam_admin_client.py | 12 ++++++++++
 django_airavata/apps/auth/views.py            | 33 +++++++++++++++++++++++++--
 2 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/django_airavata/apps/auth/iam_admin_client.py b/django_airavata/apps/auth/iam_admin_client.py
index 33f49f6..7c0aec1 100644
--- a/django_airavata/apps/auth/iam_admin_client.py
+++ b/django_airavata/apps/auth/iam_admin_client.py
@@ -27,3 +27,15 @@ def register_user(username, email_address, first_name, last_name, password):
             first_name,
             last_name,
             password)
+
+
+def is_user_enabled(username):
+    with get_iam_admin_client() as iam_admin_client:
+        authz_token = utils.get_service_account_authz_token()
+        return iam_admin_client.isUserEnabled(authz_token, username)
+
+
+def enable_user(username):
+    with get_iam_admin_client() as iam_admin_client:
+        authz_token = utils.get_service_account_authz_token()
+        return iam_admin_client.enableUser(authz_token, username)
diff --git a/django_airavata/apps/auth/views.py b/django_airavata/apps/auth/views.py
index 9df44b3..9626721 100644
--- a/django_airavata/apps/auth/views.py
+++ b/django_airavata/apps/auth/views.py
@@ -3,7 +3,8 @@ from urllib.parse import quote
 
 from django.conf import settings
 from django.contrib.auth import authenticate, login, logout
-from django.core.mail import send_mail
+from django.core.exceptions import ObjectDoesNotExist
+from django.core.mail import mail_admins, send_mail
 from django.forms import ValidationError
 from django.shortcuts import redirect, render, resolve_url
 from django.urls import reverse
@@ -145,4 +146,32 @@ def create_account(request):
 
 
 def verify_email(request, code):
-    pass
+
+    try:
+        email_verification = models.EmailVerification.objects.get(
+            verification_code=code)
+        email_verification.verified = True
+        email_verification.save()
+        # TODO: test what happens if calling iam_admin_client fails
+        # Check if user is enabled, if so redirect to login page
+        username = email_verification.username
+        logger.debug("Email address verified for {}".format(username))
+        if iam_admin_client.is_user_enabled(username):
+            logger.debug("User {} is already enabled".format(username))
+            # TODO: add success message
+            return redirect(reverse('django_airavata_auth:login'))
+        else:
+            logger.debug("Enabling user {}".format(username))
+            # enable user and inform admins
+            iam_admin_client.enable_user(username)
+            # TODO: use proper template for new user email
+            mail_admins(
+                'New User Created',
+                'New user: {}'.format(username)
+            )
+            # TODO: add success message
+            return redirect(reverse('django_airavata_auth:login'))
+    except ObjectDoesNotExist as e:
+        # TODO: if doesn't exist, give user a form where they can enter their
+        # username to resend verification code
+        pass