Skip to content

Fix stale Entra ID object ID not being repaired during user sync - #3360

Open
Lai Wei (weilai-irl) wants to merge 1 commit into
MOODLE_500_STABLEfrom
wip-135632-m500
Open

Fix stale Entra ID object ID not being repaired during user sync#3360
Lai Wei (weilai-irl) wants to merge 1 commit into
MOODLE_500_STABLEfrom
wip-135632-m500

Conversation

@weilai-irl

Copy link
Copy Markdown
Collaborator

The N+1-avoidance refactor in usersync main.php replaced a direct per-user local_o365_objects lookup with a cache keyed by objectid, which only ever contains rows whose stored objectid is still active in Entra ID. This meant the object ID repair logic could never fire for users whose linked Entra account was deleted and recreated with a new object ID, leaving them permanently mismatched. The daily enabled-status sync task then kept re-suspending these accounts even after being manually re-enabled, since it also trusts the stale objectid.

Fix by selecting the currently stored objectid alongside the existing bulk user query (no extra query) and comparing it in memory, only issuing an UPDATE when it has actually changed.

Copilot AI lite review requested due to automatic review settings August 6, 2026 11:48
@weilai-irl Lai Wei (weilai-irl) self-assigned this Aug 6, 2026
@weilai-irl Lai Wei (weilai-irl) added this to the 2026-03 milestone Aug 6, 2026
@weilai-irl Lai Wei (weilai-irl) linked an issue Aug 6, 2026 that may be closed by this pull request

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a regression in the Entra ID user sync flow where stale local_o365_objects.objectid values could no longer be detected and repaired after an Entra account was deleted/recreated, which in turn caused downstream enabled-status sync to keep re-suspending affected users.

Changes:

  • Extends the existing bulk “existing users” SQL queries to also select the currently stored Entra object GUID (obj.objectid AS o365objectid) alongside the local record id (obj.id AS objectid).
  • Updates the object ID repair logic in sync_existing_user() to compare the stored GUID from the bulk query and issue a targeted update only when the GUID has actually changed.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread local/o365/classes/feature/usersync/main.php
The N+1-avoidance refactor in usersync main.php replaced a direct per-user local_o365_objects lookup with a cache keyed by objectid, which only ever contains rows whose stored objectid is still active in Entra ID. This meant the object ID repair logic could never fire for users whose linked Entra account was deleted and recreated with a new object ID, leaving them permanently mismatched. The daily enabled-status sync task then kept re-suspending these accounts even after being manually re-enabled, since it also trusts the stale objectid.

Fix by selecting the currently stored objectid alongside the existing bulk user query (no extra query) and comparing it in memory, only issuing an UPDATE when it has actually changed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Moodle accounts connected to deleted Entra user objects

2 participants