diff options
author | Daniel GarcĂa <[email protected]> | 2018-10-14 17:35:59 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2018-10-14 17:35:59 +0200 |
commit | faec050a6d241d820a2cf11dff7815ddf613cb25 (patch) | |
tree | d0cced693f729e78895e0ec6b6765a382b0e10de | |
parent | 22304f49257986fee6ad823a1f8b812fe00ee63f (diff) | |
parent | 58a78ffa54b5513018ac034422c7eeb20c37e90a (diff) | |
download | vaultwarden-faec050a6d241d820a2cf11dff7815ddf613cb25.tar.gz vaultwarden-faec050a6d241d820a2cf11dff7815ddf613cb25.zip |
Merge pull request #217 from janost/refactor-device-save
Device::save() should return QueryResult instead of bool
-rw-r--r-- | src/api/identity.rs | 25 | ||||
-rw-r--r-- | src/db/models/device.rs | 10 |
2 files changed, 17 insertions, 18 deletions
diff --git a/src/api/identity.rs b/src/api/identity.rs index f4df90c4..175c5afc 100644 --- a/src/api/identity.rs +++ b/src/api/identity.rs @@ -46,16 +46,17 @@ fn _refresh_login(data: &ConnectData, _device_type: DeviceType, conn: DbConn) -> let orgs = UserOrganization::find_by_user(&user.uuid, &conn); let (access_token, expires_in) = device.refresh_tokens(&user, orgs); - device.save(&conn); - - Ok(Json(json!({ - "access_token": access_token, - "expires_in": expires_in, - "token_type": "Bearer", - "refresh_token": device.refresh_token, - "Key": user.key, - "PrivateKey": user.private_key, - }))) + match device.save(&conn) { + Ok(()) => Ok(Json(json!({ + "access_token": access_token, + "expires_in": expires_in, + "token_type": "Bearer", + "refresh_token": device.refresh_token, + "Key": user.key, + "PrivateKey": user.private_key, + }))), + Err(_) => err!("Failed to add device to user") + } } fn _password_login(data: &ConnectData, device_type: DeviceType, conn: DbConn, remote: Option<SocketAddr>) -> JsonResult { @@ -128,7 +129,9 @@ fn _password_login(data: &ConnectData, device_type: DeviceType, conn: DbConn, re let orgs = UserOrganization::find_by_user(&user.uuid, &conn); let (access_token, expires_in) = device.refresh_tokens(&user, orgs); - device.save(&conn); + if device.save(&conn).is_err() { + err!("Failed to add device to user") + } let mut result = json!({ "access_token": access_token, diff --git a/src/db/models/device.rs b/src/db/models/device.rs index 0e51c9e5..22b26e50 100644 --- a/src/db/models/device.rs +++ b/src/db/models/device.rs @@ -112,15 +112,11 @@ use db::schema::devices; /// Database methods impl Device { - pub fn save(&mut self, conn: &DbConn) -> bool { + pub fn save(&mut self, conn: &DbConn) -> QueryResult<()> { self.updated_at = Utc::now().naive_utc(); - match diesel::replace_into(devices::table) - .values(&*self) - .execute(&**conn) { - Ok(1) => true, // One row inserted - _ => false, - } + diesel::replace_into(devices::table) + .values(&*self).execute(&**conn).and(Ok(())) } pub fn delete(self, conn: &DbConn) -> QueryResult<()> { |