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 f2078a38496b872c631e394d2f783a32ee2775b4
parent 1049646e27f481392a3582041df6e482128be594
Author: Daniel GarcĂ­a <dani-garcia@users.noreply.github.com>
Date:   Sun,  7 Oct 2018 16:06:47 +0200

Merge pull request #213 from janost/refactor-collectioncipher-save-delete

CollectionCipher::save() and delete() should return QueryResult instead of bool
Diffstat:
Msrc/api/core/ciphers.rs | 15++++++++++++---
Msrc/api/core/organizations.rs | 5++++-
Msrc/db/models/collection.rs | 18++++++------------
3 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/src/api/core/ciphers.rs b/src/api/core/ciphers.rs @@ -333,9 +333,15 @@ fn post_collections_admin(uuid: String, data: JsonUpcase<CollectionsAdminData>, Some(collection) => { if collection.is_writable_by_user(&headers.user.uuid, &conn) { if posted_collections.contains(&collection.uuid) { // Add to collection - CollectionCipher::save(&cipher.uuid, &collection.uuid, &conn); + match CollectionCipher::save(&cipher.uuid, &collection.uuid, &conn) { + Ok(()) => (), + Err(_) => err!("Failed to add cipher to collection") + }; } else { // Remove from collection - CollectionCipher::delete(&cipher.uuid, &collection.uuid, &conn); + match CollectionCipher::delete(&cipher.uuid, &collection.uuid, &conn) { + Ok(()) => (), + Err(_) => err!("Failed to remove cipher from collection") + }; } } else { err!("No rights to modify the collection") @@ -438,7 +444,10 @@ fn share_cipher_by_uuid(uuid: &str, data: ShareCipherData, headers: &Headers, co None => err!("Invalid collection ID provided"), Some(collection) => { if collection.is_writable_by_user(&headers.user.uuid, &conn) { - CollectionCipher::save(&cipher.uuid.clone(), &collection.uuid, &conn); + match CollectionCipher::save(&cipher.uuid.clone(), &collection.uuid, &conn) { + Ok(()) => (), + Err(_) => err!("Failed to add cipher to collection") + }; shared_to_collection = true; } else { err!("No rights to modify the collection") diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs @@ -663,7 +663,10 @@ fn post_org_import(query: OrgIdData, data: JsonUpcase<ImportData>, headers: Head Err(_) => err!("Failed to assign to collection") }; - CollectionCipher::save(cipher_id, coll_id, &conn); + match CollectionCipher::save(cipher_id, coll_id, &conn) { + Ok(()) => (), + Err(_) => err!("Failed to add cipher to collection") + }; } let mut user = headers.user; diff --git a/src/db/models/collection.rs b/src/db/models/collection.rs @@ -259,25 +259,19 @@ pub struct CollectionCipher { /// Database methods impl CollectionCipher { - pub fn save(cipher_uuid: &str, collection_uuid: &str, conn: &DbConn) -> bool { - match diesel::replace_into(ciphers_collections::table) + pub fn save(cipher_uuid: &str, collection_uuid: &str, conn: &DbConn) -> QueryResult<()> { + diesel::replace_into(ciphers_collections::table) .values(( ciphers_collections::cipher_uuid.eq(cipher_uuid), ciphers_collections::collection_uuid.eq(collection_uuid), - )).execute(&**conn) { - Ok(1) => true, // One row inserted - _ => false, - } + )).execute(&**conn).and(Ok(())) } - pub fn delete(cipher_uuid: &str, collection_uuid: &str, conn: &DbConn) -> bool { - match diesel::delete(ciphers_collections::table + pub fn delete(cipher_uuid: &str, collection_uuid: &str, conn: &DbConn) -> QueryResult<()> { + diesel::delete(ciphers_collections::table .filter(ciphers_collections::cipher_uuid.eq(cipher_uuid)) .filter(ciphers_collections::collection_uuid.eq(collection_uuid))) - .execute(&**conn) { - Ok(1) => true, // One row deleted - _ => false, - } + .execute(&**conn).and(Ok(())) } pub fn delete_all_by_cipher(cipher_uuid: &str, conn: &DbConn) -> QueryResult<()> {