use crate::role::Role::{self, Admin};
use rocket::http::Status;
use rocket::Request;
use rocket_grants::authorities::{AuthDetails, AuthoritiesCheck};
use rocket_grants::GrantsFairing;
use std::collections::HashSet;
mod role;
#[rocket_grants::protect(any("Admin", "role::Role::Manager"), ty = Role)]
#[rocket::get("/macro_secured")]
async fn macro_secured() -> Status {
Status::Ok
}
#[rocket::get("/manual")]
async fn manual_secure(details: AuthDetails<Role>) -> &'static str {
if details.has_authority(&Role::Admin) {
return "Hello Admin!";
}
"Hello!"
}
#[rocket::launch]
async fn rocket() -> _ {
rocket::build()
.mount("/api", rocket::routes![macro_secured, manual_secure])
.attach(GrantsFairing::with_extractor_fn(|req| {
Box::pin(extract(req))
}))
}
async fn extract(_req: &mut Request<'_>) -> Option<HashSet<Role>> {
Some(HashSet::from([Role::Admin]))
}