From f04888e2d615a2b03a387704909b0fe316fbe9fc Mon Sep 17 00:00:00 2001 From: Jouni Suorsa Date: Fri, 16 Sep 2016 09:52:55 +0300 Subject: [PATCH 1/3] Use moodle's jquery and jquery-ui. Fixed some notices and modified whitespaces. --- auth.php | 49 ++++++++++++++++++++++++++++++++----------------- config.php | 34 +++++++++++++++------------------- courses.php | 24 ++++++++++++------------ roles.php | 10 +++++----- 4 files changed, 64 insertions(+), 53 deletions(-) diff --git a/auth.php b/auth.php index d1a8df3..3d48284 100755 --- a/auth.php +++ b/auth.php @@ -171,7 +171,7 @@ function config_form($config, $err, $user_fields) { $dbman = $DB->get_manager(); $table_course_mapping = $this->get_course_mapping_xmldb(); - $table_role_mapping = $this->get_role_mapping_xmldb(); + $table_role_mapping = $this->get_role_mapping_xmldb(); if(isset($config->supportcourses) && $config->supportcourses == 'internal') { if(!$dbman->table_exists($table_course_mapping)) { @@ -211,9 +211,9 @@ function validate_form($form, &$err) { } if ($form->supportcourses == 'external') { - if ($form->externalcoursemappingdsn == '' || $form->externalcoursemappingsql == '' || $form->externalrolemappingdsn == '' || $form->externalrolemappingsql == '') { + if ($form->externalcoursemappingdsn == '' || $form->externalcoursemappingsql == '' || $form->externalrolemappingdsn == '' || $form->externalrolemappingsql == '') { $err['samlexternal'] = get_string('auth_saml_errorsamlexternal', 'auth_saml', $form->samllib); - } + } } else if($form->supportcourses == 'internal') { @@ -223,7 +223,7 @@ function validate_form($form, &$err) { if (isset($form->update_courses_id)) { foreach ($form->update_courses_id as $course_id) { $course = $form->{'course_' . $course_id}; - if (!empty($course[1]) && !empty($course[2])) { + if (!empty($course[1]) && !empty($course[2])) { $lms_course_form_id[$course_id] = $course[0]; $saml_course_form_id[$course_id] = $course[1] . '_' . $course[2]; } @@ -239,7 +239,7 @@ function validate_form($form, &$err) { $lms_course_form_id[$i] = $new_course[0]; $saml_course_form_id[$i] = $new_course[1] . '_' . $new_course[2]; } - } + } } //Comment the next line if you want let duplicate lms mapping $err['course_mapping']['lms'] = array_diff_key($lms_course_form_id, array_unique($lms_course_form_id)); @@ -360,8 +360,8 @@ function process_config($config) { if (!isset ($config->samllogoinfo)) { $config->samllogoinfo = 'SAML login'; } - if (!isset ($config->autologin)) { - $config->autologin = false; + if (!isset ($config->autologin)) { + $config->autologin = false; } if (!isset ($config->samllogfile)) { $config->samllogfile = ''; @@ -376,16 +376,16 @@ function process_config($config) { $config->ignoreinactivecourses = ''; } if (!isset ($config->externalcoursemappingdsn)) { - $config->externalcoursemappingdsn = ''; + $config->externalcoursemappingdsn = ''; } if (!isset ($config->externalrolemappingdsn)) { - $config->externalrolemappingdsn = ''; + $config->externalrolemappingdsn = ''; } if (!isset ($config->externalcoursemappingsql)) { - $config->externalcoursemappingsql = ''; + $config->externalcoursemappingsql = ''; } if (!isset ($config->externalrolemappingsql)) { - $config->externalrolemappingsql = ''; + $config->externalrolemappingsql = ''; } if (!isset ($config->disablejit)) { $config->disablejit = false; @@ -493,13 +493,13 @@ function process_config($config) { } } } - } + } else { //Updating roles if (isset($config->update_roles_id) && empty($err['roles_mapping'])) { foreach($config->update_roles_id as $role_id) { $role = $config->{'role_' . $role_id}; - $sql = "UPDATE ".$DB->get_prefix() ."role_mapping SET lms_role='" . $role[0] . "', saml_role='" . $role[1] . "' where saml_role='" . $role_id . "'"; + $sql = "UPDATE ".$DB->get_prefix() ."role_mapping SET lms_role='" . $role[0] . "', saml_role='" . $role[1] . "' where saml_role='" . $role_id . "'"; try { $DB->execute($sql); } @@ -528,12 +528,27 @@ function process_config($config) { if(isset($err['role_mapping_db']) || isset($err['course_mapping_db'])) { return false; } - + //END-COURSE MAPPINGS } return true; } + /** + * Get the auth description (from core or own auth lang files) + * + * @return string The description + */ + function get_description() { + global $PAGE; + if ($PAGE->pagetype == 'admin-auth-saml') { + // Workaround require jquery and jquery-ui before auto_config.php $OUTPUT->header() + $PAGE->requires->jquery(); + $PAGE->requires->jquery_plugin('ui'); + } + return parent::get_description(); + } + /** * Cleans and returns first of potential many values (multi-valued attributes) * @@ -584,7 +599,7 @@ function create_role_mapping_db($DB, &$err) { $dbman->create_table($table); echo ''; print_string("auth_saml_sucess_creating_role_mapping", "auth_saml"); - echo '
'; + echo '
'; } catch (Exception $e) { $err['role_mapping_db'][] = get_string("auth_saml_error_creating_role_mapping", "auth_saml"); @@ -594,7 +609,7 @@ function create_role_mapping_db($DB, &$err) { function get_course_mapping_xmldb() { - $table = new xmldb_table('course_mapping'); + $table = new xmldb_table('course_mapping'); $table->add_field('course_mapping_id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); $table->add_field('saml_course_id', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, null); @@ -642,5 +657,5 @@ function initialize_roles($DB, &$err) { } } return $sucess; - } + } } diff --git a/config.php b/config.php index 031fcc8..bbd31ca 100755 --- a/config.php +++ b/config.php @@ -1,14 +1,10 @@ + - - - - - ignoreinactivecourses)) { $config->ignoreinactivecourses = true; } - if (!isset ($config->externalcoursemappingdsn)) { - $config->externalcoursemappingdsn = ''; + if (!isset ($config->externalcoursemappingdsn)) { + $config->externalcoursemappingdsn = ''; } - if (!isset ($config->externalrolemappingdsn)) { - $config->externalrolemappingdsn = ''; + if (!isset ($config->externalrolemappingdsn)) { + $config->externalrolemappingdsn = ''; } - if (!isset ($config->externalcoursemappingsql)) { - $config->externalcoursemappingsql = ''; + if (!isset ($config->externalcoursemappingsql)) { + $config->externalcoursemappingsql = ''; } - if (!isset ($config->externalrolemappingsql)) { - $config->externalrolemappingsql = ''; + if (!isset ($config->externalrolemappingsql)) { + $config->externalrolemappingsql = ''; } if (!isset ($config->disablejit)) { @@ -342,7 +338,7 @@
-'; @@ -401,29 +397,29 @@ - + : - + - + : - + : -

DSN and SQL examples:

+

DSN and SQL examples:

" . htmlspecialchars(get_string("auth_saml_mapping_dsn_examples", "auth_saml")) . "

"; echo "

" . htmlspecialchars(get_string("auth_saml_mapping_sql_examples", "auth_saml")) . "

"; diff --git a/courses.php b/courses.php index 9dcebcd..c41c7ed 100644 --- a/courses.php +++ b/courses.php @@ -42,7 +42,7 @@ function get_course_mapping_for_sync(&$err, $config) { $course_mapping = array(); if($config->supportcourses == 'external') { - require_once ("DBNewConnection.php"); + require_once ("DBNewConnection.php"); $DB_mapping = DBNewConnection($config->externalcoursemappingdsn); $rs = false; if($DB_mapping) { @@ -50,7 +50,7 @@ function get_course_mapping_for_sync(&$err, $config) { $rs = $DB_mapping->Execute($config->externalcoursemappingsql); if($rs !==false) { $res_array = $rs->GetAll(); - } + } $DB_mapping->Disconnect(); } } @@ -76,9 +76,9 @@ function get_course_mapping_for_sync(&$err, $config) { $err['course_mapping_db'][] = get_string('auth_saml_duplicated_saml_data', "auth_saml").' saml_course_id:'.$tuple['saml_course_id'].' saml_course_period:'.$tuple['saml_course_period']; } else { - $course_mapping[$tuple['saml_course_id']][$tuple['saml_course_period']] = $tuple['lms_course_id']; + $course_mapping[$tuple['saml_course_id']][$tuple['saml_course_period']] = $tuple['lms_course_id']; } - } + } } unset($res_array); unset($rs); @@ -92,8 +92,8 @@ function print_course_mapping_options($course_mapping, $config, $err) { if(isset($err['course_mapping_db'])) { foreach ($err['course_maping_db'] as $value) { echo ''; - echo $value; - echo ''; + echo $value; + echo ''; } } @@ -104,7 +104,7 @@ function print_course_mapping_options($course_mapping, $config, $err) { } if (!empty($err['course_mapping']['lms'])) { echo get_string("auth_saml_duplicated_lms_data", "auth_saml") . implode(', ', $err['course_mapping']['lms']); - } + } echo ''; } if (array_key_exists('missed_course_mapping', $err)) { @@ -158,17 +158,17 @@ function print_course_mapping_options($course_mapping, $config, $err) { $new_course_param = optional_param_array('new_course_' . $i, array(), PARAM_ALPHANUMEXT); - echo ''; echo ''; echo ''; - echo ''; - echo ''; + echo ''; + echo ''; echo ''; $i++; } @@ -181,6 +181,6 @@ function print_course_mapping_options($course_mapping, $config, $err) { echo ''; echo ''; echo ''; - echo ''; + echo ''; echo ''; } diff --git a/roles.php b/roles.php index ac2ee18..b6dc91d 100644 --- a/roles.php +++ b/roles.php @@ -30,7 +30,7 @@ function get_role_mapping_for_sync(&$err, $config) { $role_mapping = array(); if($config->supportcourses == 'external') { - require_once ("DBNewConnection.php"); + require_once ("DBNewConnection.php"); $DB_mapping = DBNewConnection($config->externalrolemappingdsn); $rs = false; if($DB_mapping) { @@ -78,7 +78,7 @@ function get_role_mapping_for_sync(&$err, $config) { function print_role_mapping_options($role_mapping, $config, $err) { - + if(isset($err['role_mapping_db'])) { foreach ($err['role_mapping_db'] as $value) { echo ''; @@ -94,7 +94,7 @@ function print_role_mapping_options($role_mapping, $config, $err) { } if (!empty($err['role_mapping']['lms'])) { echo get_string("auth_saml_duplicated_lms_data", "auth_saml") . implode(', ', $err['role_mapping']['lms']); - } + } echo ''; } @@ -145,12 +145,12 @@ function print_role_mapping_options($role_mapping, $config, $err) { echo ''; echo ''; echo ''; - echo ''; + echo ''; echo ''; echo ''; $i++; From 2ddbf10cee71f3e73043c0a9ada66501dc3cbfb6 Mon Sep 17 00:00:00 2001 From: Jouni Suorsa Date: Fri, 16 Sep 2016 10:28:29 +0300 Subject: [PATCH 2/3] Added some errorcode param handling to login.php. --- login.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/login.php b/login.php index e017161..1f351c7 100644 --- a/login.php +++ b/login.php @@ -2,7 +2,9 @@ include_once("../../config.php"); + global $CFG, $PAGE, $OUTPUT; +include_once($CFG->libdir . "/authlib.php"); //HTTPS is required in this page when $CFG->loginhttps enabled $PAGE->https_required(); @@ -21,6 +23,9 @@ exit; } +$errorcode = optional_param('errorcode', 0, PARAM_INT); + + $context = CONTEXT_SYSTEM::instance(); $PAGE->set_url("$CFG->httpswwwroot/auth/saml/login.php"); $PAGE->set_context($context); @@ -72,6 +77,22 @@ $frm = data_submitted(); +if (empty($errormsg) && $errorcode == AUTH_LOGIN_UNAUTHORISED) { + $errormsg = get_string("unauthorisedlogin", "", $frm->username); +} else if(empty($errormsg) && $errorcode == AUTH_LOGIN_FAILED) { + $errormsg = get_string("invalidlogin"); +} else if (empty($errormsg) && $errorcode == AUTH_LOGIN_LOCKOUT) { + $errormsg = get_string('sessionerroruser', 'error'); +} else if (empty($errormsg) && !empty($SESSION->loginerrormsg)) { + // We had some errors before redirect, show them now. + $errormsg = $SESSION->loginerrormsg; + unset($SESSION->loginerrormsg); +} + +if (!empty($errormsg) && method_exists($PAGE->requires, 'js_init_call')) { + $PAGE->requires->js_init_call('M.util.focus_login_error', null, true); +} + echo '
'; if (in_array('saml', $authsequence)){ From 310d2b5fec7bae0655da0ba94ec1cdc6e5f68d84 Mon Sep 17 00:00:00 2001 From: Jouni Suorsa Date: Tue, 11 Oct 2016 15:45:34 +0300 Subject: [PATCH 3/3] Fixed self-register link, when no auth_instructions config present. Was pointing to auth/saml/signup.php, instead of wwwroot/login/signup.php. --- login.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/login.php b/login.php index 1f351c7..884a73f 100644 --- a/login.php +++ b/login.php @@ -172,7 +172,7 @@ if (!empty($CFG->auth_instructions)) { echo format_text($CFG->auth_instructions); } else { - print_string("loginsteps", "", "signup.php"); + print_string("loginsteps", "", $CFG->httpswwwroot . "/login/signup.php"); } ?>