Skip to content
Snippets Groups Projects
Unverified Commit 0427fb13 authored by Tim van Dijen's avatar Tim van Dijen Committed by GitHub
Browse files

Merge pull request #855 from aguvillalba/feature/remember-my-organization

Feature/remember my organization
parents d58a3e03 22e98c77
No related branches found
No related tags found
No related merge requests found
...@@ -354,6 +354,11 @@ $config = array( ...@@ -354,6 +354,11 @@ $config = array(
//'remember.username.enabled' => FALSE, //'remember.username.enabled' => FALSE,
//'remember.username.checked' => FALSE, //'remember.username.checked' => FALSE,
// Give the user an option to save their organization choice for future login
// attempts. And when enabled, what should the default be, checked or not.
//'remember.organization.enabled' => false,
//'remember.organization.checked' => false,
// The way the organization as part of the username should be handled. // The way the organization as part of the username should be handled.
// Three possible values: // Three possible values:
// - 'none': No handling of the organization. Allows '@' to be part // - 'none': No handling of the organization. Allows '@' to be part
......
...@@ -62,7 +62,10 @@ ...@@ -62,7 +62,10 @@
"remember_username": { "remember_username": {
"en": "Remember my username" "en": "Remember my username"
}, },
"remember_me": { "remember_me": {
"en": "Remember me" "en": "Remember me"
} },
"remember_organization": {
"en": "Remember my organization"
}
} }
...@@ -57,6 +57,22 @@ abstract class sspmod_core_Auth_UserPassOrgBase extends SimpleSAML_Auth_Source { ...@@ -57,6 +57,22 @@ abstract class sspmod_core_Auth_UserPassOrgBase extends SimpleSAML_Auth_Source {
*/ */
protected $rememberUsernameChecked = FALSE; protected $rememberUsernameChecked = FALSE;
/**
* Storage for authsource config option remember.organization.enabled
* loginuserpassorg.php page/template use this option to present users
* with a checkbox to save their organization choice for the next login request.
* @var bool
*/
protected $rememberOrganizationEnabled = false;
/**
* Storage for authsource config option remember.organization.checked
* loginuserpassorg.php page/template use this option to
* default the remember organization checkbox to checked or not.
* @var bool
*/
protected $rememberOrganizationChecked = false;
/** /**
* Constructor for this authentication source. * Constructor for this authentication source.
...@@ -83,6 +99,15 @@ abstract class sspmod_core_Auth_UserPassOrgBase extends SimpleSAML_Auth_Source { ...@@ -83,6 +99,15 @@ abstract class sspmod_core_Auth_UserPassOrgBase extends SimpleSAML_Auth_Source {
$this->rememberUsernameChecked = (bool) $config['remember.username.checked']; $this->rememberUsernameChecked = (bool) $config['remember.username.checked'];
unset($config['remember.username.checked']); unset($config['remember.username.checked']);
} }
// Get the remember organization config options
if (isset($config['remember.organization.enabled'])) {
$this->rememberOrganizationEnabled = (bool) $config['remember.organization.enabled'];
unset($config['remember.organization.enabled']);
}
if (isset($config['remember.organization.checked'])) {
$this->rememberOrganizationChecked = (bool) $config['remember.organization.checked'];
unset($config['remember.organization.checked']);
}
$this->usernameOrgMethod = 'none'; $this->usernameOrgMethod = 'none';
} }
...@@ -133,10 +158,27 @@ abstract class sspmod_core_Auth_UserPassOrgBase extends SimpleSAML_Auth_Source { ...@@ -133,10 +158,27 @@ abstract class sspmod_core_Auth_UserPassOrgBase extends SimpleSAML_Auth_Source {
* Getter for the authsource config option remember.username.checked * Getter for the authsource config option remember.username.checked
* @return bool * @return bool
*/ */
public function getRememberUsernameChecked() { public function getRememberUsernameChecked()
{
return $this->rememberUsernameChecked; return $this->rememberUsernameChecked;
} }
/**
* Getter for the authsource config option remember.organization.enabled
* @return bool
*/
public function getRememberOrganizationEnabled()
{
return $this->rememberOrganizationEnabled;
}
/**
* Getter for the authsource config option remember.organization.checked
* @return bool
*/
public function getRememberOrganizationChecked() {
return $this->rememberOrganizationChecked;
}
/** /**
* Initialize login. * Initialize login.
......
...@@ -142,6 +142,16 @@ if ($this->data['errorcode'] !== null) { ...@@ -142,6 +142,16 @@ if ($this->data['errorcode'] !== null) {
} }
?> ?>
</select></td> </select></td>
<td style="padding: .4em;">
<?php
if ($this->data['rememberOrganizationEnabled']) {
echo str_repeat("\t", 4);
echo '<input type="checkbox" id="remember_organization" tabindex="5" name="remember_organization" value="Yes" ';
echo ($this->data['rememberOrganizationChecked'] ? 'checked="Yes" /> ' : '/> ');
echo $this->t('{login:remember_organization}');
}
?>
</td>
</tr> </tr>
<?php <?php
} }
......
...@@ -41,6 +41,8 @@ if (array_key_exists('password', $_REQUEST)) { ...@@ -41,6 +41,8 @@ if (array_key_exists('password', $_REQUEST)) {
if (array_key_exists('organization', $_REQUEST)) { if (array_key_exists('organization', $_REQUEST)) {
$organization = $_REQUEST['organization']; $organization = $_REQUEST['organization'];
} elseif ($source->getRememberOrganizationEnabled() && array_key_exists($source->getAuthId() . '-organization', $_COOKIE)) {
$organization = $_COOKIE[$source->getAuthId() . '-organization'];
} elseif (isset($state['core:organization'])) { } elseif (isset($state['core:organization'])) {
$organization = (string)$state['core:organization']; $organization = (string)$state['core:organization'];
} else { } else {
...@@ -65,9 +67,17 @@ if ($organizations === NULL || !empty($organization)) { ...@@ -65,9 +67,17 @@ if ($organizations === NULL || !empty($organization)) {
$params = $sessionHandler->getCookieParams(); $params = $sessionHandler->getCookieParams();
$params['expire'] = time(); $params['expire'] = time();
$params['expire'] += (isset($_REQUEST['remember_username']) && $_REQUEST['remember_username'] == 'Yes' ? 31536000 : -300); $params['expire'] += (isset($_REQUEST['remember_username']) && $_REQUEST['remember_username'] == 'Yes' ? 31536000 : -300);
\SimpleSAML\Utils\HTTP::setCookie($source->getAuthId() . '-username', $username, $params, FALSE); \SimpleSAML\Utils\HTTP::setCookie($source->getAuthId() . '-username', $username, $params, false);
} }
if ($source->getRememberOrganizationEnabled()) {
$sessionHandler = SimpleSAML_SessionHandler::getSessionHandler();
$params = $sessionHandler->getCookieParams();
$params['expire'] = time();
$params['expire'] += (isset($_REQUEST['remember_organization']) && $_REQUEST['remember_organization'] == 'Yes' ? 31536000 : -300);
setcookie($source->getAuthId() . '-organization', $organization, $params['expire'], $params['path'], $params['domain'], $params['secure'], $params['httponly']);
}
try { try {
sspmod_core_Auth_UserPassOrgBase::handleLogin($authStateId, $username, $password, $organization); sspmod_core_Auth_UserPassOrgBase::handleLogin($authStateId, $username, $password, $organization);
} catch (SimpleSAML_Error_Error $e) { } catch (SimpleSAML_Error_Error $e) {
...@@ -97,6 +107,9 @@ $t->data['rememberUsernameChecked'] = $source->getRememberUsernameChecked(); ...@@ -97,6 +107,9 @@ $t->data['rememberUsernameChecked'] = $source->getRememberUsernameChecked();
$t->data['rememberMeEnabled'] = false; $t->data['rememberMeEnabled'] = false;
$t->data['rememberMeChecked'] = false; $t->data['rememberMeChecked'] = false;
if (isset($_COOKIE[$source->getAuthId() . '-username'])) $t->data['rememberUsernameChecked'] = TRUE; if (isset($_COOKIE[$source->getAuthId() . '-username'])) $t->data['rememberUsernameChecked'] = TRUE;
$t->data['rememberOrganizationEnabled'] = $source->getRememberOrganizationEnabled();
$t->data['rememberOrganizationChecked'] = $source->getRememberOrganizationChecked();
if (isset($_COOKIE[$source->getAuthId() . '-organization'])) $t->data['rememberOrganizationChecked'] = true;
$t->data['errorcode'] = $errorCode; $t->data['errorcode'] = $errorCode;
$t->data['errorcodes'] = SimpleSAML\Error\ErrorCodes::getAllErrorCodeMessages(); $t->data['errorcodes'] = SimpleSAML\Error\ErrorCodes::getAllErrorCodeMessages();
$t->data['errorparams'] = $errorParams; $t->data['errorparams'] = $errorParams;
......
<?php
/**
* Created by PhpStorm.
* User: agustin
* Date: 16.10.2017
* Time: 12:17
*/
namespace SimpleSAML\Test\Module\core\Auth;
use SimpleSAML\Module\core\Auth\UserPassOrgBase;
class UserPassOrgBaseTest extends \PHPUnit_Framework_TestCase
{
public function testRememberOrganizationEnabled()
{
$config = array(
'ldap:LDAPMulti',
'remember.organization.enabled' => true,
'remember.organization.checked' => false,
'my-org' => array(
'description' => 'My organization',
// The rest of the options are the same as those available for
// the LDAP authentication source.
'hostname' => 'ldap://ldap.myorg.com',
'dnpattern' => 'uid=%username%,ou=employees,dc=example,dc=org',
// Whether SSL/TLS should be used when contacting the LDAP server.
'enable_tls' => false,
)
);
$mockUserPassOrgBase = $this->getMockBuilder('\sspmod_core_Auth_UserPassOrgBase')
->setConstructorArgs(array(array('AuthId' => 'my-org'), &$config))
->setMethods(array())
->getMockForAbstractClass();
$this->assertTrue($mockUserPassOrgBase->getRememberOrganizationEnabled());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment