Commit 35d2f822 authored by Iñaki Arenaza's avatar Iñaki Arenaza
Browse files

Add support to map Student ID and Display Name from LDAP server (bug #906996)



Change-Id: I2924716222817fc60e2358354866c591900fd03d
Signed-off-by: default avatarIñaki Arenaza <iarenaza@mondragon.edu>
parent c42f1e66
......@@ -641,7 +641,7 @@ class ArtefactTypeEmail extends ArtefactTypeProfileField {
}
}
class ArtefactTypeStudentid extends ArtefactTypeProfileField {}
class ArtefactTypeStudentid extends ArtefactTypeCachedProfileField {}
class ArtefactTypeIntroduction extends ArtefactTypeProfileField {
public function render_self($options) {
return array('html' => clean_html($this->title), 'javascript' => null);
......
......@@ -34,9 +34,11 @@ $string['notusable'] = 'Please install the PHP LDAP extension';
$string['contexts'] = 'Contexts';
$string['distinguishedname'] = 'Distinguished name';
$string['hosturl'] = 'Host URL';
$string['ldapfieldforpreferredname'] = 'LDAP field for Display Name';
$string['ldapfieldforemail'] = 'LDAP field for Email';
$string['ldapfieldforfirstname'] = 'LDAP field for First Name';
$string['ldapfieldforsurname'] = 'LDAP field for Surname';
$string['ldapfieldforstudentid'] = 'LDAP field for Student ID';
$string['ldapversion'] = 'LDAP version';
$string['starttls'] = 'TLS encryption';
$string['password'] = 'Password';
......
<h3>LDAP field for Display Name</h3>
<p>Enter the name of the field in the LDAP record
that contains the user's Display Name</p>
<h3>LDAP field for Student ID</h3>
<p>Enter the name of the field in the LDAP record
that contains the user's Student ID</p>
......@@ -52,6 +52,8 @@ class AuthLdap extends Auth {
$this->config['firstnamefield' ] = '';
$this->config['surnamefield'] = '';
$this->config['emailfield'] = '';
$this->config['studentidfield'] = '';
$this->config['preferrednamefield'] = '';
if (!empty($id)) {
return $this->init($id);
......@@ -123,14 +125,16 @@ class AuthLdap extends Auth {
// Define ldap attributes
$ldapattributes = array();
$ldapattributes['firstname'] = $this->config['firstnamefield'];
$ldapattributes['lastname'] = $this->config['surnamefield' ];
$ldapattributes['email'] = $this->config['emailfield' ];
$ldapattributes['lastname'] = $this->config['surnamefield'];
$ldapattributes['email'] = $this->config['emailfield'];
$ldapattributes['studentid'] = $this->config['studentidfield'];
$ldapattributes['preferredname'] = $this->config['preferrednamefield'];
// Retrieve information of user from LDAP
$ldapdetails = $this->get_userinfo_ldap($username, $ldapattributes);
// Match database and ldap entries and update in database if required
$fieldstoimport = array('firstname', 'lastname', 'email');
$fieldstoimport = array_keys($ldapattributes);
foreach ($fieldstoimport as $field) {
$sanitizer = "sanitize_$field";
$ldapdetails[$field] = $sanitizer($ldapdetails[$field]);
......@@ -306,8 +310,10 @@ class AuthLdap extends Auth {
// get the attribute field names
$attributes = array();
$attributes['firstname'] = $this->config['firstnamefield'];
$attributes['lastname'] = $this->config['surnamefield' ];
$attributes['email'] = $this->config['emailfield'];
$attributes['lastname'] = $this->config['surnamefield' ];
$attributes['email'] = $this->config['emailfield'];
$attributes['studentid'] = $this->config['studentidfield'];
$attributes['preferredname'] = $this->config['preferrednamefield'];
$userinfo = $this->get_userinfo_ldap($username, $attributes);
......@@ -432,7 +438,9 @@ class PluginAuthLdap extends PluginAuth {
'weautocreateusers' => 1,
'firstnamefield' => '',
'surnamefield' => '',
'emailfield' => ''
'emailfield' => '',
'studentidfield' => '',
'preferrednamefield' => '',
);
public static function has_config() {
......@@ -612,6 +620,18 @@ class PluginAuthLdap extends PluginAuth {
'defaultvalue' => self::$default_config['emailfield'],
'help' => true,
),
'studentidfield' => array(
'type' => 'text',
'title' => get_string('ldapfieldforstudentid', 'auth.ldap'),
'defaultvalue' => self::$default_config['studentidfield'],
'help' => true,
),
'preferrednamefield' => array(
'type' => 'text',
'title' => get_string('ldapfieldforpreferredname', 'auth.ldap'),
'defaultvalue' => self::$default_config['preferrednamefield'],
'help' => true,
),
);
return array(
......@@ -673,7 +693,9 @@ class PluginAuthLdap extends PluginAuth {
'weautocreateusers' => $values['weautocreateusers'],
'firstnamefield' => $values['firstnamefield'],
'surnamefield' => $values['surnamefield'],
'emailfield' => $values['emailfield']
'emailfield' => $values['emailfield'],
'studentidfield' => $values['studentidfield'],
'preferrednamefield' => $values['preferrednamefield'],
);
foreach(self::$default_config as $field => $value) {
......
......@@ -1244,6 +1244,12 @@ function login_submit(Pieform $form, $values) {
// The user will be asked to populate this when they log in.
$USER->email = null;
}
if (isset($userdata->studentid)) {
$USER->studentid = sanitize_studentid($userdata->studentid);
}
if (isset($userdata->preferredname)) {
$USER->preferredname = sanitize_preferredname($userdata->preferredname);
}
try {
// If this authinstance is a parent auth for some xmlrpc authinstance, pass it along to create_user
// so that this username also gets recorded as the username for sso from the remote sites.
......
......@@ -2919,3 +2919,17 @@ function sanitize_lastname($value) {
}
return $value;
}
function sanitize_studentid($value) {
if (!preg_match('/\S/', $value)) {
return '';
}
return $value;
}
function sanitize_preferredname($value) {
if (!preg_match('/\S/', $value)) {
return '';
}
return $value;
}
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