8
0
webauthn/src/Setup/Actions.php

264 Zeilen
8.3 KiB
PHP

2022-12-10 23:55:53 +01:00
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\Webauthn\Setup;
2022-12-11 23:12:17 +01:00
use D3\TestingTools\Production\IsMockable;
2022-12-10 23:55:53 +01:00
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
use Exception;
2022-12-11 23:12:17 +01:00
use OxidEsales\Eshop\Application\Controller\FrontendController;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface;
2022-12-10 23:55:53 +01:00
use OxidEsales\Eshop\Core\DatabaseProvider;
use OxidEsales\Eshop\Core\DbMetaDataHandler;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
2022-12-11 23:12:17 +01:00
use OxidEsales\Eshop\Core\SeoEncoder;
use OxidEsales\Eshop\Core\Utils;
2022-12-10 23:55:53 +01:00
use OxidEsales\Eshop\Core\UtilsView;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
2022-12-11 23:12:17 +01:00
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\ShopConfigurationDaoBridgeInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Exception\ModuleConfigurationNotFoundException;
2022-12-10 23:55:53 +01:00
use Psr\Container\ContainerExceptionInterface;
2022-12-11 23:12:17 +01:00
use Psr\Container\ContainerInterface;
2022-12-10 23:55:53 +01:00
use Psr\Container\NotFoundExceptionInterface;
class Actions
{
2022-12-11 23:12:17 +01:00
use IsMockable;
public $seo_de = 'sicherheitsschluessel';
public $seo_en = 'en/key-authentication';
public $stdClassName = 'd3_account_webauthn';
2022-12-10 23:55:53 +01:00
/**
* SQL statement, that will be executed only at the first time of module installation.
*
2022-12-12 23:41:07 +01:00
* @var string
2022-12-10 23:55:53 +01:00
*/
2022-12-12 23:41:07 +01:00
protected $createCredentialSql =
2022-12-10 23:55:53 +01:00
"CREATE TABLE `d3wa_usercredentials` (
`OXID` char(32) NOT NULL,
`OXUSERID` char(32) NOT NULL,
`OXSHOPID` int(11) NOT NULL,
`NAME` varchar(100) NOT NULL,
`CREDENTIALID` char(128) NOT NULL,
`CREDENTIAL` varchar(2000) NOT NULL,
`OXTIMESTAMP` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`OXID`),
KEY `CREDENTIALID_IDX` (`CREDENTIALID`),
KEY `SHOPUSER_IDX` (`OXUSERID`,`OXSHOPID`) USING BTREE
) ENGINE=InnoDB COMMENT='WebAuthn Credentials';";
/**
* Execute the sql at the first time of the module installation.
* @return void
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
*/
public function setupModule()
{
if (!$this->tableExists('d3wa_usercredentials')) {
$this->executeSQL($this->createCredentialSql);
}
}
/**
* Check if table exists
*
* @param string $sTableName table name
*
* @return bool
*/
public function tableExists(string $sTableName): bool
{
2022-12-11 23:12:17 +01:00
$oDbMetaDataHandler = $this->d3GetMockableOxNewObject(DbMetaDataHandler::class);
2022-12-10 23:55:53 +01:00
return $oDbMetaDataHandler->tableExists($sTableName);
}
2022-12-11 23:12:17 +01:00
/**
* @return DatabaseInterface|null
* @throws DatabaseConnectionException
*/
protected function d3GetDb(): ?DatabaseInterface
{
return DatabaseProvider::getDb();
}
2022-12-10 23:55:53 +01:00
/**
* Executes given sql statement.
*
* @param string $sSQL Sql to execute.
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
*/
public function executeSQL(string $sSQL)
{
2022-12-11 23:12:17 +01:00
$this->d3GetDb()->execute($sSQL);
2022-12-10 23:55:53 +01:00
}
/**
* Check if field exists in table
*
* @param string $sFieldName field name
* @param string $sTableName table name
*
* @return bool
*/
public function fieldExists(string $sFieldName, string $sTableName): bool
{
2022-12-11 23:12:17 +01:00
$oDbMetaDataHandler = $this->d3GetMockableOxNewObject(DbMetaDataHandler::class);
2022-12-10 23:55:53 +01:00
return $oDbMetaDataHandler->fieldExists($sFieldName, $sTableName);
}
/**
* Regenerate views for changed tables
*/
public function regenerateViews()
{
2022-12-11 23:12:17 +01:00
$oDbMetaDataHandler = $this->d3GetMockableOxNewObject(DbMetaDataHandler::class);
2022-12-10 23:55:53 +01:00
$oDbMetaDataHandler->updateViews();
}
/**
* clear cache
*/
public function clearCache()
{
2022-12-11 23:12:17 +01:00
try {
$oUtils = $this->d3GetMockableRegistryObject( Utils::class );
$oUtils->resetTemplateCache( $this->getModuleTemplates() );
$oUtils->resetLanguageCache();
} catch (ContainerExceptionInterface|NotFoundExceptionInterface|ModuleConfigurationNotFoundException $e) {
$this->d3GetMockableLogger()->error($e->getMessage(), [$this]);
2022-12-12 23:41:07 +01:00
$this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e->getMessage());
2022-12-10 23:55:53 +01:00
}
}
2022-12-11 23:12:17 +01:00
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ModuleConfigurationNotFoundException
*/
protected function getModuleTemplates(): array
{
$container = $this->getDIContainer();
$shopConfiguration = $container->get(ShopConfigurationDaoBridgeInterface::class)->get();
$moduleConfiguration = $shopConfiguration->getModuleConfiguration('d3webauthn');
return array_unique(array_merge(
$this->getModuleTemplatesFromTemplates($moduleConfiguration),
$this->getModuleTemplatesFromBlocks($moduleConfiguration)
));
}
/**
* @param ModuleConfiguration $moduleConfiguration
*
* @return array
*/
protected function getModuleTemplatesFromTemplates(ModuleConfiguration $moduleConfiguration): array
{
/** @var $template ModuleConfiguration\Template */
return array_map(
function($template) {
return $template->getTemplateKey();
},
$moduleConfiguration->getTemplates()
);
}
/**
* @param ModuleConfiguration $moduleConfiguration
*
* @return array
*/
protected function getModuleTemplatesFromBlocks(ModuleConfiguration $moduleConfiguration): array
{
/** @var $templateBlock ModuleConfiguration\TemplateBlock */
return array_map(
function($templateBlock) {
return basename($templateBlock->getShopTemplatePath());
},
$moduleConfiguration->getTemplateBlocks()
);
}
2022-12-10 23:55:53 +01:00
/**
* @return void
*/
public function seoUrl()
{
try {
2022-12-11 23:12:17 +01:00
if (!$this->hasSeoUrl()) {
$this->createSeoUrl();
2022-12-10 23:55:53 +01:00
}
} catch (Exception|NotFoundExceptionInterface|DoctrineDriverException|ContainerExceptionInterface $e) {
2022-12-11 23:12:17 +01:00
$this->d3GetMockableLogger()->error($e->getMessage(), [$this]);
$this->d3GetMockableRegistryObject(UtilsView::class)
->addErrorToDisplay('error wile creating SEO URLs: '.$e->getMessage());
2022-12-10 23:55:53 +01:00
}
}
/**
* @return bool
*/
public function hasSeoUrl(): bool
{
2022-12-11 23:12:17 +01:00
$seoEncoder = $this->d3GetMockableOxNewObject(SeoEncoder::class);
$seoUrl = $seoEncoder->getStaticUrl(
$this->d3GetMockableOxNewObject(FrontendController::class)->getViewConfig()->getSelfLink() .
"cl=".$this->stdClassName
);
return (bool) strlen($seoUrl);
2022-12-10 23:55:53 +01:00
}
/**
* @return void
*/
public function createSeoUrl()
{
2022-12-11 23:12:17 +01:00
$seoEncoder = $this->d3GetMockableOxNewObject(SeoEncoder::class);
$seoEncoder->addSeoEntry(
'ff57646b47249ee33c6b672741ac371a',
$this->d3GetMockableRegistryObject(Config::class)->getShopId(),
0,
'index.php?cl='.$this->stdClassName,
$this->seo_de,
'static',
2022-12-12 23:41:07 +01:00
false
2022-12-11 23:12:17 +01:00
);
$seoEncoder->addSeoEntry(
'ff57646b47249ee33c6b672741ac371a',
$this->d3GetMockableRegistryObject(Config::class)->getShopId(),
1,
'index.php?cl='.$this->stdClassName,
$this->seo_en,
'static',
2022-12-12 23:41:07 +01:00
false
2022-12-11 23:12:17 +01:00
);
}
2022-12-10 23:55:53 +01:00
2022-12-11 23:12:17 +01:00
/**
* @return ContainerInterface|null
*/
protected function getDIContainer(): ?ContainerInterface
{
return ContainerFactory::getInstance()->getContainer();
2022-12-10 23:55:53 +01:00
}
}