2018-10-17 23:48:20 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2022-09-26 15:22:26 +02:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*
|
|
|
|
* https://www.d3data.de
|
2018-10-17 23:48:20 +02:00
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
2022-09-26 15:22:26 +02:00
|
|
|
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
|
|
|
|
* @link https://www.oxidmodule.com
|
2018-10-17 23:48:20 +02:00
|
|
|
*/
|
|
|
|
|
2022-09-28 00:08:36 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-10-17 23:48:20 +02:00
|
|
|
namespace D3\Totp\Application\Controller\Admin;
|
|
|
|
|
2018-10-18 15:33:59 +02:00
|
|
|
use D3\Totp\Application\Model\d3totp;
|
2019-07-31 22:43:34 +02:00
|
|
|
use D3\Totp\Application\Model\d3backupcodelist;
|
2018-10-19 14:16:37 +02:00
|
|
|
use D3\Totp\Modules\Application\Model\d3_totp_user;
|
2019-07-28 23:00:30 +02:00
|
|
|
use Exception;
|
2018-10-17 23:48:20 +02:00
|
|
|
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
|
2018-10-19 14:16:37 +02:00
|
|
|
use OxidEsales\Eshop\Application\Model\User;
|
2019-07-28 23:34:39 +02:00
|
|
|
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
2018-10-19 14:16:37 +02:00
|
|
|
use OxidEsales\Eshop\Core\Exception\StandardException;
|
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
2019-08-18 13:24:49 +02:00
|
|
|
use OxidEsales\Eshop\Core\UtilsView;
|
2018-10-17 23:48:20 +02:00
|
|
|
|
|
|
|
class d3user_totp extends AdminDetailsController
|
|
|
|
{
|
2018-10-19 14:16:37 +02:00
|
|
|
protected $_sSaveError = null;
|
|
|
|
|
2018-10-17 23:48:20 +02:00
|
|
|
protected $_sThisTemplate = 'd3user_totp.tpl';
|
2018-10-18 15:33:59 +02:00
|
|
|
|
2019-08-02 00:23:21 +02:00
|
|
|
public $aBackupCodes = [];
|
2019-07-28 23:00:30 +02:00
|
|
|
|
2018-10-18 15:33:59 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
parent::render();
|
|
|
|
|
2018-10-19 14:16:37 +02:00
|
|
|
$soxId = $this->getEditObjectId();
|
|
|
|
|
2022-10-01 14:44:36 +02:00
|
|
|
if ($soxId && $soxId != "-1") {
|
2018-10-19 14:16:37 +02:00
|
|
|
/** @var d3_totp_user $oUser */
|
2019-08-05 00:17:23 +02:00
|
|
|
$oUser = $this->getUserObject();
|
2018-10-19 14:16:37 +02:00
|
|
|
if ($oUser->load($soxId)) {
|
|
|
|
$this->addTplParam("oxid", $oUser->getId());
|
|
|
|
} else {
|
|
|
|
$this->addTplParam("oxid", '-1');
|
|
|
|
}
|
|
|
|
$this->addTplParam("edit", $oUser);
|
2018-10-18 15:33:59 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 14:16:37 +02:00
|
|
|
if ($this->_sSaveError) {
|
|
|
|
$this->addTplParam("sSaveError", $this->_sSaveError);
|
2018-10-18 15:33:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_sThisTemplate;
|
|
|
|
}
|
2018-10-19 14:16:37 +02:00
|
|
|
|
2019-08-05 00:17:23 +02:00
|
|
|
/**
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
public function getUserObject()
|
|
|
|
{
|
|
|
|
return oxNew(User::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return d3totp
|
|
|
|
*/
|
|
|
|
public function getTotpObject()
|
|
|
|
{
|
|
|
|
return oxNew(d3totp::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return d3backupcodelist
|
|
|
|
*/
|
|
|
|
public function getBackupcodeListObject()
|
|
|
|
{
|
|
|
|
return oxNew(d3backupcodelist::class);
|
|
|
|
}
|
|
|
|
|
2018-10-19 14:16:37 +02:00
|
|
|
/**
|
2019-07-28 23:00:30 +02:00
|
|
|
* @throws Exception
|
2018-10-19 14:16:37 +02:00
|
|
|
*/
|
|
|
|
public function save()
|
|
|
|
{
|
|
|
|
parent::save();
|
|
|
|
|
|
|
|
$aParams = Registry::getRequest()->getRequestEscapedParameter("editval");
|
|
|
|
|
|
|
|
try {
|
2019-08-05 00:17:23 +02:00
|
|
|
$oTotp = $this->getTotpObject();
|
2019-08-18 13:24:49 +02:00
|
|
|
if ($oTotp->checkIfAlreadyExist($this->getEditObjectId())) {
|
2022-09-28 00:08:36 +02:00
|
|
|
throw oxNew(StandardException::class, 'D3_TOTP_ALREADY_EXIST');
|
|
|
|
}
|
2019-08-18 13:24:49 +02:00
|
|
|
|
2019-08-05 00:17:23 +02:00
|
|
|
$oTotpBackupCodes = $this->getBackupcodeListObject();
|
2018-10-19 14:16:37 +02:00
|
|
|
if ($aParams['d3totp__oxid']) {
|
|
|
|
$oTotp->load($aParams['d3totp__oxid']);
|
|
|
|
} else {
|
|
|
|
$aParams['d3totp__usetotp'] = 1;
|
|
|
|
$seed = Registry::getRequest()->getRequestEscapedParameter("secret");
|
|
|
|
$otp = Registry::getRequest()->getRequestEscapedParameter("otp");
|
|
|
|
|
2018-10-22 12:10:43 +02:00
|
|
|
$oTotp->saveSecret($seed);
|
2018-10-19 14:16:37 +02:00
|
|
|
$oTotp->assign($aParams);
|
|
|
|
$oTotp->verify($otp, $seed);
|
2019-07-28 23:00:30 +02:00
|
|
|
$oTotpBackupCodes->generateBackupCodes($this->getEditObjectId());
|
2018-10-19 14:16:37 +02:00
|
|
|
$oTotp->setId();
|
|
|
|
}
|
|
|
|
$oTotp->save();
|
2019-07-28 23:00:30 +02:00
|
|
|
$oTotpBackupCodes->save();
|
|
|
|
} catch (Exception $oExcp) {
|
2018-10-19 14:16:37 +02:00
|
|
|
$this->_sSaveError = $oExcp->getMessage();
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 22:33:14 +02:00
|
|
|
|
2019-08-02 00:23:21 +02:00
|
|
|
/**
|
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
*/
|
2018-10-23 22:33:14 +02:00
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
$aParams = Registry::getRequest()->getRequestEscapedParameter("editval");
|
|
|
|
|
|
|
|
/** @var d3totp $oTotp */
|
2019-08-05 00:17:23 +02:00
|
|
|
$oTotp = $this->getTotpObject();
|
2018-10-23 22:33:14 +02:00
|
|
|
if ($aParams['d3totp__oxid']) {
|
|
|
|
$oTotp->load($aParams['d3totp__oxid']);
|
|
|
|
$oTotp->delete();
|
2019-08-18 13:24:49 +02:00
|
|
|
Registry::get(UtilsView::class)->addErrorToDisplay('D3_TOTP_REGISTERDELETED');
|
2018-10-23 22:33:14 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-28 23:00:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $aCodes
|
|
|
|
*/
|
|
|
|
public function setBackupCodes($aCodes)
|
|
|
|
{
|
|
|
|
$this->aBackupCodes = $aCodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getBackupCodes()
|
|
|
|
{
|
|
|
|
return implode(PHP_EOL, $this->aBackupCodes);
|
|
|
|
}
|
2019-07-28 23:34:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
*/
|
|
|
|
public function getAvailableBackupCodeCount()
|
|
|
|
{
|
2019-08-05 00:17:23 +02:00
|
|
|
$oBackupCodeList = $this->getBackupcodeListObject();
|
2019-08-11 00:33:59 +02:00
|
|
|
return $oBackupCodeList->getAvailableCodeCount($this->getEditObjectId());
|
2019-07-28 23:34:39 +02:00
|
|
|
}
|
2022-09-30 21:06:30 +02:00
|
|
|
}
|