Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue 225 #229

Merged
merged 3 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions Block/Adminhtml/Settings/Whitelist.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class Whitelist extends \Magento\Backend\Block\Widget\Container
/**
* @inheritDoc
*/
protected function _prepareLayout()
protected function _prepareLayout(): self
{
$restoreDefautsButtonProps = [
'id' => 'restore_defaults',
'label' => __('Restore Defaults'),
'class' => 'primary add',
'button_class' => '',
'onclick' => "setLocation('" . $this->getRestoreDefaultsUrl() . "')",
'on_click' => "deleteConfirm('{$this->getRestoreDefaultsConfirmationText()}', '{$this->getRestoreDefaultsUrl()}')",
'class_name' => 'Magento\Backend\Block\Widget\Button'
];
$this->buttonList->add('restore_defaults', $restoreDefautsButtonProps);
Expand All @@ -47,12 +47,22 @@ protected function _prepareLayout()
return parent::_prepareLayout();
}

/**
* Retrieve restore defaults confirmation text
*/
protected function getRestoreDefaultsConfirmationText(): string
{
return sprintf(
'<p>%s</p><p>%s</p>',
__('You will remove all existing whitelist entries and restore the defaults.'),
__('Are you sure you want to do this?')
);
}

/**
* Retrieve restore defaults url
*
* @return string
*/
protected function getRestoreDefaultsUrl()
protected function getRestoreDefaultsUrl(): string
{
return $this->getUrl(
'ForceCustomerLogin/Manage/RestoreDefault'
Expand All @@ -61,10 +71,8 @@ protected function getRestoreDefaultsUrl()

/**
* Retrieve create url
*
* @return string
*/
protected function getCreateUrl()
protected function getCreateUrl(): string
{
return $this->getUrl(
'ForceCustomerLogin/Manage/Create'
Expand Down
71 changes: 22 additions & 49 deletions Controller/Adminhtml/Manage/RestoreDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

namespace BitExpert\ForceCustomerLogin\Controller\Adminhtml\Manage;

use BitExpert\ForceCustomerLogin\Api\Repository\WhitelistRepositoryInterface;
use BitExpert\ForceCustomerLogin\Model\WhitelistEntry;
use BitExpert\ForceCustomerLogin\Model\WhitelistDefaultInstaller;
use BitExpert\ForceCustomerLogin\Model\ResourceModel\WhitelistEntry as WhitelistEntryResource;
use BitExpert\ForceCustomerLogin\Model\ResourceModel\WhitelistEntry\CollectionFactory as WhitelistEntryCollectionFactory;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
Expand All @@ -26,35 +27,25 @@
*/
class RestoreDefault extends \Magento\Backend\App\Action
{
/**
* @var WhitelistRepositoryInterface
*/
private $whitelistRepository;
/**
* @var RedirectFactory
*/
private $redirectFactory;
/**
* @var array Default routes
*/
private $defaultRoutes;
private RedirectFactory $redirectFactory;
private WhitelistDefaultInstaller $whitelistDefaultInstaller;
private WhitelistEntryResource $whitelistEntryResource;
private WhitelistEntryCollectionFactory $whitelistEntryCollectionFactory;

/**
* Save constructor.
*
* @param WhitelistRepositoryInterface $whitelistRepository
* @param Context $context
* @param array $defaultRoutes
* RestoreDefault constructor.
*/
public function __construct(
WhitelistRepositoryInterface $whitelistRepository,
Context $context,
array $defaultRoutes
WhitelistDefaultInstaller $whitelistDefaultInstaller,
WhitelistEntryResource $whitelistEntryResource,
WhitelistEntryCollectionFactory $whitelistEntryCollectionFactory
) {
parent::__construct($context);
$this->whitelistRepository = $whitelistRepository;
$this->redirectFactory = $context->getResultRedirectFactory();
$this->defaultRoutes = $defaultRoutes;
$this->whitelistDefaultInstaller = $whitelistDefaultInstaller;
$this->whitelistEntryResource = $whitelistEntryResource;
$this->whitelistEntryCollectionFactory = $whitelistEntryCollectionFactory;
}

/**
Expand All @@ -67,16 +58,16 @@ public function execute()
$result = $this->redirectFactory->create();
$result->setPath('ForceCustomerLogin/Manage/index');

$whiteLists = $this->getWhiteListEntriesIndexedByPath();

try {
foreach ($this->defaultRoutes as $route => $description) {
if (\array_key_exists($route, $whiteLists)) {
continue;
}
$resource = $this->whitelistEntryResource;
$collection = $this->whitelistEntryCollectionFactory->create();

$this->whitelistRepository->createEntry(null, $description, $route);
}
$resource->beginTransaction();
$collection->walk(function ($entry) {
$entry->delete();
});
$this->whitelistDefaultInstaller->install();
$resource->commit();
} catch (\Exception $e) {
$result->setHttpResponseCode(\Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR);
$this->messageManager->addErrorMessage(
Expand All @@ -93,24 +84,6 @@ public function execute()
return $result;
}

/**
* Get all current whitelists indexed by it's url rule
*
* @return array
*/
protected function getWhiteListEntriesIndexedByPath()
{
$whiteListCollection = $this->whitelistRepository->getCollection();
$whiteLists = [];

foreach ($whiteListCollection->getItems() as $whiteList) {
$whiteLists[$whiteList->getData(WhitelistEntry::KEY_URL_RULE)] =
$whiteList->getData(WhitelistEntry::KEY_LABEL);
}

return $whiteLists;
}

/**
* @inheritDoc
* @codeCoverageIgnore
Expand Down
50 changes: 50 additions & 0 deletions Model/WhitelistDefaultInstaller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Force Login module for Magento2.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace BitExpert\ForceCustomerLogin\Model;

use BitExpert\ForceCustomerLogin\Api\Repository\WhitelistRepositoryInterface;
use BitExpert\ForceCustomerLogin\Model\WhitelistDefaultPool;

/**
* Class WhitelistEntryDefaultInstaller
*
* @package BitExpert\ForceCustomerLogin\Model
* @codingStandardsIgnoreFile
*/
class WhitelistDefaultInstaller
{
private WhitelistDefaultPool $whitelistDefaultPool;
private WhitelistRepositoryInterface $whitelistRepository;

/**
* Installer constructor
*/
public function __construct(
WhitelistDefaultPool $whitelistDefaultPool,
WhitelistRepositoryInterface $whitelistRepository,
) {
$this->whitelistDefaultPool = $whitelistDefaultPool;
$this->whitelistRepository = $whitelistRepository;
}

public function install(): void
{
foreach ($this->whitelistDefaultPool->getEntries() as $route => $data) {
$this->whitelistRepository->createEntry(
null,
$data['label'],
$route,
$data['strategy'] ?: 'default',
);
}
}
}
44 changes: 44 additions & 0 deletions Model/WhitelistDefaultPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Force Login module for Magento2.
*
* (c) bitExpert AG
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace BitExpert\ForceCustomerLogin\Model;

/**
* Class WhitelistEntryDefaultPool
*
* @package BitExpert\ForceCustomerLogin\Model
* @codingStandardsIgnoreFile
*/
class WhitelistDefaultPool
{
private array $entries = [];

/**
* Pool constructor
*
* @param array[] $entries
*/
public function __construct(
array $entries = []
) {
$this->entries = $entries;
}

/**
* Get default entries from pool
*
* @return array[string, array]
*/
public function getEntries(): array
{
return $this->entries;
}
}
74 changes: 12 additions & 62 deletions Setup/Patch/Data/Patch501.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace BitExpert\ForceCustomerLogin\Setup\Patch\Data;

use BitExpert\ForceCustomerLogin\Model\WhitelistDefaultInstaller;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

Expand All @@ -21,12 +22,20 @@ class Patch501 implements DataPatchInterface
*/
private ModuleDataSetupInterface $moduleDataSetup;

/**
* @var WhitelistDefaultInstaller
*/
private WhitelistDefaultInstaller $whitelistDefaultInstaller;

/**
* @param ModuleDataSetupInterface $moduleDataSetup
*/
public function __construct(ModuleDataSetupInterface $moduleDataSetup)
{
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
WhitelistDefaultInstaller $whitelistDefaultInstaller
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->whitelistDefaultInstaller = $whitelistDefaultInstaller;
}

/**
Expand All @@ -36,42 +45,7 @@ public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();

$whitelistEntries = [
$this->getWhitelistEntryAsArray(0, 'Rest API', '/rest', true, 'regex-all'),
$this->getWhitelistEntryAsArray(0, 'Customer Account Login', '/customer/account/login', true, 'regex-all'),
$this->getWhitelistEntryAsArray(0, 'Customer Account Logout', '/customer/account/logout', true, 'regex-all'),
$this->getWhitelistEntryAsArray(0, 'Customer Account Logout Success', '/customer/account/logoutSuccess', true, 'regex-all'),
$this->getWhitelistEntryAsArray(0, 'Customer Account Create', '/customer/account/create', true, 'regex-all'),
$this->getWhitelistEntryAsArray(0, 'Customer Account Create Password', '/customer/account/createPassword', true, 'regex-all'),
$this->getWhitelistEntryAsArray(0, 'Customer Account Forgot Password', '/customer/account/forgotpassword', true, 'regex-all'),
$this->getWhitelistEntryAsArray(0, 'Customer Account Forgot Password Post', '/customer/account/forgotpasswordpost', true, 'regex-all'),
$this->getWhitelistEntryAsArray(0, 'Customer Section Load', '/customer/section/load', true, 'regex-all'),
$this->getWhitelistEntryAsArray(0, 'Contact Us', '/contact', true, 'regex-all'),
$this->getWhitelistEntryAsArray(0, 'Help', '/help', true, 'regex-all'),
$this->getWhitelistEntryAsArray(0, 'Sitemap.xml', '/sitemap.xml', true, 'regex-all'),
$this->getWhitelistEntryAsArray(0, 'Robots.txt', '/robots.txt', true, 'regex-all'),
$this->getWhitelistEntryAsArray(0, 'Customer Account Dashboard', '/customer/account', true, 'regex-all'),
$this->getWhitelistEntryAsArray(0, 'Customer Account Reset Password Post', '/customer/account/resetpasswordpost'),
$this->getWhitelistEntryAsArray(0, 'Varnish ESI url', '/page_cache/block/esi/blocks'),
$this->getWhitelistEntryAsArray(0, 'Store-Switcher Redirect', '/stores/store/redirect'),
$this->getWhitelistEntryAsArray(0, 'Store-Switcher Switch', '/stores/store/switch'),
$this->getWhitelistEntryAsArray(0, 'Customer Create (Post)', '/customer/account/createpost'),
$this->getWhitelistEntryAsArray(0, 'Paypal', '/paypal/ipn/'),
];

foreach ($whitelistEntries as $entry) {
try {
// if the migration happens from an older version of the module, an exception will be thrown since the
// label needs to be unique. Splitting this patch into chunks and bind them to the specific module
// version as in UpgradeData.php does not seem to apply the older patches for a fresh install. This
// seems the only way to solve the issue for now.
$this->moduleDataSetup->getConnection()->insert(
$this->moduleDataSetup->getTable('bitexpert_forcelogin_whitelist'),
$entry
);
} catch (\Exception $e) {
}
}
$this->whitelistDefaultInstaller->install();

$this->moduleDataSetup->getConnection()->endSetup();

Expand All @@ -93,28 +67,4 @@ public static function getDependencies()
{
return [];
}

/**
* @param int $storeId
* @param string $label
* @param string $urlRule
* @param boolean $editable
* @param string $strategy
* @return array
*/
public static function getWhitelistEntryAsArray(
$storeId,
$label,
$urlRule,
$editable = false,
$strategy = 'default'
) {
return [
'store_id' => $storeId,
'label' => $label,
'url_rule' => $urlRule,
'editable' => $editable,
'strategy' => $strategy
];
}
}
24 changes: 24 additions & 0 deletions Test/DataProviders/WhitelistDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace BitExpert\ForceCustomerLogin\Test\DataProviders;

class WhitelistDataProvider
{
public static function get(): array
{
return [
'default' => [
[
'/url1' => [
'label' => 'label 1',
'strategy' => '',
],
'/url2' => [
'label' => 'label 2',
'strategy' => 'regex-all',
],
],
],
];
}
}
Loading
Loading