Skip to content

Commit

Permalink
Add button for manually syncing with Keycloak
Browse files Browse the repository at this point in the history
  • Loading branch information
danieladugyan committed Oct 18, 2024
1 parent e8cdd43 commit 7582cb4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/lib/server/keycloak/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ async function updateEmails(prisma: PrismaClient) {
if (currentUserEmail.size === 0) return;

const userEmails = await getManyUserEmails(currentUserEmail);
console.log(`updating ${userEmails.size} emails`);
console.log(`[${new Date().toISOString()}] updating ${userEmails.size}`);

for (const [studentId, email] of userEmails) {
await prisma.member.update({
Expand All @@ -211,13 +211,14 @@ async function getManyUserEmails(
const userEmails = new Map<string, string>();

(await client.users.find({ username: "" })).forEach((user) => {
const { username, email } = user;
if (!username || !email) return;

if (
user.email !== undefined && // if keycloak has an email for the user
user.username !== undefined && // if keycloak has a username for the user
currentUserEmail.has(user.username) && // if we have the user in our database
currentUserEmail.get(user.username) !== user.email // if the email has changed
currentUserEmail.has(username) && // if the user exists in our database
currentUserEmail.get(username) !== user.email // if the email has changed
) {
userEmails.set(user.username, user.email);
userEmails.set(username, email);
}
});
return userEmails;
Expand Down
8 changes: 8 additions & 0 deletions src/routes/(app)/admin/debug/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { env } from "$env/dynamic/private";
import keycloak from "$lib/server/keycloak";
import authorizedPrismaClient from "$lib/server/shop/authorizedPrisma";
import { isNollningPeriod } from "$lib/utils/adminSettings/nollning";

export const load = async () => {
Expand All @@ -7,3 +9,9 @@ export const load = async () => {
prismaLogLevel: env.PRISMA_LOG_LEVEL,
};
};

export const actions = {
keycloakSync: async () => {
keycloak.sync(authorizedPrismaClient);
},
};
11 changes: 11 additions & 0 deletions src/routes/(app)/admin/debug/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
<script lang="ts">
import { version } from "$app/environment";
import { isAuthorized } from "$lib/utils/authorization";
export let data;
$: user = data.user;
$: policies = user.policies.toSorted();
</script>

{#if isAuthorized("core:admin")}
<section class="mb-4">
<h1 class="mb-2 text-lg font-semibold">Actions</h1>
<form action="?/keycloakSync" method="POST" class="flex items-center gap-4">
<button type="submit" class="btn">Sync with Keycloak</button>
<p>This will push mandates and pull email addresses.</p>
</form>
</section>
{/if}

<section>
<h1 class="text-lg font-semibold">Metadata</h1>
<ul class="ml-4 list-disc">
Expand Down

0 comments on commit 7582cb4

Please sign in to comment.