Admin creation

This commit is contained in:
Bat 2018-06-19 16:14:52 +01:00
parent 6916abca7e
commit d00688e526
4 changed files with 60 additions and 4 deletions

12
Cargo.lock generated
View File

@ -1024,6 +1024,7 @@ dependencies = [
"rocket_codegen 0.4.0-dev (git+https://github.com/SergioBenitez/Rocket?rev=df7111143e466c18d1f56377a8d9530a5a306aba)", "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_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)", "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 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_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)", "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)", "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]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.7" 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)" = "<none>" "checksum rocket_contrib 0.4.0-dev (git+https://github.com/SergioBenitez/Rocket?rev=df7111143e466c18d1f56377a8d9530a5a306aba)" = "<none>"
"checksum rocket_http 0.4.0-dev (git+https://github.com/SergioBenitez/Rocket?rev=df7111143e466c18d1f56377a8d9530a5a306aba)" = "<none>" "checksum rocket_http 0.4.0-dev (git+https://github.com/SergioBenitez/Rocket?rev=df7111143e466c18d1f56377a8d9530a5a306aba)" = "<none>"
"checksum rocket_i18n 0.1.1 (git+https://github.com/BaptisteGelez/rocket_i18n?rev=5b4225d5bed5769482dc926a7e6d6b79f1217be6)" = "<none>" "checksum rocket_i18n 0.1.1 (git+https://github.com/BaptisteGelez/rocket_i18n?rev=5b4225d5bed5769482dc926a7e6d6b79f1217be6)" = "<none>"
"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 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 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" "checksum schannel 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "85fd9df495640643ad2d00443b3d78aae69802ad488debab4f1dd52fc1806ade"

View File

@ -20,6 +20,7 @@ hyper = "*"
lazy_static = "*" lazy_static = "*"
openssl = "0.10.6" openssl = "0.10.6"
reqwest = "0.8" reqwest = "0.8"
rpassword = "2.0"
serde = "*" serde = "*"
serde_derive = "1.0" serde_derive = "1.0"
serde_json = "1.0" serde_json = "1.0"

View File

@ -27,6 +27,7 @@ extern crate reqwest;
extern crate rocket; extern crate rocket;
extern crate rocket_contrib; extern crate rocket_contrib;
extern crate rocket_i18n; extern crate rocket_i18n;
extern crate rpassword;
extern crate serde; extern crate serde;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;

View File

@ -4,10 +4,12 @@ use dotenv::dotenv;
use std::fs; use std::fs;
use std::io; use std::io;
use std::process::{exit, Command}; use std::process::{exit, Command};
use rpassword;
use DB_URL; use DB_URL;
use db_conn::DbConn; use db_conn::DbConn;
use models::instance::*; use models::instance::*;
use models::users::*;
type PgPool = Pool<ConnectionManager<PgConnection>>; type PgPool = Pool<ConnectionManager<PgConnection>>;
@ -48,6 +50,12 @@ fn run_setup(conn: Option<DbConn>) {
read_line(); read_line();
check_native_deps(); check_native_deps();
setup_type(conn.expect("Couldn't connect to the Plume database")); 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) { fn setup_type(conn: DbConn) {
@ -69,18 +77,20 @@ fn setup_type(conn: DbConn) {
fn quick_setup(conn: DbConn) { fn quick_setup(conn: DbConn) {
println!("What is your instance domain?"); println!("What is your instance domain?");
let domain = read_line(); let domain = read_line();
write_to_dotenv("BASE_URL", domain); write_to_dotenv("BASE_URL", domain.clone());
println!("\nWhat is your instance name?"); println!("\nWhat is your instance name?");
let name = read_line(); let name = read_line();
let inst = Instance::insert(&*conn, NewInstance { let instance = Instance::insert(&*conn, NewInstance {
public_domain: domain, public_domain: domain,
name: name, name: name,
local: true local: true
}); });
create_admin(); println!("{}\n", " ✔️ Your instance was succesfully created!".green());
create_admin(instance, conn);
} }
fn complete_setup(conn: DbConn) { fn complete_setup(conn: DbConn) {
@ -88,7 +98,30 @@ fn complete_setup(conn: DbConn) {
quick_setup(conn); 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() { fn check_native_deps() {
let mut not_found = Vec::new(); let mut not_found = Vec::new();
@ -127,6 +160,15 @@ fn read_line() -> String {
input 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) { 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"); fs::write(".env", format!("{}\n{}={}", fs::read_to_string(".env").expect("Unable to read .env"), var, val)).expect("Unable to write .env");
} }