Skip to content

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

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

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

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:47
@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

Fixes a regression in the user sync flow where stale Entra ID object IDs could no longer be detected/repaired after an N+1-avoidance refactor, which in turn caused downstream tasks (like enabled-status sync) to keep acting on mismatched/stale identities.

Changes:

  • Extend the bulk “existing user” SQL projections to also fetch the stored local_o365_objects.objectid GUID alongside the local_o365_objects.id.
  • Update sync_existing_user() to compare the stored GUID with the current Entra ID GUID and update local_o365_objects.objectid when it differs.

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

Comment thread local/o365/classes/feature/usersync/main.php Outdated
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