feat: Add ListUsers operation
This commit is contained in:
parent
10548295cd
commit
dba07912d2
9 changed files with 166 additions and 28 deletions
|
|
@ -5,7 +5,7 @@ use http::StatusCode;
|
|||
use minijinja::context;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::db::DatabaseInterface;
|
||||
use crate::db::{DatabaseInterface, UserRef};
|
||||
use crate::http::HttpState;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
|
@ -23,14 +23,14 @@ pub enum LoginError {
|
|||
pub async fn login_page<D: DatabaseInterface>(
|
||||
State(state): State<HttpState<D>>,
|
||||
login_error: Option<LoginError>,
|
||||
) -> (StatusCode, Html<String>) {
|
||||
) -> Response {
|
||||
let page = state
|
||||
.templates
|
||||
.get_template("login.html")
|
||||
.unwrap()
|
||||
.render(context! {login_error => login_error})
|
||||
.unwrap();
|
||||
(StatusCode::OK, Html(page))
|
||||
(StatusCode::OK, Html(page)).into_response()
|
||||
}
|
||||
|
||||
pub async fn post_login<D: DatabaseInterface>(
|
||||
|
|
@ -40,9 +40,37 @@ pub async fn post_login<D: DatabaseInterface>(
|
|||
) -> Response {
|
||||
if let Some(_session) = state.sessions.get_session(&cookies) {
|
||||
// Already logged in
|
||||
(cookies, Redirect::to("/")).into_response()
|
||||
} else if form.username == "admin" && form.password == "adminadmin" {
|
||||
let cookies = state.sessions.add_session("admin", true, cookies);
|
||||
return (cookies, Redirect::to("/")).into_response();
|
||||
}
|
||||
|
||||
let req_user = match UserRef::from_user_maybe_domain(&form.username) {
|
||||
Ok(req_user) => req_user,
|
||||
Err(e) => {
|
||||
return e.to_string().into_response();
|
||||
}
|
||||
};
|
||||
|
||||
let success = match state.db.check_password(&req_user, &form.password).await {
|
||||
Ok(success) => success,
|
||||
Err(e) => {
|
||||
return format!("Database error: {e}").into_response();
|
||||
}
|
||||
};
|
||||
|
||||
if success {
|
||||
let maybe_user = match state.db.get_user(&req_user).await {
|
||||
Ok(user) => user,
|
||||
Err(e) => {
|
||||
return format!("Database error: {e}").into_response();
|
||||
}
|
||||
};
|
||||
|
||||
let Some(user) = maybe_user else {
|
||||
return "Woops, user has been deleted while you were logging in. What are the chances?!".to_string()
|
||||
.into_response();
|
||||
};
|
||||
|
||||
let cookies = state.sessions.add_session(user, cookies);
|
||||
(cookies, Redirect::to("/")).into_response()
|
||||
} else {
|
||||
login_page(State(state), Some(LoginError::InvalidCredentials))
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ use uuid::Uuid;
|
|||
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use crate::db::User;
|
||||
|
||||
pub const COOKIE_NAME: &str = "lldap_session";
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
|
@ -17,17 +19,13 @@ impl HttpSessionManager {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn add_session(&self, username: &str, is_admin: bool, cookies: CookieJar) -> CookieJar {
|
||||
pub fn add_session(&self, user: User, cookies: CookieJar) -> CookieJar {
|
||||
let uuid = Uuid::new_v4();
|
||||
|
||||
let mut cookie = Cookie::new(COOKIE_NAME, uuid.to_string());
|
||||
cookie.set_path("/");
|
||||
|
||||
let session = HttpSession {
|
||||
username: username.to_string(),
|
||||
is_admin,
|
||||
uuid,
|
||||
};
|
||||
let session = HttpSession { user, uuid };
|
||||
|
||||
{
|
||||
self.inner.write().unwrap().push(session);
|
||||
|
|
@ -67,9 +65,16 @@ impl HttpSessionManager {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct HttpSession {
|
||||
pub username: String,
|
||||
pub is_admin: bool,
|
||||
// TODO: by storing the session here, it means
|
||||
// it needs to be updated every time we change the user.
|
||||
pub user: User,
|
||||
pub uuid: Uuid,
|
||||
}
|
||||
|
||||
impl PartialEq<HttpSession> for HttpSession {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.uuid == other.uuid
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue