From 8b4a6f2a64e2b0194bee1da4ef9c7b9dc68876cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa?= Date: Fri, 8 Feb 2019 18:45:07 +0100 Subject: [PATCH] Fixed some clippy lints and changed update_uuid_revision to only use one db query --- src/api/core/mod.rs | 4 ++-- src/api/core/organizations.rs | 9 +++++---- src/api/core/two_factor.rs | 4 ++-- src/config.rs | 4 ++-- src/db/models/two_factor.rs | 2 +- src/db/models/user.rs | 30 +++++++++++++++++------------- src/error.rs | 2 +- src/mail.rs | 4 ++-- 8 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/api/core/mod.rs b/src/api/core/mod.rs index 08e3f974..2aab77b5 100644 --- a/src/api/core/mod.rs +++ b/src/api/core/mod.rs @@ -117,8 +117,8 @@ fn post_eq_domains(data: JsonUpcase, 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 index 5fee26ef..5a6fca0a 100644 --- 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, 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 index ba4d01a1..7c35c7ae 100644 --- 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 { 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) -> serde_json::Value { diff --git a/src/config.rs b/src/config.rs index 6d7aedc1..86a6fe70 100644 --- 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 index 1086f0e8..0772ef99 100644 --- 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 index 390302a9..21ac1531 100644 --- 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 { - 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 index 137ae8e6..15014f1e 100644 --- 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 index 8c0d65d7..468fa37d 100644 --- 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,