Automatically create admin on first start
This commit is contained in:
19
src/data/app_config.rs
Normal file
19
src/data/app_config.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
use rocket::serde::Deserialize;
|
||||
use crate::constants::USERS_LIST_FILE;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
pub struct AppConfig {
|
||||
storage_path: PathBuf,
|
||||
}
|
||||
|
||||
impl AppConfig {
|
||||
pub fn storage_path(&self) -> &Path {
|
||||
self.storage_path.as_ref()
|
||||
}
|
||||
|
||||
pub fn users_file(&self) -> PathBuf {
|
||||
self.storage_path.join(USERS_LIST_FILE)
|
||||
}
|
||||
}
|
43
src/data/entity_manager.rs
Normal file
43
src/data/entity_manager.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::utils::err::Res;
|
||||
|
||||
pub struct EntityManager<E> {
|
||||
file_path: PathBuf,
|
||||
list: Vec<E>,
|
||||
}
|
||||
|
||||
impl<E> EntityManager<E> where E: rocket::serde::Serialize + rocket::serde::DeserializeOwned + Eq + Clone {
|
||||
/// Open entity
|
||||
pub fn open_or_create<A: AsRef<Path>>(path: A) -> Res<Self> {
|
||||
if !path.as_ref().is_file() {
|
||||
log::warn!("Entities at {:?} does not point to a file, creating a new empty entity container...", path.as_ref());
|
||||
return Ok(Self {
|
||||
file_path: path.as_ref().to_path_buf(),
|
||||
list: vec![],
|
||||
});
|
||||
}
|
||||
|
||||
log::info!("Open existing entity file {:?}", path.as_ref());
|
||||
Ok(Self {
|
||||
file_path: path.as_ref().to_path_buf(),
|
||||
list: serde_json::from_str(&std::fs::read_to_string(path.as_ref())?)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the number of entries in the list
|
||||
pub fn len(&self) -> usize {
|
||||
self.list.len()
|
||||
}
|
||||
|
||||
/// Save the list
|
||||
fn save(&self) -> Res {
|
||||
Ok(std::fs::write(&self.file_path, serde_json::to_string(&self.list)?)?)
|
||||
}
|
||||
|
||||
/// Insert a new element in the list
|
||||
pub fn insert(&mut self, el: E) -> Res {
|
||||
self.list.push(el);
|
||||
self.save()
|
||||
}
|
||||
}
|
4
src/data/mod.rs
Normal file
4
src/data/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub mod app_config;
|
||||
pub mod user;
|
||||
pub mod service;
|
||||
pub mod entity_manager;
|
2
src/data/service.rs
Normal file
2
src/data/service.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
|
||||
pub struct ServiceID(String);
|
48
src/data/user.rs
Normal file
48
src/data/user.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use crate::data::service::ServiceID;
|
||||
use crate::utils::err::Res;
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct User {
|
||||
pub uid: String,
|
||||
pub first_name: String,
|
||||
pub last_last: String,
|
||||
pub username: String,
|
||||
pub email: String,
|
||||
pub password: String,
|
||||
pub need_reset_password: bool,
|
||||
pub enabled: bool,
|
||||
pub admin: bool,
|
||||
|
||||
/// None = all services
|
||||
/// Some([]) = no service
|
||||
pub authorized_services: Option<Vec<ServiceID>>,
|
||||
}
|
||||
|
||||
impl PartialEq for User {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.uid.eq(&other.uid)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for User {}
|
||||
|
||||
impl Default for User {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
uid: uuid::Uuid::new_v4().to_string(),
|
||||
first_name: "".to_string(),
|
||||
last_last: "".to_string(),
|
||||
username: "".to_string(),
|
||||
email: "".to_string(),
|
||||
password: "".to_string(),
|
||||
need_reset_password: false,
|
||||
enabled: true,
|
||||
admin: false,
|
||||
authorized_services: None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hash_password<P: AsRef<[u8]>>(pwd: P) -> Res<String> {
|
||||
Ok(bcrypt::hash(pwd, bcrypt::DEFAULT_COST)?)
|
||||
}
|
Reference in New Issue
Block a user