diff --git a/backend/.env.dist b/backend/.env.dist index 580f7ad2b..0164abff0 100644 --- a/backend/.env.dist +++ b/backend/.env.dist @@ -65,5 +65,7 @@ WEBHOOK_ELOPAGE_SECRET=secret FEDERATION_VALIDATE_COMMUNITY_TIMER=60000 # GMS +# GMS_ACTIVE=true +# Coordinates of Illuminz test instance GMS_HOST=54.176.169.179 GMS_PORT=3071 diff --git a/backend/.env.template b/backend/.env.template index 05cec536e..290d180b3 100644 --- a/backend/.env.template +++ b/backend/.env.template @@ -63,5 +63,6 @@ WEBHOOK_ELOPAGE_SECRET=$WEBHOOK_ELOPAGE_SECRET FEDERATION_VALIDATE_COMMUNITY_TIMER=$FEDERATION_VALIDATE_COMMUNITY_TIMER # GMS +GMS_ACTIVE=$GMS_ACTIVE GMS_HOST=$GMS_HOST GMS_PORT=$GMS_PORT diff --git a/backend/src/apis/gms/GmsClient.ts b/backend/src/apis/gms/GmsClient.ts index 6f92f3ee3..e4bdd4371 100644 --- a/backend/src/apis/gms/GmsClient.ts +++ b/backend/src/apis/gms/GmsClient.ts @@ -8,11 +8,11 @@ import { CONFIG } from '@/config' import { LogError } from '@/server/LogError' import { backendLogger as logger } from '@/server/logger' -import { GmsCommunity } from './model/GmsCommunity' import { GmsUser } from './model/GmsUser' +/* export async function communityList(): Promise { - const baseUrl = 'http://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/') + const baseUrl = 'https://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/') const service = 'community/list?page=1&perPage=20' const config = { headers: { @@ -32,10 +32,10 @@ export async function communityList(): Promise { - const baseUrl = 'http://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/') + const baseUrl = 'https://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/') const service = 'community-user/list?page=1&perPage=20' const config = { headers: { @@ -68,10 +68,10 @@ export async function userList(): Promise { } logger.debug('responseData:', result.data.responseData.data) // eslint-disable-next-line @typescript-eslint/no-unsafe-call - const gmsUser = JSON.parse(result.data.responseData.data) - logger.debug('gmsUser:', gmsUser) + // const gmsUser = JSON.parse(result.data.responseData.data) + // logger.debug('gmsUser:', gmsUser) // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return gmsUser + return result.data.responseData.data } catch (error: any) { logger.error('Error in Get community-user/list:', error) const errMsg: string = error.message @@ -80,7 +80,7 @@ export async function userList(): Promise { } export async function userByUuid(uuid: string): Promise { - const baseUrl = 'http://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/') + const baseUrl = 'https://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/') const service = 'community-user/list?page=1&perPage=20' const config = { headers: { @@ -104,19 +104,20 @@ export async function userByUuid(uuid: string): Promise { - const baseUrl = 'http://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/') + const baseUrl = 'https://'.concat(CONFIG.GMS_HOST).concat(':').concat(CONFIG.GMS_PORT).concat('/') const service = 'community-user' const config = { headers: { @@ -125,7 +126,6 @@ export async function createGmsUser(apiKey: string, user: GmsUser): Promise a.trim()) + .filter(Boolean) + .join(', ') + return address + } } } diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index d28256119..7efb1e91f 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -135,6 +135,7 @@ const federation = { } const gms = { + GMS_ACTIVE: process.env.GMS_ACTIVE === 'true' || false, // koordinates of Illuminz-instance of GMS GMS_HOST: process.env.GMS_HOST ?? 'localhost', GMS_PORT: process.env.GMS_PORT ?? '4044', diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index 1f21abbb9..902abaf99 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -71,6 +71,7 @@ import { findUserByIdentifier } from './util/findUserByIdentifier' import { findUsers } from './util/findUsers' import { getKlicktippState } from './util/getKlicktippState' import { setUserRole, deleteUserRole } from './util/modifyUserRole' +import { sendUserToGms } from './util/sendUserToGms' import { validateAlias } from './util/validateAlias' const LANGUAGES = ['de', 'en', 'es', 'fr', 'nl'] @@ -361,6 +362,18 @@ export class UserResolver { } else { await EVENT_USER_REGISTER(dbUser) } + + if (CONFIG.GMS_ACTIVE) { + logger.info('GMS deactivated per configuration! New user is not published to GMS.') + } else { + try { + if (dbUser.gmsAllowed && !dbUser.gmsRegistered) { + await sendUserToGms(dbUser, homeCom) + } + } catch (err) { + logger.error('Error publishing new created user to GMS:', err) + } + } return new User(dbUser) } diff --git a/backend/src/graphql/resolver/util/sendUserToGms.ts b/backend/src/graphql/resolver/util/sendUserToGms.ts new file mode 100644 index 000000000..335141ffe --- /dev/null +++ b/backend/src/graphql/resolver/util/sendUserToGms.ts @@ -0,0 +1,27 @@ +import { Community as DbCommunity } from '@entity/Community' +import { User as DbUser } from '@entity/User' + +import { createGmsUser } from '@/apis/gms/GmsClient' +import { GmsUser } from '@/apis/gms/model/GmsUser' +import { LogError } from '@/server/LogError' +import { backendLogger as logger } from '@/server/logger' + +export async function sendUserToGms(user: DbUser, homeCom: DbCommunity): Promise { + if (homeCom.gmsApiKey === null) { + throw new LogError('HomeCommunity needs GMS-ApiKey to publish user data to GMS.') + } + logger.debug('User send to GMS:', user) + const gmsUser = new GmsUser(user) + try { + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + if (await createGmsUser(homeCom.gmsApiKey, gmsUser)) { + logger.debug('GMS user published successfully:', gmsUser) + user.gmsRegistered = true + user.gmsRegisteredAt = new Date() + await DbUser.save(user) + logger.debug('mark user as gms published:', user) + } + } catch (err) { + logger.warn('publishing user fails with ', err) + } +} diff --git a/backend/src/seeds/gmsUserList.ts b/backend/src/seeds/gmsUserList.ts index 009c097a7..68bb86650 100644 --- a/backend/src/seeds/gmsUserList.ts +++ b/backend/src/seeds/gmsUserList.ts @@ -5,15 +5,10 @@ /* eslint-disable @typescript-eslint/no-unsafe-return */ import { entities } from '@entity/index' -import { User as DbUser } from '@entity/User' import { createTestClient } from 'apollo-server-testing' -import { communityList, createGmsUser, userList } from '@/apis/gms/GmsClient' -import { GmsUser } from '@/apis/gms/model/GmsUser' import { CONFIG } from '@/config' -import { getHomeCommunity } from '@/graphql/resolver/util/communities' import { createServer } from '@/server/createServer' -import { LogError } from '@/server/LogError' import { backendLogger as logger } from '@/server/logger' CONFIG.EMAIL = false @@ -52,10 +47,10 @@ const run = async () => { // test GMS-Api Client try { - const gmsComArray = await communityList() - logger.debug('GMS-Community-List:', gmsComArray) - const gmsUserArray = await userList() - logger.debug('GMS-Community-User-List:', gmsUserArray) + // const gmsComArray = await communityList() + // logger.debug('GMS-Community-List:', gmsComArray) + // const gmsUserArray = await userList() + // logger.debug('GMS-Community-User-List:', gmsUserArray) } catch (err) { logger.error('Error in GMS-API:', err) }