From 1c5c82286ed9c857c1fe65a2668e4e7d140c510e Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Thu, 1 Aug 2019 21:30:33 +0000 Subject: [PATCH] [CORL-474] Harmonize SSO User ID's (#2440) * feat: harmonized user id's * fix: doctoc --- .../middleware/passport/strategies/verifiers/sso.ts | 1 + src/core/server/models/user/user.ts | 10 ++++------ src/core/server/services/users/index.ts | 10 ++++++++++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/core/server/app/middleware/passport/strategies/verifiers/sso.ts b/src/core/server/app/middleware/passport/strategies/verifiers/sso.ts index 35344e4d6..2e0792870 100644 --- a/src/core/server/app/middleware/passport/strategies/verifiers/sso.ts +++ b/src/core/server/app/middleware/passport/strategies/verifiers/sso.ts @@ -125,6 +125,7 @@ export async function findOrCreateSSOUser( mongo, tenant, { + id, username, role: GQLUSER_ROLE.COMMENTER, email, diff --git a/src/core/server/models/user/user.ts b/src/core/server/models/user/user.ts index 899fd170d..96ef5319c 100644 --- a/src/core/server/models/user/user.ts +++ b/src/core/server/models/user/user.ts @@ -374,21 +374,18 @@ export type InsertUserInput = Omit< | "ignoredUsers" | "emailVerificationID" | "createdAt" ->; +> & + Partial>; export async function insertUser( mongo: Db, tenantID: string, - input: InsertUserInput, + { id = uuid.v4(), ...input }: InsertUserInput, now = new Date() ) { - // Create a new ID for the user. - const id = uuid.v4(); - // default are the properties set by the application when a new user is // created. const defaults: Sub = { - id, tenantID, tokens: [], ignoredUsers: [], @@ -425,6 +422,7 @@ export async function insertUser( const user: Readonly = { ...defaults, ...input, + id, profiles, }; diff --git a/src/core/server/services/users/index.ts b/src/core/server/services/users/index.ts index f54c9cf97..b36ff7e6e 100644 --- a/src/core/server/services/users/index.ts +++ b/src/core/server/services/users/index.ts @@ -3,6 +3,7 @@ import { Db } from "mongodb"; import { DuplicateEmailError, + DuplicateUserError, EmailAlreadySetError, EmailNotSetError, LocalProfileAlreadySetError, @@ -85,6 +86,15 @@ export async function insert( } } + if (input.id) { + // Try to check to see if there is a user with the same ID before we try to + // create the user again. + const alreadyFoundUser = await retrieveUser(mongo, tenant.id, input.id); + if (alreadyFoundUser) { + throw new DuplicateUserError(); + } + } + const localProfile = getLocalProfile(input); if (localProfile) { validateEmail(localProfile.id);