|
| 1 | +use crate::operations::{Operation, OperationExample, OperationParameter, OperationRoute}; |
| 2 | +use crate::providers::user::UserProvider; |
| 3 | +use axum::http::Method; |
| 4 | +use std::sync::Arc; |
| 5 | +use tracing::{error, info}; |
| 6 | + |
| 7 | +use crate::types::{Permission, User}; |
| 8 | + |
| 9 | +/// Delete user operation |
| 10 | +#[derive(Clone)] |
| 11 | +pub struct UserDeleteOperation { |
| 12 | + user_provider: Arc<dyn UserProvider>, |
| 13 | +} |
| 14 | + |
| 15 | +impl UserDeleteOperation { |
| 16 | + pub fn new(user_provider: Arc<dyn UserProvider>) -> Self { |
| 17 | + Self { user_provider } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +impl Operation for UserDeleteOperation { |
| 22 | + fn name(&self) -> &'static str { |
| 23 | + "user/delete" |
| 24 | + } |
| 25 | + |
| 26 | + fn response_content_type(&self) -> &'static str { |
| 27 | + "text/x-moo" |
| 28 | + } |
| 29 | + |
| 30 | + fn description(&self) -> &'static str { |
| 31 | + "Delete a user from the system" |
| 32 | + } |
| 33 | + |
| 34 | + fn philosophy(&self) -> &'static str { |
| 35 | + "Permanently deletes a user account from the system, removing all their data, permissions, \ |
| 36 | + and API keys. This operation is irreversible and should be used with caution. System users \ |
| 37 | + (like Wizard and Everyone) cannot be deleted to maintain system integrity. This operation \ |
| 38 | + requires the DeleteUser permission and is intended for removing accounts that are no longer \ |
| 39 | + needed or were created in error." |
| 40 | + } |
| 41 | + |
| 42 | + fn parameters(&self) -> Vec<OperationParameter> { |
| 43 | + vec![OperationParameter { |
| 44 | + name: "user_id".to_string(), |
| 45 | + description: "ID of the user to delete".to_string(), |
| 46 | + required: true, |
| 47 | + }] |
| 48 | + } |
| 49 | + |
| 50 | + fn examples(&self) -> Vec<OperationExample> { |
| 51 | + vec![OperationExample { |
| 52 | + description: "Delete a user".to_string(), |
| 53 | + moocode: r#"result = worker_request("vcs", {"user/delete", "alice"}); |
| 54 | +// Permanently deletes user 'alice' and all associated data"# |
| 55 | + .to_string(), |
| 56 | + http_curl: Some( |
| 57 | + r#"curl -X POST http://localhost:8081/api/user/delete \ |
| 58 | + -H "Content-Type: application/json" \ |
| 59 | + -d '{"operation": "user/delete", "args": ["alice"]}' |
| 60 | +"# |
| 61 | + .to_string(), |
| 62 | + ), |
| 63 | + }] |
| 64 | + } |
| 65 | + |
| 66 | + fn routes(&self) -> Vec<OperationRoute> { |
| 67 | + vec![OperationRoute { |
| 68 | + path: "/api/user/delete".to_string(), |
| 69 | + method: Method::POST, |
| 70 | + is_json: true, |
| 71 | + }] |
| 72 | + } |
| 73 | + |
| 74 | + fn responses(&self) -> Vec<crate::operations::OperationResponse> { |
| 75 | + use crate::operations::OperationResponse; |
| 76 | + vec![ |
| 77 | + OperationResponse::success( |
| 78 | + "Operation executed successfully", |
| 79 | + r#""Operation completed successfully""#, |
| 80 | + ), |
| 81 | + OperationResponse::new( |
| 82 | + 400, |
| 83 | + "Bad Request - Invalid arguments", |
| 84 | + r#"E_INVARG("Error: Invalid operation arguments")"#, |
| 85 | + ), |
| 86 | + OperationResponse::new( |
| 87 | + 404, |
| 88 | + "Not Found - Resource not found", |
| 89 | + r#"E_INVARG("Error: Resource not found")"#, |
| 90 | + ), |
| 91 | + OperationResponse::new( |
| 92 | + 500, |
| 93 | + "Internal Server Error - Database or system error", |
| 94 | + r#"E_INVARG("Error: Database error: operation failed")"#, |
| 95 | + ), |
| 96 | + ] |
| 97 | + } |
| 98 | + |
| 99 | + fn execute(&self, args: Vec<String>, user: &User) -> moor_var::Var { |
| 100 | + info!("Executing user/delete operation for user: {}", user.id); |
| 101 | + |
| 102 | + // Check permission |
| 103 | + if !user.has_permission(&Permission::DeleteUser) { |
| 104 | + error!("User {} does not have DeleteUser permission", user.id); |
| 105 | + return moor_var::v_error( |
| 106 | + moor_var::E_INVARG.msg("Error: You do not have permission to delete users"), |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + // Validate arguments |
| 111 | + if args.is_empty() { |
| 112 | + error!("Invalid arguments for user/delete: expected 1, got 0"); |
| 113 | + return moor_var::v_error( |
| 114 | + moor_var::E_INVARG.msg("Error: Expected 1 argument: user_id"), |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + let target_user_id = &args[0]; |
| 119 | + |
| 120 | + // Delete the user |
| 121 | + match self.user_provider.delete_user(target_user_id) { |
| 122 | + Ok(true) => { |
| 123 | + info!("Deleted user '{}'", target_user_id); |
| 124 | + moor_var::v_str(&format!("Successfully deleted user '{target_user_id}'")) |
| 125 | + } |
| 126 | + Ok(false) => { |
| 127 | + error!("User '{}' not found", target_user_id); |
| 128 | + moor_var::v_error(moor_var::E_INVARG.msg(format!( |
| 129 | + "Error: User '{target_user_id}' not found" |
| 130 | + ))) |
| 131 | + } |
| 132 | + Err(e) => { |
| 133 | + error!("Failed to delete user: {}", e); |
| 134 | + moor_var::v_error(moor_var::E_INVARG.msg(format!("Error: {e}"))) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
0 commit comments