webauthn/src/Setup/Events.php

220 lines
6.7 KiB
PHP
Raw Normal View History

2022-10-24 22:24:40 +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
*/
2022-11-04 22:02:44 +01:00
declare(strict_types=1);
2022-10-24 22:24:40 +02:00
namespace D3\Webauthn\Setup;
2022-10-31 23:17:04 +01:00
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
use Doctrine\DBAL\Exception as DoctrineException;
use Doctrine\DBAL\Query\QueryBuilder;
use Exception;
2022-10-26 10:17:31 +02:00
use OxidEsales\Eshop\Core\DatabaseProvider;
use OxidEsales\Eshop\Core\DbMetaDataHandler;
2022-10-31 23:17:04 +01:00
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
2022-10-26 10:17:31 +02:00
use OxidEsales\Eshop\Core\Registry;
2022-10-31 23:17:04 +01:00
use OxidEsales\Eshop\Core\UtilsView;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2022-10-24 22:24:40 +02:00
class Events
{
/**
2022-10-26 10:17:31 +02:00
* SQL statement, that will be executed only at the first time of module installation.
*
* @var array
*/
private static $_createCredentialSql =
"CREATE TABLE `d3wa_usercredentials` (
`OXID` char(32) NOT NULL,
`OXUSERID` char(32) NOT NULL,
`OXSHOPID` int(11) NOT NULL,
`NAME` varchar(100) NOT NULL,
2022-11-03 10:32:49 +01:00
`CREDENTIALID` char(128) NOT NULL,
2022-10-26 10:17:31 +02:00
`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 action on activate event
2022-10-31 23:17:04 +01:00
* @return void
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
2022-10-24 22:24:40 +02:00
*/
public static function onActivate()
{
2022-10-26 10:17:31 +02:00
self::setupModule();
self::regenerateViews();
self::clearCache();
2022-10-31 23:17:04 +01:00
self::seoUrl();
2022-10-24 22:24:40 +02:00
}
public static function onDeactivate()
{
}
2022-10-26 10:17:31 +02:00
/**
* Execute the sql at the first time of the module installation.
2022-10-31 23:17:04 +01:00
* @return void
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
2022-10-26 10:17:31 +02:00
*/
private static function setupModule()
{
if (!self::tableExists('d3wa_usercredentials')) {
self::executeSQL(self::$_createCredentialSql);
}
}
/**
* Check if table exists
*
* @param string $sTableName table name
*
* @return bool
*/
2022-10-31 23:17:04 +01:00
protected static function tableExists(string $sTableName): bool
2022-10-26 10:17:31 +02:00
{
$oDbMetaDataHandler = oxNew(DbMetaDataHandler::class );
return $oDbMetaDataHandler->tableExists($sTableName);
}
/**
* Executes given sql statement.
*
* @param string $sSQL Sql to execute.
2022-10-31 23:17:04 +01:00
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
2022-10-26 10:17:31 +02:00
*/
private static function executeSQL($sSQL)
{
DatabaseProvider::getDb()->execute($sSQL);
}
/**
* Check if field exists in table
*
* @param string $sFieldName field name
* @param string $sTableName table name
*
* @return bool
*/
2022-10-31 23:17:04 +01:00
protected static function fieldExists(string $sFieldName, string $sTableName): bool
2022-10-26 10:17:31 +02:00
{
$oDbMetaDataHandler = oxNew(DbMetaDataHandler::class );
return $oDbMetaDataHandler->fieldExists($sFieldName, $sTableName);
}
/**
* Regenerate views for changed tables
*/
protected static function regenerateViews()
{
$oDbMetaDataHandler = oxNew(DbMetaDataHandler::class );
$oDbMetaDataHandler->updateViews();
}
/**
2022-10-31 23:17:04 +01:00
* clear cache
2022-10-26 10:17:31 +02:00
*/
private static function clearCache()
{
2022-10-31 23:17:04 +01:00
/** @var UtilsView $oUtilsView */
2022-10-26 10:17:31 +02:00
$oUtilsView = Registry::getUtilsView();
$sSmartyDir = $oUtilsView->getSmartyDir();
if ($sSmartyDir && is_readable($sSmartyDir)) {
foreach (glob($sSmartyDir . '*') as $sFile) {
if (!is_dir($sFile)) {
@unlink($sFile);
}
}
}
}
2022-10-31 23:17:04 +01:00
/**
* @return void
*/
private static function seoUrl()
{
try {
if (!self::hasSeoUrl()) {
self::createSeoUrl();
}
} catch (Exception|NotFoundExceptionInterface|DoctrineDriverException|ContainerExceptionInterface $e) {
Registry::getUtilsView()->addErrorToDisplay('error wile creating SEO URLs: '.$e->getMessage());
}
}
/**
* @return bool
* @throws DoctrineDriverException
* @throws DoctrineException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private static function hasSeoUrl(): bool
{
/** @var QueryBuilder $qb */
$qb = ContainerFactory::getInstance()->getContainer()->get(QueryBuilderFactoryInterface::class)->create();
$qb->select('1')
->from('oxseo')
->where(
$qb->expr()->and(
$qb->expr()->eq(
'oxstdurl',
$qb->createNamedParameter('index.php?cl=d3_account_webauthn')
),
$qb->expr()->eq(
'oxshopid',
$qb->createNamedParameter(Registry::getConfig()->getShopId())
),
$qb->expr()->eq(
'oxlang',
$qb->createNamedParameter('1')
)
)
)
->setMaxResults(1);
return $qb->execute()->fetchOne();
}
/**
* @return void
* @throws DatabaseConnectionException
* @throws DatabaseErrorException
*/
private static function createSeoUrl()
{
$query = "INSERT INTO `oxseo` (`OXOBJECTID`, `OXIDENT`, `OXSHOPID`, `OXLANG`, `OXSTDURL`, `OXSEOURL`, `OXTYPE`, `OXFIXED`, `OXEXPIRED`, `OXPARAMS`, `OXTIMESTAMP`) VALUES
2022-11-02 14:13:41 +01:00
('ff57646b47249ee33c6b672741ac371a', 'bd3b6183c9a2f94682f4c62e714e4d6b', 1, 1, 'index.php?cl=d3_account_webauthn', 'en/key-authentication/', 'static', 0, 0, '', NOW()),
('ff57646b47249ee33c6b672741ac371a', '94d0d3ec07f10e8838a71e54084be885', 1, 0, 'index.php?cl=d3_account_webauthn', 'sicherheitsschluessel/', 'static', 0, 0, '', NOW());";
2022-10-31 23:17:04 +01:00
DatabaseProvider::getDb()->execute($query);
}
2022-10-24 22:24:40 +02:00
}