From 9baa94e14864d09735ad9ddb2256c96d2e290f78 Mon Sep 17 00:00:00 2001 From: Claus-Peter Huebner Date: Mon, 9 Oct 2023 15:46:02 +0200 Subject: [PATCH] extent query users --- backend/src/graphql/resolver/UserResolver.ts | 7 ++++-- .../resolver/util/findUserByIdentifier.ts | 25 +++++++++++++++---- backend/src/seeds/graphql/queries.ts | 8 ++++-- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index 6408b9dda..7b5bcdd91 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -809,8 +809,11 @@ export class UserResolver { @Authorized([RIGHTS.USER]) @Query(() => User) - async user(@Arg('identifier') identifier: string): Promise { - return new User(await findUserByIdentifier(identifier)) + async user( + @Arg('identifier') identifier: string, + @Arg('communityIdentifier') communityIdentifier?: string, + ): Promise { + return new User(await findUserByIdentifier(identifier, communityIdentifier)) } } diff --git a/backend/src/graphql/resolver/util/findUserByIdentifier.ts b/backend/src/graphql/resolver/util/findUserByIdentifier.ts index 96c9eb458..0118dd865 100644 --- a/backend/src/graphql/resolver/util/findUserByIdentifier.ts +++ b/backend/src/graphql/resolver/util/findUserByIdentifier.ts @@ -6,12 +6,18 @@ import { LogError } from '@/server/LogError' import { VALID_ALIAS_REGEX } from './validateAlias' -export const findUserByIdentifier = async (identifier: string): Promise => { +export const findUserByIdentifier = async ( + identifier: string, + communityIdentifier?: string, +): Promise => { let user: DbUser | null if (validate(identifier) && version(identifier) === 4) { - user = await DbUser.findOne({ where: { gradidoID: identifier }, relations: ['emailContact'] }) + user = await DbUser.findOne({ + where: { gradidoID: identifier, communityUuid: communityIdentifier }, + relations: ['emailContact'], + }) if (!user) { - throw new LogError('No user found to given identifier', identifier) + throw new LogError('No user found to given identifier(s)', identifier, communityIdentifier) } } else if (/^.{2,}@.{2,}\..{2,}$/.exec(identifier)) { const userContact = await DbUserContact.findOne({ @@ -27,12 +33,21 @@ export const findUserByIdentifier = async (identifier: string): Promise if (!userContact.user) { throw new LogError('No user to given contact', identifier) } + if (userContact.user.communityUuid !== communityIdentifier) { + throw new LogError( + 'Found user to given contact, but belongs to foreign community', + identifier, + ) + } user = userContact.user user.emailContact = userContact } else if (VALID_ALIAS_REGEX.exec(identifier)) { - user = await DbUser.findOne({ where: { alias: identifier }, relations: ['emailContact'] }) + user = await DbUser.findOne({ + where: { alias: identifier, communityUuid: communityIdentifier }, + relations: ['emailContact'], + }) if (!user) { - throw new LogError('No user found to given identifier', identifier) + throw new LogError('No user found to given identifier(s)', identifier, communityIdentifier) } } else { throw new LogError('Unknown identifier type', identifier) diff --git a/backend/src/seeds/graphql/queries.ts b/backend/src/seeds/graphql/queries.ts index b7ef87aa8..bb665c4c7 100644 --- a/backend/src/seeds/graphql/queries.ts +++ b/backend/src/seeds/graphql/queries.ts @@ -370,10 +370,14 @@ export const adminListContributionMessages = gql` ` export const user = gql` - query ($identifier: String!) { - user(identifier: $identifier) { + query ($identifier: String!, $communityIdentifier?: String) { + user(identifier: $identifier, $communityIdentifier: $communityIdentifier) { firstName lastName + foreign + communityUuid + gradidoID + alias } } `