Auth bug fixes
This commit is contained in:
@@ -116,3 +116,16 @@ impl User {
|
||||
self.permissions = UserPermissions::from(permissions);
|
||||
}
|
||||
}
|
||||
|
||||
impl NewUser {
|
||||
pub fn new_root() -> Self {
|
||||
Self {
|
||||
username: "root".to_string(),
|
||||
email: None,
|
||||
password: "rootpassword".to_string(),
|
||||
first_name: None,
|
||||
last_name: None,
|
||||
permissions: UserPermissions::root(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,3 +25,13 @@ impl Default for UserPermissions {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UserPermissions {
|
||||
pub fn root() -> Self {
|
||||
Self {
|
||||
root: true,
|
||||
manage_users: true,
|
||||
login: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,7 +146,7 @@ pub async fn exists_by_username(pool: &PgPool, username: &str) -> Result<bool> {
|
||||
SELECT EXISTS(
|
||||
SELECT 1
|
||||
FROM users
|
||||
WHERE uuid = $1
|
||||
WHERE username = $1
|
||||
)
|
||||
"#,
|
||||
)
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{Ok, Result};
|
||||
use rustymine_daemon::{config::AppCfg, router, state::AppState};
|
||||
use rustymine_daemon::{
|
||||
config::AppCfg,
|
||||
router,
|
||||
state::{AppState, check_root},
|
||||
};
|
||||
use tracing::{Level, debug, info};
|
||||
|
||||
pub const APP_NAME: &str = env!("CARGO_PKG_NAME");
|
||||
@@ -57,6 +61,7 @@ async fn main() -> Result<()> {
|
||||
};
|
||||
|
||||
let state = Arc::new(AppState::new(&config).await);
|
||||
check_root(state.clone()).await;
|
||||
|
||||
let app_result = router::init_router(state.clone()).await;
|
||||
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
use std::process::exit;
|
||||
use std::{process::exit, sync::Arc};
|
||||
|
||||
use crate::prelude::*;
|
||||
use crate::{
|
||||
core,
|
||||
domain::user::{InternalNewUser, NewUser},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
use sqlx::PgPool;
|
||||
|
||||
@@ -31,6 +35,30 @@ impl AppState {
|
||||
})
|
||||
.unwrap();
|
||||
info!("database ready after connect and migrate");
|
||||
|
||||
Self { db_pool: db_pool }
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn check_root(state: Arc<AppState>) {
|
||||
let root_exists = db::user::exists_by_username(&state.db_pool, "root")
|
||||
.await
|
||||
.map_err(|e| {
|
||||
error!(error = %e, "check root exists failed");
|
||||
exit(23);
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
if !root_exists {
|
||||
info!("No root user found in db, creating one");
|
||||
let new_root = NewUser::new_root();
|
||||
core::user_routines::create(state, new_root)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
error!(error = %e, "create root failed");
|
||||
exit(24);
|
||||
})
|
||||
.unwrap();
|
||||
info!("New default root created username: root, password: rootpassword");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user