Skip to content

feat(actix-web): add contains, all, any to AuthorityGuard #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 47 additions & 12 deletions actix-web-grants/src/guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::hash::Hash;
/// .wrap(GrantsMiddleware::with_extractor(extract))
/// .service(web::resource("/admin")
/// .to(|| async { HttpResponse::Ok().finish() })
/// .guard(AuthorityGuard::new("ROLE_ADMIN".to_string())))
/// .guard(AuthorityGuard::contains("ROLE_ADMIN".to_string())))
/// });
/// }
///
Expand All @@ -29,22 +29,57 @@ use std::hash::Hash;
/// Ok(HashSet::from(["ROLE_ADMIN".to_string()]))
/// }
/// ```
pub struct AuthorityGuard<Type> {
allow_authority: Type,

pub struct AuthorityGuard<T> {
allow_authority: Type<T>,
}

pub enum Type<T> {
Single(T),
Any(Vec<T>),
All(Vec<T>),
}

impl<Type: Eq + Hash + 'static> AuthorityGuard<Type> {
pub fn new(allow_authority: Type) -> AuthorityGuard<Type> {
AuthorityGuard { allow_authority }
impl<T: Eq + Hash + 'static> AuthorityGuard<T> {
fn create(allow_authority: Type<T>) -> AuthorityGuard<T> {
AuthorityGuard {
allow_authority: allow_authority,
}
}

#[deprecated]
pub fn new(allow_authority: T) -> AuthorityGuard<T> {
Self::contains(allow_authority)
}

pub fn contains(allow_authority: T) -> AuthorityGuard<T> {
Self::create(Type::Single(allow_authority))
}

pub fn all(allow_authority: impl IntoIterator<Item = T>) -> AuthorityGuard<T> {
Self::create(Type::All(allow_authority.into_iter().collect()))
}

pub fn any(allow_authority: impl IntoIterator<Item = T>) -> AuthorityGuard<T> {
Self::create(Type::Any(allow_authority.into_iter().collect()))
}
}

impl<Type: Eq + Hash + 'static> Guard for AuthorityGuard<Type> {
impl<T: Eq + Hash + 'static> Guard for AuthorityGuard<T> {
fn check(&self, request: &GuardContext) -> bool {
request
.req_data()
.get::<AuthDetails<Type>>()
.filter(|details| details.has_authority(&self.allow_authority))
.is_some()
let req_data = request.req_data();
let details = req_data.get::<AuthDetails<T>>();
match &self.allow_authority {
Type::Single(s) => details
.filter(|details| details.has_authority(&s))
.is_some(),
Type::Any(items) => details
.filter(|details| details.has_any_authority(&items.iter().collect::<Vec<_>>()))
.is_some(),
Type::All(items) => details
.filter(|details| details.has_authorities(&items.iter().collect::<Vec<_>>()))
.is_some(),
}
}
}

Loading