Skip to content

Commit

Permalink
Updated store view controls and configuration
Browse files Browse the repository at this point in the history
Updated observer to include store ID parameter passing to helper
  • Loading branch information
magemaclean committed May 21, 2020
1 parent 1883ad9 commit 463e14f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
20 changes: 10 additions & 10 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ public function isEnabled($storeId = null)
);
}

public function canAccessCategory() {
return !$this->getConfigData('catalog', 'category');
public function canAccessCategory($storeId = null) {
return !$this->getConfigData('catalog', 'category', $storeId);
}

public function canAccessProduct() {
return !$this->getConfigData('catalog', 'product');
public function canAccessProduct($storeId = null) {
return !$this->getConfigData('catalog', 'product', $storeId);
}

public function canAccessSearch() {
return !$this->getConfigData('catalog', 'search');
public function canAccessSearch($storeId = null) {
return !$this->getConfigData('catalog', 'search', $storeId);
}

public function canAccessCmsPage($page) {
$pages = $this->getConfigData('cms', 'pages');
public function canAccessCmsPage($page, $storeId = null) {
$pages = $this->getConfigData('cms', 'pages', $storeId);
if($pages && !empty($pages)) {
$pages = explode(",", $pages);
return (in_array($page, $pages)) ? false : true;
Expand All @@ -49,12 +49,12 @@ public function canAccessCmsPage($page) {
return true;
}

public function canAccessCmsPageId($pageId) {
public function canAccessCmsPageId($pageId, $storeId = null) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cmsPage = $objectManager->get('\Magento\Cms\Model\Page');
$cmsPage->load($pageId);

return $this->canAccessCmsPage($cmsPage->getIdentifier());
return $this->canAccessCmsPage($cmsPage->getIdentifier(), $storeId);
}

public function getNoroutePage($storeId = null) {
Expand Down
35 changes: 20 additions & 15 deletions Observer/RestrictAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,51 @@
use Magento\Customer\Model\Context;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;

use MageMaclean\RestrictAccess\Helper\Data as Helper;

class RestrictAccess implements ObserverInterface
{
protected $_logger;
protected $_response;
protected $_urlInterface;
protected $_urlFactory;
protected $_context;
protected $_actionFlag;
protected $_messageManagaer;
protected $_customerSession;
protected $_storeManager;
protected $_helper;

public function __construct(
\Magento\Framework\Event\ManagerInterface $eventManager,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\App\Response\Http $response,
\Magento\Framework\UrlInterface $urlInterface,
\Magento\Framework\UrlFactory $urlFactory,
\Magento\Framework\App\Http\Context $context,
\Magento\Framework\App\ActionFlag $actionFlag,
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Customer\Model\Session $customerSession,
\Magento\Store\Model\StoreManagerInterface $storeManager,
Helper $helper
)
{
$this->_logger = $logger;
$this->_response = $response;
$this->_urlFactory = $urlFactory;
$this->_urlInterface = $urlInterface;
$this->_context = $context;
$this->_actionFlag = $actionFlag;
$this->_messageManager = $messageManager;
$this->_customerSession = $customerSession;
$this->_storeManager = $storeManager;
$this->_helper = $helper;
}

public function execute(Observer $observer)
{
if(!$this->_helper->isEnabled()) {
$storeId = $this->_storeManager->getStore()->getId();
if(!$this->_helper->isEnabled($storeId)) {
return;
}

Expand All @@ -57,42 +62,42 @@ public function execute(Observer $observer)
$actionFullName = strtolower($request->getFullActionName());

if($actionFullName === 'cms_noroute_index') {
$noroutePage = $this->_helper->getNoroutePage();
if(!$this->_helper->canAccessCmsPage($noroutePage)) {
return $this->_restrictAccessRedirect($this->_helper->getConfigData("cms", "message"));
$noroutePage = $this->_helper->getNoroutePage($storeId);
if(!$this->_helper->canAccessCmsPage($noroutePage, $storeId)) {
return $this->_restrictAccessRedirect($this->_helper->getConfigData("cms", "message", $storeId));
}
} else if($actionFullName === 'cms_page_view') {
$pageId = $request->getParam('page_id', false);
if($pageId && !$this->_helper->canAccessCmsPageId($pageId)) {
return $this->_restrictAccessRedirect($this->_helper->getConfigData("cms", "message"));
if($pageId && !$this->_helper->canAccessCmsPageId($pageId, $storeId)) {
return $this->_restrictAccessRedirect($this->_helper->getConfigData("cms", "message", $storeId));
}
} else {
$restrictRoutes = [];
if($customRoutes = $this->_helper->getCustomRoutes()) {
if($customRoutes = $this->_helper->getCustomRoutes($storeId)) {
foreach($customRoutes as $customRoute) {
$restrictRoutes[] = $customRoute['route'];
$restrictRoutes[] = $customRoute['value'];
}
if (in_array($actionFullName, $restrictRoutes)) {
return $this->_restrictAccessRedirect($this->_helper->getConfigData("custom", "message"));
return $this->_restrictAccessRedirect($this->_helper->getConfigData("custom", "message", $storeId));
}
}

$restrictRoutes = [];
if(!$this->_helper->canAccessCategory()) {
if(!$this->_helper->canAccessCategory($storeId)) {
$restrictRoutes[] = 'catalog_category_view';
$restrictRoutes[] = 'catalog_category_index';
}

if(!$this->_helper->canAccessProduct()) {
if(!$this->_helper->canAccessProduct($storeId)) {
$restrictRoutes[] = 'catalog_product_view';
}

if(!$this->_helper->canAccessSearch()) {
if(!$this->_helper->canAccessSearch($storeId)) {
$restrictRoutes[] = 'catalogsearch_result_index';
}

if (in_array($actionFullName, $restrictRoutes)) {
return $this->_restrictAccessRedirect($this->_helper->getConfigData("catalog", "message"));
return $this->_restrictAccessRedirect($this->_helper->getConfigData("catalog", "message", $storeId));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"magemaclean/m2-core": "^1.0.0"
},
"type": "magento2-module",
"version": "1.0.5",
"version": "1.0.6",
"license": [
"OSL-3.0"
],
Expand Down
8 changes: 4 additions & 4 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="restrictaccess" translate="label" type="text" sortOrder="401" showInDefault="1" showInWebsite="1" showInStore="0">
<section id="restrictaccess" translate="label" type="text" sortOrder="401" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Restrict Guest Access</label>
<tab>magemaclean</tab>
<resource>MageMaclean_RestrictAccess::configuration</resource>
Expand All @@ -12,7 +12,7 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
<group id="catalog" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
<group id="catalog" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Restrict Catalog</label>
<field id="category" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Category</label>
Expand All @@ -30,7 +30,7 @@
<label>Message</label>
</field>
</group>
<group id="cms" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
<group id="cms" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Restrict CMS</label>
<field id="pages" translate="label" type="multiselect" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Pages</label>
Expand All @@ -41,7 +41,7 @@
</field>
</group>

<group id="custom" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
<group id="custom" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Custom Routes</label>
<field id="routes" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Routes</label>
Expand Down

0 comments on commit 463e14f

Please sign in to comment.