From 31f4c516e45558eb0b4c74484783f3db8d749fcd Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sun, 11 Oct 2026 00:44:56 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20(user):=20Unifica=20la=20t?= =?UTF-8?q?abla=20de=20datos=20del=20perfil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit «Mi cuenta» y la ficha de administración muestran los datos del usuario con la misma función, user_details(), en lugar de dos copias idénticas. --- extensions/pagetop-user/src/component.rs | 83 ++++++++++++++++++ .../pagetop-user/src/handlers/account.rs | 85 +----------------- .../pagetop-user/src/handlers/admin/users.rs | 86 +------------------ 3 files changed, 90 insertions(+), 164 deletions(-) diff --git a/extensions/pagetop-user/src/component.rs b/extensions/pagetop-user/src/component.rs index 7f94f830..a057bf98 100644 --- a/extensions/pagetop-user/src/component.rs +++ b/extensions/pagetop-user/src/component.rs @@ -23,6 +23,10 @@ pub use user_name::UserName; use pagetop::prelude::*; use crate::LOCALES_USER; +use crate::account::UserStatus; +use crate::component::admin::status_key; +use crate::config::{user_language_applies, user_timezone_applies}; +use crate::entity::user; // Texto libre de varias líneas como contenido seguro: cada salto de línea se convierte en `
`, // sin depender del CSS del tema. Se usa para mostrar el "Sobre mí" del usuario. @@ -80,3 +84,82 @@ pub(crate) fn error_banner(error: Option) -> Html { None => html! {}, }) } + +// Bloque de sólo lectura con los datos de perfil de un usuario, para «Mi cuenta» y para la ficha de +// administración. +pub(crate) async fn user_details(user: &user::Model, cx: &mut Context) -> Block { + let status = UserStatus::from_i16(user.status); + let mut table = Table::new() + .with_prop(PropsOp::add_classes("user-admin-table")) + .with_row( + table::Row::new() + .with_cell(Lc::t("field-username-admin", &LOCALES_USER)) + .with_cell(user.username.as_str()), + ) + .with_row( + table::Row::new() + .with_cell(Lc::t("field-email", &LOCALES_USER)) + .with_cell(user.email.as_str()), + ) + .with_row( + table::Row::new() + .with_cell(Lc::t("field-display-name", &LOCALES_USER)) + .with_cell(user.display_name.as_deref().unwrap_or("-")), + ) + .with_row( + table::Row::new() + .with_cell(Lc::t("field-about", &LOCALES_USER)) + .with_cell(multiline_text( + user.about.clone().unwrap_or_else(|| "-".into()), + )), + ); + // Sólo se muestran si se aplican, igual que en el formulario de edición. + if user_language_applies() { + table = table.with_row( + table::Row::new() + .with_cell(Lc::t("field-language", &LOCALES_USER)) + .with_cell(language_name(user.language.as_deref())), + ); + } + if user_timezone_applies() { + table = table.with_row( + table::Row::new() + .with_cell(Lc::t("field-timezone", &LOCALES_USER)) + .with_cell(user.timezone.as_deref().unwrap_or("-")), + ); + } + table = table + .with_row( + table::Row::new() + .with_cell(Lc::t("field-theme", &LOCALES_USER)) + .with_cell(theme_name(user.theme.as_deref())), + ) + .with_row( + table::Row::new() + .with_cell(Lc::t("col-status", &LOCALES_USER)) + .with_cell(Lc::t(status_key(status), &LOCALES_USER)), + ) + .with_row( + table::Row::new() + .with_cell(Lc::t("field-member-since", &LOCALES_USER)) + .with_cell(cx.format_date( + user.created_at.with_timezone(&cx.timezone()).date_naive(), + DateFormat::Long, + )), + ); + + if user.is_admin { + let badge = Badge::severe(Lc::t("badge-admin", &LOCALES_USER)) + .render(cx) + .await; + table = table.with_row( + table::Row::new() + .with_cell("") + .with_cell(Html::with(move |_| badge.clone())), + ); + } + + Block::new() + .with_title(Lc::t("title-user-details", &LOCALES_USER)) + .with_child(table) +} diff --git a/extensions/pagetop-user/src/handlers/account.rs b/extensions/pagetop-user/src/handlers/account.rs index 241db472..8886a6a9 100644 --- a/extensions/pagetop-user/src/handlers/account.rs +++ b/extensions/pagetop-user/src/handlers/account.rs @@ -7,9 +7,8 @@ use pagetop::prelude::*; use crate::account::{Account, UserStatus}; use crate::auth; -use crate::component::admin::{UserForm, UserFormMode, status_key}; -use crate::component::{ChangePasswordForm, language_name, multiline_text, theme_name}; -use crate::config::{user_language_applies, user_timezone_applies}; +use crate::component::admin::{UserForm, UserFormMode}; +use crate::component::{ChangePasswordForm, multiline_text, user_details}; use crate::entity::{role, user}; use crate::error::AuthError; use crate::handlers::admin::map_auth_error; @@ -50,12 +49,11 @@ pub(crate) async fn profile_get(request: HttpRequest) -> Response { Ok(roles) => roles, Err(_) => return ErrorPage::InternalError(Some(request)).into_response(), }; - let status = UserStatus::from_i16(user.status); let can_edit = has_permission(&request, &UserPermission::EditOwnProfile); let can_change_password = has_permission(&request, &UserPermission::ChangeOwnPassword); let mut page = Page::new(request); - let details_block = profile_details(&user, status, page.context()).await; + let details_block = user_details(&user, page.context()).await; let roles_block = profile_roles(&roles, page.context()).await; let actions = profile_actions(can_edit, can_change_password, page.context()); @@ -95,83 +93,6 @@ fn profile_actions(can_edit: bool, can_change_password: bool, cx: &Context) -> F actions } -// Bloque de sólo lectura con los datos de perfil del usuario autenticado. -async fn profile_details(user: &user::Model, status: UserStatus, cx: &mut Context) -> Block { - let mut table = Table::new() - .with_prop(PropsOp::add_classes("user-admin-table")) - .with_row( - table::Row::new() - .with_cell(Lc::t("field-username-admin", &LOCALES_USER)) - .with_cell(user.username.as_str()), - ) - .with_row( - table::Row::new() - .with_cell(Lc::t("field-email", &LOCALES_USER)) - .with_cell(user.email.as_str()), - ) - .with_row( - table::Row::new() - .with_cell(Lc::t("field-display-name", &LOCALES_USER)) - .with_cell(user.display_name.as_deref().unwrap_or("-")), - ) - .with_row( - table::Row::new() - .with_cell(Lc::t("field-about", &LOCALES_USER)) - .with_cell(multiline_text( - user.about.clone().unwrap_or_else(|| "-".into()), - )), - ); - // Sólo se muestran si se aplican, igual que en el formulario de edición. - if user_language_applies() { - table = table.with_row( - table::Row::new() - .with_cell(Lc::t("field-language", &LOCALES_USER)) - .with_cell(language_name(user.language.as_deref())), - ); - } - if user_timezone_applies() { - table = table.with_row( - table::Row::new() - .with_cell(Lc::t("field-timezone", &LOCALES_USER)) - .with_cell(user.timezone.as_deref().unwrap_or("-")), - ); - } - table = table - .with_row( - table::Row::new() - .with_cell(Lc::t("field-theme", &LOCALES_USER)) - .with_cell(theme_name(user.theme.as_deref())), - ) - .with_row( - table::Row::new() - .with_cell(Lc::t("col-status", &LOCALES_USER)) - .with_cell(Lc::t(status_key(status), &LOCALES_USER)), - ) - .with_row( - table::Row::new() - .with_cell(Lc::t("field-member-since", &LOCALES_USER)) - .with_cell(cx.format_date( - user.created_at.with_timezone(&cx.timezone()).date_naive(), - DateFormat::Long, - )), - ); - - if user.is_admin { - let badge = Badge::severe(Lc::t("badge-admin", &LOCALES_USER)) - .render(cx) - .await; - table = table.with_row( - table::Row::new() - .with_cell("") - .with_cell(Html::with(move |_| badge.clone())), - ); - } - - Block::new() - .with_title(Lc::t("title-user-details", &LOCALES_USER)) - .with_child(table) -} - // Bloque de sólo lectura con los roles del usuario autenticado. A diferencia de la vista de // administración, no enlaza cada rol a su pantalla de detalle: un usuario sin permisos de // administración no puede acceder a ella. diff --git a/extensions/pagetop-user/src/handlers/admin/users.rs b/extensions/pagetop-user/src/handlers/admin/users.rs index ce8fbeda..ddfd7767 100644 --- a/extensions/pagetop-user/src/handlers/admin/users.rs +++ b/extensions/pagetop-user/src/handlers/admin/users.rs @@ -11,10 +11,10 @@ use crate::AUTHENTICATED_ROLE_ID; use crate::LOCALES_USER; use crate::account::{Account, UserStatus}; use crate::component::admin::{ - AdminPasswordForm, USER_ADMIN_FORM_ID, UserForm, UserFormMode, UserTable, status_key, + AdminPasswordForm, USER_ADMIN_FORM_ID, UserForm, UserFormMode, UserTable, }; -use crate::component::{language_name, multiline_text, theme_name}; -use crate::config::{SETTINGS, user_language_applies, user_timezone_applies}; +use crate::component::user_details; +use crate::config::SETTINGS; use crate::entity::{role, user}; use crate::error::AuthError; use crate::handlers::admin::{back_link, frame, map_auth_error}; @@ -543,11 +543,10 @@ pub(crate) async fn view_get( Ok(roles) => roles, Err(_) => return Err(ErrorPage::InternalError(Some(request))), }; - let status = UserStatus::from_i16(user.status); let mut page = Page::admin(request); let back_href = waypoint.or(page.context().route(ADMIN_USERS_PATH)); - let details_block = user_view_details(&user, status, page.context()).await; + let details_block = user_details(&user, page.context()).await; let roles_block = user_view_roles(&roles, user.is_admin, page.context()).await; let title = Lc::t("title-admin-user-view", &LOCALES_USER); @@ -564,83 +563,6 @@ pub(crate) async fn view_get( .into_response()) } -// Bloque de sólo lectura con los datos de perfil del usuario. -async fn user_view_details(user: &user::Model, status: UserStatus, cx: &mut Context) -> Block { - let mut table = Table::new() - .with_prop(PropsOp::add_classes("user-admin-table")) - .with_row( - table::Row::new() - .with_cell(Lc::t("field-username-admin", &LOCALES_USER)) - .with_cell(user.username.as_str()), - ) - .with_row( - table::Row::new() - .with_cell(Lc::t("field-email", &LOCALES_USER)) - .with_cell(user.email.as_str()), - ) - .with_row( - table::Row::new() - .with_cell(Lc::t("field-display-name", &LOCALES_USER)) - .with_cell(user.display_name.as_deref().unwrap_or("-")), - ) - .with_row( - table::Row::new() - .with_cell(Lc::t("field-about", &LOCALES_USER)) - .with_cell(multiline_text( - user.about.clone().unwrap_or_else(|| "-".into()), - )), - ); - // Sólo se muestran si se aplican, igual que en el formulario de edición. - if user_language_applies() { - table = table.with_row( - table::Row::new() - .with_cell(Lc::t("field-language", &LOCALES_USER)) - .with_cell(language_name(user.language.as_deref())), - ); - } - if user_timezone_applies() { - table = table.with_row( - table::Row::new() - .with_cell(Lc::t("field-timezone", &LOCALES_USER)) - .with_cell(user.timezone.as_deref().unwrap_or("-")), - ); - } - table = table - .with_row( - table::Row::new() - .with_cell(Lc::t("field-theme", &LOCALES_USER)) - .with_cell(theme_name(user.theme.as_deref())), - ) - .with_row( - table::Row::new() - .with_cell(Lc::t("col-status", &LOCALES_USER)) - .with_cell(Lc::t(status_key(status), &LOCALES_USER)), - ) - .with_row( - table::Row::new() - .with_cell(Lc::t("field-member-since", &LOCALES_USER)) - .with_cell(cx.format_date( - user.created_at.with_timezone(&cx.timezone()).date_naive(), - DateFormat::Long, - )), - ); - - if user.is_admin { - let badge = Badge::severe(Lc::t("badge-admin", &LOCALES_USER)) - .render(cx) - .await; - table = table.with_row( - table::Row::new() - .with_cell("") - .with_cell(Html::with(move |_| badge.clone())), - ); - } - - Block::new() - .with_title(Lc::t("title-user-details", &LOCALES_USER)) - .with_child(table) -} - // Bloque de sólo lectura con los roles asignados al usuario, cada uno enlazado a su propia // pantalla de vista. async fn user_view_roles(roles: &[role::Model], is_admin: bool, cx: &mut Context) -> Block {