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

@ -6,6 +6,8 @@ input EqualityConstraint {
type Mutation { type Mutation {
createUser(user: CreateUserInput!): User! createUser(user: CreateUserInput!): User!
updateUser(user: UpdateUserInput!): Success! updateUser(user: UpdateUserInput!): Success!
addUserToGroup(userId: String!, groupId: Int!): Success!
removeUserFromGroup(userId: String!, groupId: Int!): Success!
} }
type Group { type Group {

View File

@ -87,6 +87,7 @@ pub trait BackendHandler: Clone + Send {
async fn delete_user(&self, user_id: &str) -> Result<()>; async fn delete_user(&self, user_id: &str) -> Result<()>;
async fn create_group(&self, group_name: &str) -> Result<GroupId>; async fn create_group(&self, group_name: &str) -> Result<GroupId>;
async fn add_user_to_group(&self, user_id: &str, group_id: GroupId) -> Result<()>; async fn add_user_to_group(&self, user_id: &str, group_id: GroupId) -> Result<()>;
async fn remove_user_from_group(&self, user_id: &str, group_id: GroupId) -> Result<()>;
async fn get_user_groups(&self, user: &str) -> Result<HashSet<String>>; async fn get_user_groups(&self, user: &str) -> Result<HashSet<String>>;
} }
@ -107,6 +108,7 @@ mockall::mock! {
async fn create_group(&self, group_name: &str) -> Result<GroupId>; async fn create_group(&self, group_name: &str) -> Result<GroupId>;
async fn get_user_groups(&self, user: &str) -> Result<HashSet<String>>; async fn get_user_groups(&self, user: &str) -> Result<HashSet<String>>;
async fn add_user_to_group(&self, user_id: &str, group_id: GroupId) -> Result<()>; async fn add_user_to_group(&self, user_id: &str, group_id: GroupId) -> Result<()>;
async fn remove_user_from_group(&self, user_id: &str, group_id: GroupId) -> Result<()>;
} }
#[async_trait] #[async_trait]
impl LoginHandler for TestBackendHandler { impl LoginHandler for TestBackendHandler {

View File

@ -247,7 +247,17 @@ impl BackendHandler for SqlBackendHandler {
let query = Query::insert() let query = Query::insert()
.into_table(Memberships::Table) .into_table(Memberships::Table)
.columns(vec![Memberships::UserId, Memberships::GroupId]) .columns(vec![Memberships::UserId, Memberships::GroupId])
.values_panic(vec![user_id.into(), group_id.0.into()]) .values_panic(vec![user_id.into(), group_id.into()])
.to_string(DbQueryBuilder {});
sqlx::query(&query).execute(&self.sql_pool).await?;
Ok(())
}
async fn remove_user_from_group(&self, user_id: &str, group_id: GroupId) -> Result<()> {
let query = Query::delete()
.from_table(Memberships::Table)
.and_where(Expr::col(Memberships::GroupId).eq(group_id))
.and_where(Expr::col(Memberships::UserId).eq(user_id))
.to_string(DbQueryBuilder {}); .to_string(DbQueryBuilder {});
sqlx::query(&query).execute(&self.sql_pool).await?; sqlx::query(&query).execute(&self.sql_pool).await?;
Ok(()) Ok(())

View File

@ -1,3 +1,4 @@
use super::handler::GroupId;
use sea_query::*; use sea_query::*;
pub type Pool = sqlx::sqlite::SqlitePool; pub type Pool = sqlx::sqlite::SqlitePool;
@ -5,6 +6,12 @@ pub type PoolOptions = sqlx::sqlite::SqlitePoolOptions;
pub type DbRow = sqlx::sqlite::SqliteRow; pub type DbRow = sqlx::sqlite::SqliteRow;
pub type DbQueryBuilder = SqliteQueryBuilder; pub type DbQueryBuilder = SqliteQueryBuilder;
impl From<GroupId> for Value {
fn from(group_id: GroupId) -> Self {
group_id.0.into()
}
}
#[derive(Iden)] #[derive(Iden)]
pub enum Users { pub enum Users {
Table, Table,

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 juniper::{graphql_object, FieldResult, GraphQLInputObject, GraphQLObject};
use super::api::Context; use super::api::Context;
@ -93,4 +93,34 @@ impl<Handler: BackendHandler + Sync> Mutation<Handler> {
.await?; .await?;
Ok(Success::new()) 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 delete_user(&self, user_id: &str) -> DomainResult<()>;
async fn create_group(&self, group_name: &str) -> DomainResult<GroupId>; 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 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] #[async_trait]
impl TcpBackendHandler for TestTcpBackendHandler { impl TcpBackendHandler for TestTcpBackendHandler {