microsoft / o365-moodle

Office 365 and Azure Active Directory plugins for Moodle
GNU General Public License v3.0
182 stars 137 forks source link

Upgrade crash in version 2022112805 "Update systemtokens config" #2280

Closed tim1mw closed 1 year ago

tim1mw commented 1 year ago

I'm getting the following error installing release 2022112805 on Moodle 4.1.1/PHP 8.1:

Exception - array_key_exists(): Argument #2 ($array) must be of type array, string given

line 733 of /local/o365/db/upgrade.php: TypeError thrown
line 784 of /lib/upgradelib.php: call to xmldb_local_o365_upgrade()
line 1953 of /lib/upgradelib.php: call to upgrade_plugins()
line 721 of /admin/index.php: call to upgrade_noncore()

The problem is that this code:

    // Update systemtokens config.
    $systemtokensconfig = get_config('local_o365', 'systemtokens');
    if ($systemtokensconfig !== false) {
        $systemtokensconfig = unserialize($systemtokensconfig);
        foreach ($systemtokensconfig as $resource => $tokenconfig) {
            if (array_key_exists('resource', $tokenconfig)) {
                $systemtokensconfig[$resource]['tokenresource'] = $tokenconfig['resource'];
                unset($systemtokensconfig[$resource]['resource']);
            }
        }
        $systemtokensconfig = serialize($systemtokensconfig);
        set_config('systemtokens', $systemtokensconfig, 'local_o365');
    }

Expects each element of $systemtokensconfig to always be an array, however when I dump this variable out on my Moodle I get:

Array ( [idtoken] => [https://graph.windows.net] => Array ( [scope] => Calendars.ReadWrite Directory.AccessAsUser.All Directory.ReadWrite.All Files.ReadWrite Group.ReadWrite.All Notes.ReadWrite.All openid Sites.Read.All User.Read.All User.ReadWrite.All [token] => [expiry] => 1677305777 [refreshtoken] => [resource] => https://graph.windows.net )

[https://graph.microsoft.com] => Array
    (
        [scope] => Calendars.ReadWrite Directory.AccessAsUser.All Directory.ReadWrite.All Files.ReadWrite Group.ReadWrite.All Notes.ReadWrite.All openid Sites.Read.All User.Read.All User.ReadWrite.All
        [token] => **<string redacted>**
        [expiry] => 1677478323
        [refreshtoken] => **<string redacted>**
        [resource] => https://graph.microsoft.com
    )

)

So the first element of $systemtokensconfig is a string, not an array triggering the crash. I've fixed this on my Moodle by amending the code as shown below:

    // Update systemtokens config.
    $systemtokensconfig = get_config('local_o365', 'systemtokens');
    if ($systemtokensconfig !== false) {
        $systemtokensconfig = unserialize($systemtokensconfig);
        foreach ($systemtokensconfig as $resource => $tokenconfig) {
            // Make sure this is an array
               if (!is_array($tokenconfig)) {
                continue;
            }
            if (array_key_exists('resource', $tokenconfig)) {
                $systemtokensconfig[$resource]['tokenresource'] = $tokenconfig['resource'];
                unset($systemtokensconfig[$resource]['resource']);
            }
        }
        $systemtokensconfig = serialize($systemtokensconfig);
        set_config('systemtokens', $systemtokensconfig, 'local_o365');
    }
anton-catalyst-eu commented 1 year ago

Good morning! Any chance this can be looked into please? The issue has been open for a while and is quite a nuisance as it breaks upgrades on PHP 8. Thank you in advance!

y00see commented 1 year ago

`Exception - array_key_exists(): Argument #2 ($array) must be of type array, string given

More information about this error Debug info: Error code: generalexceptionmessage Stack trace:

line 726 of /local/o365/db/upgrade.php: TypeError thrown
line 794 of /lib/upgradelib.php: call to xmldb_local_o365_upgrade()
line 1974 of /lib/upgradelib.php: call to upgrade_plugins()
line 726 of /admin/index.php: call to upgrade_noncore()`

Same error, breaks indeed the plugin upgrade and have to rollback the database to get it working again.

weilai-irl commented 1 year ago

Hi @tim1mw @anton-catalyst-eu and @y00see

A fix to the issue has been provided in the latest release from today (4.0.5, 4.1.3 and 4.2.2). Please check it out.

Regards, Lai