Fix: Create admin promote correct account

fixes #10
This commit is contained in:
Dietrich 2021-03-09 11:50:36 +01:00
parent a0aa251673
commit 56ab3ea893
2 changed files with 13 additions and 7 deletions

View File

@ -11,6 +11,7 @@ readme = "README.md"
repository = "https://git.teilgedanken.de/dietrich/Pslink" repository = "https://git.teilgedanken.de/dietrich/Pslink"
build = "build.rs" build = "build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
@ -22,7 +23,7 @@ serde = "1.0"
diesel = { version = "1.4", features = ["sqlite", "chrono"] } diesel = { version = "1.4", features = ["sqlite", "chrono"] }
diesel_codegen = { version = "0.16.1", features = ["sqlite"] } diesel_codegen = { version = "0.16.1", features = ["sqlite"] }
diesel_migrations = "1.4" diesel_migrations = "1.4"
libsqlite3-sys = { version = "*", features = ["bundled"] } libsqlite3-sys = { version = "0.8", features = ["bundled"] }
dotenv = "0.10.1" dotenv = "0.10.1"
actix-identity = "0.3" actix-identity = "0.3"
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }

View File

@ -255,9 +255,10 @@ pub(crate) fn setup() -> Result<Option<crate::ServerConfig>, ServerError> {
} }
} }
/// Interactively create a new admin user.
fn create_admin(config: &ServerConfig) -> Result<(), ServerError> { fn create_admin(config: &ServerConfig) -> Result<(), ServerError> {
use schema::users; use schema::users;
use schema::users::dsl::{id, role}; use schema::users::dsl::{email, role, username};
slog_info!(&config.log, "Creating an admin user."); slog_info!(&config.log, "Creating an admin user.");
let sin = io::stdin(); let sin = io::stdin();
@ -272,7 +273,7 @@ fn create_admin(config: &ServerConfig) -> Result<(), ServerError> {
print!("Please enter the emailadress for {}: ", new_username); print!("Please enter the emailadress for {}: ", new_username);
io::stdout().flush().unwrap(); io::stdout().flush().unwrap();
let email = sin.lock().lines().next().unwrap().unwrap(); let new_email = sin.lock().lines().next().unwrap().unwrap();
print!("Please enter the password for {}: ", new_username); print!("Please enter the password for {}: ", new_username);
io::stdout().flush().unwrap(); io::stdout().flush().unwrap();
@ -281,17 +282,21 @@ fn create_admin(config: &ServerConfig) -> Result<(), ServerError> {
&config.log, &config.log,
"Creating {} ({}) with given password ", "Creating {} ({}) with given password ",
&new_username, &new_username,
&email &new_email
); );
let new_admin = NewUser::new(new_username, email, &password, config)?; let new_admin = NewUser::new(new_username.clone(), new_email.clone(), &password, config)?;
diesel::insert_into(users::table) diesel::insert_into(users::table)
.values(&new_admin) .values(&new_admin)
.execute(&connection)?; .execute(&connection)?;
// Add admin rights to the first user (which should be the only one) let created_user = users::table
diesel::update(users::dsl::users.filter(id.eq(&1))) .filter(username.eq(new_username))
.filter(email.eq(new_email));
// Add admin rights to the user identified by (username, email) this should be unique according to sqlite constraints
diesel::update(created_user)
.set((role.eq(2),)) .set((role.eq(2),))
.execute(&connection)?; .execute(&connection)?;
slog_info!(&config.log, "Admin user created: {}", &new_admin.username); slog_info!(&config.log, "Admin user created: {}", &new_admin.username);