Implement deleting Organization

pull/25/head
Miroslav Prasil 7 years ago
parent 726ba36e5b
commit 941747f9e8

@ -55,13 +55,30 @@ fn create_organization(headers: Headers, data: Json<OrgData>, conn: DbConn) -> J
} }
#[post("/organizations/<org_id>/delete", data = "<data>")] #[post("/organizations/<org_id>/delete", data = "<data>")]
fn delete_organization(org_id: String, data: Json<PasswordData>, headers: Headers, conn: DbConn) -> JsonResult { fn delete_organization(org_id: String, data: Json<PasswordData>, headers: Headers, conn: DbConn) -> EmptyResult {
let data: PasswordData = data.into_inner(); let data: PasswordData = data.into_inner();
let password_hash = data.masterPasswordHash; let password_hash = data.masterPasswordHash;
// TODO: Delete ciphers from organization, collection_users, collections, organization_users and the org itself if !headers.user.check_valid_password(&password_hash) {
err!("Invalid password")
}
let org_user = match UserOrganization::find_by_user_and_org(&headers.user.uuid, &org_id, &conn) {
Some(user) => user,
None => err!("The current user isn't member of the organization")
};
if org_user.type_ != UserOrgType::Owner as i32 {
err!("Only owner is able to delete organization")
}
unimplemented!() match Organization::find_by_uuid(&org_id, &conn) {
None => err!("Organization not found"),
Some(org) => match org.delete(&conn) {
Ok(()) => Ok(()),
Err(_) => err!("Failed deleting the organization")
}
}
} }
#[get("/organizations/<org_id>")] #[get("/organizations/<org_id>")]
@ -513,11 +530,8 @@ fn delete_user(org_id: String, user_id: String, headers: Headers, conn: DbConn)
} }
} }
user_to_delete.delete(&conn); match user_to_delete.delete(&conn) {
Ok(()) => Ok(()),
for c in Collection::find_by_organization_and_user_uuid(&org_id, &current_user.uuid, &conn) { Err(_) => err!("Failed deleting user from organization")
CollectionUser::delete(&current_user.uuid, &c.uuid, &conn);
} }
Ok(())
} }

@ -145,6 +145,13 @@ impl Cipher {
).execute(&**conn).and(Ok(())) ).execute(&**conn).and(Ok(()))
} }
pub fn delete_all_by_organization(org_uuid: &str, conn: &DbConn) -> QueryResult<()> {
for cipher in Self::find_by_org(org_uuid, &conn) {
cipher.delete(&conn)?;
}
Ok(())
}
pub fn move_to_folder(&self, folder_uuid: Option<String>, user_uuid: &str, conn: &DbConn) -> Result<(), &str> { pub fn move_to_folder(&self, folder_uuid: Option<String>, user_uuid: &str, conn: &DbConn) -> Result<(), &str> {
match self.get_folder_uuid(&user_uuid, &conn) { match self.get_folder_uuid(&user_uuid, &conn) {
None => { None => {

@ -62,6 +62,13 @@ impl Collection {
).execute(&**conn).and(Ok(())) ).execute(&**conn).and(Ok(()))
} }
pub fn delete_all_by_organization(org_uuid: &str, conn: &DbConn) -> QueryResult<()> {
for collection in Self::find_by_organization(org_uuid, &conn) {
collection.delete(&conn)?;
}
Ok(())
}
pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> { pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> {
collections::table collections::table
.filter(collections::uuid.eq(uuid)) .filter(collections::uuid.eq(uuid))
@ -176,6 +183,12 @@ impl CollectionUser {
.filter(users_collections::collection_uuid.eq(collection_uuid)) .filter(users_collections::collection_uuid.eq(collection_uuid))
).execute(&**conn).and(Ok(())) ).execute(&**conn).and(Ok(()))
} }
pub fn delete_all_by_user(user_uuid: &str, conn: &DbConn) -> QueryResult<()> {
diesel::delete(users_collections::table
.filter(users_collections::user_uuid.eq(user_uuid))
).execute(&**conn).and(Ok(()))
}
} }
use super::Cipher; use super::Cipher;

@ -122,13 +122,18 @@ impl Organization {
} }
} }
pub fn delete(self, conn: &DbConn) -> bool { pub fn delete(self, conn: &DbConn) -> QueryResult<()> {
match diesel::delete(organizations::table.filter( use super::{Cipher, Collection};
organizations::uuid.eq(self.uuid)))
.execute(&**conn) { Cipher::delete_all_by_organization(&self.uuid, &conn)?;
Ok(1) => true, // One row deleted Collection::delete_all_by_organization(&self.uuid, &conn)?;
_ => false, UserOrganization::delete_all_by_organization(&self.uuid, &conn)?;
}
diesel::delete(
organizations::table.filter(
organizations::uuid.eq(self.uuid)
)
).execute(&**conn).and(Ok(()))
} }
pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> { pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> {
@ -215,13 +220,23 @@ impl UserOrganization {
} }
} }
pub fn delete(self, conn: &DbConn) -> bool { pub fn delete(self, conn: &DbConn) -> QueryResult<()> {
match diesel::delete(users_organizations::table.filter( use super::CollectionUser;
users_organizations::uuid.eq(self.uuid)))
.execute(&**conn) { CollectionUser::delete_all_by_user(&self.user_uuid, &conn)?;
Ok(1) => true, // One row deleted
_ => false, diesel::delete(
users_organizations::table.filter(
users_organizations::uuid.eq(self.uuid)
)
).execute(&**conn).and(Ok(()))
}
pub fn delete_all_by_organization(org_uuid: &str, conn: &DbConn) -> QueryResult<()> {
for user_org in Self::find_by_org(&org_uuid, &conn) {
user_org.delete(&conn)?;
} }
Ok(())
} }
pub fn has_full_access(self) -> bool { pub fn has_full_access(self) -> bool {

Loading…
Cancel
Save