From b36398e2d2449ec9819a53c3cb2d3b2e9bde1728 Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Wed, 21 Feb 2024 19:19:20 +1100 Subject: [PATCH] fix: use email address instead of username when looking up users --- services/api/src/models/user.ts | 18 +++++++++--------- services/api/src/resources/group/resolvers.ts | 4 ++-- .../src/resources/organization/resolvers.ts | 2 +- services/api/src/resources/sshKey/resolvers.ts | 2 +- services/api/src/resources/user/resolvers.ts | 10 +++++----- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/services/api/src/models/user.ts b/services/api/src/models/user.ts index 0934d2be6d..0606353a06 100644 --- a/services/api/src/models/user.ts +++ b/services/api/src/models/user.ts @@ -40,7 +40,7 @@ interface UserEdit { interface UserModel { loadAllUsers: () => Promise; loadUserById: (id: string) => Promise; - loadUserByUsername: (username: string) => Promise; + loadUserByUsername: (email: string) => Promise; loadUserByIdOrUsername: (userInput: UserEdit) => Promise; loadUsersByOrganizationId: (organizationId: number) => Promise; getAllOrganizationIdsForUser: (userInput: User) => Promise; @@ -235,22 +235,22 @@ export const User = (clients: { }; // used by project resolver only, so leave this one out of redis for now - const loadUserByUsername = async (username: string): Promise => { + const loadUserByUsername = async (email: string): Promise => { const keycloakUsers = await keycloakAdminClient.users.find({ - username + email }); if (R.isEmpty(keycloakUsers)) { - throw new UserNotFoundError(`User not found: ${username}`); + throw new UserNotFoundError(`User not found: ${email}`); } const userId = R.pipe( - R.filter(R.propEq('username', username)), + R.filter(R.propEq('email', email)), R.path(['0', 'id']) )(keycloakUsers); if (R.isNil(userId)) { - throw new UserNotFoundError(`User not found: ${username}`); + throw new UserNotFoundError(`User not found: ${email}`); } // @ts-ignore @@ -262,11 +262,11 @@ export const User = (clients: { return loadUserById(R.prop('id', userInput)); } - if (R.prop('username', userInput)) { - return loadUserByUsername(R.prop('username', userInput)); + if (R.prop('email', userInput)) { + return loadUserByUsername(R.prop('email', userInput)); } - throw new Error('You must provide a user id or username'); + throw new Error('You must provide a user id or email'); }; // used to list onwers of organizations diff --git a/services/api/src/resources/group/resolvers.ts b/services/api/src/resources/group/resolvers.ts index 04e4db9f7d..8694649f3a 100644 --- a/services/api/src/resources/group/resolvers.ts +++ b/services/api/src/resources/group/resolvers.ts @@ -546,7 +546,7 @@ export const addUserToGroup: ResolverFn = async ( const user = await models.UserModel.loadUserByIdOrUsername({ id: R.prop('id', userInput), - username: R.prop('email', userInput) + email: R.prop('email', userInput) }); if (R.isEmpty(groupInput)) { @@ -598,7 +598,7 @@ export const removeUserFromGroup: ResolverFn = async ( const user = await models.UserModel.loadUserByIdOrUsername({ id: R.prop('id', userInput), - username: R.prop('email', userInput) + email: R.prop('email', userInput) }); if (R.isEmpty(groupInput)) { diff --git a/services/api/src/resources/organization/resolvers.ts b/services/api/src/resources/organization/resolvers.ts index 9cc23a0411..6cb0161ba9 100644 --- a/services/api/src/resources/organization/resolvers.ts +++ b/services/api/src/resources/organization/resolvers.ts @@ -932,7 +932,7 @@ export const removeUserFromOrganizationGroups: ResolverFn = async ( const user = await models.UserModel.loadUserByIdOrUsername({ id: R.prop('id', userInput), - username: R.prop('email', userInput) + email: R.prop('email', userInput) }); // check the organization exists diff --git a/services/api/src/resources/sshKey/resolvers.ts b/services/api/src/resources/sshKey/resolvers.ts index c632b09bf3..f5f3705650 100644 --- a/services/api/src/resources/sshKey/resolvers.ts +++ b/services/api/src/resources/sshKey/resolvers.ts @@ -49,7 +49,7 @@ export const addSshKey: ResolverFn = async ( const user = await models.UserModel.loadUserByIdOrUsername({ id: R.prop('id', userInput), - username: R.prop('email', userInput) + email: R.prop('email', userInput) }); await hasPermission('ssh_key', 'add', { diff --git a/services/api/src/resources/user/resolvers.ts b/services/api/src/resources/user/resolvers.ts index 919f06f71b..aaf0df8c43 100644 --- a/services/api/src/resources/user/resolvers.ts +++ b/services/api/src/resources/user/resolvers.ts @@ -160,7 +160,7 @@ export const updateUser: ResolverFn = async ( const user = await models.UserModel.loadUserByIdOrUsername({ id: R.prop('id', userInput), - username: R.prop('email', userInput), + email: R.prop('email', userInput), }); await hasPermission('user', 'update', { @@ -187,7 +187,7 @@ export const resetUserPassword: ResolverFn = async ( ) => { const user = await models.UserModel.loadUserByIdOrUsername({ id: R.prop('id', userInput), - username: R.prop('email', userInput), + email: R.prop('email', userInput), }); // someone can reset their own password if they want to, but admins will be able to do this @@ -207,7 +207,7 @@ export const deleteUser: ResolverFn = async ( ) => { const user = await models.UserModel.loadUserByIdOrUsername({ id: R.prop('id', userInput), - username: R.prop('email', userInput), + email: R.prop('email', userInput), }); await hasPermission('user', 'delete', { @@ -233,7 +233,7 @@ export const addUserToOrganization: ResolverFn = async ( const user = await models.UserModel.loadUserByIdOrUsername({ id: R.prop('id', userInput), - username: R.prop('email', userInput), + email: R.prop('email', userInput), }); let updateUser = { @@ -284,7 +284,7 @@ export const removeUserFromOrganization: ResolverFn = async ( const user = await models.UserModel.loadUserByIdOrUsername({ id: R.prop('id', userInput), - username: R.prop('email', userInput), + email: R.prop('email', userInput), }); await hasPermission('organization', 'addOwner', {