Commit c440bdb8 authored by Martin Juhas's avatar Martin Juhas
Browse files

backport fix

parent 5186711b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
- `INJECT_EMAIL_HOST_USER`: _string, default=""_ - Username to use for authentication to the SMTP server defined in _INJECT_EMAIL_HOST_.
- `INJECT_EMAIL_HOST_PASSWORD`: _string, default=""_ - Password to use for the SMTP server defined in _INJECT_EMAIL_HOST_. This setting is used in conjunction with _INJECT_EMAIL_HOST_USER_ when authenticating to the SMTP server.
- `INJECT_EMAIL_SENDER_ADDRESS`: _string, default=""_ - The sender address for automatic emails.
- `INJECT_EMAIL_PROTOCOL`: _string, default=None_ - Optional variable for defining the preferred protocol for communication with SMTP server. The choices (values) can be _ssl_ or _tls_ (case insensitive). This variable can also be left undefined - no encryption will be used.
- `INJECT_LOGS`: _string, default=backend-logs.log_ - Path to a file where to save logs.
- `INJECT_DOMAIN`: _string, default=""_ - Domain where yours instance of the INJECT is available.
- `INJECT_SECRET_KEY`: _string_ - Used to provide cryptographic signing. Must be at least 50 long characters string.
+12 −1
Original line number Diff line number Diff line
@@ -206,10 +206,21 @@ else:
    EMAIL_PORT = 25
EMAIL_HOST_USER = os.environ.get("INJECT_EMAIL_HOST_USER", "")
EMAIL_HOST_PASSWORD = os.environ.get("INJECT_EMAIL_HOST_PASSWORD", "")
EMAIL_USE_SSL = True
EMAIL_SENDER_ADDRESS = os.environ.get("INJECT_EMAIL_SENDER_ADDRESS", "")
DOMAIN = os.environ.get("INJECT_DOMAIN", "")

if protocol := os.environ.get("INJECT_EMAIL_PROTOCOL"):
    formatted_protocol = protocol.lower().strip()
    if formatted_protocol == "tls":
        EMAIL_USE_TLS = True
    elif formatted_protocol == "ssl":
        EMAIL_USE_SSL = True
    else:
        raise Exception(
            f"Invalid SMTP protocol '{protocol}'. "
            f"Options are 'ssl', 'tls' or leave 'undefined' for unencrypted."
        )

# CORS settings
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOWED_ORIGINS = [
+6 −2
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ from django.core.mail import get_connection, EmailMultiAlternatives, send_mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from email.mime.image import MIMEImage
from common_lib.logger import logger

from user.models import User
from ttxbackend.settings import EMAIL_SENDER_ADDRESS, DOMAIN
@@ -41,8 +42,11 @@ def send_credentials(new_users: List[Tuple[User, str]]):
        message.attach_alternative(html_content, "text/html")
        messages.append(message)

    connection = get_connection(fail_silently=True)
    try:
        connection = get_connection(fail_silently=False)
        connection.send_messages(messages)
    except Exception as e:
        logger.error(f"Failed to send credentials via SMTP server: {e}")


def send_password_change_notification(user: User):
+4 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ from user.lib.user_validator import (
from user.models import User, Tag, UserTag
from aai.models import UserGroup
from user.email.email_sender import send_credentials
from common_lib.logger import logger


TAG_DELIMITER = "|"
@@ -126,6 +127,9 @@ class UserUploader:
            if created_user is not None:
                created_users.append(created_user)

        logger.info(
            f"Users: {[new_user for new_user, _ in created_users]} were created."
        )
        if not settings.DEBUG and not settings.TESTING_MODE:
            send_credentials(created_users)
        return validator.result_handler.create_response()