Skip to content

Commit

Permalink
388 Updates keycloak whenever names are updated (#518)
Browse files Browse the repository at this point in the history
* now syncs names with keycloak

* updates for onboarding as well
  • Loading branch information
Steindt authored Oct 16, 2024
1 parent cdf5186 commit cce9610
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/lib/server/keycloak/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ async function getGroupId(client: KcAdminClient, positionId: string) {
return group?.id;
}

async function updateProfile(
username: string,
firstName: string,
lastName: string,
) {
if (!enabled) return;

try {
const client = await connect();
const id = await _getUserId(client, username);
await client.users.update(
{ id: id! },
{
firstName: firstName,
lastName: lastName,
},
);
console.log(`updated profile`);
} catch (error) {
console.log(error);
}
}

async function addMandate(username: string, positionId: string) {
if (!enabled) return;

Expand Down Expand Up @@ -131,7 +154,7 @@ async function updateMandate(prisma: PrismaClient) {
},
});

console.log(`updating ${result.length} users`);
console.log(`updating ${result.length} users groups`);

result.forEach(async ({ positionId, member: { studentId } }) => {
await deleteMandate(studentId!, positionId);
Expand Down Expand Up @@ -236,6 +259,7 @@ async function sync(prisma: PrismaClient) {
}

export default {
updateProfile,
addMandate,
deleteMandate,
getUserId,
Expand Down
5 changes: 5 additions & 0 deletions src/routes/(app)/members/[studentId]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ export const actions: Actions = {
...form.data,
},
});
keycloak.updateProfile(
studentId,
form.data.firstName ?? "",
form.data.lastName ?? "",
);
return message(form, {
message: m.members_memberUpdated(),
type: "success",
Expand Down
6 changes: 6 additions & 0 deletions src/routes/(app)/onboarding/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Actions, PageServerLoad } from "./$types";
import { error, fail } from "@sveltejs/kit";
import { redirect } from "$lib/utils/redirect";
import * as m from "$paraglide/messages";
import keycloak from "$lib/server/keycloak";

export const load: PageServerLoad = async ({ locals }) => {
const { prisma } = locals;
Expand Down Expand Up @@ -69,6 +70,11 @@ export const actions: Actions = {
} else {
throw error(500, m.onboarding_errors_studentIDNotFound());
}
keycloak.updateProfile(
studentId,
form.data.firstName ?? "",
form.data.lastName ?? "",
);
return redirect(
"/",
{
Expand Down

0 comments on commit cce9610

Please sign in to comment.