Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle backend crash on startup if mailer failed #65

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions backend/core/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions backend/iam/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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


Expand Down
Loading