Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

fix(user_ldap): dispatch UserRemovedEvent for members of removed groups - #67444

Open
susnux wants to merge 4 commits into
masterfrom
fix/user-ldap-removed-group-events
Open

susnux wants to merge 4 commits into
masterfrom
fix/user-ldap-removed-group-events

Conversation

@susnux

@susnux susnux commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When a group disappears from LDAP, the UpdateGroups background job never dispatched UserRemovedEvent for its former members. UpdateGroupsService::handleRemovedGroups() deleted the cached memberships first and then read them to send the events, so it always found nothing. This has been the case since the move to the flat ldap_group_membership table (Nextcloud 28).

Even with the order fixed, the events would still be skipped: IGroupManager::get() returns null for such a group because Group_LDAP::groupExists() can no longer read it from LDAP. A group object that has no backends but carries the group id is now used as a fallback. All listeners in server only read the gid from it (share manager, external storage mount cache, activity, admin audit, collaboration resources).

As a result, listeners now learn that members lost the group, which also covers apps that keep their own membership copies (e.g. Teams).

Some listeners clean up irreversibly (the share manager removes the user's group share customizations and, with "share with group members only", user shares without a common group). This matches what already happens when a user leaves a group that still exists, but to keep it from firing by accident:

  • A group missing from the group list is first checked against LDAP without cache (groupExistsOnLDAP($gid, true)). If it still exists, e.g. because one of several configurations returned incomplete results, its memberships are kept and no event is sent. An unreachable server throws and aborts the job as before.
  • Users that are still in a group with the same id from another backend (e.g. the database backend) get no event. Only non-LDAP backends are asked, because the cached LDAP membership of a just removed group may be stale and would suppress the event for good.
  • Groups are handled one at a time, and their memberships are deleted only after the events were sent. A failing listener no longer loses the events of the remaining members (some may get the event twice on the next run), and the job no longer loads all memberships of all removed groups at once.

Deactivating an LDAP configuration does not trigger mass events: its users can no longer be resolved, so no event is sent for them.

The dispatching is shared by all removal paths: removed groups, members leaving a group that still exists, and LoginListener. The login path had the same problem for a single user: it deleted the membership and then dropped the event because the group could not be resolved.

GroupDeletedEvent is intentionally still not dispatched. It was removed in 7732de7 because a group that is no longer returned by LDAP is not necessarily deleted (filter change, temporary partial results), and its listeners delete the group shares themselves. That is why #38618 (group shares staying in place) is related but not resolved by this PR.

The test class also gets the missing parent::setUp() call; without it UpdateGroupsServiceTest errors in tearDown() when run on its own.

How to test

  1. Configure an LDAP group with two users, log in once with both (or run occ ldap:check-group <gid> --update).
  2. Remove the group in LDAP, or exclude it via the group filter.
  3. Run the OCA\User_LDAP\Jobs\UpdateGroups job, e.g. occ background-job:execute --force-execute <id>.
  4. Before: no "removed from group" activity / admin_audit entries. After: one entry per former member.

TODO

  • Verify on a live instance with an LDAP server

Checklist

AI (if applicable)

  • The content of this PR was partly or fully generated using AI

Assisted by ClaudeCode:claude-opus-5-5

🤖 Generated with Claude Code

When a group disappeared from LDAP, the cached memberships were deleted
before they were read to dispatch the events, so no UserRemovedEvent was
ever sent. Listeners like the share manager, the external storage mount
cache or the activity app therefore never learned that the members lost
the group.

The group manager also usually cannot resolve such a group anymore, as
the LDAP backend no longer reports it, so fall back to a backend-less
group object carrying the group id.

The test also calls the parent setUp, without which the test class fails
when run on its own.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Sending UserRemovedEvent for removed groups lets listeners clean up,
some of them irreversibly (e.g. share cleanup). Therefore:

- Do not treat all known groups as removed when LDAP returns no groups
  at all, which usually means a broken group configuration.
- Skip users that are still in a group with the same id provided by
  another backend.
- Handle one group at a time and delete its memberships only after the
  events were sent, so a failing listener does not lose the events of
  the remaining members and memory stays bounded.
- Apply the same group fallback on login, where the membership was
  removed but the event was dropped for groups no longer in LDAP.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Being absent from the group list does not prove that a group is gone,
e.g. when one of several configurations returns incomplete results.
Removal events trigger irreversible cleanup in listeners, so verify the
group against LDAP without cache first and keep the memberships of
groups that still exist. This makes the stricter empty-result guard
unnecessary, which also blocked the cleanup when the last group was
removed on purpose.

The check for a same-named group from another backend now only asks the
non-LDAP backends, because the cached LDAP membership of a just removed
group can be stale and would suppress the event for good. It is shared
by all places that dispatch removal events, including the login path
and members leaving a group that still exists.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Groups that still exist on LDAP are now kept, so listing every
candidate as removed would make the log misleading.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
@susnux
susnux marked this pull request as ready for review December 2, 2026 11:32
@susnux
susnux requested a review from a team as a code owner December 2, 2026 11:32
@susnux
susnux requested review from a team, Altahrim and come-nc and removed request for a team December 2, 2026 11:32

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants