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 2021/01/13 20:33:10 UTC

[airavata-django-portal] branch develop updated: AIRAVATA-3399 Only send new user email when user profile created

This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git


The following commit(s) were added to refs/heads/develop by this push:
     new d5cdfd1  AIRAVATA-3399 Only send new user email when user profile created
d5cdfd1 is described below

commit d5cdfd1a31314d9823354395b7506c39ad342528
Author: Marcus Christie <ma...@apache.org>
AuthorDate: Wed Jan 13 15:30:09 2021 -0500

    AIRAVATA-3399 Only send new user email when user profile created
---
 django_airavata/apps/auth/backends.py     |  2 --
 django_airavata/apps/auth/signals.py      | 28 ++++++++++++++++++++++++++++
 django_airavata/apps/workspace/apps.py    |  3 ---
 django_airavata/apps/workspace/signals.py | 26 --------------------------
 4 files changed, 28 insertions(+), 31 deletions(-)

diff --git a/django_airavata/apps/auth/backends.py b/django_airavata/apps/auth/backends.py
index d25f53e..260d0e6 100644
--- a/django_airavata/apps/auth/backends.py
+++ b/django_airavata/apps/auth/backends.py
@@ -208,6 +208,4 @@ class KeycloakBackend(object):
                         last_name=last_name,
                         email=email)
             user.save()
-            utils.send_new_user_email(
-                request, username, email, first_name, last_name)
             return user
diff --git a/django_airavata/apps/auth/signals.py b/django_airavata/apps/auth/signals.py
index 32f3cb0..dfc76cd 100644
--- a/django_airavata/apps/auth/signals.py
+++ b/django_airavata/apps/auth/signals.py
@@ -1,12 +1,18 @@
+import logging
+
 from django.conf import settings
+from django.contrib.auth.signals import user_logged_in
 from django.dispatch import receiver
 from django.shortcuts import reverse
 from django.template import Context
 
 from django_airavata.apps.api.signals import user_added_to_group
+from django_airavata.utils import user_profile_client_pool
 
 from . import models, utils
 
+log = logging.getLogger(__name__)
+
 
 @receiver(user_added_to_group, dispatch_uid="auth_email_user_added_to_group")
 def email_user_added_to_group(sender, user, groups, request, **kwargs):
@@ -23,3 +29,25 @@ def email_user_added_to_group(sender, user, groups, request, **kwargs):
         "group_names": [g.name for g in groups]
     })
     utils.send_email_to_user(models.USER_ADDED_TO_GROUP_TEMPLATE, context)
+
+
+@receiver(user_logged_in, dispatch_uid="auth_initialize_user_profile")
+def initialize_user_profile(sender, request, user, **kwargs):
+    """Initialize user profile in Airavata in case this is a new user."""
+    # NOTE: if the user verified their email address then they should already
+    # have an Airavata user profile (See IAMAdminServices.enableUser). The
+    # following is necessary for users coming from federated login who don't
+    # need to verify their email.
+    authz_token = utils.get_authz_token(request)
+    if authz_token is not None:
+        if not user_profile_client_pool.doesUserExist(authz_token,
+                                                      user.username,
+                                                      settings.GATEWAY_ID):
+            user_profile_client_pool.initializeUserProfile(authz_token)
+            log.info("initialized user profile for {}".format(user.username))
+            # Since user profile created, inform admins of new user
+            utils.send_new_user_email(
+                request, user.username, user.email, user.first_name, user.last_name)
+            log.info("sent new user email for user {}".format(user.username))
+    else:
+        log.warning(f"Logged in user {user.username} has no access token")
diff --git a/django_airavata/apps/workspace/apps.py b/django_airavata/apps/workspace/apps.py
index 368fa12..fca2121 100644
--- a/django_airavata/apps/workspace/apps.py
+++ b/django_airavata/apps/workspace/apps.py
@@ -37,6 +37,3 @@ class WorkspaceConfig(AiravataAppConfig):
             'active_prefixes': ['storage']
         },
     ]
-
-    def ready(self):
-        import django_airavata.apps.workspace.signals  # noqa
diff --git a/django_airavata/apps/workspace/signals.py b/django_airavata/apps/workspace/signals.py
deleted file mode 100644
index 2099801..0000000
--- a/django_airavata/apps/workspace/signals.py
+++ /dev/null
@@ -1,26 +0,0 @@
-"""Signal receivers for the workspace app."""
-
-import logging
-
-from django.contrib.auth.signals import user_logged_in
-from django.dispatch import receiver
-
-from django_airavata.apps.auth.utils import get_authz_token
-from django_airavata.utils import user_profile_client_pool
-
-log = logging.getLogger(__name__)
-
-
-@receiver(user_logged_in)
-def initialize_user_profile(sender, request, user, **kwargs):
-    """Initialize user profile in Airavata in case this is a new user."""
-    # NOTE: if the user verified their email address then they should already
-    # have an Airavata user profile (See IAMAdminServices.enableUser). The
-    # following is necessary for users coming from federated login who don't
-    # need to verify their email.
-    authz_token = get_authz_token(request)
-    if authz_token is not None:
-        user_profile_client_pool.initializeUserProfile(authz_token)
-        log.debug("initialized user profile for {}".format(user.username))
-    else:
-        log.warning(f"Logged in user {user.username} has no access token")