From 54d949d7c5e318239248b4d01e398de47aa5b857 Mon Sep 17 00:00:00 2001 From: Jacobtread Date: Fri, 1 Sep 2023 12:49:13 +1200 Subject: [PATCH] Handle assignment of super admin at account creation and super admin password when password is not present --- src/database/entities/players.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/database/entities/players.rs b/src/database/entities/players.rs index 9941c239..bc128e35 100644 --- a/src/database/entities/players.rs +++ b/src/database/entities/players.rs @@ -1,6 +1,8 @@ //! SeaORM Entity. Generated by sea-orm-codegen 0.9.3 use crate::database::DbResult; +use crate::state::App; +use crate::utils::hashing::hash_password; use sea_orm::prelude::*; use sea_orm::{ ActiveModelTrait, ActiveValue::Set, ColumnTrait, DatabaseConnection, DeleteResult, EntityTrait, @@ -70,12 +72,36 @@ impl Model { db: &DatabaseConnection, email: String, display_name: String, - password: Option, + mut password: Option, ) -> DbFuture { + let config = App::config(); + + let mut role = PlayerRole::Default; + + if config + .dashboard + .super_email + .as_ref() + .is_some_and(|super_email| super_email == &email) + { + role = PlayerRole::SuperAdmin; + + // Don't override the password if they're created form the dashboard + if password.is_none() { + if let Some(super_password) = config.dashboard.super_password.as_ref() { + let password_hash = + hash_password(super_password).expect("Failed to hash super user password"); + + password = Some(password_hash); + } + } + } + let active_model = ActiveModel { email: Set(email), display_name: Set(display_name), password: Set(password), + role: Set(role), ..Default::default() }; active_model.insert(db)