From 226ac96fc7247e6f907d9755a741e9a1a1fa66f3 Mon Sep 17 00:00:00 2001 From: Mohamed-Hacene Date: Thu, 22 Feb 2024 13:08:17 +0100 Subject: [PATCH 1/2] fix: handle backend crash on startup if mailer failed --- backend/core/apps.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/backend/core/apps.py b/backend/core/apps.py index c7b067b95..85cd2e71e 100644 --- a/backend/core/apps.py +++ b/backend/core/apps.py @@ -238,9 +238,12 @@ def startup(): CISO_ASSISTANT_SUPERUSER_EMAIL and not User.objects.filter(email=CISO_ASSISTANT_SUPERUSER_EMAIL).exists() ): - User.objects.create_superuser( - email=CISO_ASSISTANT_SUPERUSER_EMAIL, is_superuser=True - ) + try: + User.objects.create_superuser( + email=CISO_ASSISTANT_SUPERUSER_EMAIL, is_superuser=True + ) + except Exception as e: + print(e) #NOTE: Add this exception in the logger # if root folder does not exist, then create it if not Folder.objects.filter(content_type=Folder.ContentType.ROOT).exists(): Folder.objects.create( From c6caa6be78cc8f94088af3a24f9d53a1a0d1be2a Mon Sep 17 00:00:00 2001 From: eric-intuitem <71850047+eric-intuitem@users.noreply.github.com> Date: Thu, 22 Feb 2024 19:27:36 +0100 Subject: [PATCH 2/2] Update models.py avoid sending mail when password provided for superuser creation. Improve logs. --- backend/iam/models.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/iam/models.py b/backend/iam/models.py index 75f2b10bb..c49bdbf83 100644 --- a/backend/iam/models.py +++ b/backend/iam/models.py @@ -232,6 +232,7 @@ def _create_user(self, email, password, mailing=True, **extra_fields): subject=_("Welcome to Ciso Assistant!"), ) except Exception as exception: + print(f"sending email to {email} failed") raise exception return user @@ -247,14 +248,13 @@ def create_superuser(self, email, password=None, **extra_fields): extra_fields.setdefault("is_superuser", True) if extra_fields.get("is_superuser") is not True: raise ValueError("Superuser must have is_superuser=True.") - if not (EMAIL_HOST or EMAIL_HOST_RESCUE): - extra_fields.setdefault("mailing", False) + extra_fields.setdefault("mailing", not(password) and (EMAIL_HOST or EMAIL_HOST_RESCUE)) superuser = self._create_user(email, password, **extra_fields) # when possible, add superuser to admin group try: UserGroup.objects.get(name="BI-UG-ADM").user_set.add(superuser) except ObjectDoesNotExist: - pass + print("cannot add superuser to admin group - will be done on next startup") return superuser