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

@@ -16,4 +16,5 @@ pub const SHIELD_USER: &str = include_str!("shield-user.svg");
pub const TAG: &str = include_str!("tag.svg"); pub const TAG: &str = include_str!("tag.svg");
pub const USER: &str = include_str!("user.svg"); pub const USER: &str = include_str!("user.svg");
pub const USER_KEY: &str = include_str!("user-key.svg"); pub const USER_KEY: &str = include_str!("user-key.svg");
pub const USER_PLUS: &str = include_str!("user-plus.svg");
pub const USERS: &str = include_str!("users.svg"); pub const USERS: &str = include_str!("users.svg");

View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-user-plus-icon lucide-user-plus"><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><line x1="19" x2="19" y1="8" y2="14"/><line x1="22" x2="16" y1="11" y2="11"/></svg>

After

Width:  |  Height:  |  Size: 401 B

View File

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

View File

@@ -8,6 +8,7 @@ use crate::{
users::{ users::{
User, User,
auth::{AuthError, UserAuthenticate}, auth::{AuthError, UserAuthenticate},
permissions::Permission,
}, },
web::{ web::{
components::{nav::nav, user_miniprofile::user_miniprofile}, components::{nav::nav, user_miniprofile::user_miniprofile},
@@ -16,6 +17,7 @@ use crate::{
}, },
}; };
pub mod create;
pub mod profile; pub mod profile;
pub async fn page(req: Request) -> Result<Response, AuthError> { pub async fn page(req: Request) -> Result<Response, AuthError> {
@@ -30,7 +32,7 @@ pub async fn page(req: Request) -> Result<Response, AuthError> {
html!( html!(
(nav(u.as_ref(), req.uri().path())) (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" { div class="mx-auto max-w-4xl px-2 my-4" {
p class="flex items-center gap-2" { p class="flex items-center gap-2" {
span class="text-neutral-500" {(PreEscaped(icons::USERS))} 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" { p class="text-neutral-500 text-sm font-light" {
@if let Ok(v) = &us { @if let Ok(v) = &us {
(v.len()) " users registered with Mnemosyne." (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())
}

File diff suppressed because one or more lines are too long