feat: Add ListUsers operation
This commit is contained in:
parent
10548295cd
commit
dba07912d2
9 changed files with 166 additions and 28 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use axum::Router;
|
||||
use axum::extract::State;
|
||||
use axum::response::Html;
|
||||
use axum::response::{Html, IntoResponse, Response};
|
||||
use axum::routing::{get, post};
|
||||
use axum::serve::Listener as AxumListener;
|
||||
use axum_extra::extract::cookie::CookieJar;
|
||||
|
|
@ -8,7 +8,7 @@ use http::StatusCode;
|
|||
use minijinja::{Environment, context, path_loader};
|
||||
use static_serve::embed_assets;
|
||||
|
||||
use crate::db::{Database, DatabaseInterface};
|
||||
use crate::db::{Database, DatabaseInterface, Operation};
|
||||
use crate::listener::{Listener, ListenerKind};
|
||||
use crate::stream::{AbstractSocketAddr, AbstractStreamKind};
|
||||
|
||||
|
|
@ -92,15 +92,39 @@ pub async fn http_listen<D: DatabaseInterface>(listener: Listener, db: Database<
|
|||
pub async fn home<D: DatabaseInterface>(
|
||||
State(state): State<HttpState<D>>,
|
||||
cookies: CookieJar,
|
||||
) -> (StatusCode, Html<String>) {
|
||||
) -> 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_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(context! {username => session.username})
|
||||
.render(ctx)
|
||||
.unwrap();
|
||||
(StatusCode::OK, Html(page))
|
||||
(StatusCode::OK, Html(page)).into_response()
|
||||
} else {
|
||||
login::login_page(State(state), None).await
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue