mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-10-30 23:24:42 +00:00 
			
		
		
		
	Can reset user password from command line
This commit is contained in:
		
							
								
								
									
										43
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								src/main.rs
									
									
									
									
									
								
							| @@ -1,15 +1,17 @@ | ||||
| use comunic_server::{cleanup_thread, server}; | ||||
| use comunic_server::data::config::{conf, Config}; | ||||
| use comunic_server::helpers::database; | ||||
| use comunic_server::data::error::Res; | ||||
| use comunic_server::data::user::UserID; | ||||
| use comunic_server::helpers::{account_helper, database}; | ||||
| use comunic_server::utils::date_utils::current_year; | ||||
|  | ||||
| type MainActionFunction = std::io::Result<()>; | ||||
| type MainActionFunction = Res; | ||||
|  | ||||
| struct Action { | ||||
|     name: String, | ||||
|     description: String, | ||||
|     arguments: Vec<String>, | ||||
|     function: Box<dyn Fn() -> MainActionFunction>, | ||||
|     function: Box<dyn Fn(Vec<String>) -> MainActionFunction>, | ||||
| } | ||||
|  | ||||
| fn get_actions() -> Vec<Action> { | ||||
| @@ -28,6 +30,14 @@ fn get_actions() -> Vec<Action> { | ||||
|             description: "Show this help".to_string(), | ||||
|             arguments: vec![], | ||||
|             function: Box::new(help), | ||||
|         }, | ||||
|  | ||||
|         // Reset password | ||||
|         Action { | ||||
|             name: "reset_password".to_string(), | ||||
|             description: "Create a password reset URL for a user".to_string(), | ||||
|             arguments: vec!["user_id".to_string()], | ||||
|             function: Box::new(reset_password), | ||||
|         } | ||||
|     ] | ||||
| } | ||||
| @@ -37,7 +47,10 @@ async fn main() -> std::io::Result<()> { | ||||
|     let args: Vec<String> = std::env::args().collect(); | ||||
|     let conf_file = match args.get(1) { | ||||
|         Some(el) => el.to_string(), | ||||
|         None => "config.yaml".to_string(), | ||||
|         None => { | ||||
|             eprintln!("Please specify configuration file as first argument!"); | ||||
|             std::process::exit(-3); | ||||
|         } | ||||
|     }; | ||||
|  | ||||
|     // Load configuration | ||||
| @@ -68,17 +81,20 @@ async fn main() -> std::io::Result<()> { | ||||
|         Some(a) => a | ||||
|     }; | ||||
|  | ||||
|     if !selected_action.arguments.is_empty() && selected_action.arguments.len() + 2 > args.len() { | ||||
|     if !selected_action.arguments.is_empty() && selected_action.arguments.len() + 3 != args.len() { | ||||
|         eprintln!("Invalid number of arguments!"); | ||||
|         std::process::exit(-2); | ||||
|     } | ||||
|  | ||||
|     (selected_action.function)()?; | ||||
|     let args = &args[3..]; | ||||
|  | ||||
|     let res = (selected_action.function)(args.to_vec()); | ||||
|     res.expect("Failed to execute action!"); | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| /// Start Comunic Server (main action) | ||||
| fn serve() -> std::io::Result<()> { | ||||
| fn serve(_a: Vec<String>) -> Res { | ||||
|     let t = std::thread::spawn(|| { | ||||
|         let sys = actix::System::new("sys"); | ||||
|  | ||||
| @@ -100,9 +116,11 @@ fn serve() -> std::io::Result<()> { | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| fn help() -> std::io::Result<()> { | ||||
| fn help(_a: Vec<String>) -> Res { | ||||
|     println!("Comunic API v3 Server - (c) Pierre HUBERT 2012 - {}", current_year()); | ||||
|  | ||||
|  | ||||
|     println!("Usage: {} [conf-file] [action] [args...]", std::env::args().next().unwrap()); | ||||
|     println!("Available actions:"); | ||||
|     for action in get_actions() { | ||||
|         println!("\t{}\t{} - {}", | ||||
| @@ -114,3 +132,12 @@ fn help() -> std::io::Result<()> { | ||||
|  | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| fn reset_password(args: Vec<String>) -> Res { | ||||
|     let user_id = UserID::new(args[0].parse::<u64>()?); | ||||
|     let token = account_helper::generate_password_reset_token(&user_id)?; | ||||
|  | ||||
|     println!("{}", conf().password_reset_url.replace("{TOKEN}", &token)); | ||||
|  | ||||
|     Ok(()) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user