Warning: foreach() argument must be of type array|object, null given in Drupal\openid_connect\OpenIDConnect->saveUserinfo()

Created on 4 January 2024, 12 months ago

Problem/Motivation

When I log in to Drupal with Windows Azure AD, I get the following warning after successful login:
Warning: foreach() argument must be of type array|object, null given in Drupal\openid_connect\OpenIDConnect->saveUserinfo() (line 692 of modules/contrib/openid_connect/src/OpenIDConnect.php).

Here the loop tries to iterate on an object / array of the loaded variable without first checking, which has also already been integrated in the patch " https://www.drupal.org/project/openid_connect/issues/3365465 ๐Ÿ› Several warning and deprecated functions for PHP > 8.0 Needs review (WARNING-2)". If the variable is null, this iteration aborts and throws the warning message above.

$role_mappings = $this->configFactory->get('openid_connect.settings')->get('role_mappings');
foreach ($role_mappings as $role => $mappings) {
    ...
 }  

Steps to reproduce

- Login with Azure AD into Drupal (OpenID Connect Microsoft Azure Active Directory client)

Proposed resolution

I would also add the line from the patch " https://www.drupal.org/project/openid_connect/issues/3365465 ๐Ÿ› Several warning and deprecated functions for PHP > 8.0 Needs review (WARNING-2)" here so that the warning message disappears and a safe type check takes place.

$role_mappings = $this->configFactory->get('openid_connect.settings')->get('role_mappings');
if (is_array($role_mappings) || is_object($role_mappings)) {
    foreach ($role_mappings as $role => $mappings) {
        ...
    }  
}

I think it is also possible that we can use an empty array as a fallback:

$role_mappings = $this->configFactory->get('openid_connect.settings')->get('role_mappings') ?? [];
foreach ($role_mappings as $role => $mappings) {
    ...
}  
๐Ÿ› Bug report
Status

Closed: duplicate

Version

3.0

Component

Code

Created by

๐Ÿ‡ฉ๐Ÿ‡ชGermany PLK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024