mirror of
https://github.com/dani-garcia/vaultwarden.git
synced 2025-08-26 04:33:20 +00:00
Better handling of collection sharing
This commit is contained in:
parent
5c7124ff95
commit
d9c6387b8d
1 changed files with 16 additions and 21 deletions
|
@ -127,29 +127,24 @@ fn post_ciphers(data: JsonUpcase<CipherData>, headers: Headers, conn: DbConn) ->
|
|||
let data: CipherData = data.into_inner().data;
|
||||
|
||||
let mut cipher = Cipher::new(data.Type, data.Name.clone());
|
||||
update_cipher_from_data(&mut cipher, data, &headers, true, &conn)?;
|
||||
update_cipher_from_data(&mut cipher, data, &headers, false, &conn)?;
|
||||
|
||||
Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, &conn)))
|
||||
}
|
||||
|
||||
fn update_cipher_from_data(cipher: &mut Cipher, data: CipherData, headers: &Headers, new_or_shared_directly: bool, conn: &DbConn) -> EmptyResult {
|
||||
if new_or_shared_directly { // Cipher is new or shared to Organization directly (not to Collection)
|
||||
if let Some(org_id) = data.OrganizationId {
|
||||
match UserOrganization::find_by_user_and_org(&headers.user.uuid, &org_id, &conn) {
|
||||
None => err!("You don't have permission to add item to organization"),
|
||||
Some(org_user) => if org_user.has_full_access() {
|
||||
cipher.organization_uuid = Some(org_id);
|
||||
cipher.user_uuid = None;
|
||||
} else {
|
||||
err!("You don't have permission to add cipher directly to organization")
|
||||
}
|
||||
fn update_cipher_from_data(cipher: &mut Cipher, data: CipherData, headers: &Headers, shared_to_collection: bool, conn: &DbConn) -> EmptyResult {
|
||||
if let Some(org_id) = data.OrganizationId {
|
||||
match UserOrganization::find_by_user_and_org(&headers.user.uuid, &org_id, &conn) {
|
||||
None => err!("You don't have permission to add item to organization"),
|
||||
Some(org_user) => if shared_to_collection || org_user.has_full_access() {
|
||||
cipher.organization_uuid = Some(org_id);
|
||||
cipher.user_uuid = None;
|
||||
} else {
|
||||
err!("You don't have permission to add cipher directly to organization")
|
||||
}
|
||||
} else {
|
||||
cipher.user_uuid = Some(headers.user.uuid.clone());
|
||||
}
|
||||
} else if let Some(org_uuid) = data.OrganizationId { // Shared to collection, but still belongs to Organization
|
||||
cipher.organization_uuid = Some(org_uuid);
|
||||
cipher.user_uuid = None;
|
||||
} else {
|
||||
cipher.user_uuid = Some(headers.user.uuid.clone());
|
||||
}
|
||||
|
||||
if let Some(ref folder_id) = data.FolderId {
|
||||
|
@ -246,7 +241,7 @@ fn post_ciphers_import(data: JsonUpcase<ImportData>, headers: Headers, conn: DbC
|
|||
.map(|i| folders[*i].uuid.clone());
|
||||
|
||||
let mut cipher = Cipher::new(cipher_data.Type, cipher_data.Name.clone());
|
||||
update_cipher_from_data(&mut cipher, cipher_data, &headers, true, &conn)?;
|
||||
update_cipher_from_data(&mut cipher, cipher_data, &headers, false, &conn)?;
|
||||
|
||||
cipher.move_to_folder(folder_uuid, &headers.user.uuid.clone(), &conn).ok();
|
||||
}
|
||||
|
@ -428,7 +423,7 @@ fn share_cipher_by_uuid(uuid: &str, data: ShareCipherData, headers: &Headers, co
|
|||
match data.Cipher.OrganizationId.clone() {
|
||||
None => err!("Organization id not provided"),
|
||||
Some(organization_uuid) => {
|
||||
let mut shared_directly = true;
|
||||
let mut shared_to_collection = false;
|
||||
for uuid in &data.CollectionIds {
|
||||
match Collection::find_by_uuid(uuid, &conn) {
|
||||
None => err!("Invalid collection ID provided"),
|
||||
|
@ -436,7 +431,7 @@ fn share_cipher_by_uuid(uuid: &str, data: ShareCipherData, headers: &Headers, co
|
|||
if collection.is_writable_by_user(&headers.user.uuid, &conn) {
|
||||
if collection.org_uuid == organization_uuid {
|
||||
CollectionCipher::save(&cipher.uuid.clone(), &collection.uuid, &conn);
|
||||
shared_directly = false;
|
||||
shared_to_collection = true;
|
||||
} else {
|
||||
err!("Collection does not belong to organization")
|
||||
}
|
||||
|
@ -446,7 +441,7 @@ fn share_cipher_by_uuid(uuid: &str, data: ShareCipherData, headers: &Headers, co
|
|||
}
|
||||
}
|
||||
}
|
||||
update_cipher_from_data(&mut cipher, data.Cipher, &headers, shared_directly, &conn)?;
|
||||
update_cipher_from_data(&mut cipher, data.Cipher, &headers, shared_to_collection, &conn)?;
|
||||
|
||||
Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, &conn)))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue