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

chore: update script #384

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
25 changes: 17 additions & 8 deletions scripts/restore-idir-users-with-roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const restoreUsers = async () => {
if (!env || !json) {
console.info(`
Pre-requisites:
* Run SELECT * FROM KC_DELETED_USERS WHERE TIMESTAMP > CURRENT_DATE AND ENVIRONMENT = <env>; to fetch deleted user data by environment
* RUN select json_agg(row_to_json(my_row)) from (select * FROM KC_DELETED_USERS WHERE TIMESTAMP > CURRENT_DATE AND ENVIRONMENT = <env>) as my_row;
* Save the output in JSON format and place it under <root>/scripts folder

Usages:
Expand All @@ -27,22 +27,21 @@ const restoreUsers = async () => {
return;
}

const keycloakAdmin = await getAdminClient(env);
const totalUsers = readJSON(`${__dirname}/${json}`);
const realmRoles = await keycloakAdmin.roles.find({ realm: 'standard' });

for (let kuser of totalUsers) {
try {
kuser.attributes = JSON.parse(kuser.attributes);
kuser.realm_roles = kuser.realm_roles.slice(1, -1).split(',');
let clientRoles = JSON.parse('[' + kuser.client_roles.slice(1, -1) + ']');
kuser.client_roles = clientRoles.map((ob) => JSON.parse(ob));

const keycloakAdmin = await getAdminClient(env);
kuser.realm_roles = kuser.realm_roles;
kuser.client_roles = kuser.client_roles.map((client) => JSON.parse(client));

let newUser = null;

const us = await keycloakAdmin.users.find({ realm: 'standard', username: kuser.username, max: 1 });

if (us.length === 0) {
console.log('creating user...');
const createdUser = await keycloakAdmin.users.create({
realm: 'standard',
username: kuser.username,
Expand All @@ -51,13 +50,22 @@ const restoreUsers = async () => {
firstName: kuser.first_name,
lastName: kuser.last_name,
attributes: kuser.attributes,
realmRoles: kuser.realm_roles,
});
newUser = createdUser;
} else {
newUser = us[0];
}

const rolesToAssign = kuser.realm_roles
.map((roleName) => realmRoles.find((role) => role.name === roleName))
.filter((role) => role);

await keycloakAdmin.users.addRealmRoleMappings({
id: newUser.id,
realm: 'standard',
roles: rolesToAssign,
});

const fedList = await keycloakAdmin.users.listFederatedIdentities({ realm: 'standard', id: newUser.id });

if (fedList.length === 0) {
Expand Down Expand Up @@ -109,6 +117,7 @@ const restoreUsers = async () => {
processed.push(kuser);
console.log('processed', processed.length);
} catch (err) {
console.log(err);
unprocessed.push(kuser);
console.log('unprocessed', unprocessed.length);
continue;
Expand Down