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: keycloak sync mistakes #93

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion collectivo/collectivo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@collectivo/collectivo",
"version": "0.3.5",
"version": "0.3.6",
"description": "Collectivo is a modular system for community plattforms.",
"type": "module",
"license": "AGPL-3.0",
Expand Down
12 changes: 8 additions & 4 deletions collectivo/collectivo/server/api/collectivo/auth.patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ async function syncKeycloakUser(event: any) {
return;
}

// Set external identifier to match email
if (user.id && email != extid) {
// Set external identifier to match new email
if (user.id && body.payload.email && email != extid) {
console.log("updating external identifier", email);
await directus.request(
updateUser(user.id, { external_identifier: user.email }),
updateUser(user.id, { external_identifier: email }),
);
}

Expand Down Expand Up @@ -156,7 +157,8 @@ async function syncKeycloakUser(event: any) {
// Update keycloak user
if ("email" in body.payload && body.payload.email !== user.email) {
console.log("updating email");

console.log("kc_user_id", kc_user_id);
console.log("email", body.payload.email);
await keycloak.users.update(
{ id: kc_user_id },
{
Expand All @@ -165,6 +167,8 @@ async function syncKeycloakUser(event: any) {
emailVerified: true, // to prevent loops
},
);

console.log("email updated");
}

if ("first_name" in body.payload) {
Expand Down
24 changes: 5 additions & 19 deletions collectivo/collectivo/server/api/collectivo/roles.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export default defineEventHandler(async (event) => {
const isDelete = body.event === "collectivo_tags_directus_users.items.delete";

body.keys = body.keys || [body.key];
console.log("Body", body);

if (isCreate) {
assignRole(body);
Expand All @@ -47,7 +46,7 @@ export default defineEventHandler(async (event) => {
await assignRole(body, key);
}
} else {
throw new Error("Unsupported event ", body.event);
throw new Error("Tag relation event can only be create or delete");
}
});

Expand All @@ -56,13 +55,10 @@ async function assignRole(body: any, deleteKey?: any) {
const directus = await useDirectusAdmin();
let tagID = "";
let userID = "";

if (deleteKey) {
console.log("Delete key", deleteKey);
const tagRelation = await directus.request(
readItem("collectivo_tags_directus_users", deleteKey),
);
console.log("Tag relation", tagRelation);
tagID = tagRelation.collectivo_tags_id;
userID = tagRelation.directus_users_id;
} else {
Expand All @@ -72,6 +68,10 @@ async function assignRole(body: any, deleteKey?: any) {
body.payload.collectivo_tags_id.id ?? body.payload.collectivo_tags_id;
}

if (!tagID || !userID) {
return;
}

const tag = await directus.request(
readItem("collectivo_tags", tagID, {
fields: ["tags_name", "tags_sync"],
Expand All @@ -97,8 +97,6 @@ async function assignRole(body: any, deleteKey?: any) {

const roleName = tag.tags_name.toLowerCase().replace(/ /g, "-");

console.log("role", roleName);

let role: any = await keycloak.roles.findOneByName({ name: roleName });

if (!role) {
Expand All @@ -107,8 +105,6 @@ async function assignRole(body: any, deleteKey?: any) {
role = await keycloak.roles.findOneByName({ name: roleName });
}

console.log("Role keycloak", role);

let kc_user_id = null;

const kc_users = await keycloak.users.find({
Expand Down Expand Up @@ -136,14 +132,4 @@ async function assignRole(body: any, deleteKey?: any) {
roles: [{ id: role.id, name: role.name }],
});
}

console.log("SHOULD BE MAPPED");
// const role = await keycloak.roles.find({ name });
// console.log("Role", role);
}

async function removeRole(body: any) {
const keycloak = await useKeycloak();
const directus = await useDirectusAdmin();
console.log("Remove role", body);
}
5 changes: 3 additions & 2 deletions collectivo/collectivo/server/api/collectivo/user.post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readRoles, updateUser } from "@directus/sdk";

// Update keycloak user
// If a new user is created, make it a keycloak user
export default defineEventHandler(async (event) => {
const config = useRuntimeConfig();
console.log("new user created called");
Expand All @@ -23,7 +23,8 @@ export default defineEventHandler(async (event) => {
body.keys = body.keys || [body.key];

if (!isCreate) {
throw new Error("Only users.create events are supported");
console.log("Only users.create events are supported");
return;
}

const roleID = await getRole("collectivo_user");
Expand Down
Loading