diff --git a/backend/onyx/auth/email_utils.py b/backend/onyx/auth/email_utils.py index fdff1f49d..9fe34bd34 100644 --- a/backend/onyx/auth/email_utils.py +++ b/backend/onyx/auth/email_utils.py @@ -10,6 +10,7 @@ from onyx.configs.app_configs import SMTP_PORT from onyx.configs.app_configs import SMTP_SERVER from onyx.configs.app_configs import SMTP_USER from onyx.configs.app_configs import WEB_DOMAIN +from onyx.configs.constants import AuthType from onyx.configs.constants import TENANT_ID_COOKIE_NAME from onyx.db.models import User @@ -187,23 +188,51 @@ def send_subscription_cancellation_email(user_email: str) -> None: send_email(user_email, subject, html_content, text_content) -def send_user_email_invite(user_email: str, current_user: User) -> None: +def send_user_email_invite( + user_email: str, current_user: User, auth_type: AuthType +) -> None: subject = "Invitation to Join Onyx Organization" heading = "You've Been Invited!" - message = ( - f"
You have been invited by {current_user.email} to join an organization on Onyx.
" - "To join the organization, please click the button below to set a password " - "or login with Google and complete your registration.
" - ) + + # the exact action taken by the user, and thus the message, depends on the auth type + message = f"You have been invited by {current_user.email} to join an organization on Onyx.
" + if auth_type == AuthType.CLOUD: + message += ( + "To join the organization, please click the button below to set a password " + "or login with Google and complete your registration.
" + ) + elif auth_type == AuthType.BASIC: + message += ( + "To join the organization, please click the button below to set a password " + "and complete your registration.
" + ) + elif auth_type == AuthType.GOOGLE_OAUTH: + message += ( + "To join the organization, please click the button below to login with Google " + "and complete your registration.
" + ) + elif auth_type == AuthType.OIDC or auth_type == AuthType.SAML: + message += ( + "To join the organization, please click the button below to" + " complete your registration.
" + ) + else: + raise ValueError(f"Invalid auth type: {auth_type}") + cta_text = "Join Organization" cta_link = f"{WEB_DOMAIN}/auth/signup?email={user_email}" html_content = build_html_email(heading, message, cta_text, cta_link) + + # text content is the fallback for clients that don't support HTML + # not as critical, so not having special cases for each auth type text_content = ( f"You have been invited by {current_user.email} to join an organization on Onyx.\n" "To join the organization, please visit the following link:\n" f"{WEB_DOMAIN}/auth/signup?email={user_email}\n" - "You'll be asked to set a password or login with Google to complete your registration." ) + if auth_type == AuthType.CLOUD: + text_content += "You'll be asked to set a password or login with Google to complete your registration." + send_email(user_email, subject, html_content, text_content) diff --git a/backend/onyx/server/manage/users.py b/backend/onyx/server/manage/users.py index 3a454547f..ad8f5098d 100644 --- a/backend/onyx/server/manage/users.py +++ b/backend/onyx/server/manage/users.py @@ -311,19 +311,23 @@ def bulk_invite_users( all_emails = list(set(new_invited_emails) | set(initial_invited_users)) number_of_invited_users = write_invited_users(all_emails) + # send out email invitations if enabled + if ENABLE_EMAIL_INVITES: + try: + for email in new_invited_emails: + send_user_email_invite(email, current_user, AUTH_TYPE) + except Exception as e: + logger.error(f"Error sending email invite to invited users: {e}") + if not MULTI_TENANT: return number_of_invited_users + + # for billing purposes, write to the control plane about the number of new users try: logger.info("Registering tenant users") fetch_ee_implementation_or_noop( "onyx.server.tenants.billing", "register_tenant_users", None )(tenant_id, get_total_users_count(db_session)) - if ENABLE_EMAIL_INVITES: - try: - for email in new_invited_emails: - send_user_email_invite(email, current_user) - except Exception as e: - logger.error(f"Error sending email invite to invited users: {e}") return number_of_invited_users except Exception as e: