feat: List domains user can see on homepage
This commit is contained in:
parent
58a8c375f0
commit
8a7e644960
11 changed files with 136 additions and 58 deletions
|
|
@ -3,7 +3,7 @@ use tokio::sync::RwLock;
|
|||
use std::sync::Arc;
|
||||
|
||||
use crate::db::error::BoxedError;
|
||||
use crate::db::{DatabaseInterface, UserRef};
|
||||
use crate::db::{DatabaseInterface, Domain, User, UserRef};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Database<D: DatabaseInterface> {
|
||||
|
|
@ -36,4 +36,13 @@ impl<D: DatabaseInterface> Database<D> {
|
|||
tracing::debug!("Comparing {} and {}", user.password, password);
|
||||
Ok(user.password == password)
|
||||
}
|
||||
|
||||
pub async fn domains_user_can_see(&self, user: &User) -> Result<Vec<Domain>, BoxedError> {
|
||||
Ok(self
|
||||
.list_all_domains()
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter(|d| user.role.can_see_domain(&d.name))
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
#[derive(Clone, Debug, Default)]
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Clone, Debug, Default, Serialize)]
|
||||
pub struct Domain {
|
||||
pub name: String,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,10 @@ impl<D: DatabaseInterface> DatabaseInterface for Database<D> {
|
|||
.await
|
||||
}
|
||||
|
||||
async fn list_all_domains(&self) -> Result<Vec<Domain>, BoxedError> {
|
||||
self.inner.read().await.list_all_domains().await
|
||||
}
|
||||
|
||||
async fn list_all_users(&self) -> Result<Vec<User>, BoxedError> {
|
||||
self.inner.read().await.list_all_users().await
|
||||
}
|
||||
|
|
@ -67,8 +71,9 @@ pub trait DatabaseInterface: Clone + Send + Sync + 'static {
|
|||
current_user: &User,
|
||||
) -> Result<Result<(), UserCreationError>, BoxedError>;
|
||||
|
||||
fn list_all_domains(&self) -> impl Future<Output = Result<Vec<Domain>, BoxedError>> + Send;
|
||||
#[expect(unused)]
|
||||
fn list_all_users(&self) -> impl Future<Output = Result<Vec<User>, BoxedError>>;
|
||||
fn list_all_users(&self) -> impl Future<Output = Result<Vec<User>, BoxedError>> + Send;
|
||||
|
||||
/// List users on a specific domain.
|
||||
///
|
||||
|
|
|
|||
|
|
@ -104,6 +104,10 @@ impl DatabaseInterface for MemoryDatabase {
|
|||
self.create_user(new_user).await
|
||||
}
|
||||
|
||||
fn list_all_domains(&self) -> impl Future<Output = Result<Vec<Domain>, BoxedError>> {
|
||||
ready(Ok(self.domains.clone()))
|
||||
}
|
||||
|
||||
fn list_all_users(&self) -> impl Future<Output = Result<Vec<User>, BoxedError>> {
|
||||
ready(Ok(self.users.clone()))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,12 @@ use serde::Serialize;
|
|||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Operation {
|
||||
CreateDomain,
|
||||
CreateUser(String),
|
||||
ListUsers(Option<String>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
#[derive(Clone, Debug, PartialEq, Serialize)]
|
||||
pub enum Role {
|
||||
/// Can do anything
|
||||
Admin,
|
||||
|
|
@ -28,6 +29,7 @@ pub enum Role {
|
|||
impl Role {
|
||||
pub fn can_perform(&self, operation: &Operation) -> bool {
|
||||
match operation {
|
||||
Operation::CreateDomain => self == &Self::Admin,
|
||||
Operation::CreateUser(op_domain) => match self {
|
||||
Self::Admin => true,
|
||||
Self::DomainAdmin(usr_domain) | Self::DomainModerator(usr_domain) => {
|
||||
|
|
@ -44,4 +46,12 @@ impl Role {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn can_see_domain(&self, domain: &str) -> bool {
|
||||
match self {
|
||||
Self::Admin | Self::ReadonlyAdmin => true,
|
||||
Self::DomainAdmin(d) | Self::DomainModerator(d) => domain == d,
|
||||
Self::User => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,6 +80,10 @@ impl User {
|
|||
pub fn can_perform(&self, operation: &Operation) -> bool {
|
||||
self.role.can_perform(operation)
|
||||
}
|
||||
|
||||
pub fn can_create_domain(&self) -> bool {
|
||||
self.role.can_perform(&Operation::CreateDomain)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for User {
|
||||
|
|
|
|||
63
src/http/home.rs
Normal file
63
src/http/home.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
use axum::extract::State;
|
||||
use axum::response::{Html, IntoResponse, Response};
|
||||
use axum_extra::extract::cookie::CookieJar;
|
||||
use http::StatusCode;
|
||||
use minijinja::context;
|
||||
|
||||
use crate::db::{DatabaseInterface, Operation};
|
||||
use crate::http::HttpState;
|
||||
use crate::http::login::login_page;
|
||||
|
||||
pub async fn home<D: DatabaseInterface>(
|
||||
State(state): State<HttpState<D>>,
|
||||
cookies: CookieJar,
|
||||
) -> Response {
|
||||
if let Some(session) = state.sessions.get_session(&cookies) {
|
||||
// When the user has no domain (service admin) list all domains
|
||||
let op = Operation::ListUsers(session.user.domain.clone());
|
||||
let other_users = if session.user.can_perform(&op) {
|
||||
match state
|
||||
.db
|
||||
.list_domain_users(session.user.domain.clone())
|
||||
.await
|
||||
{
|
||||
Ok(other_users) => other_users,
|
||||
Err(e) => {
|
||||
return format!("Database error: {e}").into_response();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
|
||||
tracing::info!(
|
||||
"Found {} users on domain {:?}",
|
||||
other_users.len(),
|
||||
session.user.domain
|
||||
);
|
||||
|
||||
let domains = match state.db.domains_user_can_see(&session.user).await {
|
||||
Ok(domains) => domains,
|
||||
Err(e) => {
|
||||
return format!("Database error: {e}").into_response();
|
||||
}
|
||||
};
|
||||
|
||||
let ctx = context! {
|
||||
domains,
|
||||
user => session.user,
|
||||
can_create_domain => session.user.can_create_domain(),
|
||||
other_users,
|
||||
};
|
||||
|
||||
let page = state
|
||||
.templates
|
||||
.get_template("home.html")
|
||||
.unwrap()
|
||||
.render(ctx)
|
||||
.unwrap();
|
||||
(StatusCode::OK, Html(page)).into_response()
|
||||
} else {
|
||||
login_page(State(state), None).await
|
||||
}
|
||||
}
|
||||
|
|
@ -1,22 +1,19 @@
|
|||
use axum::Router;
|
||||
use axum::extract::State;
|
||||
use axum::response::{Html, IntoResponse, Response};
|
||||
use axum::routing::{get, post};
|
||||
use axum::serve::Listener as AxumListener;
|
||||
use axum_extra::extract::cookie::CookieJar;
|
||||
use http::StatusCode;
|
||||
use minijinja::Environment;
|
||||
#[cfg(not(feature = "embed"))]
|
||||
use minijinja::path_loader;
|
||||
use minijinja::{Environment, context};
|
||||
#[cfg(feature = "embed")]
|
||||
use static_serve::embed_assets;
|
||||
#[cfg(not(feature = "embed"))]
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
use crate::db::{Database, DatabaseInterface, Operation};
|
||||
use crate::db::{Database, DatabaseInterface};
|
||||
use crate::listener::{Listener, ListenerKind};
|
||||
use crate::stream::{AbstractSocketAddr, AbstractStreamKind};
|
||||
|
||||
mod home;
|
||||
mod login;
|
||||
mod logout;
|
||||
mod session;
|
||||
|
|
@ -98,56 +95,11 @@ pub async fn http_listen<D: DatabaseInterface>(listener: Listener, db: Database<
|
|||
#[cfg(not(feature = "embed"))]
|
||||
let app = { Router::new().nest_service("/assets", ServeDir::new("assets")) };
|
||||
let app = app
|
||||
.route("/", get(home))
|
||||
.route("/login", get(home))
|
||||
.route("/", get(home::home))
|
||||
.route("/login", get(home::home))
|
||||
.route("/login", post(login::post_login))
|
||||
.route("/logout", get(logout::logout))
|
||||
.with_state(HttpState::new(db));
|
||||
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
pub async fn home<D: DatabaseInterface>(
|
||||
State(state): State<HttpState<D>>,
|
||||
cookies: CookieJar,
|
||||
) -> Response {
|
||||
if let Some(session) = state.sessions.get_session(&cookies) {
|
||||
// When the user has no domain (service admin) list all domains
|
||||
let op = Operation::ListUsers(session.user.domain.clone());
|
||||
let other_users = if session.user.can_perform(&op) {
|
||||
match state
|
||||
.db
|
||||
.list_domain_users(session.user.domain.clone())
|
||||
.await
|
||||
{
|
||||
Ok(other_users) => other_users,
|
||||
Err(e) => {
|
||||
return format!("Database error: {e}").into_response();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
|
||||
tracing::info!(
|
||||
"Found {} users on domain {:?}",
|
||||
other_users.len(),
|
||||
session.user.domain
|
||||
);
|
||||
|
||||
let ctx = context! {
|
||||
user => session.user,
|
||||
other_users,
|
||||
};
|
||||
|
||||
let page = state
|
||||
.templates
|
||||
.get_template("home.html")
|
||||
.unwrap()
|
||||
.render(ctx)
|
||||
.unwrap();
|
||||
(StatusCode::OK, Html(page)).into_response()
|
||||
} else {
|
||||
login::login_page(State(state), None).await
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue