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/10/06 13:41:33 UTC
[airavata-django-portal] 09/13: AIRAVATA-3319 Add admin email
alerting when user ends up with invalid username
This is an automated email from the ASF dual-hosted git repository.
machristie pushed a commit to branch AIRAVATA-3319-handle-missing-name-and-email-attributes-from-cilo
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git
commit 95582e7cb7969217d5b0a727e2d955ce1ea61e89
Author: Marcus Christie <ma...@apache.org>
AuthorDate: Tue Jul 27 15:19:03 2021 -0400
AIRAVATA-3319 Add admin email alerting when user ends up with invalid username
---
django_airavata/apps/auth/backends.py | 10 ++++++++
django_airavata/apps/auth/utils.py | 45 +++++++++++++++++++++++++++++++++++
2 files changed, 55 insertions(+)
diff --git a/django_airavata/apps/auth/backends.py b/django_airavata/apps/auth/backends.py
index 2197928..a60fd59 100644
--- a/django_airavata/apps/auth/backends.py
+++ b/django_airavata/apps/auth/backends.py
@@ -239,6 +239,16 @@ class KeycloakBackend(object):
user.first_name = first_name
user.last_name = last_name
user.save()
+
+ # Since only Admins can fix a bad username, alert Admins if the user has
+ # an invalid username
+ if not user_profile.is_username_valid:
+ try:
+ utils.send_admin_alert_about_invalid_username(
+ request, username, email, first_name, last_name)
+ except Exception:
+ logger.exception(f"Failed to send alert about username being invalid: {username}")
+
return user
def _get_or_create_user(self, sub, username):
diff --git a/django_airavata/apps/auth/utils.py b/django_airavata/apps/auth/utils.py
index bb9e82d..b9419a6 100644
--- a/django_airavata/apps/auth/utils.py
+++ b/django_airavata/apps/auth/utils.py
@@ -141,6 +141,51 @@ def send_new_user_email(request, username, email, first_name, last_name):
msg.send()
+def send_admin_alert_about_invalid_username(request, username, email, first_name, last_name):
+ domain, port = split_domain_port(request.get_host())
+ context = Context({
+ "username": username,
+ "email": email,
+ "first_name": first_name,
+ "last_name": last_name,
+ "portal_title": settings.PORTAL_TITLE,
+ "gateway_id": settings.GATEWAY_ID,
+ "http_host": domain,
+ })
+ subject = Template("Please fix invalid username: a user of {{portal_title}} ({{http_host}}) has an invalid username ({{username}})").render(context)
+ body = Template("""
+ <p>
+ Dear Admin,
+ </p>
+
+ <p>
+ The following user has an invalid username:
+ </p>
+
+ <p>Username: {{username}}</p>
+ <p>Name: {{first_name}} {{last_name}}</p>
+ <p>Email: {{email}}</p>
+
+ <p>
+ This likely happened because there was no appropriate user attribute to use
+ for the user's username when the user logged in through an external identity
+ provider. Please update the username to the user's email address or some
+ other valid value in the <a href="https://{{http_host}}/admin/users/">Manage
+ Users</a> view in the portal.
+ </p>
+ """.strip()).render(context)
+ msg = EmailMessage(subject=subject,
+ body=body,
+ from_email="{} <{}>".format(
+ settings.PORTAL_TITLE,
+ settings.SERVER_EMAIL),
+ to=[a[1] for a in getattr(settings,
+ 'PORTAL_ADMINS',
+ settings.ADMINS)])
+ msg.content_subtype = 'html'
+ msg.send()
+
+
def send_email_to_user(template_id, context):
email_template = models.EmailTemplate.objects.get(pk=template_id)
subject = Template(email_template.subject).render(context)