Skip to content

Fix OIDC login for manually matched users with mismatching Entra username - #3366

Open
Lai Wei (weilai-irl) wants to merge 1 commit into
MOODLE_502_STABLEfrom
wip-135651-m502
Open

Fix OIDC login for manually matched users with mismatching Entra username#3366
Lai Wei (weilai-irl) wants to merge 1 commit into
MOODLE_502_STABLEfrom
wip-135651-m502

Conversation

@weilai-irl

Copy link
Copy Markdown
Collaborator

Manually matched users are keyed by Entra UPN, which often differs from the Moodle username. handlelogin() found the matched user but then discarded it, continuing with the UPN as the username and failing with "User not found in Moodle." Now it binds to the matched user's actual username instead. Also normalizes entraidupn casing on write and read to avoid case-mismatch failures.

Copilot AI lite review requested due to automatic review settings August 6, 2026 15:15
@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
@weilai-irl Lai Wei (weilai-irl) changed the title fix OIDC login for manually matched users with mismatching Entra username Fix OIDC login for manually matched users with mismatching Entra username Aug 6, 2026

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 OIDC auth-code login when a user has been manually matched via local_o365_connections using their Entra UPN but their Moodle username differs (e.g., UPN vs UPN prefix), and adds casing normalization to reduce case-mismatch failures.

Changes:

  • Normalize manually-entered Entra UPN to lowercase during “Manual match” in the O365 ACP UI.
  • In OIDC auth-code login, when a manual match exists and the matched Moodle user is already auth=oidc, bind the login to the matched Moodle username (not the Microsoft-derived username).
  • Add a PHPUnit regression test covering the manually-matched differing-username login path.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
local/o365/classes/page/acp.php Lowercases manually entered Entra UPN during manual match flow.
auth/oidc/classes/loginflow/authcode.php Ensures matched OIDC user login binds to the Moodle username; lowercases Entra UPN input when checking for manual matches.
auth/oidc/tests/loginflow/authcode_test.php Adds regression test for manually matched user whose Moodle username differs from their Entra UPN.
Suppressed comments (1)

local/o365/classes/page/acp.php:2085

  • This existing-match check is still case-sensitive on the DB column. On case-sensitive DBs, an existing entraidupn stored with different casing won’t be detected, and the insert can create a second connection row differing only by case (unique index won’t prevent it). Use a case-insensitive comparison (e.g. LOWER(entraidupn) = ?) for the lookup.
            $o365username = core_text::strtolower(trim($fromform->o365username));

            // Check existing matches for Microsoft user.
            $existingmatchforo365user = $DB->get_record('local_o365_connections', ['entraidupn' => $o365username]);
            if (!empty($existingmatchforo365user)) {

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

Comment thread auth/oidc/classes/loginflow/authcode.php
Comment thread auth/oidc/tests/loginflow/authcode_test.php Outdated
…name

Manually matched users are keyed by Entra UPN, which often differs from the Moodle username. handlelogin() found the matched user but then discarded it, continuing with the UPN as the username and failing with "User not found in Moodle." Now it binds to the matched user's actual username instead. Also normalizes entraidupn casing on write and read to avoid case-mismatch failures.
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.

User Account Matching and Completion Authentication

2 participants