♻️ (minimal): Reimplementa join! y join_pair!
Usa `[&str]::concat()` de la biblioteca estándar y elimina la dependencia de `concat-string`. Cada argumento se evalúa una sola vez y no se consume.
This commit is contained in:
parent
c6cd4fcd7d
commit
8651b91ab2
19 changed files with 105 additions and 108 deletions
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
use pagetop::prelude::*;
|
||||
|
||||
use crate::ADMIN_USERS_PATH;
|
||||
use crate::LOCALES_USER;
|
||||
use crate::user_path;
|
||||
|
||||
use crate::component::{PasswordConfirm, error_banner};
|
||||
|
||||
|
|
@ -22,7 +22,7 @@ impl Component for AdminPasswordForm {
|
|||
}
|
||||
|
||||
async fn prepare(&self, cx: &mut Context) -> Result<Markup, ComponentError> {
|
||||
let action = format!("{ADMIN_USERS_PATH}/{}/password", self.user_id());
|
||||
let action = user_path(self.user_id(), "password");
|
||||
let action = self.waypoint().append_to(cx.route(action));
|
||||
|
||||
let mut form = Form::new()
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ use pagetop::prelude::*;
|
|||
|
||||
use crate::ADMIN_ROLES_PATH;
|
||||
use crate::LOCALES_USER;
|
||||
use crate::role_path;
|
||||
|
||||
use crate::component::error_banner;
|
||||
|
||||
|
|
@ -35,13 +36,8 @@ impl Component for RoleForm {
|
|||
|
||||
async fn prepare(&self, cx: &mut Context) -> Result<Markup, ComponentError> {
|
||||
let action = match self.mode() {
|
||||
RoleFormMode::New => format!("{ADMIN_ROLES_PATH}/new"),
|
||||
RoleFormMode::Edit => {
|
||||
format!(
|
||||
"{ADMIN_ROLES_PATH}/{}/edit",
|
||||
self.role_id().copied().unwrap_or_default()
|
||||
)
|
||||
}
|
||||
RoleFormMode::New => util::join!(ADMIN_ROLES_PATH, "/new"),
|
||||
RoleFormMode::Edit => role_path(self.role_id().copied().unwrap_or_default(), "edit"),
|
||||
};
|
||||
let action = self.waypoint().append_to(cx.route(action));
|
||||
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@
|
|||
|
||||
use pagetop::prelude::*;
|
||||
|
||||
use crate::ADMIN_ROLES_PATH;
|
||||
use crate::LOCALES_USER;
|
||||
use crate::role_path;
|
||||
|
||||
use crate::component::admin::PermissionGroups;
|
||||
use crate::component::error_banner;
|
||||
|
|
@ -26,7 +26,7 @@ impl Component for RolePermissionsForm {
|
|||
}
|
||||
|
||||
async fn prepare(&self, cx: &mut Context) -> Result<Markup, ComponentError> {
|
||||
let action = format!("{ADMIN_ROLES_PATH}/{}/permissions", self.role_id());
|
||||
let action = role_path(self.role_id(), "permissions");
|
||||
let action = self.waypoint().append_to(cx.route(action));
|
||||
|
||||
let mut form = Form::new()
|
||||
|
|
@ -37,12 +37,12 @@ impl Component for RolePermissionsForm {
|
|||
|
||||
for (idx, (group_label, perms)) in self.groups().iter().enumerate() {
|
||||
let mut field = form::check::Field::new()
|
||||
.with_id(format!("permission-group-{idx}"))
|
||||
.with_id(util::join!("permission-group-", idx.to_string()))
|
||||
.with_name("permission_keys");
|
||||
for (key, label, checked) in perms {
|
||||
let text = label.lookup(cx).unwrap_or_default();
|
||||
field = field.with_item(
|
||||
form::check::Item::new(key, Lc::n(format!("{text} ({key})")))
|
||||
form::check::Item::new(key, Lc::n(util::join!(text, " (", key, ")")))
|
||||
.with_checked(*checked),
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use pagetop_htmx::hx_table::sort_link;
|
|||
|
||||
use crate::ADMIN_ROLES_PATH;
|
||||
use crate::LOCALES_USER;
|
||||
use crate::role_path;
|
||||
use crate::service::role_admin::{RoleListItem, RoleSortField};
|
||||
|
||||
#[derive(AutoDefault, Clone, Debug, Getters)]
|
||||
|
|
@ -108,7 +109,7 @@ impl Component for RoleTable {
|
|||
);
|
||||
}
|
||||
|
||||
let new_href = waypoint.append_to(cx.route(format!("{ADMIN_ROLES_PATH}/new")));
|
||||
let new_href = waypoint.append_to(cx.route(util::join!(ADMIN_ROLES_PATH, "/new")));
|
||||
|
||||
Ok(html! {
|
||||
div (self.props().unpack(cx)) {
|
||||
|
|
@ -212,7 +213,7 @@ fn label_cell(role: &RoleListItem, waypoint: &Waypoint) -> Html {
|
|||
let id = role.id;
|
||||
let waypoint = waypoint.clone();
|
||||
Html::with(move |cx| {
|
||||
let view_href = waypoint.append_to(cx.route(format!("{ADMIN_ROLES_PATH}/{id}/view")));
|
||||
let view_href = waypoint.append_to(cx.route(role_path(id, "view")));
|
||||
html! {
|
||||
a href=(view_href) { (label.as_str()) }
|
||||
}
|
||||
|
|
@ -234,8 +235,7 @@ async fn actions_cell(
|
|||
let locked = role.locked;
|
||||
let waypoint = waypoint.clone();
|
||||
|
||||
let permissions_href =
|
||||
waypoint.append_to(cx.route(format!("{ADMIN_ROLES_PATH}/{id}/permissions")));
|
||||
let permissions_href = waypoint.append_to(cx.route(role_path(id, "permissions")));
|
||||
|
||||
// Los botones se renderizan aquí, no dentro del `Html::with()` de abajo: necesitan pasar por
|
||||
// su propio ciclo de renderizado (`.render().await`) para que el tema activo los estilice
|
||||
|
|
@ -253,7 +253,7 @@ async fn actions_cell(
|
|||
let (edit_button, delete_button) = if locked {
|
||||
(None, None)
|
||||
} else {
|
||||
let edit_href = waypoint.append_to(cx.route(format!("{ADMIN_ROLES_PATH}/{id}/edit")));
|
||||
let edit_href = waypoint.append_to(cx.route(role_path(id, "edit")));
|
||||
let edit_button = Button::anchor(Lc::t("btn-edit", &LOCALES_USER), edit_href)
|
||||
.with_style(button::Style::Solid(Intent::Primary))
|
||||
.with_size(button::Size::Small)
|
||||
|
|
@ -263,7 +263,7 @@ async fn actions_cell(
|
|||
// Viaja como query string para que, tanto si el borrado falla como si tiene éxito, la
|
||||
// tabla vuelva a mostrarse en la misma página/orden en que estaba, en vez de reiniciarse.
|
||||
let confirm_href = cx
|
||||
.route(format!("{ADMIN_ROLES_PATH}/{id}/delete/confirm"))
|
||||
.route(role_path(id, "delete/confirm"))
|
||||
.alter_param("sort", sort.as_str())
|
||||
.alter_param("dir", dir)
|
||||
.alter_param("page", page.to_string())
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ use pagetop::prelude::*;
|
|||
|
||||
use crate::ADMIN_USERS_PATH;
|
||||
use crate::LOCALES_USER;
|
||||
use crate::user_path;
|
||||
|
||||
use crate::component::{PasswordConfirm, error_banner};
|
||||
|
||||
|
|
@ -43,13 +44,8 @@ impl Component for UserForm {
|
|||
|
||||
async fn prepare(&self, cx: &mut Context) -> Result<Markup, ComponentError> {
|
||||
let action = match self.mode() {
|
||||
UserFormMode::New => format!("{ADMIN_USERS_PATH}/new"),
|
||||
UserFormMode::Edit => {
|
||||
format!(
|
||||
"{ADMIN_USERS_PATH}/{}/edit",
|
||||
self.user_id().copied().unwrap_or_default()
|
||||
)
|
||||
}
|
||||
UserFormMode::New => util::join!(ADMIN_USERS_PATH, "/new"),
|
||||
UserFormMode::Edit => user_path(self.user_id().copied().unwrap_or_default(), "edit"),
|
||||
};
|
||||
let action = self.waypoint().append_to(cx.route(action));
|
||||
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
use pagetop::prelude::*;
|
||||
|
||||
use crate::ADMIN_USERS_PATH;
|
||||
use crate::LOCALES_USER;
|
||||
use crate::user_path;
|
||||
|
||||
use crate::component::error_banner;
|
||||
|
||||
|
|
@ -25,7 +25,7 @@ impl Component for UserRolesForm {
|
|||
}
|
||||
|
||||
async fn prepare(&self, cx: &mut Context) -> Result<Markup, ComponentError> {
|
||||
let action = format!("{ADMIN_USERS_PATH}/{}/roles", self.user_id());
|
||||
let action = user_path(self.user_id(), "roles");
|
||||
let action = self.waypoint().append_to(cx.route(action));
|
||||
|
||||
let mut form = Form::new()
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ use crate::LOCALES_USER;
|
|||
use crate::account::UserStatus;
|
||||
use crate::permission::UserPermission;
|
||||
use crate::service::user_admin::{UserListItem, UserSortField};
|
||||
use crate::user_path;
|
||||
|
||||
#[derive(AutoDefault, Clone, Debug, Getters)]
|
||||
pub(crate) struct UserTable {
|
||||
|
|
@ -84,7 +85,7 @@ impl Component for UserTable {
|
|||
);
|
||||
}
|
||||
|
||||
let new_href = waypoint.append_to(cx.route(format!("{ADMIN_USERS_PATH}/new")));
|
||||
let new_href = waypoint.append_to(cx.route(util::join!(ADMIN_USERS_PATH, "/new")));
|
||||
|
||||
Ok(html! {
|
||||
div (self.props().unpack(cx)) {
|
||||
|
|
@ -191,7 +192,7 @@ fn username_cell(user: &UserListItem, waypoint: &Waypoint) -> Html {
|
|||
let id = user.id;
|
||||
let waypoint = waypoint.clone();
|
||||
Html::with(move |cx| {
|
||||
let view_href = waypoint.append_to(cx.route(format!("{ADMIN_USERS_PATH}/{id}/view")));
|
||||
let view_href = waypoint.append_to(cx.route(user_path(id, "view")));
|
||||
html! {
|
||||
a href=(view_href) { (username.as_str()) }
|
||||
}
|
||||
|
|
@ -208,7 +209,7 @@ async fn actions_cell(
|
|||
cx: &mut Context,
|
||||
) -> Html {
|
||||
let id = user.id;
|
||||
let edit_href = waypoint.append_to(cx.route(format!("{ADMIN_USERS_PATH}/{id}/edit")));
|
||||
let edit_href = waypoint.append_to(cx.route(user_path(id, "edit")));
|
||||
|
||||
// El botón se renderiza aquí, no dentro del `Html::with()` de abajo: necesita pasar por su
|
||||
// propio ciclo de renderizado (`.render().await`) para que el tema activo lo estilice igual
|
||||
|
|
@ -220,7 +221,7 @@ async fn actions_cell(
|
|||
.await;
|
||||
|
||||
let roles_button = if can_assign_roles {
|
||||
let roles_href = waypoint.append_to(cx.route(format!("{ADMIN_USERS_PATH}/{id}/roles")));
|
||||
let roles_href = waypoint.append_to(cx.route(user_path(id, "roles")));
|
||||
Some(
|
||||
Button::anchor(Lc::t("btn-manage-roles", &LOCALES_USER), roles_href)
|
||||
.with_style(button::Style::Solid(Intent::Neutral))
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ use crate::config::SETTINGS;
|
|||
use crate::entity::role;
|
||||
use crate::handlers::admin::{back_link, frame, map_auth_error};
|
||||
use crate::permission::{self, UserPermission};
|
||||
use crate::role_path;
|
||||
use crate::service::role_admin::{self, RolePageParams, RoleSortField};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
@ -451,7 +452,7 @@ pub(crate) async fn delete_confirm_get(
|
|||
|
||||
let mut cx = Context::admin(request);
|
||||
let delete_href = cx
|
||||
.route(format!("{ADMIN_ROLES_PATH}/{id}/delete"))
|
||||
.route(role_path(id, "delete"))
|
||||
.alter_param(
|
||||
"sort",
|
||||
RoleSortField::from_query(query.sort.as_deref()).as_str(),
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ use pagetop::base::component::table::Row;
|
|||
use pagetop::prelude::*;
|
||||
use pagetop_htmx::prelude::*;
|
||||
|
||||
use crate::ADMIN_ROLES_PATH;
|
||||
use crate::ADMIN_USERS_PATH;
|
||||
use crate::ANONYMOUS_ROLE_ID;
|
||||
use crate::AUTHENTICATED_ROLE_ID;
|
||||
|
|
@ -22,8 +21,10 @@ use crate::error::AuthError;
|
|||
use crate::handlers::admin::{back_link, frame, map_auth_error};
|
||||
use crate::password;
|
||||
use crate::permission::UserPermission;
|
||||
use crate::role_path;
|
||||
use crate::service::role_admin;
|
||||
use crate::service::user_admin::{self, UserListParams, UserSortField};
|
||||
use crate::user_path;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub(crate) struct UsersQuery {
|
||||
|
|
@ -341,12 +342,10 @@ fn edit_actions(
|
|||
("true", "btn-grant-admin", "confirm-grant-admin")
|
||||
};
|
||||
|
||||
let roles_href = waypoint.append_to(cx.route(format!("{ADMIN_USERS_PATH}/{user_id}/roles")));
|
||||
let password_href =
|
||||
waypoint.append_to(cx.route(format!("{ADMIN_USERS_PATH}/{user_id}/password")));
|
||||
let status_action =
|
||||
waypoint.append_to(cx.route(format!("{ADMIN_USERS_PATH}/{user_id}/status")));
|
||||
let admin_action = waypoint.append_to(cx.route(format!("{ADMIN_USERS_PATH}/{user_id}/admin")));
|
||||
let roles_href = waypoint.append_to(cx.route(user_path(user_id, "roles")));
|
||||
let password_href = waypoint.append_to(cx.route(user_path(user_id, "password")));
|
||||
let status_action = waypoint.append_to(cx.route(user_path(user_id, "status")));
|
||||
let admin_action = waypoint.append_to(cx.route(user_path(user_id, "admin")));
|
||||
|
||||
let mut status_form = Form::new()
|
||||
.with_action(status_action.clone())
|
||||
|
|
@ -593,9 +592,10 @@ async fn user_view_roles(roles: &[role::Model], cx: &mut Context) -> Block {
|
|||
table.user-admin-table {
|
||||
tbody {
|
||||
@for (id, machine_name, label, system_badge) in &items {
|
||||
@let href = cx.route(role_path(*id, "view")).to_string();
|
||||
tr {
|
||||
td {
|
||||
a href=(cx.route(format!("{ADMIN_ROLES_PATH}/{id}/view")).to_string()) {
|
||||
a href=(href) {
|
||||
(label.as_str())
|
||||
}
|
||||
}
|
||||
|
|
@ -744,7 +744,7 @@ pub(crate) async fn status_post(
|
|||
match user_admin::set_user_status(id, new_status, account.id).await {
|
||||
Ok(()) => {
|
||||
let cx = Context::admin(request);
|
||||
let edit_href = waypoint.append_to(cx.route(format!("{ADMIN_USERS_PATH}/{id}/edit")));
|
||||
let edit_href = waypoint.append_to(cx.route(user_path(id, "edit")));
|
||||
if is_htmx {
|
||||
Ok(HtmxResponse::empty().redirect(edit_href).into_response())
|
||||
} else {
|
||||
|
|
@ -791,7 +791,7 @@ pub(crate) async fn admin_post(
|
|||
match user_admin::set_user_admin(id, new_is_admin, account.id).await {
|
||||
Ok(()) => {
|
||||
let cx = Context::admin(request);
|
||||
let edit_href = waypoint.append_to(cx.route(format!("{ADMIN_USERS_PATH}/{id}/edit")));
|
||||
let edit_href = waypoint.append_to(cx.route(user_path(id, "edit")));
|
||||
if is_htmx {
|
||||
Ok(HtmxResponse::empty().redirect(edit_href).into_response())
|
||||
} else {
|
||||
|
|
@ -816,10 +816,7 @@ pub(crate) async fn password_get(
|
|||
return Err(ErrorPage::NotFound(Some(request)));
|
||||
}
|
||||
let mut page = Page::admin(request);
|
||||
let edit_href = waypoint.append_to(
|
||||
page.context()
|
||||
.route(format!("{ADMIN_USERS_PATH}/{id}/edit")),
|
||||
);
|
||||
let edit_href = waypoint.append_to(page.context().route(user_path(id, "edit")));
|
||||
let title = Lc::t("title-admin-user-password", &LOCALES_USER);
|
||||
Ok(page
|
||||
.with_title(title.clone())
|
||||
|
|
@ -861,15 +858,12 @@ pub(crate) async fn password_post(
|
|||
match result {
|
||||
Ok(()) => {
|
||||
let cx = Context::admin(request);
|
||||
let edit_href = waypoint.append_to(cx.route(format!("{ADMIN_USERS_PATH}/{id}/edit")));
|
||||
let edit_href = waypoint.append_to(cx.route(user_path(id, "edit")));
|
||||
Ok(Redirect::see_other(edit_href).into_response())
|
||||
}
|
||||
Err(err) => {
|
||||
let mut page = Page::admin(request);
|
||||
let edit_href = waypoint.append_to(
|
||||
page.context()
|
||||
.route(format!("{ADMIN_USERS_PATH}/{id}/edit")),
|
||||
);
|
||||
let edit_href = waypoint.append_to(page.context().route(user_path(id, "edit")));
|
||||
let title = Lc::t("title-admin-user-password", &LOCALES_USER);
|
||||
Ok(page
|
||||
.with_title(title.clone())
|
||||
|
|
|
|||
|
|
@ -131,6 +131,16 @@ const ADMIN_ROLES_PATH: &str = "/admin/user/roles";
|
|||
// Catálogo de permisos registrados, agrupado por extensión (solo lectura).
|
||||
const ADMIN_PERMISSIONS_PATH: &str = "/admin/user/permissions";
|
||||
|
||||
// Ruta de una acción sobre el usuario `id`: `{ADMIN_USERS_PATH}/{id}/{tail}`.
|
||||
fn user_path(id: i32, tail: &str) -> String {
|
||||
util::join!(ADMIN_USERS_PATH, "/", id.to_string(), "/", tail)
|
||||
}
|
||||
|
||||
// Ruta de una acción sobre el rol `id`: `{ADMIN_ROLES_PATH}/{id}/{tail}`.
|
||||
fn role_path(id: i32, tail: &str) -> String {
|
||||
util::join!(ADMIN_ROLES_PATH, "/", id.to_string(), "/", tail)
|
||||
}
|
||||
|
||||
// **< Registro en pagetop-admin (fijo) >***********************************************************
|
||||
|
||||
// Registra las páginas de usuarios, roles y permisos en el portal de `pagetop-admin`, bajo la
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
//! Gestión de sesiones de usuario (creación, carga, destrucción).
|
||||
|
||||
use pagetop::auth::CurrentUser;
|
||||
use pagetop::datetime::{Duration, Utc};
|
||||
use pagetop::web::http::{HeaderMap, header};
|
||||
use pagetop::prelude::*;
|
||||
use pagetop_seaorm::db::{
|
||||
ActiveModelTrait, ColumnTrait, DbErr, EntityTrait, QueryFilter, Set, dbconn,
|
||||
};
|
||||
|
|
@ -35,32 +33,33 @@ fn bytes_to_hex(bytes: &[u8]) -> String {
|
|||
|
||||
/// Construye el valor de la cabecera `Set-Cookie` para la cookie de sesión.
|
||||
pub fn build_cookie(sid: &str, remember: bool) -> String {
|
||||
let mut parts = vec![
|
||||
format!("{}={}", SETTINGS.session_cookie_name, sid),
|
||||
"HttpOnly".into(),
|
||||
"SameSite=Lax".into(),
|
||||
"Path=/".into(),
|
||||
];
|
||||
let mut cookie = util::join!(
|
||||
&SETTINGS.session_cookie_name,
|
||||
"=",
|
||||
sid,
|
||||
"; HttpOnly; SameSite=Lax; Path=/"
|
||||
);
|
||||
if SETTINGS.secure_cookie {
|
||||
parts.push("Secure".into());
|
||||
cookie.push_str("; Secure");
|
||||
}
|
||||
if remember {
|
||||
parts.push(format!("Max-Age={}", SETTINGS.session_ttl_secs));
|
||||
cookie.push_str("; Max-Age=");
|
||||
cookie.push_str(&SETTINGS.session_ttl_secs.to_string());
|
||||
}
|
||||
parts.join("; ")
|
||||
cookie
|
||||
}
|
||||
|
||||
/// Construye la cookie de expiración (Max-Age=0) para borrar la sesión del navegador.
|
||||
pub fn expiry_cookie() -> String {
|
||||
format!(
|
||||
"{}=; HttpOnly; SameSite=Lax; Path=/; Max-Age=0",
|
||||
SETTINGS.session_cookie_name
|
||||
util::join!(
|
||||
&SETTINGS.session_cookie_name,
|
||||
"=; HttpOnly; SameSite=Lax; Path=/; Max-Age=0"
|
||||
)
|
||||
}
|
||||
|
||||
/// Extrae el session ID de las cabeceras HTTP de la petición, si existe.
|
||||
pub fn extract_sid(headers: Option<&HeaderMap>) -> Option<String> {
|
||||
let cookie_str = headers?.get(header::COOKIE)?.to_str().ok()?;
|
||||
pub fn extract_sid(headers: Option<&web::http::HeaderMap>) -> Option<String> {
|
||||
let cookie_str = headers?.get(web::http::header::COOKIE)?.to_str().ok()?;
|
||||
|
||||
let name = SETTINGS.session_cookie_name.as_str();
|
||||
for part in cookie_str.split(';') {
|
||||
|
|
@ -78,7 +77,7 @@ pub fn extract_sid(headers: Option<&HeaderMap>) -> Option<String> {
|
|||
///
|
||||
/// Si no hay cookie o la sesión ha expirado, devuelve `(CurrentUser::Anonymous, None)`.
|
||||
/// Se llama desde el middleware de sesión, que es async.
|
||||
pub async fn resolve_session(headers: &HeaderMap) -> (CurrentUser, Option<Account>) {
|
||||
pub async fn resolve_session(headers: &web::http::HeaderMap) -> (CurrentUser, Option<Account>) {
|
||||
let Some(sid) = extract_sid(Some(headers)) else {
|
||||
return (CurrentUser::Anonymous, None);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue