From 61cc2d8b2e5efe5e1bd27c298809dda2d9fb8486 Mon Sep 17 00:00:00 2001 From: BlackDex Date: Sat, 12 Oct 2024 19:50:10 +0200 Subject: [PATCH] Fix fallback code to actually work The fallback now works by using an alternative `reg!` macro. This adds an extra template register which prefixes the template with `fallback_`. Signed-off-by: BlackDex --- src/api/web.rs | 15 +++++++++------ src/config.rs | 15 ++++++++++----- src/static/templates/scss/vaultwarden.scss.hbs | 15 ++------------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/api/web.rs b/src/api/web.rs index 853e0f1cbd..a96d7e2a16 100644 --- a/src/api/web.rs +++ b/src/api/web.rs @@ -96,16 +96,17 @@ fn vaultwarden_css() -> Cached> { "yubico_enabled": CONFIG._enable_yubico() && (CONFIG.yubico_client_id().is_some() == CONFIG.yubico_secret_key().is_some()), "emergency_access_allowed": CONFIG.emergency_access_allowed(), "sends_allowed": CONFIG.sends_allowed(), + "load_user_scss": true, }); let scss = match CONFIG.render_template("scss/vaultwarden.scss", &css_options) { Ok(t) => t, Err(e) => { - // Something went wrong loading the template. Fallback to the built-in templates + // Something went wrong loading the template. Use the fallback warn!("Loading scss/vaultwarden.scss.hbs or scss/user.vaultwarden.scss.hbs failed. {e}"); CONFIG - .render_inner_template("scss/vaultwarden.scss", &css_options) - .expect("Inner scss/vaultwarden.scss.hbs to render") + .render_fallback_template("scss/vaultwarden.scss", &css_options) + .expect("Fallback scss/vaultwarden.scss.hbs to render") } }; @@ -115,11 +116,13 @@ fn vaultwarden_css() -> Cached> { ) { Ok(css) => css, Err(e) => { - // Something went wrong compiling the scss. Fallback to the built-in tempaltes + // Something went wrong compiling the scss. Use the fallback warn!("Compiling the Vaultwarden SCSS styles failed. {e}"); + let mut css_options = css_options; + css_options["load_user_scss"] = json!(false); let scss = CONFIG - .render_inner_template("scss/vaultwarden.scss", &css_options) - .expect("Inner scss/vaultwarden.scss.hbs to render"); + .render_fallback_template("scss/vaultwarden.scss", &css_options) + .expect("Fallback scss/vaultwarden.scss.hbs to render"); grass_compiler::from_string( scss, &grass_compiler::Options::default().style(grass_compiler::OutputStyle::Compressed), diff --git a/src/config.rs b/src/config.rs index c8e793bb63..84800a0f44 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1269,14 +1269,14 @@ impl Config { let hb = load_templates(CONFIG.templates_folder()); hb.render(name, data).map_err(Into::into) } else { - self.render_inner_template(name, data) + let hb = &self.inner.read().unwrap().templates; + hb.render(name, data).map_err(Into::into) } } - #[inline] - pub fn render_inner_template(&self, name: &str, data: &T) -> Result { + pub fn render_fallback_template(&self, name: &str, data: &T) -> Result { let hb = &self.inner.read().unwrap().templates; - hb.render(name, data).map_err(Into::into) + hb.render(&format!("fallback_{name}"), data).map_err(Into::into) } pub fn set_rocket_shutdown_handle(&self, handle: rocket::Shutdown) { @@ -1317,6 +1317,11 @@ where reg!($name); reg!(concat!($name, $ext)); }}; + (@withfallback $name:expr) => {{ + let template = include_str!(concat!("static/templates/", $name, ".hbs")); + hb.register_template_string($name, template).unwrap(); + hb.register_template_string(concat!("fallback_", $name), template).unwrap(); + }}; } // First register default templates here @@ -1360,7 +1365,7 @@ where reg!("404"); - reg!("scss/vaultwarden.scss"); + reg!(@withfallback "scss/vaultwarden.scss"); reg!("scss/user.vaultwarden.scss"); // And then load user templates to overwrite the defaults diff --git a/src/static/templates/scss/vaultwarden.scss.hbs b/src/static/templates/scss/vaultwarden.scss.hbs index 385ed47efe..3fc3e70ed2 100644 --- a/src/static/templates/scss/vaultwarden.scss.hbs +++ b/src/static/templates/scss/vaultwarden.scss.hbs @@ -62,19 +62,7 @@ app-organization-plans > form > bit-section:nth-child(2) { app-security > app-two-factor-setup > form { @extend %vw-hide; } - -/* Replace the Bitwarden Shield at the top left with a Vaultwarden icon */ -.bwi-shield:before { - content: "" !important; - width: 32px !important; - height: 40px !important; - display: block !important; - background-image: url(../images/icon-white.png) !important; - background-repeat: no-repeat; - background-position-y: bottom; -} /**** END Static Vaultwarden Changes ****/ - /**** START Dynamic Vaultwarden Changes ****/ {{#if signup_disabled}} /* Hide the register link on the login screen */ @@ -111,6 +99,7 @@ bit-nav-item[route="sends"] { } {{/unless}} /**** End Dynamic Vaultwarden Changes ****/ - /**** Include a special user stylesheet for custom changes ****/ +{{#if load_user_scss}} {{> scss/user.vaultwarden.scss }} +{{/if}}