From e5fe26ee1c17d50eb8b8af23c1faed95ac7c5287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=9Al=C4=99zak?= <102536422+filipslezaklab@users.noreply.github.com> Date: Thu, 25 Jan 2024 11:48:19 +0100 Subject: [PATCH] fix: return default logo if empty (#519) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: ensure to return default logo * cleanup --------- Co-authored-by: Maciek Co-authored-by: Maciej Wójcik --- src/handlers/settings.rs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/handlers/settings.rs b/src/handlers/settings.rs index bcf633700..118531784 100644 --- a/src/handlers/settings.rs +++ b/src/handlers/settings.rs @@ -17,12 +17,26 @@ use crate::{ AppState, }; +static DEFAULT_NAV_LOGO_URL: &str = "/svg/defguard-nav-logo.svg"; +static DEFAULT_MAIN_LOGO_URL: &str = "/svg/logo-defguard-white.svg"; + pub async fn get_settings(State(appstate): State) -> ApiResult { debug!("Retrieving settings"); - let settings = Settings::find_by_id(&appstate.pool, 1).await?; - info!("Retrieved settings"); + if let Some(mut settings) = Settings::find_by_id(&appstate.pool, 1).await? { + if settings.nav_logo_url.is_empty() { + settings.nav_logo_url = DEFAULT_NAV_LOGO_URL.into(); + } + if settings.main_logo_url.is_empty() { + settings.main_logo_url = DEFAULT_MAIN_LOGO_URL.into(); + } + return Ok(ApiResponse { + json: json!(settings), + status: StatusCode::OK, + }); + } + debug!("Retrieved settings"); Ok(ApiResponse { - json: json!(settings), + json: json!({}), status: StatusCode::OK, }) } @@ -42,7 +56,13 @@ pub async fn update_settings( pub async fn get_settings_essentials(State(appstate): State) -> ApiResult { debug!("Retrieving essential settings"); - let settings = SettingsEssentials::get_settings_essentials(&appstate.pool).await?; + let mut settings = SettingsEssentials::get_settings_essentials(&appstate.pool).await?; + if settings.nav_logo_url.is_empty() { + settings.nav_logo_url = DEFAULT_NAV_LOGO_URL.into(); + } + if settings.main_logo_url.is_empty() { + settings.main_logo_url = DEFAULT_MAIN_LOGO_URL.into(); + } info!("Retrieved essential settings"); Ok(ApiResponse { json: json!(settings), @@ -64,8 +84,8 @@ pub async fn set_default_branding( match settings { Some(mut settings) => { settings.instance_name = "Defguard".into(); - settings.nav_logo_url = "/svg/defguard-nav-logo.svg".into(); - settings.main_logo_url = "/svg/logo-defguard-white.svg".into(); + settings.nav_logo_url = DEFAULT_NAV_LOGO_URL.into(); + settings.main_logo_url = DEFAULT_MAIN_LOGO_URL.into(); settings.save(&appstate.pool).await?; info!( "User {} restored default branding settings", @@ -97,7 +117,7 @@ pub async fn patch_settings( pub async fn test_ldap_settings(_admin: AdminRole, State(appstate): State) -> ApiResult { debug!("Testing LDAP connection"); if LDAPConnection::create(&appstate.pool).await.is_ok() { - debug!("LDAP connected succesfully"); + debug!("LDAP connected successfully"); Ok(ApiResponse { json: json!({}), status: StatusCode::OK,