mirror of
https://github.com/nitnelave/lldap.git
synced 2023-04-12 14:25:13 +00:00
Add method to add users to groups
This commit is contained in:
parent
b3c2f5edfc
commit
352a7d90fc
@ -68,6 +68,12 @@ pub struct CreateGroupRequest {
|
||||
pub display_name: String,
|
||||
}
|
||||
|
||||
#[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<Utc>,
|
||||
|
@ -11,6 +11,7 @@ pub trait BackendHandler: Clone + Send {
|
||||
async fn list_groups(&self) -> Result<Vec<Group>>;
|
||||
async fn create_user(&self, request: CreateUserRequest) -> Result<()>;
|
||||
async fn create_group(&self, request: CreateGroupRequest) -> Result<i32>;
|
||||
async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()>;
|
||||
async fn get_user_groups(&self, user: String) -> Result<HashSet<String>>;
|
||||
}
|
||||
|
||||
@ -28,5 +29,6 @@ mockall::mock! {
|
||||
async fn create_user(&self, request: CreateUserRequest) -> Result<()>;
|
||||
async fn create_group(&self, request: CreateGroupRequest) -> Result<i32>;
|
||||
async fn get_user_groups(&self, user: String) -> Result<HashSet<String>>;
|
||||
async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> Result<()>;
|
||||
}
|
||||
}
|
||||
|
@ -242,6 +242,7 @@ impl BackendHandler for SqlBackendHandler {
|
||||
sqlx::query(&query).execute(&self.sql_pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn create_group(&self, request: CreateGroupRequest) -> Result<i32> {
|
||||
let query = Query::insert()
|
||||
.into_table(Groups::Table)
|
||||
@ -255,8 +256,17 @@ impl BackendHandler for SqlBackendHandler {
|
||||
.and_where(Expr::col(Groups::DisplayName).eq(request.display_name.as_str()))
|
||||
.to_string(DbQueryBuilder {});
|
||||
let row = sqlx::query(&query).fetch_one(&self.sql_pool).await?;
|
||||
Ok(
|
||||
row.get::<i32, _>(&*Groups::GroupId.to_string()))
|
||||
Ok(row.get::<i32, _>(&*Groups::GroupId.to_string()))
|
||||
}
|
||||
|
||||
async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> 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()])
|
||||
.to_string(DbQueryBuilder {});
|
||||
sqlx::query(&query).execute(&self.sql_pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -288,16 +298,22 @@ mod tests {
|
||||
}
|
||||
|
||||
async fn insert_group(handler: &SqlBackendHandler, name: &str) -> i32 {
|
||||
handler.create_group(CreateGroupRequest{display_name: name.to_string()}).await.unwrap()
|
||||
handler
|
||||
.create_group(CreateGroupRequest {
|
||||
display_name: name.to_string(),
|
||||
})
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
async fn insert_membership(sql_pool: &Pool, group_id: i32, user_id: &str) {
|
||||
let query = Query::insert()
|
||||
.into_table(Memberships::Table)
|
||||
.columns(vec![Memberships::UserId, Memberships::GroupId])
|
||||
.values_panic(vec![user_id.into(), group_id.into()])
|
||||
.to_string(DbQueryBuilder {});
|
||||
sqlx::query(&query).execute(sql_pool).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();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@ -437,10 +453,10 @@ mod tests {
|
||||
insert_user(&handler, "John", "Pa33w0rd!").await;
|
||||
let group_1 = insert_group(&handler, "Best Group").await;
|
||||
let group_2 = insert_group(&handler, "Worst Group").await;
|
||||
insert_membership(&sql_pool, group_1, "bob").await;
|
||||
insert_membership(&sql_pool, group_1, "patrick").await;
|
||||
insert_membership(&sql_pool, group_2, "patrick").await;
|
||||
insert_membership(&sql_pool, group_2, "John").await;
|
||||
insert_membership(&handler, group_1, "bob").await;
|
||||
insert_membership(&handler, group_1, "patrick").await;
|
||||
insert_membership(&handler, group_2, "patrick").await;
|
||||
insert_membership(&handler, group_2, "John").await;
|
||||
assert_eq!(
|
||||
handler.list_groups().await.unwrap(),
|
||||
vec![
|
||||
@ -466,9 +482,9 @@ mod tests {
|
||||
insert_user(&handler, "John", "Pa33w0rd!").await;
|
||||
let group_1 = insert_group(&handler, "Group1").await;
|
||||
let group_2 = insert_group(&handler, "Group2").await;
|
||||
insert_membership(&sql_pool, group_1, "bob").await;
|
||||
insert_membership(&sql_pool, group_1, "patrick").await;
|
||||
insert_membership(&sql_pool, group_2, "patrick").await;
|
||||
insert_membership(&handler, group_1, "bob").await;
|
||||
insert_membership(&handler, group_1, "patrick").await;
|
||||
insert_membership(&handler, group_2, "patrick").await;
|
||||
let mut bob_groups = HashSet::new();
|
||||
bob_groups.insert("Group1".to_string());
|
||||
let mut patrick_groups = HashSet::new();
|
||||
|
@ -29,6 +29,7 @@ mockall::mock! {
|
||||
async fn get_user_groups(&self, user: String) -> DomainResult<HashSet<String>>;
|
||||
async fn create_user(&self, request: CreateUserRequest) -> DomainResult<()>;
|
||||
async fn create_group(&self, request: CreateGroupRequest) -> DomainResult<i32>;
|
||||
async fn add_user_to_group(&self, request: AddUserToGroupRequest) -> DomainResult<()>;
|
||||
}
|
||||
#[async_trait]
|
||||
impl TcpBackendHandler for TestTcpBackendHandler {
|
||||
|
Loading…
Reference in New Issue
Block a user