From a1fe703bf024e58bc437aa453f8a2b815d4cbd98 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Fri, 11 Feb 2022 09:10:28 +0100 Subject: [PATCH] server: rename RequestFilter to UserRequestFilter --- server/src/domain/handler.rs | 12 +++--- server/src/domain/sql_backend_handler.rs | 28 ++++++++------ server/src/infra/graphql/query.rs | 14 ++++--- server/src/infra/ldap_handler.rs | 48 ++++++++++++------------ server/src/infra/tcp_backend_handler.rs | 2 +- 5 files changed, 55 insertions(+), 49 deletions(-) diff --git a/server/src/domain/handler.rs b/server/src/domain/handler.rs index 1f09153..bbb3315 100644 --- a/server/src/domain/handler.rs +++ b/server/src/domain/handler.rs @@ -43,10 +43,10 @@ pub struct BindRequest { } #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)] -pub enum RequestFilter { - And(Vec), - Or(Vec), - Not(Box), +pub enum UserRequestFilter { + And(Vec), + Or(Vec), + Not(Box), Equality(String, String), // Check if a user belongs to a group identified by name. MemberOf(String), @@ -93,7 +93,7 @@ pub struct GroupIdAndName(pub GroupId, pub String); #[async_trait] pub trait BackendHandler: Clone + Send { - async fn list_users(&self, filters: Option) -> Result>; + async fn list_users(&self, filters: Option) -> Result>; async fn list_groups(&self) -> Result>; async fn get_user_details(&self, user_id: &str) -> Result; async fn get_group_details(&self, group_id: GroupId) -> Result; @@ -116,7 +116,7 @@ mockall::mock! { } #[async_trait] impl BackendHandler for TestBackendHandler { - async fn list_users(&self, filters: Option) -> Result>; + async fn list_users(&self, filters: Option) -> Result>; async fn list_groups(&self) -> Result>; async fn get_user_details(&self, user_id: &str) -> Result; async fn get_group_details(&self, group_id: GroupId) -> Result; diff --git a/server/src/domain/sql_backend_handler.rs b/server/src/domain/sql_backend_handler.rs index c961073..8b5312d 100644 --- a/server/src/domain/sql_backend_handler.rs +++ b/server/src/domain/sql_backend_handler.rs @@ -21,10 +21,10 @@ impl SqlBackendHandler { struct RequiresGroup(bool); // Returns the condition for the SQL query, and whether it requires joining with the groups table. -fn get_filter_expr(filter: RequestFilter) -> (RequiresGroup, SimpleExpr) { - use RequestFilter::*; +fn get_filter_expr(filter: UserRequestFilter) -> (RequiresGroup, SimpleExpr) { + use UserRequestFilter::*; fn get_repeated_filter( - fs: Vec, + fs: Vec, field: &dyn Fn(SimpleExpr, SimpleExpr) -> SimpleExpr, ) -> (RequiresGroup, SimpleExpr) { let mut requires_group = false; @@ -74,7 +74,7 @@ fn get_filter_expr(filter: RequestFilter) -> (RequiresGroup, SimpleExpr) { #[async_trait] impl BackendHandler for SqlBackendHandler { - async fn list_users(&self, filters: Option) -> Result> { + async fn list_users(&self, filters: Option) -> Result> { let query = { let mut query_builder = Query::select() .column((Users::Table, Users::UserId)) @@ -88,11 +88,15 @@ impl BackendHandler for SqlBackendHandler { .order_by((Users::Table, Users::UserId), Order::Asc) .to_owned(); if let Some(filter) = filters { - if filter == RequestFilter::Not(Box::new(RequestFilter::And(Vec::new()))) { + if filter + == UserRequestFilter::Not(Box::new( + UserRequestFilter::And(Vec::new()), + )) + { return Ok(Vec::new()); } - if filter != RequestFilter::And(Vec::new()) - && filter != RequestFilter::Or(Vec::new()) + if filter != UserRequestFilter::And(Vec::new()) + && filter != UserRequestFilter::Or(Vec::new()) { let (RequiresGroup(requires_group), condition) = get_filter_expr(filter); query_builder.and_where(condition); @@ -516,7 +520,7 @@ mod tests { } { let users = handler - .list_users(Some(RequestFilter::Equality( + .list_users(Some(UserRequestFilter::Equality( "user_id".to_string(), "bob".to_string(), ))) @@ -529,9 +533,9 @@ mod tests { } { let users = handler - .list_users(Some(RequestFilter::Or(vec![ - RequestFilter::Equality("user_id".to_string(), "bob".to_string()), - RequestFilter::Equality("user_id".to_string(), "John".to_string()), + .list_users(Some(UserRequestFilter::Or(vec![ + UserRequestFilter::Equality("user_id".to_string(), "bob".to_string()), + UserRequestFilter::Equality("user_id".to_string(), "John".to_string()), ]))) .await .unwrap() @@ -542,7 +546,7 @@ mod tests { } { let users = handler - .list_users(Some(RequestFilter::Not(Box::new(RequestFilter::Equality( + .list_users(Some(UserRequestFilter::Not(Box::new(UserRequestFilter::Equality( "user_id".to_string(), "bob".to_string(), ))))) diff --git a/server/src/infra/graphql/query.rs b/server/src/infra/graphql/query.rs index 1de7520..26593b1 100644 --- a/server/src/infra/graphql/query.rs +++ b/server/src/infra/graphql/query.rs @@ -2,7 +2,7 @@ use crate::domain::handler::{BackendHandler, GroupId, GroupIdAndName}; use juniper::{graphql_object, FieldResult, GraphQLInputObject}; use serde::{Deserialize, Serialize}; -type DomainRequestFilter = crate::domain::handler::RequestFilter; +type DomainRequestFilter = crate::domain::handler::UserRequestFilter; type DomainUser = crate::domain::handler::User; type DomainGroup = crate::domain::handler::Group; use super::api::Context; @@ -269,7 +269,10 @@ impl From for Group { #[cfg(test)] mod tests { use super::*; - use crate::{domain::handler::MockTestBackendHandler, infra::auth_service::ValidationResults}; + use crate::{ + domain::handler::{MockTestBackendHandler, UserRequestFilter}, + infra::auth_service::ValidationResults, + }; use juniper::{ execute, graphql_value, DefaultScalarValue, EmptyMutation, EmptySubscription, GraphQLType, RootNode, Variables, @@ -358,11 +361,10 @@ mod tests { }"#; let mut mock = MockTestBackendHandler::new(); - use crate::domain::handler::RequestFilter; mock.expect_list_users() - .with(eq(Some(RequestFilter::Or(vec![ - RequestFilter::Equality("id".to_string(), "bob".to_string()), - RequestFilter::Equality("email".to_string(), "robert@bobbers.on".to_string()), + .with(eq(Some(UserRequestFilter::Or(vec![ + UserRequestFilter::Equality("id".to_string(), "bob".to_string()), + UserRequestFilter::Equality("email".to_string(), "robert@bobbers.on".to_string()), ])))) .return_once(|_| { Ok(vec![ diff --git a/server/src/infra/ldap_handler.rs b/server/src/infra/ldap_handler.rs index 27f63f3..ad707ed 100644 --- a/server/src/infra/ldap_handler.rs +++ b/server/src/infra/ldap_handler.rs @@ -1,6 +1,6 @@ use crate::domain::{ handler::{ - BackendHandler, BindRequest, Group, GroupIdAndName, LoginHandler, RequestFilter, User, + BackendHandler, BindRequest, Group, GroupIdAndName, LoginHandler, UserRequestFilter, User, }, opaque_handler::OpaqueHandler, }; @@ -494,7 +494,7 @@ impl LdapHandler Result { let users = backend_handler - .list_users(Some(RequestFilter::MemberOfId(g.0))) + .list_users(Some(UserRequestFilter::MemberOfId(g.0))) .await?; Ok(Group { id: g.0, @@ -605,21 +605,21 @@ impl LdapHandler Result { + fn convert_user_filter(&self, filter: &LdapFilter) -> Result { match filter { - LdapFilter::And(filters) => Ok(RequestFilter::And( + LdapFilter::And(filters) => Ok(UserRequestFilter::And( filters .iter() .map(|f| self.convert_user_filter(f)) .collect::>()?, )), - LdapFilter::Or(filters) => Ok(RequestFilter::Or( + LdapFilter::Or(filters) => Ok(UserRequestFilter::Or( filters .iter() .map(|f| self.convert_user_filter(f)) .collect::>()?, )), - LdapFilter::Not(filter) => Ok(RequestFilter::Not(Box::new( + LdapFilter::Not(filter) => Ok(UserRequestFilter::Not(Box::new( self.convert_user_filter(&*filter)?, ))), LdapFilter::Equality(field, value) => { @@ -629,27 +629,27 @@ impl LdapHandler { // Check that it's a field we support. if field.to_lowercase() == "objectclass" || map_field(field).is_ok() { - Ok(RequestFilter::And(vec![])) + Ok(UserRequestFilter::And(vec![])) } else { - Ok(RequestFilter::Not(Box::new(RequestFilter::And(vec![])))) + Ok(UserRequestFilter::Not(Box::new(UserRequestFilter::And(vec![])))) } } _ => bail!("Unsupported user filter: {:?}", filter), @@ -678,7 +678,7 @@ mod tests { } #[async_trait] impl BackendHandler for TestBackendHandler { - async fn list_users(&self, filters: Option) -> Result>; + async fn list_users(&self, filters: Option) -> Result>; async fn list_groups(&self) -> Result>; async fn get_user_details(&self, user_id: &str) -> Result; async fn get_group_details(&self, group_id: GroupId) -> Result; @@ -1133,7 +1133,7 @@ mod tests { Ok(set) }); mock.expect_list_users() - .with(eq(Some(RequestFilter::MemberOfId(GroupId(1))))) + .with(eq(Some(UserRequestFilter::MemberOfId(GroupId(1))))) .times(1) .return_once(|_| { Ok(vec![User { @@ -1172,16 +1172,16 @@ mod tests { async fn test_search_filters() { let mut mock = MockTestBackendHandler::new(); mock.expect_list_users() - .with(eq(Some(RequestFilter::And(vec![RequestFilter::Or(vec![ - RequestFilter::Not(Box::new(RequestFilter::Equality( + .with(eq(Some(UserRequestFilter::And(vec![UserRequestFilter::Or(vec![ + UserRequestFilter::Not(Box::new(UserRequestFilter::Equality( "user_id".to_string(), "bob".to_string(), ))), - RequestFilter::And(vec![]), - RequestFilter::Not(Box::new(RequestFilter::And(vec![]))), - RequestFilter::And(vec![]), - RequestFilter::And(vec![]), - RequestFilter::Not(Box::new(RequestFilter::And(vec![]))), + UserRequestFilter::And(vec![]), + UserRequestFilter::Not(Box::new(UserRequestFilter::And(vec![]))), + UserRequestFilter::And(vec![]), + UserRequestFilter::And(vec![]), + UserRequestFilter::Not(Box::new(UserRequestFilter::And(vec![]))), ])])))) .times(1) .return_once(|_| Ok(vec![])); @@ -1210,7 +1210,7 @@ mod tests { async fn test_search_member_of() { let mut mock = MockTestBackendHandler::new(); mock.expect_list_users() - .with(eq(Some(RequestFilter::MemberOf("group_1".to_string())))) + .with(eq(Some(UserRequestFilter::MemberOf("group_1".to_string())))) .times(1) .return_once(|_| Ok(vec![])); let mut ldap_handler = setup_bound_handler(mock).await; @@ -1256,8 +1256,8 @@ mod tests { async fn test_search_filters_lowercase() { let mut mock = MockTestBackendHandler::new(); mock.expect_list_users() - .with(eq(Some(RequestFilter::And(vec![RequestFilter::Or(vec![ - RequestFilter::Not(Box::new(RequestFilter::Equality( + .with(eq(Some(UserRequestFilter::And(vec![UserRequestFilter::Or(vec![ + UserRequestFilter::Not(Box::new(UserRequestFilter::Equality( "first_name".to_string(), "bob".to_string(), ))), diff --git a/server/src/infra/tcp_backend_handler.rs b/server/src/infra/tcp_backend_handler.rs index e36ae50..f0dfa02 100644 --- a/server/src/infra/tcp_backend_handler.rs +++ b/server/src/infra/tcp_backend_handler.rs @@ -35,7 +35,7 @@ mockall::mock! { } #[async_trait] impl BackendHandler for TestTcpBackendHandler { - async fn list_users(&self, filters: Option) -> Result>; + async fn list_users(&self, filters: Option) -> Result>; async fn list_groups(&self) -> Result>; async fn get_user_details(&self, user_id: &str) -> Result; async fn get_group_details(&self, group_id: GroupId) -> Result;