Refactor users management (#6)
* Use asynchronous interface to set authorized clients list
This commit is contained in:
@@ -11,6 +11,23 @@ use crate::utils::time::{fmt_time, time};
|
||||
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct UserID(pub String);
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Debug)]
|
||||
pub enum GrantedClients {
|
||||
AllClients,
|
||||
SomeClients(Vec<ClientID>),
|
||||
NoClient,
|
||||
}
|
||||
|
||||
impl GrantedClients {
|
||||
pub fn to_user(self) -> Option<Vec<ClientID>> {
|
||||
match self {
|
||||
GrantedClients::AllClients => None,
|
||||
GrantedClients::SomeClients(users) => Some(users),
|
||||
GrantedClients::NoClient => Some(vec![]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct FactorID(pub String);
|
||||
|
||||
@@ -124,10 +141,19 @@ impl User {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn granted_clients(&self) -> GrantedClients {
|
||||
match self.authorized_clients.as_deref() {
|
||||
None => GrantedClients::AllClients,
|
||||
Some(&[]) => GrantedClients::NoClient,
|
||||
Some(clients) => GrantedClients::SomeClients(clients.to_vec()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn can_access_app(&self, id: &ClientID) -> bool {
|
||||
match &self.authorized_clients {
|
||||
None => true,
|
||||
Some(c) => c.contains(id),
|
||||
match self.granted_clients() {
|
||||
GrantedClients::AllClients => true,
|
||||
GrantedClients::SomeClients(c) => c.contains(id),
|
||||
GrantedClients::NoClient => false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user