graphql: Add a method to update a user

This commit is contained in:
Valentin Tolmer
2021-09-01 10:00:51 +02:00
committed by nitnelave
parent 0ac9e134de
commit 2954109d96
7 changed files with 101 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
use crate::domain::handler::{BackendHandler, CreateUserRequest};
use juniper::{graphql_object, FieldResult, GraphQLInputObject};
use crate::domain::handler::{BackendHandler, CreateUserRequest, UpdateUserRequest};
use juniper::{graphql_object, FieldResult, GraphQLInputObject, GraphQLObject};
use super::api::Context;
@@ -19,7 +19,7 @@ impl<Handler: BackendHandler> Mutation<Handler> {
#[derive(PartialEq, Eq, Debug, GraphQLInputObject)]
/// The details required to create a user.
pub struct UserInput {
pub struct CreateUserInput {
id: String,
email: String,
display_name: Option<String>,
@@ -27,11 +27,32 @@ pub struct UserInput {
last_name: Option<String>,
}
#[derive(PartialEq, Eq, Debug, GraphQLInputObject)]
/// The fields that can be updated for a user.
pub struct UpdateUserInput {
id: String,
email: Option<String>,
display_name: Option<String>,
first_name: Option<String>,
last_name: Option<String>,
}
#[derive(PartialEq, Eq, Debug, GraphQLObject)]
pub struct Success {
ok: bool,
}
impl Success {
fn new() -> Self {
Self { ok: true }
}
}
#[graphql_object(context = Context<Handler>)]
impl<Handler: BackendHandler + Sync> Mutation<Handler> {
async fn create_user(
context: &Context<Handler>,
user: UserInput,
user: CreateUserInput,
) -> FieldResult<super::query::User<Handler>> {
if !context.validation_result.is_admin {
return Err("Unauthorized user creation".into());
@@ -52,4 +73,24 @@ impl<Handler: BackendHandler + Sync> Mutation<Handler> {
.await
.map(Into::into)?)
}
async fn update_user(
context: &Context<Handler>,
user: UpdateUserInput,
) -> FieldResult<Success> {
if !context.validation_result.can_access(&user.id) {
return Err("Unauthorized user update".into());
}
context
.handler
.update_user(UpdateUserRequest {
user_id: user.id,
email: user.email,
display_name: user.display_name,
first_name: user.first_name,
last_name: user.last_name,
})
.await?;
Ok(Success::new())
}
}

View File

@@ -31,6 +31,7 @@ mockall::mock! {
async fn get_user_details(&self, user_id: &str) -> DomainResult<User>;
async fn get_user_groups(&self, user: &str) -> DomainResult<HashSet<String>>;
async fn create_user(&self, request: CreateUserRequest) -> DomainResult<()>;
async fn update_user(&self, request: UpdateUserRequest) -> DomainResult<()>;
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<()>;