oxtotp/src/Modules/Application/Controller/Admin/d3_totp_LoginController.php

116 lignes
3.5 KiB
PHP
Brut Vue normale Historique

2018-10-17 12:50:10 +02:00
<?php
/**
* This Software is the property of Data Development and is protected
* by copyright law - it is NOT Freeware.
* Any unauthorized use of this software without a valid license
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
* http://www.shopmodule.com
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link http://www.oxidmodule.com
*/
namespace D3\Totp\Modules\Application\Controller\Admin;
2018-10-17 16:21:49 +02:00
use D3\Totp\Application\Model\d3totp;
2018-10-18 15:33:59 +02:00
use D3\Totp\Application\Model\Exceptions\d3totp_wrongOtpException;
2018-10-17 16:21:49 +02:00
use Doctrine\DBAL\DBALException;
use OxidEsales\Eshop\Application\Model\User;
2018-10-17 16:21:49 +02:00
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Registry;
2018-10-17 12:50:10 +02:00
class d3_totp_LoginController extends d3_totp_LoginController_parent
{
2018-10-17 16:21:49 +02:00
/**
* @return string
* @throws DBALException
* @throws DatabaseConnectionException
*/
2018-10-17 12:50:10 +02:00
public function render()
{
2018-10-17 16:21:49 +02:00
$auth = Registry::getSession()->getVariable("auth");
$return = parent::render();
2018-10-18 15:33:59 +02:00
$totp = oxNew(d3totp::class);
$totp->loadByUserId($auth);
2018-10-17 16:21:49 +02:00
if ($auth
2018-10-18 15:33:59 +02:00
&& $totp->UserUseTotp()
&& false == Registry::getSession()->getVariable(d3totp::TOTP_SESSION_VARNAME)
2018-10-20 00:45:49 +02:00
&& Registry::getSession()->hasVariable('pwdTransmit')
2018-10-17 16:21:49 +02:00
) {
// set auth as secured parameter;
2018-10-18 15:33:59 +02:00
Registry::getSession()->setVariable("auth", $auth);
$this->addTplParam('request_totp', true);
2018-10-17 16:21:49 +02:00
}
return $return;
}
/**
* @return mixed|string
* @throws DBALException
* @throws DatabaseConnectionException
*/
public function checklogin()
{
2018-10-18 15:33:59 +02:00
$sTotp = Registry::getRequest()->getRequestEscapedParameter('d3totp', true);
$totp = oxNew(d3totp::class);
$totp->loadByUserId(Registry::getSession()->getVariable("auth"));
2018-10-17 16:21:49 +02:00
if (Registry::getRequest()->getRequestParameter('pwd')) {
Registry::getSession()->setVariable('pwdTransmit', Registry::getRequest()->getRequestParameter('pwd'));
}
2018-10-18 15:33:59 +02:00
$return = 'login';
try {
if ($this->isNoTotpOrNoLogin($totp)) {
$return = parent::checklogin();
} elseif ($this->hasValidTotp($sTotp, $totp)) {
Registry::getSession()->setVariable(d3totp::TOTP_SESSION_VARNAME, $sTotp);
Registry::getSession()->deleteVariable('pwdTransmit');
2018-10-18 15:33:59 +02:00
$return = "admin_start";
2018-10-17 16:21:49 +02:00
}
2018-10-18 15:33:59 +02:00
} catch (d3totp_wrongOtpException $oEx) {
Registry::getUtilsView()->addErrorToDisplay($oEx);
2018-10-17 16:21:49 +02:00
}
return $return;
2018-10-17 12:50:10 +02:00
}
2018-10-18 15:33:59 +02:00
/**
* @param d3totp $totp
* @return bool
*/
public function isNoTotpOrNoLogin($totp)
{
return false == Registry::getSession()->getVariable("auth")
|| false == $totp->UserUseTotp();
}
/**
* @param string $sTotp
* @param d3totp $totp
* @return bool
* @throws d3totp_wrongOtpException
*/
public function hasValidTotp($sTotp, $totp)
{
return Registry::getSession()->getVariable(d3totp::TOTP_SESSION_VARNAME) ||
2018-10-18 15:33:59 +02:00
(
$sTotp && $totp->verify($sTotp)
);
}
public function d3CancelLogin()
{
$oUser = oxNew(User::class);
$oUser->logout();
}
2018-10-17 12:50:10 +02:00
}