Load a list of clients
This commit is contained in:
@ -2,7 +2,7 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
use crate::constants::USERS_LIST_FILE;
|
||||
use crate::constants::{CLIENTS_LIST_FILE, USERS_LIST_FILE};
|
||||
|
||||
/// Basic OIDC provider
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
@ -41,4 +41,8 @@ impl AppConfig {
|
||||
pub fn users_file(&self) -> PathBuf {
|
||||
self.storage_path().join(USERS_LIST_FILE)
|
||||
}
|
||||
|
||||
pub fn clients_file(&self) -> PathBuf {
|
||||
self.storage_path().join(CLIENTS_LIST_FILE)
|
||||
}
|
||||
}
|
||||
|
32
src/data/client.rs
Normal file
32
src/data/client.rs
Normal file
@ -0,0 +1,32 @@
|
||||
use crate::data::entity_manager::EntityManager;
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
|
||||
pub struct ClientID(pub String);
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Client {
|
||||
pub id: ClientID,
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
impl PartialEq for Client {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id.eq(&other.id)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Client {}
|
||||
|
||||
pub type ClientManager = EntityManager<Client>;
|
||||
|
||||
impl EntityManager<Client> {
|
||||
pub fn find_by_id(&self, u: &ClientID) -> Option<Client> {
|
||||
for entry in self.iter() {
|
||||
if entry.id.eq(u) {
|
||||
return Some(entry.clone());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
use std::future::Future;
|
||||
use std::ops::Deref;
|
||||
use std::pin::Pin;
|
||||
|
||||
use actix::Addr;
|
||||
@ -19,6 +20,14 @@ impl From<CurrentUser> for User {
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for CurrentUser {
|
||||
type Target = User;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRequest for CurrentUser {
|
||||
type Error = Error;
|
||||
type Future = Pin<Box<dyn Future<Output=Result<Self, Self::Error>>>>;
|
||||
|
@ -3,14 +3,16 @@ use std::slice::Iter;
|
||||
|
||||
use crate::utils::err::Res;
|
||||
|
||||
enum FileFormat { Json, Yaml }
|
||||
|
||||
pub struct EntityManager<E> {
|
||||
file_path: PathBuf,
|
||||
list: Vec<E>,
|
||||
}
|
||||
|
||||
impl<E> EntityManager<E>
|
||||
where
|
||||
E: serde::Serialize + serde::de::DeserializeOwned + Eq + Clone,
|
||||
where
|
||||
E: serde::Serialize + serde::de::DeserializeOwned + Eq + Clone,
|
||||
{
|
||||
/// Open entity
|
||||
pub fn open_or_create<A: AsRef<Path>>(path: A) -> Res<Self> {
|
||||
@ -23,12 +25,34 @@ where
|
||||
}
|
||||
|
||||
log::info!("Open existing entity file {:?}", path.as_ref());
|
||||
let file_content = std::fs::read_to_string(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())?)?,
|
||||
list: match Self::file_format(path.as_ref()) {
|
||||
FileFormat::Json => serde_json::from_str(&file_content)?,
|
||||
FileFormat::Yaml => serde_yaml::from_str(&file_content)?
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/// Save the list
|
||||
fn save(&self) -> Res {
|
||||
Ok(std::fs::write(
|
||||
&self.file_path,
|
||||
match Self::file_format(self.file_path.as_ref()) {
|
||||
FileFormat::Json => serde_json::to_string(&self.list)?,
|
||||
FileFormat::Yaml => serde_yaml::to_string(&self.list)?,
|
||||
},
|
||||
)?)
|
||||
}
|
||||
|
||||
fn file_format(p: &Path) -> FileFormat {
|
||||
match p.to_string_lossy().ends_with(".json") {
|
||||
true => FileFormat::Json,
|
||||
false => FileFormat::Yaml
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the number of entries in the list
|
||||
pub fn len(&self) -> usize {
|
||||
self.list.len()
|
||||
@ -38,14 +62,6 @@ where
|
||||
self.len() == 0
|
||||
}
|
||||
|
||||
/// 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);
|
||||
@ -54,8 +70,8 @@ where
|
||||
|
||||
/// Replace entries in the list that matches a criteria
|
||||
pub fn replace_entries<F>(&mut self, filter: F, el: &E) -> Res
|
||||
where
|
||||
F: Fn(&E) -> bool,
|
||||
where
|
||||
F: Fn(&E) -> bool,
|
||||
{
|
||||
for i in 0..self.list.len() {
|
||||
if filter(&self.list[i]) {
|
||||
@ -70,4 +86,9 @@ where
|
||||
pub fn iter(&self) -> Iter<'_, E> {
|
||||
self.list.iter()
|
||||
}
|
||||
|
||||
/// Get a cloned list of entries
|
||||
pub fn cloned(&self) -> Vec<E> {
|
||||
self.list.clone()
|
||||
}
|
||||
}
|
||||
|
@ -3,5 +3,6 @@ pub mod entity_manager;
|
||||
pub mod service;
|
||||
pub mod session_identity;
|
||||
pub mod user;
|
||||
pub mod client;
|
||||
pub mod remote_ip;
|
||||
pub mod current_user;
|
Reference in New Issue
Block a user