Skip to content

Commit

Permalink
[stable10] Changes updated by rewriting config.php should sent events
Browse files Browse the repository at this point in the history
When changes updated by rewriting config.php, for example
using method writeData() should also be logged. This change
would help to log these data. An example is updating
email settings from UI.

Signed-off-by: Sujith H <[email protected]>
  • Loading branch information
sharidas committed Apr 12, 2018
1 parent b237192 commit 30a2983
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 14 deletions.
44 changes: 30 additions & 14 deletions lib/private/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,22 @@ public function setValue($key, $value) {
* @return bool True if the file needs to be updated, false otherwise
*/
protected function set($key, $value) {
if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) {
// Add change
$this->cache[$key] = $value;
return true;
}
return $this->emittingCall(function (&$afterArray) use (&$key, &$value) {
if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) {
if (isset($this->cache[$key])) {
$afterArray['update'] = true;
$afterArray['oldvalue'] = $this->cache[$key];
}
// Add change
$this->cache[$key] = $value;
return true;
}

return false;
return false;
},[
'before' => ['key' => $key, 'value' => $value],
'after' => ['key' => $key, 'value' => $value, 'update' => false, 'oldvalue' => null]
], 'config', 'setvalue');
}

/**
Expand All @@ -184,15 +193,16 @@ public function deleteKey($key) {
if ($this->isReadOnly()) {
throw new \Exception('Config file is read only.');
}
$value = $this->cache[$key];
$this->emittingCall(function () use (&$key) {
if ($this->delete($key)) {
// Write changes
$this->writeData();
}
return true;
}, [
'before' => ['key' => $key],
'after' => ['key' => $key]
'before' => ['key' => $key, 'value' => $value],
'after' => ['key' => $key, 'value' => $value]
], 'config', 'deletevalue');
}

Expand All @@ -203,12 +213,18 @@ public function deleteKey($key) {
* @return bool True if the file needs to be updated, false otherwise
*/
protected function delete($key) {
if (isset($this->cache[$key])) {
// Delete key from cache
unset($this->cache[$key]);
return true;
}
return false;
return $this->emittingCall(function (&$afterArray) use (&$key) {
if (isset($this->cache[$key])) {
$afterArray['value'] = $this->cache[$key];
// Delete key from cache
unset($this->cache[$key]);
return true;
}
return false;
}, [
'before' => ['key' => $key, 'value' => null],
'after' => ['key' => $key, 'value' => null]
], 'config', 'deletevalue');
}

/**
Expand Down
47 changes: 47 additions & 0 deletions tests/lib/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ public function testSetValues() {
$this->assertAttributeEquals($this->initialConfig, 'cache', $this->config);
$this->assertStringEqualsFile($this->configFile, self::TESTCONTENT);

$calledBeforeUpdate = [];
\OC::$server->getEventDispatcher()->addListener('config.beforesetvalue',
function (GenericEvent $event) use (&$calledBeforeUpdate) {
$calledBeforeUpdate[] = 'config.beforesetvalue';
$calledBeforeUpdate[] = $event;
});
$calledAfterUpdate = [];
\OC::$server->getEventDispatcher()->addListener('config.aftersetvalue',
function (GenericEvent $event) use (&$calledAfterUpdate) {
$calledAfterUpdate[] = 'config.aftersetvalue';
$calledAfterUpdate[] = $event;
});
$this->config->setValues([
'foo' => 'moo',
'alcohol_free' => null,
Expand All @@ -128,10 +140,45 @@ public function testSetValues() {
$expectedConfig['foo'] = 'moo';
unset($expectedConfig['alcohol_free']);
$this->assertAttributeEquals($expectedConfig, 'cache', $this->config);
$this->assertInstanceOf(GenericEvent::class, $calledBeforeUpdate[1]);
$this->assertInstanceOf(GenericEvent::class, $calledAfterUpdate[1]);
$this->assertEquals('config.beforesetvalue', $calledBeforeUpdate[0]);
$this->assertEquals('config.aftersetvalue', $calledAfterUpdate[0]);
$this->assertArrayHasKey('key', $calledBeforeUpdate[1]);
$this->assertArrayHasKey('value', $calledBeforeUpdate[1]);
$this->assertArrayHasKey('key', $calledAfterUpdate[1]);
$this->assertArrayHasKey('value', $calledAfterUpdate[1]);
$this->assertArrayHasKey('update', $calledAfterUpdate[1]);
$this->assertArrayHasKey('oldvalue', $calledAfterUpdate[1]);

$expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
" 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n);\n";
$this->assertStringEqualsFile($this->configFile, $expected);

$calledBeforeDelete = [];
\OC::$server->getEventDispatcher()->addListener('config.beforedeletevalue',
function (GenericEvent $event) use (&$calledBeforeDelete) {
$calledBeforeDelete[] = 'config.beforedeletevalue';
$calledBeforeDelete[] = $event;
});
$calledAfterDelete = [];
\OC::$server->getEventDispatcher()->addListener('config.afterdeletevalue',
function (GenericEvent $event) use (&$calledAfterDelete) {
$calledAfterDelete[] = 'config.afterdeletevalue';
$calledAfterDelete[] = $event;
});

$this->config->setValues([
'foo' => null
]);
$this->assertInstanceOf(GenericEvent::class, $calledBeforeDelete[1]);
$this->assertInstanceOf(GenericEvent::class, $calledAfterDelete[1]);
$this->assertEquals('config.beforedeletevalue', $calledBeforeDelete[0]);
$this->assertEquals('config.afterdeletevalue', $calledAfterDelete[0]);
$this->assertArrayHasKey('key', $calledBeforeDelete[1]);
$this->assertArrayHasKey('value', $calledBeforeDelete[1]);
$this->assertArrayHasKey('key', $calledAfterDelete[1]);
$this->assertArrayHasKey('value', $calledAfterDelete[1]);
}

public function testDeleteKey() {
Expand Down

0 comments on commit 30a2983

Please sign in to comment.