oxtotp/src/Application/Controller/Admin/d3user_totp.php

166 lines
4.3 KiB
PHP
Raw Normal View History

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;
use D3\Totp\Application\Model\d3backupcodelist;
use D3\Totp\Modules\Application\Model\d3_totp_user;
use Exception;
2018-10-17 23:48:20 +02:00
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Exception\StandardException;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\UtilsView;
2018-10-17 23:48:20 +02:00
class d3user_totp extends AdminDetailsController
{
protected $_sSaveError = null;
2018-10-17 23:48:20 +02:00
protected $_sThisTemplate = 'd3user_totp.tpl';
2018-10-18 15:33:59 +02:00
public $aBackupCodes = [];
2018-10-18 15:33:59 +02:00
/**
* @return string
*/
public function render()
{
parent::render();
$soxId = $this->getEditObjectId();
2022-10-01 14:44:36 +02:00
if ($soxId && $soxId != "-1") {
/** @var d3_totp_user $oUser */
2019-08-05 00:17:23 +02:00
$oUser = $this->getUserObject();
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
}
if ($this->_sSaveError) {
$this->addTplParam("sSaveError", $this->_sSaveError);
2018-10-18 15:33:59 +02:00
}
return $this->_sThisTemplate;
}
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);
}
/**
* @throws Exception
*/
public function save()
{
parent::save();
$aParams = Registry::getRequest()->getRequestEscapedParameter("editval");
try {
2019-08-05 00:17:23 +02:00
$oTotp = $this->getTotpObject();
if ($oTotp->checkIfAlreadyExist($this->getEditObjectId())) {
2022-09-28 00:08:36 +02:00
throw oxNew(StandardException::class, 'D3_TOTP_ALREADY_EXIST');
}
2019-08-05 00:17:23 +02:00
$oTotpBackupCodes = $this->getBackupcodeListObject();
if ($aParams['d3totp__oxid']) {
$oTotp->load($aParams['d3totp__oxid']);
} else {
$aParams['d3totp__usetotp'] = 1;
$seed = Registry::getRequest()->getRequestEscapedParameter("secret");
$otp = Registry::getRequest()->getRequestEscapedParameter("otp");
$oTotp->saveSecret($seed);
$oTotp->assign($aParams);
$oTotp->verify($otp, $seed);
$oTotpBackupCodes->generateBackupCodes($this->getEditObjectId());
$oTotp->setId();
}
$oTotp->save();
$oTotpBackupCodes->save();
} catch (Exception $oExcp) {
$this->_sSaveError = $oExcp->getMessage();
}
}
2018-10-23 22:33:14 +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();
Registry::get(UtilsView::class)->addErrorToDisplay('D3_TOTP_REGISTERDELETED');
2018-10-23 22:33:14 +02:00
}
}
/**
* @param $aCodes
*/
public function setBackupCodes($aCodes)
{
$this->aBackupCodes = $aCodes;
}
/**
* @return string
*/
public function getBackupCodes()
{
return implode(PHP_EOL, $this->aBackupCodes);
}
/**
* @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());
}
2022-09-30 21:06:30 +02:00
}