0
0
Fork 0
mirror of https://github.com/dani-garcia/vaultwarden synced 2024-11-16 06:52:07 +01:00

Filter collection lists based on user

This commit is contained in:
Miroslav Prasil 2018-05-11 14:24:41 +01:00
parent 9cf449e1c5
commit dfb1232081
2 changed files with 20 additions and 4 deletions

View file

@ -318,7 +318,7 @@ fn post_collections_admin(uuid: String, data: Json<CollectionsAdminData>, header
} }
let posted_collections: HashSet<String> = data.collectionIds.iter().cloned().collect(); let posted_collections: HashSet<String> = data.collectionIds.iter().cloned().collect();
let current_collections: HashSet<String> = cipher.get_collections(&conn).iter().cloned().collect(); let current_collections: HashSet<String> = cipher.get_collections(&headers.user.uuid ,&conn).iter().cloned().collect();
for collection in posted_collections.symmetric_difference(&current_collections) { for collection in posted_collections.symmetric_difference(&current_collections) {
match Collection::find_by_uuid(&collection, &conn) { match Collection::find_by_uuid(&collection, &conn) {

View file

@ -3,7 +3,7 @@ use serde_json::Value as JsonValue;
use uuid::Uuid; use uuid::Uuid;
use super::{User, Organization, UserOrganization, FolderCipher}; use super::{User, Organization, UserOrganization, FolderCipher, UserOrgType};
#[derive(Debug, Identifiable, Queryable, Insertable, Associations)] #[derive(Debug, Identifiable, Queryable, Insertable, Associations)]
#[table_name = "ciphers"] #[table_name = "ciphers"]
@ -98,7 +98,7 @@ impl Cipher {
"OrganizationId": self.organization_uuid, "OrganizationId": self.organization_uuid,
"Attachments": attachments_json, "Attachments": attachments_json,
"OrganizationUseTotp": false, "OrganizationUseTotp": false,
"CollectionIds": self.get_collections(&conn), "CollectionIds": self.get_collections(user_uuid, &conn),
"Name": self.name, "Name": self.name,
"Notes": self.notes, "Notes": self.notes,
@ -242,9 +242,25 @@ impl Cipher {
.load::<Self>(&**conn).expect("Error loading ciphers") .load::<Self>(&**conn).expect("Error loading ciphers")
} }
pub fn get_collections(&self, conn: &DbConn) -> Vec<String> { pub fn get_collections(&self, user_id: &str, conn: &DbConn) -> Vec<String> {
ciphers_collections::table ciphers_collections::table
.inner_join(collections::table.on(
collections::uuid.eq(ciphers_collections::collection_uuid)
))
.inner_join(users_organizations::table.on(
users_organizations::org_uuid.eq(collections::org_uuid).and(
users_organizations::user_uuid.eq(user_id)
)
))
.left_join(users_collections::table.on(
users_collections::collection_uuid.eq(ciphers_collections::collection_uuid)
))
.filter(ciphers_collections::cipher_uuid.eq(&self.uuid)) .filter(ciphers_collections::cipher_uuid.eq(&self.uuid))
.filter(users_collections::user_uuid.eq(user_id).or( // User has access to collection
users_organizations::access_all.eq(true).or( // User has access all
users_organizations::type_.le(UserOrgType::Admin as i32) // User is admin or owner
)
))
.select(ciphers_collections::collection_uuid) .select(ciphers_collections::collection_uuid)
.load::<String>(&**conn).unwrap_or(vec![]) .load::<String>(&**conn).unwrap_or(vec![])
} }