Skip to content

Commit

Permalink
Merge pull request #3657 from uselagoon/use-email-not-username
Browse files Browse the repository at this point in the history
fix: use email address instead of username when looking up users
  • Loading branch information
tobybellwood authored Mar 4, 2024
2 parents 9e02e37 + b36398e commit e836082
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
18 changes: 9 additions & 9 deletions services/api/src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface UserEdit {
interface UserModel {
loadAllUsers: () => Promise<User[]>;
loadUserById: (id: string) => Promise<User>;
loadUserByUsername: (username: string) => Promise<User>;
loadUserByUsername: (email: string) => Promise<User>;
loadUserByIdOrUsername: (userInput: UserEdit) => Promise<User>;
loadUsersByOrganizationId: (organizationId: number) => Promise<User[]>;
getAllOrganizationIdsForUser: (userInput: User) => Promise<number[]>;
Expand Down Expand Up @@ -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<User> => {
const loadUserByUsername = async (email: string): Promise<User> => {
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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions services/api/src/resources/group/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion services/api/src/resources/organization/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion services/api/src/resources/sshKey/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
10 changes: 5 additions & 5 deletions services/api/src/resources/user/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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
Expand All @@ -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', {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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', {
Expand Down

0 comments on commit e836082

Please sign in to comment.