From d1d0f5818faf06bea8440bf617e03bb1ad305e19 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 4 May 2021 19:51:18 +0200 Subject: [PATCH] Can reset user password from command line --- src/main.rs | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index c5494e8..79736a8 100644 --- a/src/main.rs +++ b/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, - function: Box MainActionFunction>, + function: Box) -> MainActionFunction>, } fn get_actions() -> Vec { @@ -28,6 +30,14 @@ fn get_actions() -> Vec { 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 = 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) -> 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) -> 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{} - {}", @@ -112,5 +130,14 @@ fn help() -> std::io::Result<()> { ); } + Ok(()) +} + +fn reset_password(args: Vec) -> Res { + let user_id = UserID::new(args[0].parse::()?); + let token = account_helper::generate_password_reset_token(&user_id)?; + + println!("{}", conf().password_reset_url.replace("{TOKEN}", &token)); + Ok(()) } \ No newline at end of file