|
| 1 | +use crate::prelude::api::*; |
| 2 | +use crate::prelude::types::*; |
| 3 | +use axum::extract::{Path, State}; |
| 4 | +use axum::http::StatusCode; |
| 5 | +use axum::response::IntoResponse; |
| 6 | +use axum::Json; |
| 7 | +use axum_extra::extract::CookieJar; |
| 8 | +use chrono::Utc; |
| 9 | +use rustmail_types::api::panel_permissions::*; |
| 10 | +use sqlx::{Row, query}; |
| 11 | +use std::sync::Arc; |
| 12 | +use tokio::sync::Mutex; |
| 13 | + |
| 14 | +pub async fn handle_list_permissions( |
| 15 | + State(bot_state): State<Arc<Mutex<BotState>>>, |
| 16 | +) -> impl IntoResponse { |
| 17 | + let db_pool = { |
| 18 | + let state_lock = bot_state.lock().await; |
| 19 | + match &state_lock.db_pool { |
| 20 | + Some(pool) => pool.clone(), |
| 21 | + None => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": "Database not initialized"}))).into_response(), |
| 22 | + } |
| 23 | + }; |
| 24 | + |
| 25 | + let rows = match query("SELECT * FROM panel_permissions ORDER BY granted_at DESC") |
| 26 | + .fetch_all(&db_pool) |
| 27 | + .await |
| 28 | + { |
| 29 | + Ok(r) => r, |
| 30 | + Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": format!("Database error: {}", e)}))).into_response(), |
| 31 | + }; |
| 32 | + |
| 33 | + let mut permissions = Vec::new(); |
| 34 | + for row in rows { |
| 35 | + if let (Ok(id), Ok(subject_type), Ok(subject_id), Ok(permission), Ok(granted_by), Ok(granted_at)) = ( |
| 36 | + row.try_get::<i64, _>("id"), |
| 37 | + row.try_get::<String, _>("subject_type"), |
| 38 | + row.try_get::<String, _>("subject_id"), |
| 39 | + row.try_get::<String, _>("permission"), |
| 40 | + row.try_get::<String, _>("granted_by"), |
| 41 | + row.try_get::<i64, _>("granted_at"), |
| 42 | + ) { |
| 43 | + if let (Some(st), Some(perm)) = ( |
| 44 | + SubjectType::from_str(&subject_type), |
| 45 | + PanelPermission::from_str(&permission), |
| 46 | + ) { |
| 47 | + permissions.push(PanelPermissionEntry { |
| 48 | + id, |
| 49 | + subject_type: st, |
| 50 | + subject_id, |
| 51 | + permission: perm, |
| 52 | + granted_by, |
| 53 | + granted_at, |
| 54 | + }); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + (StatusCode::OK, Json(permissions)).into_response() |
| 60 | +} |
| 61 | + |
| 62 | +pub async fn handle_grant_permission( |
| 63 | + State(bot_state): State<Arc<Mutex<BotState>>>, |
| 64 | + jar: CookieJar, |
| 65 | + Json(request): Json<GrantPermissionRequest>, |
| 66 | +) -> impl IntoResponse { |
| 67 | + let (db_pool, user_id) = { |
| 68 | + let state_lock = bot_state.lock().await; |
| 69 | + let pool = match &state_lock.db_pool { |
| 70 | + Some(p) => p.clone(), |
| 71 | + None => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": "Database not initialized"}))).into_response(), |
| 72 | + }; |
| 73 | + |
| 74 | + let session_cookie = jar.get("session_id"); |
| 75 | + if session_cookie.is_none() { |
| 76 | + return (StatusCode::UNAUTHORIZED, Json(serde_json::json!({"error": "Unauthorized"}))).into_response(); |
| 77 | + } |
| 78 | + |
| 79 | + let session_id = session_cookie.unwrap().value().to_string(); |
| 80 | + let uid = get_user_id_from_session(&session_id, &pool).await; |
| 81 | + (pool, uid) |
| 82 | + }; |
| 83 | + |
| 84 | + let subject_type_str = request.subject_type.as_str(); |
| 85 | + let permission_str = request.permission.as_str(); |
| 86 | + let now = Utc::now().timestamp(); |
| 87 | + |
| 88 | + let result = query( |
| 89 | + "INSERT INTO panel_permissions (subject_type, subject_id, permission, granted_by, granted_at) |
| 90 | + VALUES (?, ?, ?, ?, ?) |
| 91 | + ON CONFLICT(subject_type, subject_id, permission) DO UPDATE SET granted_by = ?, granted_at = ?" |
| 92 | + ) |
| 93 | + .bind(subject_type_str) |
| 94 | + .bind(&request.subject_id) |
| 95 | + .bind(permission_str) |
| 96 | + .bind(&user_id) |
| 97 | + .bind(now) |
| 98 | + .bind(&user_id) |
| 99 | + .bind(now) |
| 100 | + .execute(&db_pool) |
| 101 | + .await; |
| 102 | + |
| 103 | + match result { |
| 104 | + Ok(_) => (StatusCode::OK, Json(serde_json::json!({"success": true}))).into_response(), |
| 105 | + Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": format!("Database error: {}", e)}))).into_response(), |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +pub async fn handle_revoke_permission( |
| 110 | + State(bot_state): State<Arc<Mutex<BotState>>>, |
| 111 | + Path(permission_id): Path<i64>, |
| 112 | +) -> impl IntoResponse { |
| 113 | + let db_pool = { |
| 114 | + let state_lock = bot_state.lock().await; |
| 115 | + match &state_lock.db_pool { |
| 116 | + Some(pool) => pool.clone(), |
| 117 | + None => return (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": "Database not initialized"}))).into_response(), |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + let result = query("DELETE FROM panel_permissions WHERE id = ?") |
| 122 | + .bind(permission_id) |
| 123 | + .execute(&db_pool) |
| 124 | + .await; |
| 125 | + |
| 126 | + match result { |
| 127 | + Ok(_) => (StatusCode::OK, Json(serde_json::json!({"success": true}))).into_response(), |
| 128 | + Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({"error": format!("Database error: {}", e)}))).into_response(), |
| 129 | + } |
| 130 | +} |
0 commit comments