2019-03-20 17:56:17 +01:00
|
|
|
use clap::{App, Arg, ArgMatches, SubCommand};
|
2018-10-05 14:09:04 +02:00
|
|
|
|
2019-03-20 17:56:17 +01:00
|
|
|
use plume_models::{instance::Instance, users::*, Connection};
|
2018-10-05 14:09:04 +02:00
|
|
|
use rpassword;
|
|
|
|
use std::io::{self, Write};
|
|
|
|
|
|
|
|
pub fn command<'a, 'b>() -> App<'a, 'b> {
|
|
|
|
SubCommand::with_name("users")
|
|
|
|
.about("Manage users")
|
2019-03-20 17:56:17 +01:00
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("new")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("name")
|
|
|
|
.short("n")
|
|
|
|
.long("name")
|
|
|
|
.alias("username")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("The username of the new user"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("display-name")
|
|
|
|
.short("N")
|
|
|
|
.long("display-name")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("The display name of the new user"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("biography")
|
|
|
|
.short("b")
|
|
|
|
.long("bio")
|
|
|
|
.alias("biography")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("The biography of the new user"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("email")
|
|
|
|
.short("e")
|
|
|
|
.long("email")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("Email address of the new user"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("password")
|
|
|
|
.short("p")
|
|
|
|
.long("password")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("The password of the new user"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("admin")
|
|
|
|
.short("a")
|
|
|
|
.long("admin")
|
|
|
|
.help("Makes the user an administrator of the instance"),
|
|
|
|
)
|
2019-09-13 12:28:36 +02:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("moderator")
|
|
|
|
.short("m")
|
|
|
|
.long("moderator")
|
|
|
|
.help("Makes the user a moderator of the instance"),
|
|
|
|
)
|
2019-03-20 17:56:17 +01:00
|
|
|
.about("Create a new user on this instance"),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("reset-password")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("name")
|
|
|
|
.short("u")
|
|
|
|
.long("user")
|
|
|
|
.alias("username")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("The username of the user to reset password to"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("password")
|
|
|
|
.short("p")
|
|
|
|
.long("password")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("The password new for the user"),
|
|
|
|
)
|
|
|
|
.about("Reset user password"),
|
|
|
|
)
|
2018-10-05 14:09:04 +02:00
|
|
|
}
|
|
|
|
|
2018-10-06 13:35:58 +02:00
|
|
|
pub fn run<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
2018-10-05 14:09:04 +02:00
|
|
|
let conn = conn;
|
|
|
|
match args.subcommand() {
|
|
|
|
("new", Some(x)) => new(x, conn),
|
2019-01-03 16:45:27 +01:00
|
|
|
("reset-password", Some(x)) => reset_password(x, conn),
|
2019-03-21 23:25:22 +01:00
|
|
|
("", None) => command().print_help().unwrap(),
|
2019-01-03 16:45:27 +01:00
|
|
|
_ => println!("Unknown subcommand"),
|
2018-10-05 14:09:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-06 13:35:58 +02:00
|
|
|
fn new<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
2019-03-20 17:56:17 +01:00
|
|
|
let username = args
|
|
|
|
.value_of("name")
|
|
|
|
.map(String::from)
|
|
|
|
.unwrap_or_else(|| super::ask_for("Username"));
|
|
|
|
let display_name = args
|
|
|
|
.value_of("display-name")
|
|
|
|
.map(String::from)
|
|
|
|
.unwrap_or_else(|| super::ask_for("Display name"));
|
2019-09-13 12:28:36 +02:00
|
|
|
|
2018-10-05 14:09:04 +02:00
|
|
|
let admin = args.is_present("admin");
|
2019-09-13 12:28:36 +02:00
|
|
|
let moderator = args.is_present("moderator");
|
|
|
|
let role = if admin {
|
|
|
|
Role::Admin
|
|
|
|
} else if moderator {
|
|
|
|
Role::Moderator
|
|
|
|
} else {
|
|
|
|
Role::Normal
|
|
|
|
};
|
|
|
|
|
2018-10-05 14:09:04 +02:00
|
|
|
let bio = args.value_of("biography").unwrap_or("").to_string();
|
2019-03-20 17:56:17 +01:00
|
|
|
let email = args
|
|
|
|
.value_of("email")
|
|
|
|
.map(String::from)
|
|
|
|
.unwrap_or_else(|| super::ask_for("Email address"));
|
|
|
|
let password = args
|
|
|
|
.value_of("password")
|
|
|
|
.map(String::from)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
print!("Password: ");
|
|
|
|
io::stdout().flush().expect("Couldn't flush STDOUT");
|
|
|
|
rpassword::read_password().expect("Couldn't read your password.")
|
|
|
|
});
|
2018-10-05 14:09:04 +02:00
|
|
|
|
|
|
|
NewUser::new_local(
|
|
|
|
conn,
|
|
|
|
username,
|
|
|
|
display_name,
|
2019-09-13 12:28:36 +02:00
|
|
|
role,
|
2018-11-26 10:21:52 +01:00
|
|
|
&bio,
|
2018-10-05 14:09:04 +02:00
|
|
|
email,
|
2018-12-29 09:36:07 +01:00
|
|
|
User::hash_pass(&password).expect("Couldn't hash password"),
|
2019-03-20 17:56:17 +01:00
|
|
|
)
|
|
|
|
.expect("Couldn't save new user");
|
2018-10-05 14:09:04 +02:00
|
|
|
}
|
2019-01-03 16:45:27 +01:00
|
|
|
|
|
|
|
fn reset_password<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
2019-03-20 17:56:17 +01:00
|
|
|
let username = args
|
|
|
|
.value_of("name")
|
|
|
|
.map(String::from)
|
|
|
|
.unwrap_or_else(|| super::ask_for("Username"));
|
|
|
|
let user = User::find_by_name(
|
|
|
|
conn,
|
|
|
|
&username,
|
2019-05-10 22:59:34 +02:00
|
|
|
Instance::get_local()
|
2019-03-20 17:56:17 +01:00
|
|
|
.expect("Failed to get local instance")
|
|
|
|
.id,
|
|
|
|
)
|
|
|
|
.expect("Failed to get user");
|
|
|
|
let password = args
|
|
|
|
.value_of("password")
|
|
|
|
.map(String::from)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
print!("Password: ");
|
|
|
|
io::stdout().flush().expect("Couldn't flush STDOUT");
|
|
|
|
rpassword::read_password().expect("Couldn't read your password.")
|
|
|
|
});
|
|
|
|
user.reset_password(conn, &password)
|
|
|
|
.expect("Failed to reset password");
|
2019-01-03 16:45:27 +01:00
|
|
|
}
|