Skip to content

Commit

Permalink
Add notification message when production mode is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Dec 2, 2024
1 parent eb2ec6c commit 4a14cc6
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 13 deletions.
24 changes: 19 additions & 5 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,35 @@ public function __construct(Context $context, protected DeploymentConfig $deploy
/**
* Check whether the module is enabled.
*/
public function isEnabled(): bool
public function isEnabledInCurrentArea(): bool
{
$enabled = $this->scopeConfig->isSetFlag('smile_debugtoolbar/configuration/enabled');
if (!$enabled) {
if (!$this->isEnabled()) {
return false;
}

$enabledAdmin = $this->scopeConfig->isSetFlag('smile_debugtoolbar/configuration/enabled_admin');
if (!$enabledAdmin && $this->isAdminArea()) {
if (!$this->isEnabledAdmin() && $this->isAdminArea()) {
return false;
}

return true;
}

/**
* Check whether the module is enabled globally.
*/
public function isEnabled(): bool
{
return $this->scopeConfig->isSetFlag('smile_debugtoolbar/configuration/enabled');
}

/**
* Check whether the module is enabled for the admin area.
*/
public function isEnabledAdmin(): bool
{
return $this->scopeConfig->isSetFlag('smile_debugtoolbar/configuration/enabled_admin');
}

/**
* Get the config value for keep_last_execution.
*/
Expand Down
61 changes: 61 additions & 0 deletions Model/Message/ProductionMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Smile\DebugToolbar\Model\Message;

use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\State;
use Magento\Framework\Notification\MessageInterface;
use Smile\DebugToolbar\Helper\Config;

/**
* Add zone blocks to the toolbar.
*/
class ProductionMode implements MessageInterface
{
public function __construct(
protected DeploymentConfig $deploymentConfig,
protected Config $configHelper
) {
}

/**
* @inheritdoc
*/
public function getIdentity()
{
return 'smile_debugtoolbar_production_mode';
}

/**
* @inheritdoc
*/
public function isDisplayed()
{
return $this->deploymentConfig->get(State::PARAM_MODE) !== State::MODE_PRODUCTION;
}

/**
* @inheritdoc
*/
public function getText()
{
return __(
<<<'EOT'
The DebugToolbar module is not supposed to be installed on production environments.
It may expose sensitive information if it is enabled.
EOT
);
}

/**
* @inheritdoc
*/
public function getSeverity()
{
return $this->configHelper->isEnabled()
? MessageInterface::SEVERITY_CRITICAL
: MessageInterface::SEVERITY_MAJOR;
}
}
2 changes: 1 addition & 1 deletion Observer/AddToolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(
*/
public function execute(Observer $observer)
{
if (!$this->configHelper->isEnabled()) {
if (!$this->configHelper->isEnabledInCurrentArea()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion Plugin/App/Action/AbstractActionPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(protected DataHelper $dataHelper, protected ConfigHe
*/
public function beforeDispatch(MagentoAction $subject, RequestInterface $request): array
{
if ($this->configHelper->isEnabled()) {
if ($this->configHelper->isEnabledInCurrentArea()) {
$className = get_class($subject);
$className = preg_replace('!\\\\Interceptor$!', '', $className);
$this->dataHelper->setValue('controller_classname', $className);
Expand Down
6 changes: 3 additions & 3 deletions Plugin/App/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(protected ConfigHelper $configHelper, protected Cach
*/
public function aroundLoad(CacheInterface $subject, Closure $closure, string $identifier): mixed
{
if (!$this->configHelper->isEnabled()) {
if (!$this->configHelper->isEnabledInCurrentArea()) {
return $closure($identifier);
}

Expand Down Expand Up @@ -54,7 +54,7 @@ public function aroundSave(
array $tags = [],
?int $lifeTime = null
): bool {
if (!$this->configHelper->isEnabled()) {
if (!$this->configHelper->isEnabledInCurrentArea()) {
return $closure($data, $identifier, $tags, $lifeTime);
}

Expand All @@ -77,7 +77,7 @@ public function aroundSave(
*/
public function aroundRemove(CacheInterface $subject, Closure $closure, string $identifier): bool
{
if (!$this->configHelper->isEnabled()) {
if (!$this->configHelper->isEnabledInCurrentArea()) {
return $closure($identifier);
}

Expand Down
2 changes: 1 addition & 1 deletion Plugin/App/HttpPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(protected DataHelper $dataHelper, protected ConfigHe
*/
public function beforeLaunch(MagentoHttp $subject): array
{
if ($this->configHelper->isEnabled()) {
if ($this->configHelper->isEnabledInCurrentArea()) {
$this->dataHelper->startTimer('app_http');
}

Expand Down
2 changes: 1 addition & 1 deletion ViewModel/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public function __construct(protected ConfigHelper $configHelper)
*/
public function isToolbarEnabled(): bool
{
return $this->configHelper->isEnabled();
return $this->configHelper->isEnabledInCurrentArea();
}
}
2 changes: 1 addition & 1 deletion ViewModel/Toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(protected DataHelper $dataHelper, protected ConfigHe
*/
public function isToolbarEnabled(): bool
{
return $this->configHelper->isEnabled();
return $this->configHelper->isEnabledInCurrentArea();
}

/**
Expand Down
9 changes: 9 additions & 0 deletions etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Notification\MessageList">
<arguments>
<argument name="messages" xsi:type="array">
<item name="smile_debugtoolbar" xsi:type="string">Smile\DebugToolbar\Model\Message\ProductionMode</item>
</argument>
</arguments>
</type>
</config>

0 comments on commit 4a14cc6

Please sign in to comment.