Problem/Motivation

When users are first logging into o365, we create a user and populate the username field using the displayName field:
// If we haven't found an account to link, create one from the data.
if (!$account) {
$accountData = [
'name' => $userData['displayName'],
'mail' => $userData['userPrincipalName'],
];

The issue is that in our case, the displayName contains commas and parentheses which are illegal characters in username. The system allows it, but when administrators try to edit the user (to add roles for example), the system prevents them from doing it without forcing them to change the username.

This code is similarly executed in:
UserLoginController.php
SsoUserSync.php

Steps to reproduce

Have users log into o365 for the first time to have Drupal automatically create a new user based on the displayName. Make sure that displayName contains illegal username characters. See that username is set using illegal characters.

Proposed resolution

Possible solutions:
1. Cleanse the value before we set it to strip out any illegal characters
2. Create configurations to use different fields or a combination of fields to create the username (such as surname and givenName) to override the default (while still cleansing before saving)

Would like to get community feedback and/or recommendations before implementing.

Comments

strinh created an issue. See original summary.

smulvih2’s picture

Category: Support request » Bug report
Status: Active » Needs review
StatusFileSize
new1.22 KB

The patch attached adds a function to sanitize the username before user creation.

strinh’s picture

Status: Needs review » Fixed

Confirmed patch correctly cleanses the displayName with only valid characters to be saved in username. Will likely need similar code in SsoUserSync.php should users be synching other fields from GraphAPI.

smulvih2’s picture

Status: Fixed » Reviewed & tested by the community

@strinh thanks for the review! Setting this to RTBC so maintainer can have a look.

fabianderijk’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for the work. This is now added in the dev version of the module.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.