From 90c9cc2f1fd561bab6f5d324d6e61f6813c21184 Mon Sep 17 00:00:00 2001
From: James Graham <J.Graham@software.ac.uk>
Date: Wed, 27 Feb 2019 17:29:03 +0000
Subject: [PATCH] Add admin email to account creation process - see #65

Need to add admin email addresses to settings
---
 pedasi/settings.py      | 18 ++++++++++++++++++
 profiles/social_auth.py | 14 ++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/pedasi/settings.py b/pedasi/settings.py
index 4ab252c..44f089e 100644
--- a/pedasi/settings.py
+++ b/pedasi/settings.py
@@ -243,6 +243,9 @@ SOCIAL_AUTH_PIPELINE = [
 
     # Update the user record with any changed info from the auth service.
     'social_core.pipeline.user.user_details',
+
+    # Email admins to activate the account
+    'profiles.social_auth.email_admins',
 ]
 
 SOCIAL_AUTH_LOGIN_REDIRECT_URL = reverse_lazy('index')
@@ -310,3 +313,18 @@ STATICFILES_DIRS = [
     os.path.join(BASE_DIR, 'pedasi', 'static'),
     os.path.join(BASE_DIR, 'docs', 'build'),
 ]
+
+
+# Email provider for notification emails
+EMAIL_HOST = config('EMAIL_HOST', default=None)
+EMAIL_PORT = config('EMAIL_PORT', cast=int, default=25)
+
+EMAIL_HOST_USER = config('EMAIL_HOST_USER', default=None)
+EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD', default=None)
+
+EMAIL_USE_TLS = config('EMAIL_USE_TLS', cast=bool, default=False)
+EMAIL_USE_SSL = config('EMAIL_USE_SSL', cast=bool, default=False)
+
+if DEBUG and not EMAIL_HOST:
+    EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
+    EMAIL_FILE_PATH = os.path.join(BASE_DIR, 'mail.log')
diff --git a/profiles/social_auth.py b/profiles/social_auth.py
index 1817150..1d24140 100644
--- a/profiles/social_auth.py
+++ b/profiles/social_auth.py
@@ -4,6 +4,9 @@ Module containing customisations to the Python Social Auth pipeline.
 See https://python-social-auth.readthedocs.io/en/latest/
 """
 
+from django.core.mail import mail_admins
+
+
 from social_core.pipeline.user import create_user
 
 
@@ -22,3 +25,14 @@ def create_user_disabled(strategy, details, backend, user=None, *args, **kwargs)
         django_user.save()
 
     return result
+
+
+def email_admins(strategy, details, backend, user=None, *args, **kwargs):
+    """
+    Email the PEDASI admins if a new account has been created and requires approval
+    """
+    if kwargs['is_new']:
+        mail_admins(
+            subject='PEDASI Account Created',
+            message='User account created'
+        )
-- 
GitLab