Commit 97447a21 authored by Robert Lyon's avatar Robert Lyon
Browse files

Bug 1989388: Allow 'lis_person_sourcedid' to be recorded as a remoteusername



And record it as remoteusername to the parentauth
This is because moodle can send the moodle ID as remoteauth name not
the username from the remote IdP that ties everything together

Also tidy up LTI_Advantage to be like LTI
- See Bug 1943772, commit ec27a6d7

Change-Id: Icbc3bc4511d9cb3b1fb12103f76f5d67539224e3
Signed-off-by: default avatarRobert Lyon <robertl@catalyst.net.nz>
parent b69b9602
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -3674,3 +3674,25 @@ function isolatedinstitution_access($userid, $currentuserid = null) {
    }
    return true;
}

/**
 * Add a remoteusername to a user
 *
 * param string $userid        ID of the person
 * param string $authinstance  ID of the auth instance
 * param string $remotename    The remote user name
 * @return boolean
 */
function user_add_remote($userid, $authinstance, $remotename) {
    return ensure_record_exists('auth_remote_user',
        (object) array(
            'authinstance' => $authinstance,
            'remoteusername' => $remotename
        ),
        (object) array(
            'authinstance' => $authinstance,
            'remoteusername' => $remotename,
            'localusr' => $userid
        )
    );
}
+7 −6
Original line number Diff line number Diff line
@@ -196,12 +196,13 @@ class module_lti_launch extends external_api {
                    // so we need to make the parent auth_remote_user row first via create_user()
                    $userid = create_user($user, array(), $WEBSERVICE_INSTITUTION, $needremote, $remotevalue);
                    // Then add the auth_remote_user row for this auth method second
                    $authremoteuser = new stdClass();
                    $authremoteuser->authinstance = $authinstanceid;
                    $authremoteuser->remoteusername = $params['user_id'];
                    $authremoteuser->localusr = $user->id;

                    insert_record('auth_remote_user', $authremoteuser);
                    user_add_remote($user->id, $authinstanceid, $params['user_id']);
                    // Then add the auth_remote_user row if lis_person_sourcedid exists against the parent auth
                    // so that we end up with 2 options for parent auth as Moodle can send the correct value for
                    // the parent auth on this parameter.
                    if (!empty($params['lis_person_sourcedid'])) {
                        user_add_remote($user->id, $parentauthid, $params['lis_person_sourcedid']);
                    }
                }
                else {
                    $userid = create_user($user, array(), $WEBSERVICE_INSTITUTION, true, $params['user_id']);
+19 −10
Original line number Diff line number Diff line
@@ -881,19 +881,28 @@ class PluginModuleLti_advantage extends PluginModule {
                    throw new WebserviceInvalidParameterException(get_string('usernameexists2', 'module.lti', $user->username));
                }

                if ($parentauthid) {
                    $authinstance = AuthFactory::create($parentauthid);
                    $needremote = $authinstance->needs_remote_username();
                    $remotevalue = $authinstance->needs_remote_username() ? $user->username : null;
                    // We are creating the user with the parent authentication id as the one to save in the usr table
                    // so we need to make the parent auth_remote_user row first via create_user()
                    $userid = create_user($user, array(), $institution, $needremote, $remotevalue);
                    // Then add the auth_remote_user row for this auth method second
                    user_add_remote($user->id, $authinstanceid, $params['user_id']);
                    // Then add the auth_remote_user row if lis_person_sourcedid exists against the parent auth
                    // so that we end up with 2 options for parent auth as Moodle can send the correct value for
                    // the parent auth on this parameter.
                    if (!empty($params['lis_person_sourcedid'])) {
                        user_add_remote($user->id, $parentauthid, $params['lis_person_sourcedid']);
                    }
                }
                else {
                    $userid = create_user($user, array(), $institution, true, $params['user_id']);
                }

                $updateremote = false;
                $updateuser = false;

                if ($parentauthid) {
                    $authremoteuser = new stdClass();
                    $authremoteuser->authinstance = $parentauthid;
                    $authremoteuser->remoteusername = $user->username;
                    $authremoteuser->localusr = $user->id;

                    insert_record('auth_remote_user', $authremoteuser);
                }
            }
            else {
                $USER->logout();