Skip to content

Commit

Permalink
Add dynamic CSS support
Browse files Browse the repository at this point in the history
Together with dani-garcia/bw_web_builds#180 this PR will add support for dynamic CSS changes.

For example, we could hide the register link if signups are not allowed.
In the future show or hide the SSO button depending on if it is enabled or not.

There also is a special `user.vaultwarden.scss` file so that users can add custom CSS without the need to modify the default (static) changes.
This will prevent future changes from not being applied and still have the custom user changes to be added.

Signed-off-by: BlackDex <[email protected]>
  • Loading branch information
BlackDex committed Sep 11, 2024
1 parent dca1428 commit 5068960
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 3 deletions.
43 changes: 43 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ argon2 = "0.5.3"
# Reading a password from the cli for generating the Argon2id ADMIN_TOKEN
rpassword = "7.3.1"

# Loading a dynamic CSS Stylesheet
grass_compiler = { version = "0.13.4", default-features = false }

# Strip debuginfo from the release builds
# The symbols are the provide better panic traces
# Also enable fat LTO and use 1 codegen unit for optimizations
Expand Down
44 changes: 41 additions & 3 deletions src/api/web.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
use std::path::{Path, PathBuf};
#[allow(unused_imports)]
use std::{
path::{Path, PathBuf},
sync::Mutex,
};

use rocket::{fs::NamedFile, http::ContentType, response::content::RawHtml as Html, serde::json::Json, Catcher, Route};
use rocket::{
fs::NamedFile,
http::ContentType,
response::{content::RawCss as Css, content::RawHtml as Html, Redirect},
serde::json::Json,
Catcher, Route,
};
use serde_json::Value;

use crate::{
Expand All @@ -16,7 +26,14 @@ pub fn routes() -> Vec<Route> {
// crate::utils::LOGGED_ROUTES to make sure they appear in the log
let mut routes = routes![attachments, alive, alive_head, static_files];
if CONFIG.web_vault_enabled() {
routes.append(&mut routes![web_index, web_index_head, app_id, web_files]);
routes.append(&mut routes![
web_index,
/*web_index_dynamic,*/ vaultwarden_css,
web_index_direct,
web_index_head,
app_id,
web_files
]);
}

#[cfg(debug_assertions)]
Expand Down Expand Up @@ -45,11 +62,32 @@ fn not_found() -> ApiResult<Html<String>> {
Ok(Html(text))
}

#[get("/css/vaultwarden.css")]
fn vaultwarden_css() -> Cached<Css<String>> {
let css_options = json!({
"signup_disabled": !CONFIG.signups_allowed() && CONFIG.signups_domains_whitelist().is_empty(),
});
let scss = CONFIG.render_template("scss/vaultwarden.scss", &css_options).expect("Rendered vaultwarden.css");
let css = grass_compiler::from_string(
scss,
&grass_compiler::Options::default().style(grass_compiler::OutputStyle::Compressed),
)
.expect("scss/vaultwarden.css to compile");
Cached::short(Css(css), false)
}

#[get("/")]
async fn web_index() -> Cached<Option<NamedFile>> {
Cached::short(NamedFile::open(Path::new(&CONFIG.web_vault_folder()).join("index.html")).await.ok(), false)
}

// Make sure that `/index.html` redirect to actual domain path.
// If not, this might cause issues with the web-vault
#[get("/index.html")]
fn web_index_direct() -> Redirect {
Redirect::to(format!("{}/", CONFIG.domain_path()))
}

#[head("/")]
fn web_index_head() -> EmptyResult {
// Add an explicit HEAD route to prevent uptime monitoring services from
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,9 @@ where

reg!("404");

reg!("scss/vaultwarden.scss");
reg!("scss/user.vaultwarden.scss");

// And then load user templates to overwrite the defaults
// Use .hbs extension for the files
// Templates get registered with their relative name
Expand Down
2 changes: 2 additions & 0 deletions src/static/templates/scss/user.vaultwarden.scss.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/**** START Custom User Changes ****/
/**** END Custom User Changes ****/
88 changes: 88 additions & 0 deletions src/static/templates/scss/vaultwarden.scss.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**** START Static Vaultwarden changes ****/
/* This combines all selectors extending it into one */
%vw-hide {
display: none !important;
}

/* This allows searching for the combined style in the browsers dev-tools (look into the head tag) */
.vw-hide,
head {
@extend %vw-hide;
}

/* Hide the Subscription Page tab */
bit-nav-item[route="settings/subscription"] {
@extend %vw-hide;
}

/* Hide any link pointing to Free Bitwarden Families */
a[href$="/settings/sponsored-families"] {
@extend %vw-hide;
}

/* Hide the `Enterprise Single Sign-On` button on the login page */
a[routerlink="/sso"] {
@extend %vw-hide;
}

/* Hide Two-Factor menu in Organization settings */
bit-nav-item[route="settings/two-factor"],
a[href$="/settings/two-factor"] {
@extend %vw-hide;
}

/* Hide Business Owned checkbox */
app-org-info > form:nth-child(1) > div:nth-child(3) {
@extend %vw-hide;
}

/* Hide the `This account is owned by a business` checkbox and label */
#ownedBusiness,
label[for^="ownedBusiness"] {
@extend %vw-hide;
}

/* Hide the radio button and label for the `Custom` org user type */
#userTypeCustom,
label[for^="userTypeCustom"] {
@extend %vw-hide;
}

/* Hide Business Name */
app-org-account form div bit-form-field.tw-block:nth-child(3) {
@extend %vw-hide;
}

/* Hide organization plans */
app-organization-plans > form > bit-section:nth-child(2) {
@extend %vw-hide;
}

/* Hide Device Verification form at the Two Step Login screen */
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 */
app-frontend-layout > app-login > form > div > div > div > p {
@extend %vw-hide;
}
{{/if}}
/**** End Dynamic Vaultwarden Changes ****/

/**** Include a special user stylesheet for custom changes ****/
{{> scss/user.vaultwarden.scss }}

0 comments on commit 5068960

Please sign in to comment.