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

[BUGFIX] Add plugin-enabled check condition to CustomObjectPermissions… #292

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,7 @@
'class' => ConfigProvider::class,
'arguments' => [
'mautic.helper.core_parameters',
'database_connection',
],
],
'custom_field.type.provider' => [
Expand Down
29 changes: 27 additions & 2 deletions Provider/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

namespace MauticPlugin\CustomObjectsBundle\Provider;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use MauticPlugin\CustomObjectsBundle\Entity\CustomObject;

class ConfigProvider
{
/**
* @var string
*/
public const CONFIG_PLUGIN_NAME = 'CustomObjectsBundle';
public const CONFIG_PARAM_ENABLED = 'custom_objects_enabled';
public const CONFIG_PARAM_ITEM_VALUE_TO_CONTACT_RELATION_LIMIT = 'custom_object_item_value_to_contact_relation_limit';

Expand All @@ -19,16 +23,37 @@ class ConfigProvider
*/
private $coreParametersHelper;

public function __construct(CoreParametersHelper $coreParametersHelper)
/**
* @var Connection
*/
private $connection;

public function __construct(CoreParametersHelper $coreParametersHelper, Connection $connection)
{
$this->coreParametersHelper = $coreParametersHelper;
$this->connection = $connection;
}

/**
* Returns true if the Custom Objects plugin is enabled.
*/
public function pluginIsEnabled(): bool
{
return (bool) $this->coreParametersHelper->get(self::CONFIG_PARAM_ENABLED, true);
$pluginEnabled = (bool) $this->coreParametersHelper->get(self::CONFIG_PARAM_ENABLED, true);
if (!$pluginEnabled) {
return false;
}

try {
$pluginWasInstalledBefore = $this->connection
->executeQuery('SELECT id FROM plugins WHERE bundle=:pluginName', ['pluginName' => self::CONFIG_PLUGIN_NAME])
->rowCount();
$customObjectsTableExists = $this->connection
->executeQuery('SHOW TABLES LIKE :tableName', ['tableName' => CustomObject::TABLE_NAME])
->rowCount();
return $pluginWasInstalledBefore && $customObjectsTableExists;
} catch (Exception $e) {
return false;
}
}
}
4 changes: 4 additions & 0 deletions Security/Permissions/CustomObjectPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public function __construct(
*/
public function definePermissions(): void
{
if (!$this->isEnabled()) {
return;
}

$this->addExtendedPermissions(['custom_fields', self::NAME]);

$customObjects = $this->getCustomObjects();
Expand Down