Skip to content

Commit 8a155c8

Browse files
MoongazerMedizents-navghane
authored
[BUGFIX] Add plugin-enabled check condition to CustomObjectPermissions… (#292)
* [BUGIX] Add plugin-enabled check condition to CustomObjectPermissions.php to solve issue #290 * [BUGFIX] Refactor ConfigProvider::pluginIsEnabled() to perform propper tests if the plugin was installed successfully * [TASK] Re-add check for CONFIG_PARAM_ENABLED via core-parameter-helper * Update Provider/ConfigProvider.php --------- Co-authored-by: Medizen <[email protected]> Co-authored-by: Tejas Navghane <[email protected]>
1 parent 467455c commit 8a155c8

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

Config/config.php

+1
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,7 @@
10421042
'class' => ConfigProvider::class,
10431043
'arguments' => [
10441044
'mautic.helper.core_parameters',
1045+
'database_connection',
10451046
],
10461047
],
10471048
'custom_field.type.provider' => [

Provider/ConfigProvider.php

+27-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
namespace MauticPlugin\CustomObjectsBundle\Provider;
66

7+
use Doctrine\DBAL\Connection;
8+
use Doctrine\DBAL\Exception;
79
use Mautic\CoreBundle\Helper\CoreParametersHelper;
10+
use MauticPlugin\CustomObjectsBundle\Entity\CustomObject;
811

912
class ConfigProvider
1013
{
1114
/**
1215
* @var string
1316
*/
17+
public const CONFIG_PLUGIN_NAME = 'CustomObjectsBundle';
1418
public const CONFIG_PARAM_ENABLED = 'custom_objects_enabled';
1519
public const CONFIG_PARAM_ITEM_VALUE_TO_CONTACT_RELATION_LIMIT = 'custom_object_item_value_to_contact_relation_limit';
1620

@@ -21,17 +25,38 @@ class ConfigProvider
2125
*/
2226
private $coreParametersHelper;
2327

24-
public function __construct(CoreParametersHelper $coreParametersHelper)
28+
/**
29+
* @var Connection
30+
*/
31+
private $connection;
32+
33+
public function __construct(CoreParametersHelper $coreParametersHelper, Connection $connection)
2534
{
2635
$this->coreParametersHelper = $coreParametersHelper;
36+
$this->connection = $connection;
2737
}
2838

2939
/**
3040
* Returns true if the Custom Objects plugin is enabled.
3141
*/
3242
public function pluginIsEnabled(): bool
3343
{
34-
return (bool) $this->coreParametersHelper->get(self::CONFIG_PARAM_ENABLED, true);
44+
$pluginEnabled = (bool) $this->coreParametersHelper->get(self::CONFIG_PARAM_ENABLED, true);
45+
if (!$pluginEnabled) {
46+
return false;
47+
}
48+
49+
try {
50+
$pluginWasInstalledBefore = $this->connection
51+
->executeQuery('SELECT id FROM plugins WHERE bundle=:pluginName', ['pluginName' => self::CONFIG_PLUGIN_NAME])
52+
->rowCount();
53+
$customObjectsTableExists = $this->connection
54+
->executeQuery('SHOW TABLES LIKE :tableName', ['tableName' => CustomObject::TABLE_NAME])
55+
->rowCount();
56+
return $pluginWasInstalledBefore && $customObjectsTableExists;
57+
} catch (Exception $e) {
58+
return false;
59+
}
3560
}
3661

3762
public function isCustomObjectMergeFilterEnabled(): bool

Security/Permissions/CustomObjectPermissions.php

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public function __construct(
5656
*/
5757
public function definePermissions(): void
5858
{
59+
if (!$this->isEnabled()) {
60+
return;
61+
}
62+
5963
$this->addExtendedPermissions(['custom_fields', self::NAME]);
6064

6165
$customObjects = $this->getCustomObjects();

0 commit comments

Comments
 (0)