From 8bed867798f59447b940fbf6c0b1a9d5e45056ad Mon Sep 17 00:00:00 2001 From: Miroslav Prasil Date: Fri, 11 May 2018 23:53:37 +0100 Subject: [PATCH 1/3] Also list shared ciphers in find_by_user --- src/api/core/ciphers.rs | 2 +- src/db/models/cipher.rs | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/api/core/ciphers.rs b/src/api/core/ciphers.rs index a8ba1c5e..519e67ea 100644 --- a/src/api/core/ciphers.rs +++ b/src/api/core/ciphers.rs @@ -503,7 +503,7 @@ fn delete_all(data: Json, headers: Headers, conn: DbConn) -> Empty } // Delete ciphers and their attachments - for cipher in Cipher::find_by_user(&user.uuid, &conn) { + for cipher in Cipher::find_owned_by_user(&user.uuid, &conn) { _delete_cipher(cipher, &conn); } diff --git a/src/db/models/cipher.rs b/src/db/models/cipher.rs index e72c2ab0..a47549f4 100644 --- a/src/db/models/cipher.rs +++ b/src/db/models/cipher.rs @@ -223,10 +223,34 @@ impl Cipher { .first::(&**conn).ok() } + // Find all ciphers accesible to user pub fn find_by_user(user_uuid: &str, conn: &DbConn) -> Vec { ciphers::table - .filter(ciphers::user_uuid.eq(user_uuid)) - .load::(&**conn).expect("Error loading ciphers") + .left_join(users_organizations::table.on( + ciphers::organization_uuid.eq(users_organizations::org_uuid.nullable()).and( + users_organizations::user_uuid.eq(user_uuid) + ) + )) + .left_join(ciphers_collections::table) + .left_join(users_collections::table.on( + ciphers_collections::collection_uuid.eq(users_collections::collection_uuid) + )) + .filter(ciphers::user_uuid.eq(user_uuid).or( // Cipher owner + users_organizations::access_all.eq(true).or( // access_all in Organization + users_organizations::type_.le(UserOrgType::Admin as i32).or( // Org admin or owner + users_collections::user_uuid.eq(user_uuid) // Access to Collection + ) + ) + )) + .select(ciphers::all_columns) + .load::(&**conn).expect("Error loading ciphers") + } + + // Find all ciphers directly owned by user + pub fn find_owned_by_user(user_uuid: &str, conn: &DbConn) -> Vec { + ciphers::table + .filter(ciphers::user_uuid.eq(user_uuid)) + .load::(&**conn).expect("Error loading ciphers") } pub fn find_by_org(org_uuid: &str, conn: &DbConn) -> Vec { From 89e544009faaf8e40c6b3983ccd6102b88d4fb47 Mon Sep 17 00:00:00 2001 From: Miroslav Prasil Date: Sun, 13 May 2018 13:20:00 +0100 Subject: [PATCH 2/3] Fix duplicate ciphers returned from find_by_user --- src/db/models/cipher.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/db/models/cipher.rs b/src/db/models/cipher.rs index a47549f4..24255c17 100644 --- a/src/db/models/cipher.rs +++ b/src/db/models/cipher.rs @@ -243,6 +243,7 @@ impl Cipher { ) )) .select(ciphers::all_columns) + .distinct() .load::(&**conn).expect("Error loading ciphers") } From b0472d7aabae8e1bb1ba6bd639ffb3280ccc2f36 Mon Sep 17 00:00:00 2001 From: Miroslav Prasil Date: Sun, 13 May 2018 13:20:21 +0100 Subject: [PATCH 3/3] Delete owned ciphers on account deletion --- src/api/core/accounts.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/core/accounts.rs b/src/api/core/accounts.rs index 7a4627f5..f29ef2bb 100644 --- a/src/api/core/accounts.rs +++ b/src/api/core/accounts.rs @@ -168,7 +168,7 @@ fn delete_account(data: Json, headers: Headers, conn: DbConn) -> E } // Delete ciphers and their attachments - for cipher in Cipher::find_by_user(&user.uuid, &conn) { + for cipher in Cipher::find_owned_by_user(&user.uuid, &conn) { for a in Attachment::find_by_cipher(&cipher.uuid, &conn) { a.delete(&conn); } cipher.delete(&conn);