Commit 63b373c3 authored by Richard Mansfield's avatar Richard Mansfield

Allow CSV upload by institutional admins

parent e1087140
......@@ -25,7 +25,7 @@
*/
define('INTERNAL', 1);
define('ADMIN', 1);
define('INSTITUTIONALADMIN', 1);
define('MENUITEM', 'configusers/uploadcsv');
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
define('TITLE', get_string('uploadcsv', 'admin'));
......@@ -64,17 +64,29 @@ $ALLOWEDKEYS = array(
'jabberusername',
'occupation',
'industry',
'institution',
'authinstance'
);
global $USER;
if ($USER->get('admin')) {
$authinstances = auth_get_auth_instances();
} else {
$admininstitutions = $USER->get('admininstitutions');
$authinstances = auth_get_auth_instances_for_institutions($admininstitutions);
if (empty($authinstances)) {
$SESSION->add_info_msg(get_string('uploadcsvconfigureauthplugin', 'admin'));
redirect(get_config('wwwroot').'admin/users/institutions.php?i='.key($admininstitutions).'&edit=1');
}
}
$authinstances = auth_get_auth_instances();
if (count($authinstances) > 1) {
$options = array();
foreach ($authinstances as $authinstance) {
$options[$authinstance->id .'_'. $authinstance->name] = $authinstance->displayname. ': '.$authinstance->instancename;
if ($USER->get('admin') || $USER->is_institutional_admin($authinstance->name)) {
$options[$authinstance->id .'_'. $authinstance->name] = $authinstance->displayname. ': '.$authinstance->instancename;
}
}
$default = key($options);
......@@ -125,7 +137,7 @@ $form = array(
* @param array $values The values submitted
*/
function uploadcsv_validate(Pieform $form, $values) {
global $CSVDATA, $ALLOWEDKEYS, $FORMAT;
global $CSVDATA, $ALLOWEDKEYS, $FORMAT, $USER;
// Don't even start attempting to parse if there are previous errors
if ($form->has_errors()) {
......@@ -144,6 +156,10 @@ function uploadcsv_validate(Pieform $form, $values) {
$break = strpos($values['authinstance'], '_');
$authinstance = substr($values['authinstance'], 0, $break);
$institution = substr($values['authinstance'], $break+1);
if (!$USER->get('admin') && !$USER->is_institutional_admin($institution)) {
$form->set_error('authinstance', get_string('notadminforinstitution', 'admin'));
return;
}
$conf = File_CSV::discoverFormat($values['file']['tmp_name']);
$i = 0;
......
......@@ -436,6 +436,34 @@ function auth_get_auth_instances() {
}
/**
*
* Given a list of institutions, returns all auth instances associated with them
*
* @return array Array of auth instance records
*/
function auth_get_auth_instances_for_institutions($institutions) {
if (empty($institutions)) {
return array();
}
$sql ='
SELECT DISTINCT
i.id,
inst.name,
inst.displayname,
i.instancename
FROM
{institution} inst,
{auth_instance} i
WHERE
i.institution = inst.name AND
inst.name IN (' . join(',', array_map('db_quote',$institutions)) . ')
ORDER BY
inst.displayname,
i.instancename';
return get_records_sql_array($sql, array());
}
/**
......
......@@ -182,7 +182,8 @@ $string['uploadcopyright'] = 'Upload Copyright Statement';
// Upload CSV
$string['csvfile'] = 'CSV File';
$string['uploadcsvinstitution'] = 'The institution to upload users for';
$string['uploadcsvinstitution'] = 'The institution and authentication method for the new users';
$string['uploadcsvconfigureauthplugin'] = 'You must configure an authentication plugin before you can add users by CSV';
$string['csvfiledescription'] = 'The file containing users to add';
$string['uploadcsverrorinvalidfieldname'] = 'The field name "%s" is invalid';
$string['uploadcsverrorrequiredfieldnotspecified'] = 'A required field "%s" has not been specified in the format line';
......@@ -284,6 +285,7 @@ $string['institutionadmin'] = 'Institution Admin';
$string['settingsfor'] = 'Settings for:';
$string['institutionadministration'] = 'Institution Administration';
$string['institutionmembers'] = 'Institution Members';
$string['notadminforinstitution'] = 'You are not an administrator for that institution';
// general stuff
......
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