diff --git a/server/db/auth.rs b/server/db/auth.rs index 99477d8..9561502 100644 --- a/server/db/auth.rs +++ b/server/db/auth.rs @@ -90,7 +90,7 @@ impl User { username: self.username.clone(), flags: self.flags, set_password_hash: None, - set_preferences: None, + preferences: self.preferences.clone(), unix_uid: self.unix_uid, permissions: self.permissions.clone(), } @@ -116,7 +116,7 @@ pub struct UserChange { pub username: String, pub flags: i32, set_password_hash: Option>, - set_preferences: Option, + pub preferences: UserPreferences, pub unix_uid: Option, pub permissions: Permissions, } @@ -128,7 +128,7 @@ impl UserChange { username, flags: 0, set_password_hash: None, - set_preferences: None, + preferences: UserPreferences::default(), unix_uid: None, permissions: Permissions::default(), } @@ -143,10 +143,6 @@ impl UserChange { self.set_password_hash = Some(None); } - pub fn set_preferences(&mut self, preferences: UserPreferences) { - self.set_preferences = Some(preferences); - } - pub fn disable(&mut self) { self.flags |= UserFlag::Disabled as i32; } @@ -465,10 +461,6 @@ impl State { ::std::collections::btree_map::Entry::Vacant(_) => panic!("missing uid {}!", id), ::std::collections::btree_map::Entry::Occupied(e) => e, }; - let preferences = change.set_preferences.unwrap_or_else(|| { - let u = e.get(); - u.preferences.clone() - }); { let (phash, pid, pcount) = match change.set_password_hash.as_ref() { None => { @@ -490,7 +482,7 @@ impl State { ":unix_uid": &change.unix_uid, ":id": &id, ":permissions": &permissions, - ":preferences": &preferences, + ":preferences": &change.preferences, })?; } let u = e.into_mut(); @@ -503,7 +495,7 @@ impl State { u.flags = change.flags; u.unix_uid = change.unix_uid; u.permissions = change.permissions; - u.preferences = preferences; + u.preferences = change.preferences; Ok(u) } @@ -521,14 +513,13 @@ impl State { .permissions .write_to_bytes() .expect("proto3->vec is infallible"); - let preferences = change.set_preferences.unwrap_or_default(); stmt.execute(named_params! { ":username": &change.username[..], ":password_hash": &password_hash, ":flags": &change.flags, ":unix_uid": &change.unix_uid, ":permissions": &permissions, - ":preferences": &preferences, + ":preferences": &change.preferences, })?; let id = conn.last_insert_rowid() as i32; self.users_by_name.insert(change.username.clone(), id); @@ -547,7 +538,7 @@ impl State { unix_uid: change.unix_uid, dirty: false, permissions: change.permissions, - preferences, + preferences: change.preferences, })) } @@ -1340,16 +1331,13 @@ mod tests { db::init(&mut conn).unwrap(); let mut state = State::init(&conn).unwrap(); let mut change = UserChange::add_user("slamb".to_owned()); - let mut preferences = UserPreferences::default(); - preferences.0.insert("foo".to_string(), 42.into()); - change.set_preferences(preferences.clone()); + change.preferences.0.insert("foo".to_string(), 42.into()); let u = state.apply(&conn, change).unwrap(); - assert_eq!(preferences, u.preferences); let mut change = u.change(); - preferences.0.insert("bar".to_string(), 26.into()); - change.set_preferences(preferences.clone()); + change.preferences.0.insert("bar".to_string(), 26.into()); let u = state.apply(&conn, change).unwrap(); - assert_eq!(preferences, u.preferences); + assert_eq!(u.preferences.0.get("foo"), Some(&42.into())); + assert_eq!(u.preferences.0.get("bar"), Some(&26.into())); let uid = u.id; { @@ -1359,6 +1347,7 @@ mod tests { } let state = State::init(&conn).unwrap(); let u = state.users_by_id().get(&uid).unwrap(); - assert_eq!(preferences, u.preferences); + assert_eq!(u.preferences.0.get("foo"), Some(&42.into())); + assert_eq!(u.preferences.0.get("bar"), Some(&26.into())); } } diff --git a/server/src/web.rs b/server/src/web.rs index 05356e3..23613a6 100644 --- a/server/src/web.rs +++ b/server/src/web.rs @@ -1042,7 +1042,7 @@ impl Service { if let Some(update) = r.update { let mut change = user.change(); if let Some(preferences) = update.preferences { - change.set_preferences(preferences); + change.preferences = preferences; } db.apply_user_change(change).map_err(internal_server_err)?; }