Skip to content

Commit

Permalink
Handle assignment of super admin at account creation and super admin …
Browse files Browse the repository at this point in the history
…password when password is not present
  • Loading branch information
jacobtread committed Sep 1, 2023
1 parent 52c0bbd commit 54d949d
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/database/entities/players.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -70,12 +72,36 @@ impl Model {
db: &DatabaseConnection,
email: String,
display_name: String,
password: Option<String>,
mut password: Option<String>,
) -> DbFuture<Self> {
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)
Expand Down

0 comments on commit 54d949d

Please sign in to comment.