(auth): Añade permisos tipados

Sustituye las claves de permiso `&str` sueltas por el trait `Permission`
(clave, etiqueta y grupo), y añade `require_permission()` para cortar un
handler con `ErrorPage::AccessDenied` antes de construir nada.

Para que `require_permission()` pueda fallar sin un `Context` previo,
`ErrorPage` pasa a envolver `Option<HttpRequest>`, `Context::new()`
recibe ahora `HttpRequest` sin `Option`, y se añade `Context::admin()`
para la plantilla de administración.
This commit is contained in:
Manuel Cillero 2026-07-27 22:46:41 +02:00
parent ea0dc021ce
commit 747e64ce34
10 changed files with 297 additions and 120 deletions

View file

@ -39,7 +39,7 @@ async fn current_user_propagates_from_request_extensions() {
display_name: "Bob".to_owned(),
})
.to_http_request();
let cx = Context::new(Some(req));
let cx = Context::new(req);
let user = cx.current_user();
assert!(user.is_authenticated());
assert_eq!(user.id(), Some(7));

View file

@ -50,7 +50,7 @@ async fn component_html_can_access_request_path() {
let req = web::test::TestRequest::get()
.uri("/hello/world")
.to_http_request();
let mut cx = Context::new(Some(req));
let mut cx = Context::new(req);
let mut component = Html::with(|cx| {
let path = cx

View file

@ -46,10 +46,10 @@ impl Theme for MarkerTheme {
#[pagetop::test]
async fn default_template_identity_is_independent_of_theme() {
let cx = Context::new(None);
let cx = Context::default();
assert_eq!(cx.template().name(), "standard");
let cx = Context::new(None).with_theme(&MarkerTheme);
let cx = Context::default().with_theme(&MarkerTheme);
assert_eq!(cx.template().name(), "standard");
}
@ -67,7 +67,7 @@ async fn admin_template_identity_is_independent_of_theme() {
async fn explicit_template_is_not_overridden_by_a_later_with_theme() {
// A template explicitly set with `with_template()` prevails even if `with_theme()` is called
// afterwards.
let cx = Context::new(None)
let cx = Context::default()
.with_template(&CoreTemplate::Admin)
.with_theme(&pagetop::base::theme::Basic);

View file

@ -3,7 +3,7 @@ use pagetop::prelude::*;
// Forces an effective language different from the default negotiated one (en-US, with no `?lang` in
// the request), so that `Context::route()` decides to propagate `?lang=...` in local routes.
fn cx_with_lang(lang: &str) -> Context {
Context::new(None).with_langid(&Locale::resolve(lang))
Context::default().with_langid(&Locale::resolve(lang))
}
// **< Route - automatic detection of external URLs >***********************************************
@ -107,7 +107,7 @@ async fn route_with_captures_dynamic_values_from_the_environment() {
#[pagetop::test]
async fn route_default_resolves_to_an_empty_path() {
let cx = Context::new(None);
let cx = Context::default();
assert_eq!(Route::default().resolve(&cx).to_string(), "");
}