vw_small

Hardened fork of Vaultwarden (https://github.com/dani-garcia/vaultwarden) with fewer features.
git clone https://git.philomathiclife.com/repos/vw_small
Log | Files | Refs | README

commit 8b4a6f2a64e2b0194bee1da4ef9c7b9dc68876cd
parent ef63342e20f2bf5beba1c0fdefdc869dd40f4a19
Author: Daniel GarcĂ­a <dani-garcia@users.noreply.github.com>
Date:   Fri,  8 Feb 2019 18:45:07 +0100

Fixed some clippy lints and changed update_uuid_revision to only use one db query

Diffstat:
Msrc/api/core/mod.rs | 4++--
Msrc/api/core/organizations.rs | 9+++++----
Msrc/api/core/two_factor.rs | 4++--
Msrc/config.rs | 4++--
Msrc/db/models/two_factor.rs | 2+-
Msrc/db/models/user.rs | 30+++++++++++++++++-------------
Msrc/error.rs | 2+-
Msrc/mail.rs | 4++--
8 files changed, 32 insertions(+), 27 deletions(-)

diff --git a/src/api/core/mod.rs b/src/api/core/mod.rs @@ -117,8 +117,8 @@ fn post_eq_domains(data: JsonUpcase<EquivDomainData>, headers: Headers, conn: Db let mut user = headers.user; use serde_json::to_string; - user.excluded_globals = to_string(&excluded_globals).unwrap_or("[]".to_string()); - user.equivalent_domains = to_string(&equivalent_domains).unwrap_or("[]".to_string()); + user.excluded_globals = to_string(&excluded_globals).unwrap_or_else(|_| "[]".to_string()); + user.equivalent_domains = to_string(&equivalent_domains).unwrap_or_else(|_| "[]".to_string()); user.save(&conn)?; diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs @@ -452,7 +452,7 @@ where D: Deserializer<'de>, { // Deserialize null to empty Vec - Deserialize::deserialize(deserializer).or(Ok(vec![])) + Deserialize::deserialize(deserializer).or_else(|_| Ok(vec![])) } #[derive(Deserialize)] @@ -486,9 +486,10 @@ fn send_invite(org_id: String, data: JsonUpcase<InviteData>, headers: AdminHeade } for email in data.Emails.iter() { - let mut user_org_status = match CONFIG.mail_enabled() { - true => UserOrgStatus::Invited as i32, - false => UserOrgStatus::Accepted as i32, // Automatically mark user as accepted if no email invites + let mut user_org_status = if CONFIG.mail_enabled() { + UserOrgStatus::Invited as i32 + } else { + UserOrgStatus::Accepted as i32 // Automatically mark user as accepted if no email invites }; let user = match User::find_by_mail(&email, &conn) { None => { diff --git a/src/api/core/two_factor.rs b/src/api/core/two_factor.rs @@ -462,7 +462,7 @@ pub fn validate_u2f_login(user_uuid: &str, response: &str, conn: &DbConn) -> Emp } Err(e) => { info!("E {:#}", e); - break; + // break; } } } @@ -494,7 +494,7 @@ use yubico::Yubico; fn parse_yubikeys(data: &EnableYubikeyData) -> Vec<String> { let data_keys = [&data.Key1, &data.Key2, &data.Key3, &data.Key4, &data.Key5]; - data_keys.into_iter().filter_map(|e| e.as_ref().cloned()).collect() + data_keys.iter().filter_map(|e| e.as_ref().cloned()).collect() } fn jsonify_yubikeys(yubikeys: Vec<String>) -> serde_json::Value { diff --git a/src/config.rs b/src/config.rs @@ -9,7 +9,7 @@ lazy_static! { println!("Error loading config:\n\t{:?}\n", e); exit(12) }); - pub static ref CONFIG_FILE: String = get_env("CONFIG_FILE").unwrap_or("data/config.json".into()); + pub static ref CONFIG_FILE: String = get_env("CONFIG_FILE").unwrap_or_else(|| "data/config.json".into()); } macro_rules! make_config { @@ -71,7 +71,7 @@ macro_rules! make_config { /// Returns a new builder with all the elements from self, /// except those that are equal in both sides - fn remove(&self, other: &Self) -> Self { + fn _remove(&self, other: &Self) -> Self { let mut builder = ConfigBuilder::default(); $($( if &self.$name != &other.$name { diff --git a/src/db/models/two_factor.rs b/src/db/models/two_factor.rs @@ -15,7 +15,7 @@ pub struct TwoFactor { } #[allow(dead_code)] -#[derive(FromPrimitive, ToPrimitive)] +#[derive(FromPrimitive)] pub enum TwoFactorType { Authenticator = 0, Email = 1, diff --git a/src/db/models/user.rs b/src/db/models/user.rs @@ -172,23 +172,27 @@ impl User { .map_res("Error deleting user") } - pub fn update_uuid_revision(uuid: &str, conn: &DbConn) -> Vec<String> { - if let Some(mut user) = User::find_by_uuid(&uuid, conn) { - if user.update_revision(conn).is_err() { - warn!("Failed to update revision for {}", user.email); - }; - }; - - vec![uuid.to_string()] + pub fn update_uuid_revision(uuid: &str, conn: &DbConn) { + if let Err(e) = Self::_update_revision(uuid, &Utc::now().naive_utc(), conn) { + warn!("Failed to update revision for {}: {:#?}", uuid, e); + } } pub fn update_revision(&mut self, conn: &DbConn) -> EmptyResult { self.updated_at = Utc::now().naive_utc(); - crate::util::retry( || { - diesel::update(users::table.filter(users::uuid.eq(&self.uuid))) - .set(users::updated_at.eq(&self.updated_at)) - .execute(&**conn) - }, 10) + + Self::_update_revision(&self.uuid, &self.updated_at, conn) + } + + fn _update_revision(uuid: &str, date: &NaiveDateTime, conn: &DbConn) -> EmptyResult { + crate::util::retry( + || { + diesel::update(users::table.filter(users::uuid.eq(uuid))) + .set(users::updated_at.eq(date)) + .execute(&**conn) + }, + 10, + ) .map_res("Error updating user revision") } diff --git a/src/error.rs b/src/error.rs @@ -6,7 +6,7 @@ use std::error::Error as StdError; macro_rules! make_error { ( $( $name:ident ( $ty:ty ): $src_fn:expr, $usr_msg_fun:expr ),+ $(,)? ) => { #[derive(Display)] - enum ErrorKind { $($name( $ty )),+ } + pub enum ErrorKind { $($name( $ty )),+ } pub struct Error { message: String, error: ErrorKind } $(impl From<$ty> for Error { diff --git a/src/mail.rs b/src/mail.rs @@ -86,8 +86,8 @@ pub fn send_invite( "email/send_org_invite", json!({ "url": CONFIG.domain(), - "org_id": org_id.unwrap_or("_".to_string()), - "org_user_id": org_user_id.unwrap_or("_".to_string()), + "org_id": org_id.unwrap_or_else(|| "_".to_string()), + "org_user_id": org_user_id.unwrap_or_else(|| "_".to_string()), "email": address, "org_name": org_name, "token": invite_token,