N°2311 - Refactor Login FSM Extensions

This commit is contained in:
Eric
2019-08-14 18:13:39 +02:00
parent 11f62063a6
commit 2ceb4068ad
9 changed files with 472 additions and 372 deletions

View File

@@ -1,65 +1,81 @@
<?php
/**
* Class LoginDefault
*
* @copyright Copyright (C) 2010-2019 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
class LoginDefault implements iLoginFSMExtension
/**
* Class LoginDefaultBefore
*/
class LoginDefaultBefore extends AbstractLoginFSMExtension
{
/**
* Return the list of supported login modes for this plugin
* Must be executed before the other login plugins
*
* @return array of supported login modes
*/
public function ListSupportedLoginModes()
{
return array('default');
return array('before');
}
protected function OnStart(&$iErrorCode)
{
// Check if proposed login mode is present and allowed
$aAllowedLoginTypes = MetaModel::GetConfig()->GetAllowedLoginTypes();
$sProposedLoginMode = utils::ReadParam('login_mode', '');
$index = array_search($sProposedLoginMode, $aAllowedLoginTypes);
if ($index !== false)
{
// Force login mode
$_SESSION['login_mode'] = $sProposedLoginMode;
}
else
{
unset($_SESSION['login_mode']);
}
return LoginWebPage::LOGIN_FSM_RETURN_CONTINUE;
}
protected function OnError(&$iErrorCode)
{
$_SESSION['login_error_count'] = (isset($_SESSION['login_error_count']) ? $_SESSION['login_error_count'] : 0) + 1;
return LoginWebPage::LOGIN_FSM_RETURN_CONTINUE;
}
protected function OnConnected(&$iErrorCode)
{
unset($_SESSION['login_error_count']);
return LoginWebPage::LOGIN_FSM_RETURN_CONTINUE;
}
}
/**
* Class LoginDefaultAfter
*/
class LoginDefaultAfter extends AbstractLoginFSMExtension implements iLogoutExtension
{
/**
* Must be executed after the other login plugins
*
* @return array of supported login modes
*/
public function ListSupportedLoginModes()
{
return array('after');
}
protected function OnError(&$iErrorCode)
{
unset($_SESSION['login_mode']);
return LoginWebPage::LOGIN_FSM_RETURN_CONTINUE;
}
/**
* Execute action for this login state
* If a page is displayed, the action must exit at this point
* if LoginWebPage::LOGIN_FSM_RETURN_ERROR is returned $iErrorCode must be set
* if LoginWebPage::LOGIN_FSM_RETURN_OK is returned then the login is OK and terminated
* if LoginWebPage::LOGIN_FSM_RETURN_IGNORE is returned then the FSM will proceed to next plugin or state
*
* @param string $sLoginState (see LoginWebPage::LOGIN_STATE_...)
* @param int $iErrorCode (see LoginWebPage::EXIT_CODE_...)
*
* @return int LoginWebPage::LOGIN_FSM_RETURN_ERROR, LoginWebPage::LOGIN_FSM_RETURN_OK or LoginWebPage::LOGIN_FSM_RETURN_IGNORE
* Execute all actions to log out properly
*/
public function LoginAction($sLoginState, &$iErrorCode)
public function LogoutAction()
{
switch ($sLoginState)
{
case LoginWebPage::LOGIN_STATE_START:
// Check if proposed login mode is present and allowed
$aAllowedLoginTypes = MetaModel::GetConfig()->GetAllowedLoginTypes();
$sProposedLoginMode = utils::ReadParam('login_mode', '');
$index = array_search($sProposedLoginMode, $aAllowedLoginTypes);
if ($index !== false)
{
// Force login mode
$_SESSION['login_mode'] = $sProposedLoginMode;
}
else
{
unset($_SESSION['login_mode']);
}
break;
case LoginWebPage::LOGIN_STATE_ERROR:
$_SESSION['login_error_count'] = (isset($_SESSION['login_error_count']) ? $_SESSION['login_error_count'] : 0) + 1;
break;
case LoginWebPage::LOGIN_STATE_CONNECTED:
unset($_SESSION['login_error_count']);
break;
}
return LoginWebPage::LOGIN_FSM_RETURN_CONTINUE;
unset($_SESSION['login_mode']);
}
}