Skip to content

Commit

Permalink
Fix fallback code to actually work
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
BlackDex committed Oct 13, 2024
1 parent 918cd58 commit 61cc2d8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
15 changes: 9 additions & 6 deletions src/api/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,17 @@ fn vaultwarden_css() -> Cached<Css<String>> {
"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")
}
};

Expand All @@ -115,11 +116,13 @@ fn vaultwarden_css() -> Cached<Css<String>> {
) {
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),
Expand Down
15 changes: 10 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: serde::ser::Serialize>(&self, name: &str, data: &T) -> Result<String, Error> {
pub fn render_fallback_template<T: serde::ser::Serialize>(&self, name: &str, data: &T) -> Result<String, Error> {
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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 2 additions & 13 deletions src/static/templates/scss/vaultwarden.scss.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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}}

0 comments on commit 61cc2d8

Please sign in to comment.