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.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | o365-sso-sanitize-username-3285277-2.patch | 1.22 KB | smulvih2 |
Comments
Comment #2
smulvih2The patch attached adds a function to sanitize the username before user creation.
Comment #3
strinh commentedConfirmed 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.
Comment #4
smulvih2@strinh thanks for the review! Setting this to RTBC so maintainer can have a look.
Comment #6
fabianderijkThanks for the work. This is now added in the dev version of the module.