Skip to content

Commit

Permalink
DirectorActivityLog, others: constants, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Gelf committed Aug 20, 2022
1 parent 047b14c commit 1c30412
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 87 deletions.
7 changes: 4 additions & 3 deletions application/clicommands/SyncruleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Icinga\Module\Director\Clicommands;

use Icinga\Module\Director\Cli\Command;
use Icinga\Module\Director\Objects\DirectorActivityLog;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\SyncRule;
use RuntimeException;
Expand Down Expand Up @@ -98,9 +99,9 @@ protected function getExpectedModificationCounts(SyncRule $rule)
}

return (object) [
'create' => $create,
'modify' => $modify,
'delete' => $delete,
DirectorActivityLog::ACTION_CREATE => $create,
DirectorActivityLog::ACTION_MODIFY => $modify,
DirectorActivityLog::ACTION_DELETE => $delete,
];
}

Expand Down
13 changes: 9 additions & 4 deletions application/forms/SyncCheckForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Icinga\Module\Director\Forms;

use Icinga\Module\Director\Objects\DirectorActivityLog;
use Icinga\Module\Director\Objects\SyncRule;
use Icinga\Module\Director\Web\Form\DirectorForm;

Expand Down Expand Up @@ -31,16 +32,20 @@ public function onSuccess()
$this->notifySuccess(
$this->translate(('This Sync Rule would apply new changes'))
);
$sum = array('create' => 0, 'modify' => 0, 'delete' => 0);
$sum = [
DirectorActivityLog::ACTION_CREATE => 0,
DirectorActivityLog::ACTION_MODIFY => 0,
DirectorActivityLog::ACTION_DELETE => 0
];

// TODO: Preview them? Like "hosta, hostb and 4 more would be...
foreach ($this->rule->getExpectedModifications() as $object) {
if ($object->shouldBeRemoved()) {
$sum['delete']++;
$sum[DirectorActivityLog::ACTION_DELETE]++;
} elseif (! $object->hasBeenLoadedFromDb()) {
$sum['create']++;
$sum[DirectorActivityLog::ACTION_CREATE]++;
} elseif ($object->hasBeenModified()) {
$sum['modify']++;
$sum[DirectorActivityLog::ACTION_MODIFY]++;
}
}

Expand Down
7 changes: 4 additions & 3 deletions library/Director/Db/Branch/BranchActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Icinga\Module\Director\Data\Json;
use Icinga\Module\Director\Data\SerializableValue;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\DirectorActivityLog;
use Icinga\Module\Director\Objects\IcingaObject;
use InvalidArgumentException;
use Ramsey\Uuid\Uuid;
Expand All @@ -19,9 +20,9 @@ class BranchActivity
{
const DB_TABLE = 'director_branch_activity';

const ACTION_CREATE = 'create';
const ACTION_MODIFY = 'modify';
const ACTION_DELETE = 'delete';
const ACTION_CREATE = DirectorActivityLog::ACTION_CREATE;
const ACTION_MODIFY = DirectorActivityLog::ACTION_MODIFY;
const ACTION_DELETE = DirectorActivityLog::ACTION_DELETE;

/** @var int */
protected $timestampNs;
Expand Down
61 changes: 32 additions & 29 deletions library/Director/Objects/DirectorActivityLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Util;
use Icinga\Authentication\Auth;
use Icinga\Application\Icinga;
use Icinga\Application\Logger;

class DirectorActivityLog extends DbObject
{
const ACTION_CREATE = 'create';
const ACTION_DELETE = 'delete';
const ACTION_MODIFY = 'modify';

/** @deprecated */
const AUDIT_REMOVE = 'remove';

protected $table = 'director_activity_log';

protected $keyName = 'id';
Expand Down Expand Up @@ -104,25 +110,25 @@ public static function logCreation(IcingaObject $object, Db $db)
$type = $object->getTableName();
$newProps = $object->toJson(null, true);

$data = array(
$data = [
'object_name' => $name,
'action_name' => 'create',
'action_name' => self::ACTION_CREATE,
'author' => static::username(),
'object_type' => $type,
'new_properties' => $newProps,
'change_time' => date('Y-m-d H:i:s'),
'parent_checksum' => $db->getLastActivityChecksum()
);
];

$data['checksum'] = sha1(json_encode($data), true);
$data['parent_checksum'] = hex2bin($data['parent_checksum']);

static::audit($db, array(
'action' => 'create',
static::audit($db, [
'action' => self::ACTION_CREATE,
'object_type' => $type,
'object_name' => $name,
'new_props' => $newProps,
));
]);

return static::create($data)->store($db);
}
Expand All @@ -134,27 +140,27 @@ public static function logModification(IcingaObject $object, Db $db)
$oldProps = json_encode($object->getPlainUnmodifiedObject());
$newProps = $object->toJson(null, true);

$data = array(
$data = [
'object_name' => $name,
'action_name' => 'modify',
'action_name' => self::ACTION_MODIFY,
'author' => static::username(),
'object_type' => $type,
'old_properties' => $oldProps,
'new_properties' => $newProps,
'change_time' => date('Y-m-d H:i:s'),
'parent_checksum' => $db->getLastActivityChecksum()
);
];

$data['checksum'] = sha1(json_encode($data), true);
$data['parent_checksum'] = hex2bin($data['parent_checksum']);

static::audit($db, array(
'action' => 'modify',
static::audit($db, [
'action' => self::ACTION_MODIFY,
'object_type' => $type,
'object_name' => $name,
'old_props' => $oldProps,
'new_props' => $newProps,
));
]);

return static::create($data)->store($db);
}
Expand All @@ -165,45 +171,42 @@ public static function logRemoval(IcingaObject $object, Db $db)
$type = $object->getTableName();
$oldProps = json_encode($object->getPlainUnmodifiedObject());

$data = array(
$data = [
'object_name' => $name,
'action_name' => 'delete',
'action_name' => self::ACTION_DELETE,
'author' => static::username(),
'object_type' => $type,
'old_properties' => $oldProps,
'change_time' => date('Y-m-d H:i:s'),
'parent_checksum' => $db->getLastActivityChecksum()
);
];

$data['checksum'] = sha1(json_encode($data), true);
$data['parent_checksum'] = hex2bin($data['parent_checksum']);

static::audit($db, array(
'action' => 'remove',
static::audit($db, [
'action' => self::AUDIT_REMOVE,
'object_type' => $type,
'object_name' => $name,
'old_props' => $oldProps
));
]);

return static::create($data)->store($db);
}

public static function audit(Db $db, $properties)
{
if ($db->settings()->enable_audit_log !== 'y') {
if ($db->settings()->get('enable_audit_log') !== 'y') {
return;
}

$log = array();
$properties = array_merge(
array(
'username' => static::username(),
'address' => static::ip(),
),
$properties
);
$log = [];
$properties = array_merge([
'username' => static::username(),
'address' => static::ip(),
], $properties);

foreach ($properties as $key => & $val) {
foreach ($properties as $key => $val) {
$log[] = "$key=" . json_encode($val);
}

Expand Down
7 changes: 7 additions & 0 deletions library/Director/Objects/SyncRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ public static function start(SyncRule $rule)
$rule->getConnection()
);
}

public function countActivities()
{
return (int) $this->get('objects_deleted')
+ (int) $this->get('objects_created')
+ (int) $this->get('objects_modified');
}
}
21 changes: 13 additions & 8 deletions library/Director/Web/Widget/ActivityLogInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Icinga\Module\Director\Web\Widget;

use gipfl\Json\JsonString;
use Icinga\Module\Director\Objects\DirectorActivityLog;
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlElement;
use Icinga\Date\DateFormatter;
Expand Down Expand Up @@ -343,7 +344,6 @@ protected function objectKey()
/**
* @param Url|null $url
* @return Tabs
* @throws ProgrammingError
*/
public function getTabs(Url $url = null)
{
Expand All @@ -357,13 +357,12 @@ public function getTabs(Url $url = null)
/**
* @param Url $url
* @return Tabs
* @throws ProgrammingError
*/
public function createTabs(Url $url)
{
$entry = $this->entry;
$tabs = new Tabs();
if ($entry->action_name === 'modify') {
if ($entry->action_name === DirectorActivityLog::ACTION_MODIFY) {
$tabs->add('diff', [
'label' => $this->translate('Diff'),
'url' => $url->without('show')->with('id', $entry->id)
Expand All @@ -372,7 +371,10 @@ public function createTabs(Url $url)
$this->defaultTab = 'diff';
}

if (in_array($entry->action_name, ['create', 'modify'])) {
if (in_array($entry->action_name, [
DirectorActivityLog::ACTION_CREATE,
DirectorActivityLog::ACTION_MODIFY,
])) {
$tabs->add('new', [
'label' => $this->translate('New object'),
'url' => $url->with(['id' => $entry->id, 'show' => 'new'])
Expand All @@ -383,7 +385,10 @@ public function createTabs(Url $url)
}
}

if (in_array($entry->action_name, ['delete', 'modify'])) {
if (in_array($entry->action_name, [
DirectorActivityLog::ACTION_DELETE,
DirectorActivityLog::ACTION_MODIFY,
])) {
$tabs->add('old', [
'label' => $this->translate('Former object'),
'url' => $url->with(['id' => $entry->id, 'show' => 'old'])
Expand Down Expand Up @@ -572,13 +577,13 @@ public function hasBeenDisabled()
public function getTitle()
{
switch ($this->entry->action_name) {
case 'create':
case DirectorActivityLog::ACTION_CREATE:
$msg = $this->translate('%s "%s" has been created');
break;
case 'delete':
case DirectorActivityLog::ACTION_DELETE:
$msg = $this->translate('%s "%s" has been deleted');
break;
case 'modify':
case DirectorActivityLog::ACTION_MODIFY:
$msg = $this->translate('%s "%s" has been modified');
break;
default:
Expand Down
Loading

0 comments on commit 1c30412

Please sign in to comment.