adminx 0.2.6

A powerful, modern admin panel framework for Rust built on Actix Web and MongoDB with automatic CRUD, role-based access control, and a beautiful responsive UI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// crates/adminx/src/bin/adminx.rs

use clap::{Parser, Subcommand};
use std::io::{self, Write};
use std::env;
use adminx::{
    models::adminx_model::{AdminxUser, get_admin_by_email, get_all_admins},
    utils::{
    	auth::{
    		AdminxStatus,
    	},
    	database::{
    		initiate_mongo_client,
    		initiate_database,
    		get_adminx_database,
    	},
	}
};
use mongodb::{bson::oid::ObjectId};

#[derive(Parser)]
#[command(name = "adminx")]
#[command(about = "AdminX CLI tool for managing admin users")]
#[command(version = "1.0")]
struct Cli {
    /// MongoDB connection URL
    #[arg(long, env = "MONGODB_URL")]
    mongodb_url: Option<String>,
    
    /// Database name
    #[arg(long, env = "ADMINX_DB_NAME")]
    database_name: Option<String>,
    
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Create a new admin user
    Create {
        /// Username for the admin
        #[arg(short, long)]
        username: String,
        /// Email address for the admin
        #[arg(short, long)]
        email: String,
        /// Password (will prompt if not provided)
        #[arg(short, long)]
        password: Option<String>,
        /// User status (active, inactive, suspended)
        #[arg(short, long, default_value = "active")]
        status: String,
        /// Skip confirmation prompt
        #[arg(short = 'y', long)]
        yes: bool,
    },
    /// List all admin users
    List {
        /// Include deleted users
        #[arg(short, long)]
        deleted: bool,
        /// Output format (table, json)
        #[arg(short, long, default_value = "table")]
        format: String,
    },
    /// Show details of a specific admin user
    Show {
        /// User email or ID
        identifier: String,
    },
    /// Delete an admin user (soft delete)
    Delete {
        /// User email or ID
        identifier: String,
        /// Skip confirmation prompt
        #[arg(short = 'y', long)]
        yes: bool,
    },
    /// Update admin user status
    Status {
        /// User email or ID
        identifier: String,
        /// New status (active, inactive, suspended)
        status: String,
    },
    /// Reset admin user password
    ResetPassword {
        /// User email or ID
        identifier: String,
        /// New password (will prompt if not provided)
        #[arg(short, long)]
        password: Option<String>,
    },
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cli = Cli::parse();
    
    // Get MongoDB URL and database name
    let mongodb_url = get_mongodb_url(&cli)?;
    let db_name = get_database_name(&cli)?;
    
    // Initialize database connection
    let db = initiate_mongo_client(&mongodb_url, &db_name).await;
    let _ = initiate_database(db);

    
    println!("Connected to MongoDB: {} (database: {})", mongodb_url, db_name);
    
    match cli.command {
        Commands::Create { username, email, password, status, yes } => {
            create_user(username, email, password, status, yes).await?;
        }
        Commands::List { deleted, format } => {
            list_users(deleted, format).await?;
        }
        Commands::Show { identifier } => {
            show_user(identifier).await?;
        }
        Commands::Delete { identifier, yes } => {
            delete_user(identifier, yes).await?;
        }
        Commands::Status { identifier, status } => {
            update_status(identifier, status).await?;
        }
        Commands::ResetPassword { identifier, password } => {
            reset_password(identifier, password).await?;
        }
    }
    
    Ok(())
}

fn get_mongodb_url(cli: &Cli) -> Result<String, Box<dyn std::error::Error>> {
    if let Some(url) = &cli.mongodb_url {
        return Ok(url.clone());
    }
    
    // Try environment variable
    if let Ok(url) = env::var("MONGODB_URL") {
        return Ok(url);
    }
    
    // Prompt user for MongoDB URL
    print!("Enter MongoDB URL (default: mongodb://localhost:27017): ");
    io::stdout().flush()?;
    
    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    let input = input.trim();
    
    if input.is_empty() {
        Ok("mongodb://localhost:27017".to_string())
    } else {
        Ok(input.to_string())
    }
}

fn get_database_name(cli: &Cli) -> Result<String, Box<dyn std::error::Error>> {
    if let Some(name) = &cli.database_name {
        return Ok(name.clone());
    }
    
    // Try environment variable
    if let Ok(name) = env::var("ADMINX_DB_NAME") {
        return Ok(name);
    }
    
    // Prompt user for database name
    print!("Enter database name (default: adminx): ");
    io::stdout().flush()?;
    
    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    let input = input.trim();
    
    if input.is_empty() {
        Ok("adminx".to_string())
    } else {
        Ok(input.to_string())
    }
}


async fn create_user(
    username: String,
    email: String,
    password: Option<String>,
    status_str: String,
    skip_confirm: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    // Parse status
    let status = match status_str.to_lowercase().as_str() {
        "active" => AdminxStatus::Active,
        "inactive" => AdminxStatus::Inactive,
        "suspended" => AdminxStatus::Suspended,
        _ => {
            eprintln!("Invalid status. Must be one of: active, inactive, suspended");
            return Ok(());
        }
    };
    
    // Get password if not provided
    let password = match password {
        Some(p) => p,
        None => {
            print!("Enter password: ");
            io::stdout().flush()?;
            let password = rpassword::read_password()?;
            if password.len() < 8 {
                eprintln!("Password must be at least 8 characters long");
                return Ok(());
            }
            password
        }
    };
    
    // Check if user already exists
    if let Some(_) = get_admin_by_email(&email).await {
        eprintln!("User with email {} already exists", email);
        return Ok(());
    }
    
    // Show confirmation
    if !skip_confirm {
        println!("Creating admin user:");
        println!("  Username: {}", username);
        println!("  Email: {}", email);
        println!("  Status: {:?}", status);
        print!("Continue? (y/N): ");
        io::stdout().flush()?;
        
        let mut input = String::new();
        io::stdin().read_line(&mut input)?;
        
        if !input.trim().to_lowercase().starts_with('y') {
            println!("Cancelled");
            return Ok(());
        }
    }
    
    // Create user
    match AdminxUser::create_new_user_with_status(username, email.clone(), password, status).await {
        Ok(user_id) => {
            println!("✓ Successfully created admin user");
            println!("  ID: {}", user_id);
            println!("  Email: {}", email);
        }
        Err(e) => {
            eprintln!("Failed to create user: {}", e);
        }
    }
    
    Ok(())
}

async fn list_users(include_deleted: bool, format: String) -> Result<(), Box<dyn std::error::Error>> {
    let users = get_all_admins(include_deleted).await?;
    
    if users.is_empty() {
        println!("No users found");
        return Ok(());
    }
    
    match format.as_str() {
        "json" => {
            let public_users: Vec<_> = users.iter().map(|u| u.to_public()).collect();
            println!("{}", serde_json::to_string_pretty(&public_users)?);
        }
        "table" | _ => {
            println!("{:<25} {:<30} {:<15} {:<10} {:<20}", "ID", "Email", "Username", "Status", "Created");
            println!("{}", "-".repeat(100));
            
            for user in users {
                println!(
                    "{:<25} {:<30} {:<15} {:<10} {:<20}",
                    user.id.map_or("N/A".to_string(), |id| id.to_string()),
                    user.email,
                    user.username,
                    format!("{:?}", user.status),
                    user.created_at.to_chrono().format("%Y-%m-%d %H:%M").to_string()
                );
            }
        }
    }
    
    Ok(())
}

async fn show_user(identifier: String) -> Result<(), Box<dyn std::error::Error>> {
    let user = find_user_by_identifier(&identifier).await?;
    
    match user {
        Some(user) => {
            println!("Admin User Details:");
            println!("  ID: {}", user.id.map_or("N/A".to_string(), |id| id.to_string()));
            println!("  Username: {}", user.username);
            println!("  Email: {}", user.email);
            println!("  Status: {:?}", user.status);
            println!("  Deleted: {}", user.delete);
            println!("  Created: {}", user.created_at.to_chrono().format("%Y-%m-%d %H:%M:%S"));
            println!("  Updated: {}", user.updated_at.to_chrono().format("%Y-%m-%d %H:%M:%S"));
        }
        None => {
            println!("User not found: {}", identifier);
        }
    }
    
    Ok(())
}

async fn delete_user(identifier: String, skip_confirm: bool) -> Result<(), Box<dyn std::error::Error>> {
    let user = find_user_by_identifier(&identifier).await?;
    
    let user = match user {
        Some(user) => user,
        None => {
            println!("User not found: {}", identifier);
            return Ok(());
        }
    };
    
    if user.delete {
        println!("User is already deleted");
        return Ok(());
    }
    
    if !skip_confirm {
        println!("Delete user:");
        println!("  Email: {}", user.email);
        println!("  Username: {}", user.username);
        print!("Continue? (y/N): ");
        io::stdout().flush()?;
        
        let mut input = String::new();
        io::stdin().read_line(&mut input)?;
        
        if !input.trim().to_lowercase().starts_with('y') {
            println!("Cancelled");
            return Ok(());
        }
    }
    
    if let Some(user_id) = user.id {
        match adminx::models::adminx_model::delete_admin_by_id(&user_id).await {
            Ok(true) => println!("✓ User deleted successfully"),
            Ok(false) => println!("User not found or already deleted"),
            Err(e) => eprintln!("Failed to delete user: {}", e),
        }
    }
    
    Ok(())
}

async fn update_status(identifier: String, status_str: String) -> Result<(), Box<dyn std::error::Error>> {
    let status = match status_str.to_lowercase().as_str() {
        "active" => AdminxStatus::Active,
        "inactive" => AdminxStatus::Inactive,
        "suspended" => AdminxStatus::Suspended,
        _ => {
            eprintln!("Invalid status. Must be one of: active, inactive, suspended");
            return Ok(());
        }
    };
    
    let user = find_user_by_identifier(&identifier).await?;
    
    let user = match user {
        Some(user) => user,
        None => {
            println!("User not found: {}", identifier);
            return Ok(());
        }
    };
    
    if let Some(user_id) = user.id {
        match adminx::models::adminx_model::update_admin_status(&user_id, status).await {
            Ok(true) => println!("✓ User status updated successfully"),
            Ok(false) => println!("Failed to update user status"),
            Err(e) => eprintln!("Error updating status: {}", e),
        }
    }
    
    Ok(())
}

async fn reset_password(identifier: String, password: Option<String>) -> Result<(), Box<dyn std::error::Error>> {
    let user = find_user_by_identifier(&identifier).await?;
    
    let user = match user {
        Some(user) => user,
        None => {
            println!("User not found: {}", identifier);
            return Ok(());
        }
    };
    
    let new_password = match password {
        Some(p) => p,
        None => {
            print!("Enter new password: ");
            io::stdout().flush()?;
            let password = rpassword::read_password()?;
            if password.len() < 8 {
                eprintln!("Password must be at least 8 characters long");
                return Ok(());
            }
            password
        }
    };
    
    // For password reset, we'll directly hash and update (bypass current password check)
    let hashed_password = bcrypt::hash(new_password, bcrypt::DEFAULT_COST)
        .map_err(|e| format!("Failed to hash password: {}", e))?;
    
    // Update in database directly
    if let Some(user_id) = user.id {
        let db = get_adminx_database();
        let collection = db.collection::<AdminxUser>("adminxs");
        
        let result = collection.update_one(
            mongodb::bson::doc! { "_id": user_id },
            mongodb::bson::doc! { 
                "$set": { 
                    "password": hashed_password,
                    "updated_at": mongodb::bson::DateTime::now()
                }
            },
            None,
        ).await?;
        
        if result.modified_count > 0 {
            println!("✓ Password reset successfully");
        } else {
            println!("Failed to reset password");
        }
    }
    
    Ok(())
}

async fn find_user_by_identifier(identifier: &str) -> Result<Option<AdminxUser>, Box<dyn std::error::Error>> {
    // First try to find by email
    if let Some(user) = get_admin_by_email(identifier).await {
        return Ok(Some(user));
    }
    
    // Then try to parse as ObjectId and find by ID
    if let Ok(object_id) = ObjectId::parse_str(identifier) {
        if let Some(user) = adminx::models::adminx_model::get_admin_by_id(&object_id).await {
            return Ok(Some(user));
        }
    }
    
    Ok(None)
}