Skip to content

Commit

Permalink
Updated to handle noroute page
Browse files Browse the repository at this point in the history
  • Loading branch information
magemaclean committed Oct 11, 2019
1 parent 7359595 commit 9d442d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
22 changes: 19 additions & 3 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,32 @@ public function canAccessSearch() {
return !$this->getConfigData('catalog', 'search');
}

public function canAccessCmsPage($pageId) {
public function canAccessCmsPage($page) {
$pages = $this->getConfigData('cms', 'pages');
if($pages && !empty($pages)) {
$pageIds = explode(",", $pages);
return (in_array($pageId, $pageIds)) ? true : false;
$pages = explode(",", $pages);
return (in_array($page, $pages)) ? false : true;
}

return true;
}

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

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

public function getNoroutePage($storeId = null) {
return $this->scopeConfig->getValue(
'web/default/cms_no_route',
ScopeInterface::SCOPE_STORE,
$storeId
);
}

public function getConfigData($group, $field, $storeId = null)
{
return $this->scopeConfig->getValue(
Expand Down
12 changes: 9 additions & 3 deletions Observer/RestrictAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ public function execute(Observer $observer)
$request = $observer->getEvent()->getRequest();
$actionFullName = strtolower($request->getFullActionName());

if($actionFullName === 'cms_page_view') {
if($actionFullName === 'cms_noroute_index') {
$noroutePage = $this->_helper->getNoroutePage();
if(!$this->_helper->canAccessCmsPage($noroutePage)) {
$this->_messageManager->addError($this->_helper->getConfigData("cms", "message"));
$this->_response->setRedirect($this->_urlFactory->create()->getUrl('customer/account/login'));
return;
}
} else if($actionFullName === 'cms_page_view') {
$pageId = $request->getParam('page_id', false);
if($pageId && !$this->_helper->canAccessCmsPage($pageId)) {
if($pageId && !$this->_helper->canAccessCmsPageId($pageId)) {
$this->_messageManager->addError($this->_helper->getConfigData("cms", "message"));
$this->_response->setRedirect($this->_urlFactory->create()->getUrl('customer/account/login'));
}
Expand Down Expand Up @@ -81,7 +88,6 @@ public function execute(Observer $observer)
if (in_array($actionFullName, $restrictRoutes)) {
$this->_messageManager->addError($this->_helper->getConfigData("catalog", "message"));
$this->_response->setRedirect($this->_urlFactory->create()->getUrl('customer/account/login'));
return;
}
}
}
Expand Down

0 comments on commit 9d442d0

Please sign in to comment.