1
0
Fork 0
mirror of https://github.com/dani-garcia/vaultwarden.git synced 2025-06-13 14:20:07 +00:00

Implement deleting collections

This commit is contained in:
Miroslav Prasil 2018-05-16 23:05:50 +01:00
parent e2a065cb09
commit a0d2ca3f24
5 changed files with 61 additions and 17 deletions

View file

@ -51,13 +51,15 @@ impl Collection {
}
}
pub fn delete(self, conn: &DbConn) -> bool {
match diesel::delete(collections::table.filter(
collections::uuid.eq(self.uuid)))
.execute(&**conn) {
Ok(1) => true, // One row deleted
_ => false,
}
pub fn delete(self, conn: &DbConn) -> QueryResult<()> {
CollectionCipher::delete_all_by_collection(&self.uuid, &conn)?;
CollectionUser::delete_all_by_collection(&self.uuid, &conn)?;
diesel::delete(
collections::table.filter(
collections::uuid.eq(self.uuid)
)
).execute(&**conn).and(Ok(()))
}
pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> {
@ -130,14 +132,14 @@ use super::User;
#[belongs_to(User, foreign_key = "user_uuid")]
#[belongs_to(Collection, foreign_key = "collection_uuid")]
#[primary_key(user_uuid, collection_uuid)]
pub struct CollectionUsers {
pub struct CollectionUser {
pub user_uuid: String,
pub collection_uuid: String,
pub read_only: bool,
}
/// Database methods
impl CollectionUsers {
impl CollectionUser {
pub fn find_by_organization_and_user_uuid(org_uuid: &str, user_uuid: &str, conn: &DbConn) -> Vec<Self> {
users_collections::table
.filter(users_collections::user_uuid.eq(user_uuid))
@ -168,6 +170,12 @@ impl CollectionUsers {
_ => false,
}
}
pub fn delete_all_by_collection(collection_uuid: &str, conn: &DbConn) -> QueryResult<()> {
diesel::delete(users_collections::table
.filter(users_collections::collection_uuid.eq(collection_uuid))
).execute(&**conn).and(Ok(()))
}
}
use super::Cipher;
@ -210,4 +218,10 @@ impl CollectionCipher {
.filter(ciphers_collections::cipher_uuid.eq(cipher_uuid))
).execute(&**conn).and(Ok(()))
}
pub fn delete_all_by_collection(collection_uuid: &str, conn: &DbConn) -> QueryResult<()> {
diesel::delete(ciphers_collections::table
.filter(ciphers_collections::collection_uuid.eq(collection_uuid))
).execute(&**conn).and(Ok(()))
}
}

View file

@ -14,4 +14,4 @@ pub use self::folder::{Folder, FolderCipher};
pub use self::user::User;
pub use self::organization::Organization;
pub use self::organization::{UserOrganization, UserOrgStatus, UserOrgType};
pub use self::collection::{Collection, CollectionUsers, CollectionCipher};
pub use self::collection::{Collection, CollectionUser, CollectionCipher};

View file

@ -188,8 +188,8 @@ impl UserOrganization {
let coll_uuids = if self.access_all {
vec![] // If we have complete access, no need to fill the array
} else {
use super::CollectionUsers;
let collections = CollectionUsers::find_by_organization_and_user_uuid(&self.org_uuid, &self.user_uuid, conn);
use super::CollectionUser;
let collections = CollectionUser::find_by_organization_and_user_uuid(&self.org_uuid, &self.user_uuid, conn);
collections.iter().map(|c| json!({"Id": c.collection_uuid, "ReadOnly": c.read_only})).collect()
};