Commit d1310840 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Check for authinstances in use when deleting institutions (bug #917873)



When you delete an institution, and one of the institution's
authinstances is still in use by some users, you get an sql error and
a nonrecoverable error screen. (This can only happen when the users
still using the authinstance are not institution members, because
there is already a check to stop deletion of institutions with
members).

This patch allows the institution to be deleted anyway, reassigning
the offending users to a 'default' authinstance.  If we can't find a
sensible default authinstance, it will at least display a more helpful
error message.

Also, when attempting to delete an institution with members, set an
error on the form rather than throwing a ConfigException.

Change-Id: I9c85a3c8c80411e3cf526767b9142efb12907e5c
Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent 52fe3992
......@@ -82,8 +82,28 @@ if ($institution || $add) {
if ($delete) {
function delete_validate(Pieform $form, $values) {
if (get_field('usr_institution', 'COUNT(*)', 'institution', $values['i'])) {
throw new ConfigException('Attempt to delete an institution that has members');
// Ensure the institution has no members left
if ($members = get_field('usr_institution', 'COUNT(*)', 'institution', $values['i'])) {
$form->set_error('submit', get_string('institutionstillhas', 'admin', get_string('nmembers', 'group', $members)));
}
// If some users are still using one of this institution's authinstances, it's okay if
// we can find a default authinstance for those users, otherwise it's an error.
if ($authinstanceids = get_column('auth_instance', 'id', 'institution', $values['i'])) {
$badusers = count_records_select(
'usr',
'authinstance IN (' . join(',', array_fill(0, count($authinstanceids), '?')) . ')',
$authinstanceids
);
if ($badusers) {
$defaultauth = record_exists('auth_instance', 'institution', 'mahara', 'authname', 'internal');
if ($values['i'] == 'mahara' || !$defaultauth) {
$form->set_error(
'submit',
get_string('institutionauthinuseby', 'admin', get_string('nusers', 'mahara', $badusers))
);
}
}
}
}
......@@ -117,6 +137,19 @@ if ($institution || $add) {
}
}
}
// If any users are still using this institution's authinstances, change them now.
if ($authinstanceids) {
execute_sql("
UPDATE {usr}
SET authinstance = (
SELECT MIN(id) FROM {auth_instance} WHERE institution = 'mahara' AND authname = 'internal'
)
WHERE authinstance IN (" . join(',', array_fill(0, count($authinstanceids), '?')) . ')',
$authinstanceids
);
}
foreach ($authinstanceids as $id) {
delete_records('auth_instance_config', 'instance', $id);
}
......
......@@ -683,6 +683,8 @@ $string['addinstitution'] = 'Add Institution';
$string['authplugin'] = 'Authentication plugin';
$string['deleteinstitution'] = 'Delete Institution';
$string['deleteinstitutionconfirm'] = 'Are you really sure you wish to delete this institution?';
$string['institutionstillhas'] = 'This institution still has %s';
$string['institutionauthinuseby'] = "This institution's authentication is still in use by %s";
$string['institutiononly'] = 'Institution only';
$string['institutionaddedsuccessfully2'] = 'Institution added successfully';
$string['institutiondeletedsuccessfully'] = 'Institution deleted successfully';
......
......@@ -130,6 +130,10 @@ $string['invites'] = "Invites";
$string['member'] = 'member';
$string['members'] = 'members';
$string['Members'] = 'Members';
$string['nmembers'] = array(
'1 member',
'%s members',
);
$string['memberrequests'] = 'Membership requests';
$string['declinerequest'] = 'Decline request';
......
......@@ -951,6 +951,10 @@ $string['Visits'] = 'Visits';
$string['after'] = 'after';
$string['before'] = 'before';
$string['Visibility'] = 'Visibility';
$string['nusers'] = array(
'1 user',
'%s users',
);
// import related strings (maybe separated later)
$string['importedfrom'] = 'Imported from %s';
......
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