Skip to content

Commit

Permalink
Create wrapper method to assign keycloak user role
Browse files Browse the repository at this point in the history
We only have the one keycloak user role, so extracted into it's own method and constant, so we have less magic strings.
  • Loading branch information
alex-yau-ttd committed Sep 2, 2024
1 parent f479ea9 commit 5b776c4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/api/configureApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import makeMetricsApiMiddleware from './middleware/metrics';
import { createParticipantsRouter } from './routers/participants/participantsRouter';
import { createSitesRouter } from './routers/sitesRouter';
import { createUsersRouter } from './routers/usersRouter';
import { API_PARTICIPANT_MEMBER } from './services/kcUsersService';
import { LoggerService } from './services/loggerService';
import { UserService } from './services/userService';

Expand Down Expand Up @@ -134,7 +135,7 @@ export function configureAndStartApi(useMetrics: boolean = true, portNumber: num
bypassHandlerForPaths(
claimCheck((claim: Claim) => {
const roles = claim.resource_access?.self_serve_portal_apis?.roles || [];
return roles.includes('api-participant-member');
return roles.includes(API_PARTICIPANT_MEMBER);
}),
...BYPASS_CLAIM_PATHS,
...BYPASS_AUTH_PATHS
Expand Down
9 changes: 4 additions & 5 deletions src/api/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ import {
httpPost,
httpPut,
request,
response,
response
} from 'inversify-express-utils';

import { TYPES } from '../constant/types';
import { ParticipantStatus } from '../entities/Participant';
import { getTraceId } from '../helpers/loggingHelpers';
import { getKcAdminClient } from '../keycloakAdminClient';
import {
assignClientRoleToUser,
queryUsersByEmail,
sendInviteEmailToNewUser,
assignApiParticipantMemberRole, queryUsersByEmail,
sendInviteEmailToNewUser
} from '../services/kcUsersService';
import { LoggerService } from '../services/loggerService';
import { SelfResendInvitationParser, UserService } from '../services/userService';
Expand Down Expand Up @@ -66,7 +65,7 @@ export class UserController {
const kcAdminClient = await getKcAdminClient();
const promises = [
req.user!.$query().patch({ acceptedTerms: true }),
assignClientRoleToUser(kcAdminClient, req.user?.email!, 'api-participant-member'),
assignApiParticipantMemberRole(kcAdminClient, req.user?.email!),
];
await Promise.all(promises);
res.sendStatus(200);
Expand Down
4 changes: 2 additions & 2 deletions src/api/routers/participants/participantsCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
performAsyncOperationWithAuditTrail,
} from '../../services/auditTrailService';
import {
assignClientRoleToUser,
assignApiParticipantMemberRole,
createNewUser,
sendInviteEmailToNewUser,
} from '../../services/kcUsersService';
Expand Down Expand Up @@ -163,7 +163,7 @@ async function createParticipant(
);

// assign proper api access
assignClientRoleToUser(kcAdminClient, user.email, 'api-participant-member');
await assignApiParticipantMemberRole(kcAdminClient, user.email);

// send email
await sendInviteEmailToNewUser(kcAdminClient, newKcUser);
Expand Down
6 changes: 4 additions & 2 deletions src/api/routers/participants/participantsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ import {
constructAuditTrailObject,
performAsyncOperationWithAuditTrail
} from '../../services/auditTrailService';
import { assignClientRoleToUser } from '../../services/kcUsersService';
import {
assignApiParticipantMemberRole
} from '../../services/kcUsersService';
import {
addSharingParticipants,
deleteSharingParticipants,
Expand Down Expand Up @@ -167,7 +169,7 @@ export function createParticipantsRouter() {
await setSiteClientTypes(data);
await Promise.all(
usersFromParticipant.map((currentUser) =>
assignClientRoleToUser(kcAdminClient, currentUser.email, 'api-participant-member')
assignApiParticipantMemberRole(kcAdminClient, currentUser.email)
)
);

Expand Down
19 changes: 18 additions & 1 deletion src/api/services/kcUsersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export const queryUsersByEmail = async (kcAdminClient: KeycloakAdminClient, emai
});
};

export const doesUserExistInKeycloak = async (
kcAdminClient: KeycloakAdminClient,
email: string
) => {
const existingKcUser = await queryUsersByEmail(kcAdminClient, email);
return existingKcUser.length > 0;
};

export const createNewUser = async (
kcAdminClient: KeycloakAdminClient,
firstName: string,
Expand Down Expand Up @@ -76,7 +84,7 @@ export const deleteUserByEmail = async (kcAdminClient: KeycloakAdminClient, user
});
};

export const assignClientRoleToUser = async (
const assignClientRoleToUser = async (
kcAdminClient: KeycloakAdminClient,
userEmail: string,
roleName: string
Expand All @@ -101,3 +109,12 @@ export const assignClientRoleToUser = async (
],
});
};

export const API_PARTICIPANT_MEMBER = 'api-participant-member';

export const assignApiParticipantMemberRole = async (
kcAdminClient: KeycloakAdminClient,
userEmail: string
) => {
await assignClientRoleToUser(kcAdminClient, userEmail, API_PARTICIPANT_MEMBER);
};

0 comments on commit 5b776c4

Please sign in to comment.