Skip to content

Commit

Permalink
feat: make sure config file is writable
Browse files Browse the repository at this point in the history
  • Loading branch information
jsantos42 committed Oct 11, 2024
1 parent 20b148e commit 6989fcc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
10 changes: 10 additions & 0 deletions core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,16 @@ public function getConfigNotWritableException()
return new MissingFilePermissionException(Piwik::translate('General_ConfigFileIsNotWritable', array("(" . $path . ")", "")));
}

/**
* @throws MissingFilePermissionException If config file is not writable.
*/
public function checkConfigIsWritable()
{
if (!$this->isFileWritable()) {
throw $this->getConfigNotWritableException();
}
}

/**
* Convenience method for setting settings in a single section. Will set them in a new array first
* to be compatible with certain PHP versions.
Expand Down
14 changes: 6 additions & 8 deletions core/Plugin/ControllerAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Piwik\Container\StaticContainer;
use Piwik\Date;
use Piwik\Development;
use Piwik\Exception\MissingFilePermissionException;
use Piwik\Menu\MenuAdmin;
use Piwik\Menu\MenuTop;
use Piwik\Notification;
Expand Down Expand Up @@ -202,14 +203,11 @@ private static function notifyIfDevelopmentModeOnButNotInstalledThroughGit()
*/
public static function displayWarningIfConfigFileNotWritable()
{
$isConfigFileWritable = PiwikConfig::getInstance()->isFileWritable();

if (!$isConfigFileWritable) {
$exception = PiwikConfig::getInstance()->getConfigNotWritableException();
$message = $exception->getMessage();

$notification = new Notification($message);
$notification->raw = true;
try {
PiwikConfig::getInstance()->checkConfigIsWritable();
} catch (MissingFilePermissionException $exception) {
$notification = new Notification($exception->getMessage());
$notification->raw = true;
$notification->context = Notification::CONTEXT_WARNING;
Notification\Manager::notify('ControllerAdmin_ConfigNotWriteable', $notification);
}
Expand Down
3 changes: 3 additions & 0 deletions plugins/CorePluginsAdmin/Commands/InstallPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Piwik\Plugins\CorePluginsAdmin\Commands;

use Piwik\Config as PiwikConfig;
use Piwik\Container\StaticContainer;
use Piwik\Plugin\ConsoleCommand;
use Piwik\Plugin\Manager;
Expand All @@ -29,6 +30,8 @@ protected function configure(): void

protected function doExecute(): int
{
PiwikConfig::getInstance()->checkConfigIsWritable();

$input = $this->getInput();
$output = $this->getOutput();
$pluginManager = Manager::getInstance();
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPUnit/Unit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\TestCase;
use Piwik\Application\Kernel\GlobalSettingsProvider;
use Piwik\Config;
use Piwik\Exception\MissingFilePermissionException;

class DumpConfigTestMockIniFileChain extends Config\IniFileChain
{
Expand Down Expand Up @@ -562,4 +563,27 @@ public function testSanityCheckFails()
$this->assertTrue($config->sanityCheck($userFile, $correctContent));
$this->assertSame($userFile, $expectedPath);
}

public function testCheckConfigIsWritableNotThrowsExceptionWhenWritable()
{
$this->assertTrue($this->checkConfigIsWritable('Config'));
}

public function testCheckConfigIsWritableThrowsExceptionWhenNotWritable()
{
$this->expectException(MissingFilePermissionException::class);
$this->expectExceptionMessage('ConfigFileIsNotWritable');
$this->checkConfigIsWritable('ConfigNotExists');
}

private function checkConfigIsWritable(string $directory): bool
{
$userFile = PIWIK_INCLUDE_PATH . "/tests/resources/$directory/config.ini.php";
$globalFile = PIWIK_INCLUDE_PATH . "/tests/resources/$directory/global.ini.php";
$commonFile = PIWIK_INCLUDE_PATH . "/tests/resources/$directory/common.config.ini.php";

$config = new Config(new GlobalSettingsProvider($globalFile, $userFile, $commonFile));
$config->checkConfigIsWritable();
return true;
}
}

0 comments on commit 6989fcc

Please sign in to comment.