Commit eb6d0ab8 authored by Ruslan Kabalin's avatar Ruslan Kabalin Committed by Ruslan Kabalin

Refactor login form elements (bug #1023834)

This patch moves form elements array to the separate function.

Change-Id: I546017bc59d64716d2498a9263c44188fe1274ee
Signed-off-by: default avatarRuslan Kabalin <ruslan.kabalin@luns.net.uk>
parent a18a7c52
......@@ -1021,80 +1021,14 @@ function auth_draw_login_page($message=null, Pieform $form=null) {
* @access private
*/
function auth_get_login_form() {
$elements = array(
'login_username' => array(
'type' => 'text',
'title' => get_string('username') . ':',
'description' => get_string('usernamedescription'),
'rules' => array(
'required' => true
)
),
'login_password' => array(
'type' => 'password',
'title' => get_string('password') . ':',
'description' => get_string('passworddescription'),
'defaultvalue' => '',
),
'submit' => array(
'type' => 'submit',
'value' => get_string('login')
),
'register' => array(
'type' => 'markup',
'value' => '<div id="login-helplinks">' . '<a href="' . get_config('wwwroot') . 'forgotpass.php" tabindex="2">' . get_string('lostusernamepassword') . '</a></div>'
),
'login_submitted' => array(
'type' => 'hidden',
'value' => 1
),
);
$elements = array(
'login' => array(
'type' => 'container',
'elements' => $elements
)
);
$elements = auth_get_login_form_elements();
// Get any extra elements from the enabled auth plugins
$extraelements = array();
$showbasicform = false;
$authplugins = auth_get_enabled_auth_plugins();
foreach ($authplugins as $plugin) {
$classname = 'PluginAuth' . ucfirst(strtolower($plugin));
$pluginelements = call_static_method($classname, 'login_form_elements');
if (!empty($pluginelements)) {
$extraelements = array_merge($extraelements, $pluginelements);
}
if (call_static_method($classname, 'need_basic_login_form')) {
$showbasicform = true;
}
}
if (!empty($extraelements) && $showbasicform) {
$loginlabel = array(
'type' => 'markup',
'value' => '<label>'.get_string('orloginvia') . '</label>'
);
$extraelements = array_merge(array('label' => $loginlabel), $extraelements);
}
if (count($extraelements)) {
$extraelements = array(
'login_extra' => array(
'type' => 'container',
'class' => 'login_extra',
'elements' => $extraelements
)
);
}
// Replace or supplement the standard login form elements
if ($showbasicform) {
$elements = array_merge($elements, $extraelements);
}
else {
$elements = $extraelements;
}
$elements['login']['elements']['login_submitted'] = array(
'type' => 'hidden',
'value' => 1
);
// Change login redirection for clean urls
$url = get_relative_script_path();
$getstart = strrpos($url, '?');
if ($getstart !== false) {
......@@ -1119,7 +1053,7 @@ function auth_get_login_form() {
}
// The login page is completely transient, and it is smart because it
// remembers the GET and POST data sent to it and resends that on
// afterwards.
// afterwards.
$action = '';
if ($get) {
if (isset($get['logout'])) {
......@@ -1128,7 +1062,8 @@ function auth_get_login_form() {
// of the url to include that, or be blank, else the next time
// the user logs in they will be logged out again.
$action = hsc(substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?')));
} else {
}
else {
$action .= '?';
foreach ($get as $key => $value) {
if ($key != 'login') {
......@@ -1164,6 +1099,98 @@ function auth_get_login_form() {
return $form;
}
/**
* Returns the definition of the login form elements.
*
* @return array The login form elements array.
* @access private
*/
function auth_get_login_form_elements() {
// See if user can register
if (count_records('institution', 'registerallowed', 1, 'suspended', 0)) {
$registerlink = '<a href="' . get_config('wwwroot') . 'register.php" tabindex="2">' . get_string('register') . '</a><br>';
}
else {
$registerlink = '';
}
$elements = array(
'login_username' => array(
'type' => 'text',
'title' => get_string('username') . ':',
'description' => get_string('usernamedescription'),
'defaultvalue' => (isset($_POST['login_username'])) ? $_POST['login_username'] : '',
'rules' => array(
'required' => true
)
),
'login_password' => array(
'type' => 'password',
'title' => get_string('password') . ':',
'description' => get_string('passworddescription'),
'defaultvalue' => '',
'rules' => array(
'required' => true
)
),
'submit' => array(
'type' => 'submit',
'value' => get_string('login')
),
'register' => array(
'type' => 'markup',
'value' => '<div id="login-helplinks">' . $registerlink
. '<a href="' . get_config('wwwroot') . 'forgotpass.php" tabindex="2">' . get_string('lostusernamepassword') . '</a></div>'
),
);
$elements = array(
'login' => array(
'type' => 'container',
'class' => 'login',
'elements' => $elements
)
);
// Get any extra elements from the enabled auth plugins
$extraelements = array();
$showbasicform = false;
$authplugins = auth_get_enabled_auth_plugins();
foreach ($authplugins as $plugin) {
$classname = 'PluginAuth' . ucfirst(strtolower($plugin));
$pluginelements = call_static_method($classname, 'login_form_elements');
if (!empty($pluginelements)) {
$extraelements = array_merge($extraelements, $pluginelements);
}
if (call_static_method($classname, 'need_basic_login_form')) {
$showbasicform = true;
}
}
if (!empty($extraelements) && $showbasicform) {
$loginlabel = array(
'type' => 'markup',
'value' => '<label>'.get_string('orloginvia') . '</label>'
);
$extraelements = array_merge(array('label' => $loginlabel), $extraelements);
}
if (count($extraelements)) {
$extraelements = array(
'login_extra' => array(
'type' => 'container',
'class' => 'login_extra',
'elements' => $extraelements
)
);
}
// Replace or supplement the standard login form elements
if ($showbasicform) {
$elements = array_merge($elements, $extraelements);
}
else {
$elements = $extraelements;
}
return $elements;
}
/**
* Returns javascript to assist with the rendering of the login forms. The
* javascript is used to detect whether cookies are enabled, and not show the
......@@ -1756,93 +1783,11 @@ function auth_remove_old_session_files() {
* auth_get_login_form, but keep that in mind when making changes.}}
*/
function auth_generate_login_form() {
require_once('pieforms/pieform.php');
if (!get_config('installed')) {
return;
}
require_once('pieforms/pieform.php');
if (count_records('institution', 'registerallowed', 1, 'suspended', 0)) {
$registerlink = '<a href="' . get_config('wwwroot') . 'register.php" tabindex="2">' . get_string('register') . '</a><br>';
}
else {
$registerlink = '';
}
$elements = array(
'login_username' => array(
'type' => 'text',
'title' => get_string('username') . ':',
'description' => get_string('usernamedescription'),
'defaultvalue' => (isset($_POST['login_username'])) ? $_POST['login_username'] : '',
'rules' => array(
'required' => true
)
),
'login_password' => array(
'type' => 'password',
'title' => get_string('password') . ':',
'description' => get_string('passworddescription'),
'defaultvalue' => '',
'rules' => array(
'required' => true
)
),
'submit' => array(
'type' => 'submit',
'value' => get_string('login')
),
'register' => array(
'type' => 'markup',
'value' => '<div id="login-helplinks">' . $registerlink
. '<a href="' . get_config('wwwroot') . 'forgotpass.php" tabindex="2">' . get_string('lostusernamepassword') . '</a></div>'
),
);
$elements = array(
'login' => array(
'type' => 'container',
'class' => 'login',
'elements' => $elements
)
);
// Get any extra elements from the enabled auth plugins
$extraelements = array();
$showbasicform = false;
$authplugins = auth_get_enabled_auth_plugins();
foreach ($authplugins as $plugin) {
$classname = 'PluginAuth' . ucfirst(strtolower($plugin));
$pluginelements = call_static_method($classname, 'login_form_elements');
if (!empty($pluginelements)) {
$extraelements = array_merge($extraelements, $pluginelements);
}
if (call_static_method($classname, 'need_basic_login_form')) {
$showbasicform = true;
}
}
if (!empty($extraelements) && $showbasicform) {
$loginlabel = array(
'type' => 'markup',
'value' => '<label>'.get_string('orloginvia') . '</label>'
);
$extraelements = array_merge(array('label' => $loginlabel), $extraelements);
}
if (count($extraelements)) {
$extraelements = array(
'login_extra' => array(
'type' => 'container',
'class' => 'login_extra',
'elements' => $extraelements
)
);
}
// Replace or supplement the standard login form elements
if ($showbasicform) {
$elements = array_merge($elements, $extraelements);
}
else {
$elements = $extraelements;
}
$elements = auth_get_login_form_elements();
$loginform = get_login_form_js(pieform(array(
'name' => 'login',
'renderer' => 'div',
......
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