♻️ (user): Unifica la tabla de datos del perfil
«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.
This commit is contained in:
parent
e238db1a1b
commit
31f4c516e4
3 changed files with 90 additions and 164 deletions
|
|
@ -23,6 +23,10 @@ pub use user_name::UserName;
|
||||||
use pagetop::prelude::*;
|
use pagetop::prelude::*;
|
||||||
|
|
||||||
use crate::LOCALES_USER;
|
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 `<br>`,
|
// Texto libre de varias líneas como contenido seguro: cada salto de línea se convierte en `<br>`,
|
||||||
// sin depender del CSS del tema. Se usa para mostrar el "Sobre mí" del usuario.
|
// 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<Lc>) -> Html {
|
||||||
None => 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)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,8 @@ use pagetop::prelude::*;
|
||||||
|
|
||||||
use crate::account::{Account, UserStatus};
|
use crate::account::{Account, UserStatus};
|
||||||
use crate::auth;
|
use crate::auth;
|
||||||
use crate::component::admin::{UserForm, UserFormMode, status_key};
|
use crate::component::admin::{UserForm, UserFormMode};
|
||||||
use crate::component::{ChangePasswordForm, language_name, multiline_text, theme_name};
|
use crate::component::{ChangePasswordForm, multiline_text, user_details};
|
||||||
use crate::config::{user_language_applies, user_timezone_applies};
|
|
||||||
use crate::entity::{role, user};
|
use crate::entity::{role, user};
|
||||||
use crate::error::AuthError;
|
use crate::error::AuthError;
|
||||||
use crate::handlers::admin::map_auth_error;
|
use crate::handlers::admin::map_auth_error;
|
||||||
|
|
@ -50,12 +49,11 @@ pub(crate) async fn profile_get(request: HttpRequest) -> Response {
|
||||||
Ok(roles) => roles,
|
Ok(roles) => roles,
|
||||||
Err(_) => return ErrorPage::InternalError(Some(request)).into_response(),
|
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_edit = has_permission(&request, &UserPermission::EditOwnProfile);
|
||||||
let can_change_password = has_permission(&request, &UserPermission::ChangeOwnPassword);
|
let can_change_password = has_permission(&request, &UserPermission::ChangeOwnPassword);
|
||||||
|
|
||||||
let mut page = Page::new(request);
|
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 roles_block = profile_roles(&roles, page.context()).await;
|
||||||
let actions = profile_actions(can_edit, can_change_password, page.context());
|
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
|
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
|
// 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 enlaza cada rol a su pantalla de detalle: un usuario sin permisos de
|
||||||
// administración no puede acceder a ella.
|
// administración no puede acceder a ella.
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,10 @@ use crate::AUTHENTICATED_ROLE_ID;
|
||||||
use crate::LOCALES_USER;
|
use crate::LOCALES_USER;
|
||||||
use crate::account::{Account, UserStatus};
|
use crate::account::{Account, UserStatus};
|
||||||
use crate::component::admin::{
|
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::component::user_details;
|
||||||
use crate::config::{SETTINGS, user_language_applies, user_timezone_applies};
|
use crate::config::SETTINGS;
|
||||||
use crate::entity::{role, user};
|
use crate::entity::{role, user};
|
||||||
use crate::error::AuthError;
|
use crate::error::AuthError;
|
||||||
use crate::handlers::admin::{back_link, frame, map_auth_error};
|
use crate::handlers::admin::{back_link, frame, map_auth_error};
|
||||||
|
|
@ -543,11 +543,10 @@ pub(crate) async fn view_get(
|
||||||
Ok(roles) => roles,
|
Ok(roles) => roles,
|
||||||
Err(_) => return Err(ErrorPage::InternalError(Some(request))),
|
Err(_) => return Err(ErrorPage::InternalError(Some(request))),
|
||||||
};
|
};
|
||||||
let status = UserStatus::from_i16(user.status);
|
|
||||||
|
|
||||||
let mut page = Page::admin(request);
|
let mut page = Page::admin(request);
|
||||||
let back_href = waypoint.or(page.context().route(ADMIN_USERS_PATH));
|
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 roles_block = user_view_roles(&roles, user.is_admin, page.context()).await;
|
||||||
|
|
||||||
let title = Lc::t("title-admin-user-view", &LOCALES_USER);
|
let title = Lc::t("title-admin-user-view", &LOCALES_USER);
|
||||||
|
|
@ -564,83 +563,6 @@ pub(crate) async fn view_get(
|
||||||
.into_response())
|
.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
|
// Bloque de sólo lectura con los roles asignados al usuario, cada uno enlazado a su propia
|
||||||
// pantalla de vista.
|
// pantalla de vista.
|
||||||
async fn user_view_roles(roles: &[role::Model], is_admin: bool, cx: &mut Context) -> Block {
|
async fn user_view_roles(roles: &[role::Model], is_admin: bool, cx: &mut Context) -> Block {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue