Skip to content

Commit

Permalink
delete_Account_grafana
Browse files Browse the repository at this point in the history
  • Loading branch information
dzlk17 committed Nov 21, 2024
1 parent 21eff50 commit 5507deb
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 9 deletions.
75 changes: 66 additions & 9 deletions server/src/http/cloud/delete_account_finish.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
env::is_env_production,
http::cloud::utils::{check_auth_code, validate_request},
middlewares::auth_middleware::UserId,
structs::{
Expand All @@ -10,10 +11,13 @@ use axum::{extract::State, http::StatusCode, Extension, Json};
use database::db::Db;
use garde::Validate;
use log::error;
use openapi::apis::configuration::Configuration;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use ts_rs::TS;

use super::grafana_utils::delete_user_account::handle_grafana_delete_user_account;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS, Validate)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
Expand All @@ -25,6 +29,7 @@ pub struct HttpDeleteAccountFinishRequest {
pub async fn delete_account_finish(
State(db): State<Arc<Db>>,
State(sessions_cache): State<Arc<ApiSessionsCache>>,
State(grafana_conf): State<Arc<Configuration>>,
Extension(user_id): Extension<UserId>,
Json(request): Json<HttpDeleteAccountFinishRequest>,
) -> Result<Json<()>, (StatusCode, String)> {
Expand Down Expand Up @@ -84,19 +89,71 @@ pub async fn delete_account_finish(
CloudApiErrors::DatabaseError.to_string(),
)
})?;


// Grafana, delete teams, apps and user
// TODO, fix this by fixing methods for setting up grafana datasource
if is_env_production() {
let mut owned_team_grafana_ids = Vec::new();
let mut non_owned_team_grafana_ids = Vec::new();

let teams = match db
.get_joined_teams_by_user_id(&user_id)
.await
.map_err(|err| {
error!("Failed to get user teams: {:?}", err);
(
StatusCode::INTERNAL_SERVER_ERROR,
CloudApiErrors::DatabaseError.to_string(),
)
}) {
Ok(joined_teams) => joined_teams,
Err(err) => {
error!("Failed to get user teams: {:?}", err);
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
CloudApiErrors::DatabaseError.to_string(),
));
}
};

for (team, _, _, _) in teams {
if let Some(grafana_id) = team.grafana_id {
if team.team_admin_id == user_id {
owned_team_grafana_ids.push(grafana_id);
} else {
non_owned_team_grafana_ids.push(grafana_id);
}
}
}

if let Err(err) = handle_grafana_delete_user_account(
&grafana_conf,
&owned_team_grafana_ids,
&non_owned_team_grafana_ids,
&user.email,
)
.await
{
error!("Failed to delete team from grafana: {:?}", err);
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
CloudApiErrors::GrafanaError.to_string(),
));
};
}

// Delete all invites connected to user
if let Err(err) = db
.cancel_all_team_invites_containing_email(&mut tx, &user.email, &user_id)
.await
.cancel_all_team_invites_containing_email(&mut tx, &user.email, &user_id)
.await
{
error!("Failed to delete team invites: {:?}", err);
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
CloudApiErrors::DatabaseError.to_string(),
));
}

// Delete all user apps
if let Err(err) = db.deactivate_user_apps(&mut tx, &user_id).await {
error!("Failed to delete user apps: {:?}", err);
Expand All @@ -105,7 +162,7 @@ pub async fn delete_account_finish(
CloudApiErrors::DatabaseError.to_string(),
));
}

// Leave all teams
if let Err(err) = db.remove_inactive_user_from_teams(&mut tx, &user_id).await {
error!("Failed to leave teams: {:?}", err);
Expand All @@ -117,9 +174,9 @@ pub async fn delete_account_finish(

// delete privileges
if let Err(err) = db
.remove_privileges_for_inactive_teams(&mut tx, &user_id)
.remove_privileges_for_inactive_teams(&mut tx, &user_id)
.await
{
{
error!("Failed to leave teams: {:?}", err);
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
Expand All @@ -135,7 +192,7 @@ pub async fn delete_account_finish(
CloudApiErrors::DatabaseError.to_string(),
));
}

// Deactivate the user
if let Err(err) = db.deactivate_user(&user_id, &mut tx).await {
error!("Failed to delete user: {:?}", err);
Expand All @@ -144,7 +201,7 @@ pub async fn delete_account_finish(
CloudApiErrors::DatabaseError.to_string(),
));
}

// Commit transaction
tx.commit().await.map_err(|err| {
error!("Failed to commit transaction: {:?}", err);
Expand Down
108 changes: 108 additions & 0 deletions server/src/http/cloud/grafana_utils/delete_user_account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use crate::structs::cloud::{
api_cloud_errors::CloudApiErrors, grafana_error::handle_grafana_error,
};
use axum::http::StatusCode;
use log::warn;
use openapi::apis::{
configuration::Configuration,
folders_api::delete_folder,
teams_api::{delete_team_by_id, get_team_by_id, remove_team_member},
users_api::get_user_by_login_or_email,
};
use std::sync::Arc;

pub async fn handle_grafana_delete_user_account(
grafana_conf: &Arc<Configuration>,
owned_team_grafana_ids: &Vec<String>,
non_owned_team_grafana_ids: &Vec<String>,
user_email: &String,
) -> Result<(), (StatusCode, String)> {
for team_id in owned_team_grafana_ids {
match get_team_by_id(&grafana_conf, team_id).await {
Ok(response) => match response.id {
Some(_) => (),
None => {
warn!("Failed to get team: {:?}, team_id: {:?}", response, team_id);
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
CloudApiErrors::TeamDoesNotExist.to_string(),
));
}
},
Err(err) => {
warn!("Failed to get team: {:?}, team_id: {:?}", err, team_id);
return Err(handle_grafana_error(err));
}
};

match delete_team_by_id(&grafana_conf, team_id).await {
Ok(_) => (),
Err(err) => {
warn!("Failed to delete team: {:?}, team_id: {:?}", err, team_id);
return Err(handle_grafana_error(err));
}
}

if let Err(err) = delete_folder(&grafana_conf, team_id, Some(false)).await {
warn!("Failed to delete folder: {:?}, team_id: {:?}", err, team_id);
return Err(handle_grafana_error(err));
};
}
// Check if user exists, if not return error
let user_id = match get_user_by_login_or_email(
&grafana_conf,
user_email.as_str().to_lowercase().as_str(),
)
.await
{
Ok(user) => match user.id {
Some(id) => id,
None => {
warn!("Failed to get id for user: {:?}", user);
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
CloudApiErrors::UserDoesNotExistInGrafana.to_string(),
));
}
},
Err(_) => {
warn!("Failed to get user: {:?}", user_email);
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
CloudApiErrors::UserDoesNotExistInGrafana.to_string(),
));
}
};

for team_id in non_owned_team_grafana_ids {
match get_team_by_id(&grafana_conf, team_id).await {
Ok(response) => match response.id {
Some(_) => (),
None => {
warn!("Failed to get team: {:?}, team_id: {:?}", response, team_id);
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
CloudApiErrors::TeamDoesNotExist.to_string(),
));
}
},
Err(err) => {
warn!("Failed to get team: {:?}, team_id: {:?}", err, team_id);
return Err(handle_grafana_error(err));
}
};

match remove_team_member(&grafana_conf, team_id, user_id).await {
Ok(_) => (),
Err(err) => {
warn!(
"Failed to remove user from team: {:?}, team_id: {:?}",
err, team_id
);
return Err(handle_grafana_error(err));
}
}
}

Ok(())
}
1 change: 1 addition & 0 deletions server/src/http/cloud/grafana_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod create_new_app;
pub mod create_new_team;
pub mod delete_registered_app;
pub mod delete_team;
pub mod delete_user_account;
pub mod import_template_dashboard;
pub mod remove_user_from_the_team;
pub mod setup_database_datasource;
Expand Down
1 change: 1 addition & 0 deletions server/src/structs/cloud/api_cloud_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ pub enum CloudApiErrors {
AdminCannotLeaveTeam,
GrafanaError,
TeamWithoutGrafanaId,
UserDoesNotExistInGrafana,
}

0 comments on commit 5507deb

Please sign in to comment.