From d00688e5263496a3089737a29c8609e52e71bfc8 Mon Sep 17 00:00:00 2001 From: Bat Date: Tue, 19 Jun 2018 16:14:52 +0100 Subject: [PATCH] Admin creation --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 1 + src/main.rs | 1 + src/setup.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 93008322..fc705f35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1024,6 +1024,7 @@ dependencies = [ "rocket_codegen 0.4.0-dev (git+https://github.com/SergioBenitez/Rocket?rev=df7111143e466c18d1f56377a8d9530a5a306aba)", "rocket_contrib 0.4.0-dev (git+https://github.com/SergioBenitez/Rocket?rev=df7111143e466c18d1f56377a8d9530a5a306aba)", "rocket_i18n 0.1.1 (git+https://github.com/BaptisteGelez/rocket_i18n?rev=5b4225d5bed5769482dc926a7e6d6b79f1217be6)", + "rpassword 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.42 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.43 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1291,6 +1292,16 @@ dependencies = [ "tera 0.11.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rpassword" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rustc-demangle" version = "0.1.7" @@ -2120,6 +2131,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rocket_contrib 0.4.0-dev (git+https://github.com/SergioBenitez/Rocket?rev=df7111143e466c18d1f56377a8d9530a5a306aba)" = "" "checksum rocket_http 0.4.0-dev (git+https://github.com/SergioBenitez/Rocket?rev=df7111143e466c18d1f56377a8d9530a5a306aba)" = "" "checksum rocket_i18n 0.1.1 (git+https://github.com/BaptisteGelez/rocket_i18n?rev=5b4225d5bed5769482dc926a7e6d6b79f1217be6)" = "" +"checksum rpassword 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d127299b02abda51634f14025aec43ae87a7aa7a95202b6a868ec852607d1451" "checksum rustc-demangle 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "11fb43a206a04116ffd7cfcf9bcb941f8eb6cc7ff667272246b0a1c74259a3cb" "checksum safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e27a8b19b835f7aea908818e871f5cc3a5a186550c30773be987e155e8163d8f" "checksum schannel 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "85fd9df495640643ad2d00443b3d78aae69802ad488debab4f1dd52fc1806ade" diff --git a/Cargo.toml b/Cargo.toml index 4f5b0f27..62865e72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ hyper = "*" lazy_static = "*" openssl = "0.10.6" reqwest = "0.8" +rpassword = "2.0" serde = "*" serde_derive = "1.0" serde_json = "1.0" diff --git a/src/main.rs b/src/main.rs index 3ad26d59..c9b18be1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,7 @@ extern crate reqwest; extern crate rocket; extern crate rocket_contrib; extern crate rocket_i18n; +extern crate rpassword; extern crate serde; #[macro_use] extern crate serde_derive; diff --git a/src/setup.rs b/src/setup.rs index e154162e..c87fc0f3 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -4,10 +4,12 @@ use dotenv::dotenv; use std::fs; use std::io; use std::process::{exit, Command}; +use rpassword; use DB_URL; use db_conn::DbConn; use models::instance::*; +use models::users::*; type PgPool = Pool>; @@ -48,6 +50,12 @@ fn run_setup(conn: Option) { read_line(); check_native_deps(); setup_type(conn.expect("Couldn't connect to the Plume database")); + + println!("{}\n{}\n{}", + "Your Plume instance is now ready to be used.".magenta(), + "We hope you will enjoy it.".magenta(), + "If you ever encounter a problem, feel free to report it at https://github.com/Plume-org/Plume/issues/".magenta(), + ); } fn setup_type(conn: DbConn) { @@ -69,18 +77,20 @@ fn setup_type(conn: DbConn) { fn quick_setup(conn: DbConn) { println!("What is your instance domain?"); let domain = read_line(); - write_to_dotenv("BASE_URL", domain); + write_to_dotenv("BASE_URL", domain.clone()); println!("\nWhat is your instance name?"); let name = read_line(); - let inst = Instance::insert(&*conn, NewInstance { + let instance = Instance::insert(&*conn, NewInstance { public_domain: domain, name: name, local: true }); - create_admin(); + println!("{}\n", " ✔️ Your instance was succesfully created!".green()); + + create_admin(instance, conn); } fn complete_setup(conn: DbConn) { @@ -88,7 +98,30 @@ fn complete_setup(conn: DbConn) { quick_setup(conn); } -fn create_admin() {} +fn create_admin(instance: Instance, conn: DbConn) { + println!("{}\n\n", "You are now about to create your admin account".magenta()); + + println!("What is your username? (default: admin)"); + let name = read_line_or("admin"); + + println!("What is your email?"); + let email = read_line(); + + println!("What is your password?"); + let password = rpassword::read_password().expect("Couldn't read your password."); + + User::insert(&*conn, NewUser::new_local( + name.clone(), + name, + true, + format!("Admin of {}", instance.name), + email, + User::hash_pass(password), + instance.id + )).update_boxes(&*conn); + + println!("{}\n", " ✔️ Your account was succesfully created!".green()); +} fn check_native_deps() { let mut not_found = Vec::new(); @@ -127,6 +160,15 @@ fn read_line() -> String { input } +fn read_line_or(or: &str) -> String { + let input = read_line(); + if input.len() == 0 { + or.to_string() + } else { + input + } +} + fn write_to_dotenv(var: &'static str, val: String) { fs::write(".env", format!("{}\n{}={}", fs::read_to_string(".env").expect("Unable to read .env"), var, val)).expect("Unable to write .env"); }