Commit a00ddfe1 authored by Cecilia Vela Gurovic's avatar Cecilia Vela Gurovic Committed by Robert Lyon
Browse files

Bug 1896564: need to tidy up the user auth instance saving

When a user is switch from 'No institution' to an institution,
the auth instance needs to be updated in the usr table
to be linked to an authentication method from this new institution

Also we should allocate the person with the same type of auth method
if possible, otherwise allocate the top priority auth method

behatnotneeded

Change-Id: I9431c1a5c3f6e6b44972ac14ad644abcb076023e
parent f1f4b84c
......@@ -189,24 +189,33 @@ $externalauthjs = array();
// Get a list of the user's institutions
$user_insts = $user->institutions;
// Confirm that the auth method is valid.
$valid_auth = false;
foreach ($user_insts as $inst) {
if (record_exists('auth_instance', 'id', $user->authinstance , 'institution', $inst->institution)) {
$valid_auth = true;
if ($user_insts) {
$valid_auth = false;
$institutionnames = array();
foreach ($user_insts as $inst) {
if (record_exists('auth_instance', 'id', $user->authinstance , 'institution', $inst->institution)) {
$valid_auth = true;
}
$institutionnames[] = $inst->institution;
}
// If their auth method doesn't work for any of their insts, change it to internal.
if (!$valid_auth) {
// If their auth method doesn't work for any of their insts, change it to internal.
$internal = get_field('auth_instance', 'id', 'authname', 'internal', 'institution', $inst->institution);
if (!$internal) {
// Institution has no internal auth instance. Create one.
// get all internal auth instances from institution the user belongs to
$select = 'authname = \'internal\' AND institution IN (' . join(',', array_fill(0, count($institutionnames), '?')) . ')';
if ($authinternal = get_records_select_array('auth_instance', $select, $institutionnames, '', 'id', '', '1')) {
$internal = $authinternal[0]->id;
}
else {
// There is no institution that has an internal auth instance. Create one.
$institution = array_keys($user_insts)[0];
$todb = new stdClass();
$todb->instancename = 'internal';
$todb->authname = 'internal';
$todb->active = 1;
$todb->institution = $inst->institution;
$max = get_field('auth_instance', 'MAX(priority)', 'institution', $inst->institution);
$todb->institution = $institution;
$max = get_field('auth_instance', 'MAX(priority)', 'institution', $institution);
$todb->priority = $max ? $max + 1 : 0;
$internal = insert_record('auth_instance', $todb, 'id', true);
}
......@@ -215,15 +224,14 @@ foreach ($user_insts as $inst) {
$user->commit();
}
}
$authinstances = auth_get_auth_instances();
// If the user has no institution, their inst is mahara
if (!$user_insts) {
else {
// If the user has no institution, their inst is mahara
$mahara = new stdClass();
$mahara->institution = "mahara";
$user_insts[] = $mahara;
}
$authinstances = auth_get_auth_instances();
// Now add the valid auth methods for institutions the user is in to the page.
foreach ($authinstances as $authinstance) {
foreach ($user_insts as $inst) {
......
......@@ -302,6 +302,23 @@ class Institution {
$user = get_record('usr', 'id', $user);
}
// comes from 'No institution', we need to set the auth instance in usr table
$userobj = new User();
$userobj->find_by_id($user->id);
$institutions = $userobj->get('institutions');
if (empty($institutions)) {
$oldauthmethod = get_field('auth_instance', 'authname', 'id', $userobj->get('authinstance'));
// Return auth methods with the most relevant one first
if ($authinstances = get_records_sql_array(
"SELECT * FROM {auth_instance}
WHERE institution = ? AND active = 1
ORDER BY CASE WHEN authname = ? THEN 0 ELSE 1 END, priority",
array($this->name, $oldauthmethod))) {
$user->authinstance = $authinstances[0]->id;
update_record('usr', array('id' => $user->id, 'authinstance' => $user->authinstance));
}
}
$lang = get_account_preference($user->id, 'lang');
if ($lang == 'default') {
// The user does not have a preset lang preference so we will use the institution if it has one.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment