From 0c5fef2afe9058a352aa204c4a3d7a905312982e Mon Sep 17 00:00:00 2001 From: frederik-uni <147479464+frederik-uni@users.noreply.github.com> Date: Sun, 23 Mar 2025 14:00:45 +0100 Subject: [PATCH 1/3] allow contains, all, any --- actix-web-grants/src/guards.rs | 54 +++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/actix-web-grants/src/guards.rs b/actix-web-grants/src/guards.rs index 33705b3..2c9cd25 100644 --- a/actix-web-grants/src/guards.rs +++ b/actix-web-grants/src/guards.rs @@ -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()))) /// }); /// } /// @@ -29,22 +29,54 @@ use std::hash::Hash; /// Ok(HashSet::from(["ROLE_ADMIN".to_string()])) /// } /// ``` -pub struct AuthorityGuard { - allow_authority: Type, + +pub struct AuthorityGuard { + allow_authority: Type, +} + +pub enum Type { + Single(T), + Any(Vec), + All(Vec), } -impl AuthorityGuard { - pub fn new(allow_authority: Type) -> AuthorityGuard { +impl AuthorityGuard { + pub fn new(allow_authority: Type) -> AuthorityGuard { AuthorityGuard { allow_authority } } + + pub fn contains(&self, allow_authority: T) -> AuthorityGuard { + Self::new(Type::Single(allow_authority)) + } + + pub fn all(allow_authority: impl Into>) -> AuthorityGuard { + AuthorityGuard { + allow_authority: Type::All(allow_authority.into()), + } + } + + pub fn any(allow_authority: impl Into>) -> AuthorityGuard { + AuthorityGuard { + allow_authority: Type::Any(allow_authority.into()), + } + } } -impl Guard for AuthorityGuard { +impl Guard for AuthorityGuard { fn check(&self, request: &GuardContext) -> bool { - request - .req_data() - .get::>() - .filter(|details| details.has_authority(&self.allow_authority)) - .is_some() + let req_data = request.req_data(); + let details = req_data.get::>(); + 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::>())) + .is_some(), + Type::All(items) => details + .filter(|details| details.has_authorities(&items.iter().collect::>())) + .is_some(), + } } } + From 46ff5459f377c1381133970caf75606b4bb76b5e Mon Sep 17 00:00:00 2001 From: frederik-uni <147479464+frederik-uni@users.noreply.github.com> Date: Sun, 23 Mar 2025 17:59:40 +0100 Subject: [PATCH 2/3] requested changes --- actix-web-grants/src/guards.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/actix-web-grants/src/guards.rs b/actix-web-grants/src/guards.rs index 2c9cd25..60e630d 100644 --- a/actix-web-grants/src/guards.rs +++ b/actix-web-grants/src/guards.rs @@ -41,24 +41,27 @@ pub enum Type { } impl AuthorityGuard { - pub fn new(allow_authority: Type) -> AuthorityGuard { - AuthorityGuard { allow_authority } + pub fn create(allow_authority: Type) -> AuthorityGuard { + AuthorityGuard { + allow_authority: allow_authority, + } + } + + #[deprecated] + pub fn new(allow_authority: T) -> AuthorityGuard { + Self::contains(allow_authority) } - pub fn contains(&self, allow_authority: T) -> AuthorityGuard { - Self::new(Type::Single(allow_authority)) + pub fn contains(allow_authority: T) -> AuthorityGuard { + Self::create(Type::Single(allow_authority)) } - pub fn all(allow_authority: impl Into>) -> AuthorityGuard { - AuthorityGuard { - allow_authority: Type::All(allow_authority.into()), - } + pub fn all(allow_authority: impl IntoIterator) -> AuthorityGuard { + Self::create(Type::All(allow_authority.into_iter().collect())) } - pub fn any(allow_authority: impl Into>) -> AuthorityGuard { - AuthorityGuard { - allow_authority: Type::Any(allow_authority.into()), - } + pub fn any(allow_authority: impl IntoIterator) -> AuthorityGuard { + Self::create(Type::Any(allow_authority.into_iter().collect())) } } From 16d016fbae7e57e96bb88b601ea05b9e7dd5eb6b Mon Sep 17 00:00:00 2001 From: frederik-uni <147479464+frederik-uni@users.noreply.github.com> Date: Wed, 2 Apr 2025 13:20:57 +0200 Subject: [PATCH 3/3] Update actix-web-grants/src/guards.rs Co-authored-by: Artem Medvedev --- actix-web-grants/src/guards.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actix-web-grants/src/guards.rs b/actix-web-grants/src/guards.rs index 60e630d..a8da008 100644 --- a/actix-web-grants/src/guards.rs +++ b/actix-web-grants/src/guards.rs @@ -41,7 +41,7 @@ pub enum Type { } impl AuthorityGuard { - pub fn create(allow_authority: Type) -> AuthorityGuard { + fn create(allow_authority: Type) -> AuthorityGuard { AuthorityGuard { allow_authority: allow_authority, }