Files
wekan/server/ldapGroupSync.js
T
Lauri OjansivuandClaude Opus 4.8 04b26f4702 Auth sync marks the orgs/teams it manages as Sync Members From Auth Provider (#5850).
When the LDAP/OIDC group sync (setUserOrgsTeamsFromLdap) creates or assigns an
Organization or Team, it now sets that org/team's orgSyncMembersFromAuth /
teamSyncMembersFromAuth flag to true, so the "Sync Members From Auth Provider"
column in Admin Panel > People reflects which orgs/teams are auth-managed.
Thanks to xet7.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-19 11:35:29 +03:00

85 lines
3.4 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { ReactiveCache } from '/imports/reactiveCache';
import Org from '/models/org';
import Team from '/models/team';
// #4737: helper that syncs a user's LDAP groups into Wekan Organizations or
// Teams. It is called server-to-server from the wekan-ldap package via
// Meteor.callAsync (where `this.connection` is null), and may also be called by
// an admin (admins already manage orgs/teams in the Admin Panel). Any other
// client-originated call is rejected. It finds the Org/Team by display name,
// creates it (active) if missing, and adds it to the user's membership. The
// update is add-only: existing memberships — including ones assigned manually or
// from another source — are preserved, so enabling LDAP org/team sync never
// removes a user's other memberships.
Meteor.methods({
async setUserOrgsTeamsFromLdap(userId, groupNames, asOrganization) {
// Allow server-to-server calls (connection === null, used by the LDAP sync)
// and admins; reject any other client-originated call.
if (this.connection !== null) {
const caller = this.userId ? await ReactiveCache.getUser(this.userId) : null;
if (!caller || caller.isAdmin !== true) {
throw new Meteor.Error('forbidden', 'Not authorized');
}
}
check(userId, String);
check(groupNames, [String]);
check(asOrganization, Boolean);
const user = await ReactiveCache.getUser(userId);
if (!user) {
return;
}
// De-duplicate and drop empties so a group is not processed twice.
const names = [...new Set(groupNames.map(n => (n || '').trim()).filter(Boolean))];
for (const name of names) {
if (asOrganization) {
let org = await Org.findOneAsync({ orgDisplayName: name });
if (!org) {
const orgId = await Org.insertAsync({
orgDisplayName: name,
orgShortName: name,
orgIsActive: true,
// #5850: the auth sync marks the orgs it manages.
orgSyncMembersFromAuth: true,
});
org = { _id: orgId };
} else if (org.orgSyncMembersFromAuth !== true) {
await Org.updateAsync({ _id: org._id }, { $set: { orgSyncMembersFromAuth: true } });
}
const alreadyMember = (user.orgs || []).some(o => o.orgId === org._id);
if (!alreadyMember) {
await Meteor.users.updateAsync(
{ _id: userId },
{ $push: { orgs: { orgId: org._id, orgDisplayName: name } } },
);
}
} else {
let team = await Team.findOneAsync({ teamDisplayName: name });
if (!team) {
const teamId = await Team.insertAsync({
teamDisplayName: name,
teamShortName: name,
teamIsActive: true,
// #5850: the auth sync marks the teams it manages.
teamSyncMembersFromAuth: true,
});
team = { _id: teamId };
} else if (team.teamSyncMembersFromAuth !== true) {
await Team.updateAsync({ _id: team._id }, { $set: { teamSyncMembersFromAuth: true } });
}
const alreadyMember = (user.teams || []).some(t => t.teamId === team._id);
if (!alreadyMember) {
await Meteor.users.updateAsync(
{ _id: userId },
{ $push: { teams: { teamId: team._id, teamDisplayName: name } } },
);
}
}
}
},
});