graphql: Add methods to add/remove group memberships

This commit is contained in:
Valentin Tolmer
2021-09-15 09:45:17 +02:00
committed by nitnelave
parent a54e73bded
commit e4d6b122c5
6 changed files with 54 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
use crate::domain::handler::{BackendHandler, CreateUserRequest, UpdateUserRequest};
use crate::domain::handler::{BackendHandler, CreateUserRequest, GroupId, UpdateUserRequest};
use juniper::{graphql_object, FieldResult, GraphQLInputObject, GraphQLObject};
use super::api::Context;
@@ -93,4 +93,34 @@ impl<Handler: BackendHandler + Sync> Mutation<Handler> {
.await?;
Ok(Success::new())
}
async fn add_user_to_group(
context: &Context<Handler>,
user_id: String,
group_id: i32,
) -> FieldResult<Success> {
if !context.validation_result.is_admin {
return Err("Unauthorized group membership modification".into());
}
context
.handler
.add_user_to_group(&user_id, GroupId(group_id))
.await?;
Ok(Success::new())
}
async fn remove_user_from_group(
context: &Context<Handler>,
user_id: String,
group_id: i32,
) -> FieldResult<Success> {
if !context.validation_result.is_admin {
return Err("Unauthorized group membership modification".into());
}
context
.handler
.remove_user_from_group(&user_id, GroupId(group_id))
.await?;
Ok(Success::new())
}
}

View File

@@ -35,6 +35,7 @@ mockall::mock! {
async fn delete_user(&self, user_id: &str) -> DomainResult<()>;
async fn create_group(&self, group_name: &str) -> DomainResult<GroupId>;
async fn add_user_to_group(&self, user_id: &str, group_id: GroupId) -> DomainResult<()>;
async fn remove_user_from_group(&self, user_id: &str, group_id: GroupId) -> DomainResult<()>;
}
#[async_trait]
impl TcpBackendHandler for TestTcpBackendHandler {