Struct actix_web_grants::AuthorityGuard
source · pub struct AuthorityGuard<Type> { /* private fields */ }
Expand description
Implementation of Guard trait for validate authorities
use actix_web::dev::ServiceRequest;
use actix_web::{web, App, Error, HttpResponse, HttpServer};
use actix_web_grants::{GrantsMiddleware, AuthorityGuard};
use std::sync::Arc;
use std::collections::HashSet;
fn main() {
HttpServer::new(|| {
App::new()
.wrap(GrantsMiddleware::with_extractor(extract))
.service(web::resource("/admin")
.to(|| async { HttpResponse::Ok().finish() })
.guard(AuthorityGuard::new("ROLE_ADMIN".to_string())))
});
}
async fn extract(_req: &ServiceRequest) -> Result<HashSet<String>, Error> {
// Here is a place for your code to get user permissions/roles/authorities from a request
// For example from a token or database
// Stub example
Ok(HashSet::from(["ROLE_ADMIN".to_string()]))
}
Implementations§
source§impl<Type: Eq + Hash + 'static> AuthorityGuard<Type>
impl<Type: Eq + Hash + 'static> AuthorityGuard<Type>
pub fn new(allow_authority: Type) -> AuthorityGuard<Type>
Trait Implementations§
Auto Trait Implementations§
impl<Type> RefUnwindSafe for AuthorityGuard<Type>where Type: RefUnwindSafe,
impl<Type> Send for AuthorityGuard<Type>where Type: Send,
impl<Type> Sync for AuthorityGuard<Type>where Type: Sync,
impl<Type> Unpin for AuthorityGuard<Type>where Type: Unpin,
impl<Type> UnwindSafe for AuthorityGuard<Type>where Type: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more