-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d398a32
Showing
11 changed files
with
368 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
namespace MageMaclean\RestrictAccess\Block\Adminhtml\System\Config\Form\Field; | ||
|
||
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray; | ||
|
||
class Routes extends AbstractFieldArray | ||
{ | ||
protected function _prepareToRender() | ||
{ | ||
$this->addColumn('route', ['label' => __('Route'), 'class' => 'required-entry']); | ||
$this->addColumn('name', ['label' => __('Name'), 'class' => 'required-entry']); | ||
$this->_addAfter = false; | ||
$this->_addButtonLabel = __('Add Custom Route'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
namespace MageMaclean\RestrictAccess\Config\Backend; | ||
|
||
use Magento\Framework\App\Cache\TypeListInterface; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\App\Config\Value as ConfigValue; | ||
use Magento\Framework\Data\Collection\AbstractDb; | ||
use Magento\Framework\Model\Context; | ||
use Magento\Framework\Model\ResourceModel\AbstractResource; | ||
use Magento\Framework\Registry; | ||
use Magento\Framework\Serialize\SerializerInterface; | ||
|
||
class Routes extends ConfigValue | ||
{ | ||
protected $serializer; | ||
|
||
public function __construct( | ||
SerializerInterface $serializer, | ||
Context $context, | ||
Registry $registry, | ||
ScopeConfigInterface $config, | ||
TypeListInterface $cacheTypeList, | ||
AbstractResource $resource = null, | ||
AbstractDb $resourceCollection = null, | ||
array $data = [] | ||
) { | ||
$this->serializer = $serializer; | ||
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); | ||
} | ||
|
||
public function beforeSave() | ||
{ | ||
$value = $this->getValue(); | ||
unset($value['__empty']); | ||
$encodedValue = $this->serializer->serialize($value); | ||
|
||
$this->setValue($encodedValue); | ||
} | ||
|
||
protected function _afterLoad() | ||
{ | ||
$value = $this->getValue(); | ||
if($value) { | ||
$decodedValue = $this->serializer->unserialize($value); | ||
$this->setValue($decodedValue); | ||
} else { | ||
$this->setValue(null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
namespace MageMaclean\RestrictAccess\Helper; | ||
|
||
use Magento\Framework\App\Helper\AbstractHelper; | ||
use Magento\Store\Model\ScopeInterface; | ||
use MageMaclean\CustomerShipping\Model\Carrier; | ||
|
||
class Data extends AbstractHelper | ||
{ | ||
|
||
public function __construct( | ||
\Magento\Framework\App\Helper\Context $context | ||
) { | ||
parent::__construct($context); | ||
} | ||
|
||
public function isEnabled() | ||
{ | ||
return $this->scopeConfig->isSetFlag('restrictaccess/general/enabled'); | ||
} | ||
|
||
public function canAccessCategory() { | ||
return !$this->getConfigData('catalog', 'category'); | ||
} | ||
|
||
public function canAccessProduct() { | ||
return !$this->getConfigData('catalog', 'product'); | ||
} | ||
|
||
public function canAccessSearch() { | ||
return !$this->getConfigData('catalog', 'search'); | ||
} | ||
|
||
public function canAccessCmsPage($pageId) { | ||
$pages = $this->getConfigData('cms', 'pages'); | ||
if($pages && !empty($pages)) { | ||
$pageIds = explode(",", $pages); | ||
return (in_array($pageId, $pageIds)) ? true : false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function getConfigData($group, $field, $storeId = null) | ||
{ | ||
return $this->scopeConfig->getValue( | ||
'restrictaccess/' . $group . '/' . $field, | ||
ScopeInterface::SCOPE_STORE, | ||
$storeId | ||
); | ||
} | ||
|
||
public function getCustomRoutes($storeId = null) | ||
{ | ||
$configValue = $this->getConfigData('custom', 'routes', $storeId); | ||
if(!$configValue) { | ||
return array( | ||
array('value' => 'unset', 'label' => "No custom routes have been set") | ||
); | ||
} | ||
|
||
$routes = $this->serializer->unserialize($configValue); | ||
$items = array(); | ||
if($routes && sizeof($routes)) { | ||
foreach($routes as $item) { | ||
$items[] = array('value' => $item['route'], 'label' => $item['name']); | ||
} | ||
} | ||
|
||
return $items; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace MageMaclean\RestrictAccess\Observer; | ||
|
||
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 $_helper; | ||
|
||
public function __construct( | ||
\Magento\Framework\Event\ManagerInterface $eventManager, | ||
\Magento\Framework\App\Response\Http $response, | ||
\Magento\Framework\UrlFactory $urlFactory, | ||
\Magento\Framework\App\Http\Context $context, | ||
\Magento\Framework\App\ActionFlag $actionFlag, | ||
\Magento\Framework\Message\ManagerInterface $messageManager, | ||
Helper $helper | ||
) | ||
{ | ||
$this->_response = $response; | ||
$this->_urlFactory = $urlFactory; | ||
$this->_context = $context; | ||
$this->_actionFlag = $actionFlag; | ||
$this->_messageManager = $messageManager; | ||
$this->_helper = $helper; | ||
} | ||
|
||
public function execute(Observer $observer) | ||
{ | ||
if(!$this->_helper->isEnabled()) { | ||
return; | ||
} | ||
|
||
$isCustomerLoggedIn = $this->_context->getValue(Context::CONTEXT_AUTH); | ||
if($isCustomerLoggedIn) { | ||
return; | ||
} | ||
|
||
$request = $observer->getEvent()->getRequest(); | ||
$actionFullName = strtolower($request->getFullActionName()); | ||
|
||
if($actionFullName === 'cms_page_view') { | ||
$pageId = $request->getParam('page_id', false); | ||
if($pageId && !$this->_helper->canAccessCmsPage($pageId)) { | ||
$this->_messageManager->addError($this->_helper->getConfigData("cms", "message")); | ||
$this->_response->setRedirect($this->_urlFactory->create()->getUrl('customer/account/login')); | ||
} | ||
} else { | ||
$restrictRoutes = []; | ||
if($customRoutes = $this->_helper->getCustomRoutes()) { | ||
foreach($customRoutes as $customRoute) { | ||
$restrictRoutes[] = $customRoute['route']; | ||
} | ||
if (in_array($actionFullName, $restrictRoutes)) { | ||
$this->_messageManager->addError($this->_helper->getConfigData("custom", "message")); | ||
$this->_response->setRedirect($this->_urlFactory->create()->getUrl('customer/account/login')); | ||
return; | ||
} | ||
} | ||
|
||
$restrictRoutes = []; | ||
if(!$this->_helper->canAccessCategory()) { | ||
$restrictRoutes[] = 'catalog_category_view'; | ||
$restrictRoutes[] = 'catalog_category_index'; | ||
} | ||
|
||
if(!$this->_helper->canAccessProduct()) { | ||
$restrictRoutes[] = 'catalog_product_view'; | ||
} | ||
|
||
if(!$this->_helper->canAccessSearch()) { | ||
$restrictRoutes[] = 'catalogsearch_result_index'; | ||
} | ||
|
||
if (in_array($actionFullName, $restrictRoutes)) { | ||
$this->_messageManager->addError($this->_helper->getConfigData("general", "message")); | ||
$this->_response->setRedirect($this->_urlFactory->create()->getUrl('customer/account/login')); | ||
return; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "magemaclean/m2-restrictaccess", | ||
"description": "Magento 2 Restrict Access to catalog, cms pages or custom routes", | ||
"require": { | ||
"magemaclean/m2-core": "^1.0.0" | ||
}, | ||
"type": "magento2-module", | ||
"version": "1.0.1", | ||
"license": [ | ||
"OSL-3.0" | ||
], | ||
"minimum-stability": "dev", | ||
"authors": [ | ||
{ | ||
"name": "MageMaclean", | ||
"email": "[email protected]", | ||
"homepage": "https://magemaclean.com", | ||
"role": "Leader" | ||
} | ||
], | ||
"autoload": { | ||
"files": [ | ||
"registration.php" | ||
], | ||
"psr-4": { | ||
"MageMaclean\\RestrictAccess\\": "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0"?> | ||
|
||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd"> | ||
<acl> | ||
<resources> | ||
<resource id="Magento_Backend::admin"> | ||
<resource id="Magento_Backend::stores"> | ||
<resource id="Magento_Backend::stores_settings"> | ||
<resource id="Magento_Config::config"> | ||
<resource id="MageMaclean_RestrictAccess::configuration" title="MageMaclean Restrict Access"/> | ||
</resource> | ||
</resource> | ||
</resource> | ||
</resource> | ||
</resources> | ||
</acl> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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"> | ||
<label>Login Catalog</label> | ||
<tab>magemaclean</tab> | ||
<resource>MageMaclean_RestrictAccess::configuration</resource> | ||
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0"> | ||
<label>General</label> | ||
<field id="enabled" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0"> | ||
<label>Enabled</label> | ||
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model> | ||
</field> | ||
<field id="message" translate="label" type="textarea" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<label>Message</label> | ||
</field> | ||
</group> | ||
<group id="catalog" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0"> | ||
<label>Restrict Catalog</label> | ||
<field id="category" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0"> | ||
<label>Category</label> | ||
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model> | ||
</field> | ||
<field id="product" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="0"> | ||
<label>Product</label> | ||
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model> | ||
</field> | ||
<field id="search" translate="label" type="select" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="0"> | ||
<label>Search</label> | ||
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model> | ||
</field> | ||
</group> | ||
<group id="cms" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0"> | ||
<label>Restrict CMS</label> | ||
<field id="pages" translate="label" type="multiselect" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0"> | ||
<label>Pages</label> | ||
<source_model>Magento\Cms\Model\Config\Source\Page</source_model> | ||
</field> | ||
<field id="message" translate="label" type="textarea" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<label>Message</label> | ||
</field> | ||
</group> | ||
|
||
<group id="custom" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0"> | ||
<label>Custom Routes</label> | ||
<field id="routes" translate="label" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="0"> | ||
<label>Routes</label> | ||
<frontend_model>MageMaclean\RestrictAccess\Block\Adminhtml\System\Config\Form\Field\Routes</frontend_model> | ||
<backend_model>MageMaclean\RestrictAccess\Config\Backend\Routes</backend_model> | ||
<comment>Add your custom routes you wish to restrict access to. Example: module_controller_action</comment> | ||
</field> | ||
<field id="message" translate="label" type="textarea" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<label>Message</label> | ||
</field> | ||
</group> | ||
</section> | ||
</system> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> | ||
<default> | ||
<restrictaccess> | ||
<general> | ||
<enabled>0</enabled> | ||
<message>You must be logged in to access the catalog.</message> | ||
</general> | ||
<catalog> | ||
<category>1</category> | ||
<product>1</product> | ||
<search>1</search> | ||
</catalog> | ||
<cms> | ||
<page></page> | ||
<message>You must be logged in to access the requested page.</message> | ||
</cms> | ||
</restrictaccess> | ||
</default> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | ||
<event name="controller_action_predispatch"> | ||
<observer name="restrict_access" instance="MageMaclean\RestrictAccess\Observer\RestrictAccess" /> | ||
</event> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="MageMaclean_RestrictAccess" setup_version="1.0.0"> | ||
<sequence> | ||
<module name="MageMaclean_Core"/> | ||
</sequence> | ||
</module> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
\Magento\Framework\Component\ComponentRegistrar::register( | ||
\Magento\Framework\Component\ComponentRegistrar::MODULE, | ||
'MageMaclean_RestrictAccess', | ||
__DIR__ | ||
); |