create user page w/ functionality

This commit is contained in:
2026-03-24 18:10:14 +01:00
parent 98b93345c2
commit e115bf4391
6 changed files with 95 additions and 2 deletions

View File

@@ -21,6 +21,8 @@ pub fn pages() -> Router {
.route("/user-settings/handle", post(usersettings::change_handle))
.route("/users", get(users::page))
.route("/users/{id}", get(users::profile::page))
.route("/users/create", get(users::create::page))
.route("/users/create-form", post(users::create::create_user))
.route("/tags", get(tags::page))
.route("/tags/create", post(tags::create))
.route("/persons", get(persons::page))

View File

@@ -8,6 +8,7 @@ use crate::{
users::{
User,
auth::{AuthError, UserAuthenticate},
permissions::Permission,
},
web::{
components::{nav::nav, user_miniprofile::user_miniprofile},
@@ -16,6 +17,7 @@ use crate::{
},
};
pub mod create;
pub mod profile;
pub async fn page(req: Request) -> Result<Response, AuthError> {
@@ -30,7 +32,7 @@ pub async fn page(req: Request) -> Result<Response, AuthError> {
html!(
(nav(u.as_ref(), req.uri().path()))
@if let Some(_) = u {
@if let Some(u) = u {
div class="mx-auto max-w-4xl px-2 my-4" {
p class="flex items-center gap-2" {
span class="text-neutral-500" {(PreEscaped(icons::USERS))}
@@ -39,6 +41,14 @@ pub async fn page(req: Request) -> Result<Response, AuthError> {
p class="text-neutral-500 text-sm font-light" {
@if let Ok(v) = &us {
(v.len()) " users registered with Mnemosyne."
} @else {
"Could not fetch user count."
}
@if let Ok(true) = u.has_permission(Permission::ManuallyCreateUsers) {
" "
a href="/users/create" class="text-blue-500 hover:text-blue-400 hover:underline" {
"Create a new user"
}
}
}
}

View File

@@ -0,0 +1,79 @@
use axum::{
Form,
extract::Request,
http::{HeaderMap, StatusCode},
response::{IntoResponse, Redirect, Response},
};
use maud::{PreEscaped, html};
use serde::Deserialize;
use crate::{
api::CompositeError,
users::{
User,
auth::{AuthError, UserAuthRequired, UserAuthenticate},
handle::UserHandle,
permissions::Permission,
},
web::{components::nav::nav, icons, pages::base},
};
pub async fn page(req: Request) -> Result<Response, AuthError> {
let u = User::authenticate(req.headers())?;
Ok(base(
"Users | Mnemosyne",
html!(
(nav(u.as_ref(), req.uri().path()))
@if let Some(u) = u {
div class="mx-auto max-w-4xl px-2 my-4" {
p class="flex items-center gap-2" {
span class="text-neutral-500" {(PreEscaped(icons::USER_PLUS))}
span class="text-2xl font-semibold font-lora" {"Create a new user"}
}
}
@if let Ok(true) = u.has_permission(Permission::ManuallyCreateUsers) {
div class="mx-auto max-w-4xl px-2 mt-4" {
form action="/users/create-form" method="post" class="flex flex-col" {
label for="handle" class="font-light text-neutral-500" {"Handle"}
div class="flex w-64 items-center border border-neutral-200/25 rounded bg-neutral-950/50" {
span class="pl-2 text-neutral-500 select-none" {"@"}
input id="handle" name="handle" type="text" autocomplete="off"
class="w-fit pl-0.5 pr-1 py-1 outline-none";
}
label for="password" class="font-light text-neutral-500 mt-4" {"Password"} br;
input id="password" name="password" type="password" autocomplete="off"
class="px-2 w-64 py-1 border border-neutral-200/25 bg-neutral-950/50 rounded";
input type="submit" value="Create"
class="px-4 mt-4 w-64 py-1 border border-neutral-200/25 bg-neutral-200/5 rounded cursor-pointer hover:border-neutral-200/40";
}
}
} @else {
p class="text-center p-2" {"You must have permission to view this page."}
}
} @else {
p class="text-center p-2" {"You must be logged in to view this page."}
}
),
)
.into_response())
}
#[derive(Deserialize)]
pub struct CreateUserWithPasswordForm {
handle: UserHandle,
password: String,
}
pub async fn create_user(
headers: HeaderMap,
Form(form): Form<CreateUserWithPasswordForm>,
) -> Result<Response, CompositeError> {
let u = User::authenticate(&headers)?.required()?;
if !u.has_permission(Permission::ManuallyCreateUsers)? {
return Ok((StatusCode::FORBIDDEN).into_response());
}
let mut nu = User::create(form.handle)?;
nu.set_password(Some(&form.password))?;
Ok(Redirect::to("/users").into_response())
}