From 641018ff567b582e182700379de74c136a4cac9a Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Tue, 31 Aug 2021 16:16:17 +0200 Subject: [PATCH] model: remove AddUserToGroupRequest --- model/src/lib.rs | 6 ------ src/domain/handler.rs | 13 ++++++++----- src/domain/sql_backend_handler.rs | 20 +++++++------------- src/infra/tcp_backend_handler.rs | 4 ++-- src/main.rs | 5 +---- 5 files changed, 18 insertions(+), 30 deletions(-) diff --git a/model/src/lib.rs b/model/src/lib.rs index 1570574..349d7a6 100644 --- a/model/src/lib.rs +++ b/model/src/lib.rs @@ -98,12 +98,6 @@ pub struct Group { pub users: Vec, } -#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)] -pub struct AddUserToGroupRequest { - pub user_id: String, - pub group_id: i32, -} - #[derive(Clone, Serialize, Deserialize)] pub struct JWTClaims { pub exp: DateTime, diff --git a/src/domain/handler.rs b/src/domain/handler.rs index 6fd661c..2aaf456 100644 --- a/src/domain/handler.rs +++ b/src/domain/handler.rs @@ -3,7 +3,7 @@ use async_trait::async_trait; use serde::{Deserialize, Serialize}; use std::collections::HashSet; -pub use lldap_model::{AddUserToGroupRequest, Group, User}; +pub use lldap_model::{Group, User}; #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone)] pub struct BindRequest { @@ -34,6 +34,9 @@ pub trait LoginHandler: Clone + Send { async fn bind(&self, request: BindRequest) -> Result<()>; } +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub struct GroupId(pub i32); + #[async_trait] pub trait BackendHandler: Clone + Send { async fn list_users(&self, filters: Option) -> Result>; @@ -41,8 +44,8 @@ pub trait BackendHandler: Clone + Send { async fn get_user_details(&self, user_id: &str) -> Result; async fn create_user(&self, request: CreateUserRequest) -> Result<()>; async fn delete_user(&self, user_id: &str) -> Result<()>; - async fn create_group(&self, group_name: &str) -> Result; - async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()>; + async fn create_group(&self, group_name: &str) -> Result; + async fn add_user_to_group(&self, user_id: &str, group_id: GroupId) -> Result<()>; async fn get_user_groups(&self, user: &str) -> Result>; } @@ -59,9 +62,9 @@ mockall::mock! { async fn get_user_details(&self, user_id: &str) -> Result; async fn create_user(&self, request: CreateUserRequest) -> Result<()>; async fn delete_user(&self, user_id: &str) -> Result<()>; - async fn create_group(&self, group_name: &str) -> Result; + async fn create_group(&self, group_name: &str) -> Result; async fn get_user_groups(&self, user: &str) -> Result>; - async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()>; + async fn add_user_to_group(&self, user_id: &str, group_id: GroupId) -> Result<()>; } #[async_trait] impl LoginHandler for TestBackendHandler { diff --git a/src/domain/sql_backend_handler.rs b/src/domain/sql_backend_handler.rs index f234cb2..44e3efd 100644 --- a/src/domain/sql_backend_handler.rs +++ b/src/domain/sql_backend_handler.rs @@ -202,7 +202,7 @@ impl BackendHandler for SqlBackendHandler { Ok(()) } - async fn create_group(&self, group_name: &str) -> Result { + async fn create_group(&self, group_name: &str) -> Result { let query = Query::insert() .into_table(Groups::Table) .columns(vec![Groups::DisplayName]) @@ -215,14 +215,14 @@ impl BackendHandler for SqlBackendHandler { .and_where(Expr::col(Groups::DisplayName).eq(group_name)) .to_string(DbQueryBuilder {}); let row = sqlx::query(&query).fetch_one(&self.sql_pool).await?; - Ok(row.get::(&*Groups::GroupId.to_string())) + Ok(GroupId(row.get::(&*Groups::GroupId.to_string()))) } - async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()> { + async fn add_user_to_group(&self, user_id: &str, group_id: GroupId) -> Result<()> { let query = Query::insert() .into_table(Memberships::Table) .columns(vec![Memberships::UserId, Memberships::GroupId]) - .values_panic(vec![request.user_id.into(), request.group_id.into()]) + .values_panic(vec![user_id.into(), group_id.0.into()]) .to_string(DbQueryBuilder {}); sqlx::query(&query).execute(&self.sql_pool).await?; Ok(()) @@ -292,18 +292,12 @@ mod tests { .unwrap(); } - async fn insert_group(handler: &SqlBackendHandler, name: &str) -> i32 { + async fn insert_group(handler: &SqlBackendHandler, name: &str) -> GroupId { handler.create_group(name).await.unwrap() } - async fn insert_membership(handler: &SqlBackendHandler, group_id: i32, user_id: &str) { - handler - .add_user_to_group(AddUserToGroupRequest { - user_id: user_id.to_string(), - group_id, - }) - .await - .unwrap(); + async fn insert_membership(handler: &SqlBackendHandler, group_id: GroupId, user_id: &str) { + handler.add_user_to_group(user_id, group_id).await.unwrap(); } #[tokio::test] diff --git a/src/infra/tcp_backend_handler.rs b/src/infra/tcp_backend_handler.rs index fbf24b2..ed896cc 100644 --- a/src/infra/tcp_backend_handler.rs +++ b/src/infra/tcp_backend_handler.rs @@ -32,8 +32,8 @@ mockall::mock! { async fn get_user_groups(&self, user: &str) -> DomainResult>; async fn create_user(&self, request: CreateUserRequest) -> DomainResult<()>; async fn delete_user(&self, user_id: &str) -> DomainResult<()>; - async fn create_group(&self, group_name: &str) -> DomainResult; - async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> DomainResult<()>; + async fn create_group(&self, group_name: &str) -> DomainResult; + async fn add_user_to_group(&self, user_id: &str, group_id: GroupId) -> DomainResult<()>; } #[async_trait] impl TcpBackendHandler for TestTcpBackendHandler { diff --git a/src/main.rs b/src/main.rs index 13c33bc..73f760f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,10 +32,7 @@ async fn create_admin_user(handler: &SqlBackendHandler, config: &Configuration) .await .context("Error creating admin group")?; handler - .add_user_to_group(lldap_model::AddUserToGroupRequest { - user_id: config.ldap_user_dn.clone(), - group_id: admin_group_id, - }) + .add_user_to_group(&config.ldap_user_dn, admin_group_id) .await .context("Error adding admin user to group") }