Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restore users during realm restoration #186

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion app/pages/api/realms/[id]/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { checkAdminRole, createEvent } from 'utils/helpers';
import prisma from 'utils/prisma';
import { EventEnum, StatusEnum } from 'validators/create-realm';
import { sendRestoreEmail } from 'utils/mailer';
import { manageCustomRealm } from 'controllers/keycloak';
import { addUserAsRealmAdmin, manageCustomRealm } from 'controllers/keycloak';
import { generateXML, makeSoapRequest, getBceidAccounts } from 'utils/idir';
import getConfig from 'next/config';

const { serverRuntimeConfig = {} } = getConfig() || {};
const { idir_requestor_user_guid } = serverRuntimeConfig;

interface ErrorData {
success: boolean;
Expand Down Expand Up @@ -69,6 +74,25 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
return res.status(422).send('Unable to process the restore request at this time');
}

try {
if (allEnvRealmsRestored) {
[realm?.productOwnerIdirUserId, realm?.technicalContactIdirUserId].forEach(async (idirUserId) => {
const samlPayload = generateXML('userId', idirUserId as string, idir_requestor_user_guid);
const { response }: any = await makeSoapRequest(samlPayload);
const accounts = await getBceidAccounts(response);

if (accounts.length > 0) {
await addUserAsRealmAdmin(`${accounts[0].guid}@idir`, realm?.environments!, realm?.realm!);
} else {
console.error(`No guid found for user ${String(idirUserId)}`);
}
});
}
} catch (err) {
console.error('failed to create realm admins', err);
}

//emails
await sendRestoreEmail(realm, `${session.user.given_name} ${session.user.family_name}`);

res.status(200).send('success');
Expand Down