1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00

Can reset user password from command line

This commit is contained in:
Pierre HUBERT 2021-05-04 19:51:18 +02:00
parent e73ebe9c12
commit d1d0f5818f

View File

@ -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{} - {}",
@ -112,5 +130,14 @@ 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(())
}