The protect macro supports advanced logic using expr and ty attributes for Attribute-Based Access Control (ABAC) style checks.
expr: Allows including logic checks based on function parameters. You can combine these with authorities using all or any.ty: Allows specifying a custom type for authorities (requires the middleware to be configured for that type).
Examples
Single authority with expression check:
#[poem_grants::protect("ADMIN", expr = "*user_id == user.id", ty = "Role")]
#[poem::handler]
async fn macro_secured(user_id: web::Path<i32>, user: web::Data<User>) -> Response { ... }
Logical OR with expression check:
#[poem_grants::protect(any("ADMIN", expr = "user.is_super_user()"), ty = "Role")]
#[poem::handler]
async fn admin_or_super_user(user_id: web::Path<i32>, user: web::Data<User>) -> Response { ... }
#[poem_grants::protect("ADMIN", expr = "*user_id == user.id", ty = "Role")]
#[poem::handler]
async fn macro_secured(user_id: web::Path<i32>, user: web::Data<User>) -> Response {
Response::builder().status(StatusCode::OK).body("some secured response")
}