From c97f9d4e13e4e4e7b34ffa354deafb519d8dda07 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Christian=20B=C3=B6ttcher?= <c.boettcher@fz-juelich.de>
Date: Wed, 8 Mar 2023 12:32:54 +0100
Subject: [PATCH] do not create duplicate email, return 500 on error

---
 plugins/unity_integration.py | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/plugins/unity_integration.py b/plugins/unity_integration.py
index 3306ab7..0dd0334 100644
--- a/plugins/unity_integration.py
+++ b/plugins/unity_integration.py
@@ -67,17 +67,26 @@ class UnityIntegrationView(AppBuilderBaseView):
         fab_user = sec_manager.find_user(username=persistent_identifier)
         log.debug("Searching for user by name gave '" + (str(fab_user)) + "'")
 
-        if fab_user is None: # TODO check if None is the rioght thing to compare to
+        if fab_user is None:
+            fab_user = sec_manager.find_user(email=email)
+            log.debug("Searching for user by email gave '" + (str(fab_user)) + "'")
+
+        if fab_user is None:
             log.debug("Trying to create non-existing user")
             characters = string.ascii_letters + string.digits + string.punctuation
-            fab_user = sec_manager.add_user(
+            if sec_manager.add_user(
                 username=persistent_identifier,
                 first_name=first_name,
                 last_name=last_name,
                 email=email,
                 role=sec_manager.find_role(role),
                 password=''.join(random.choice(characters) for i in range(20))
-            )
+            ):
+                fab_user = sec_manager.find_user(name=persistent_identifier)
+                log.info("Successfully created user " + str(fab_user))
+            else:
+                log.error("User creation unsuccessful.")
+                abort(500)
         # login as that user
         login_user(fab_user, remember=False)
         return redirect(url_for('home'))
-- 
GitLab