Activity log: initial basic implementation

fixes #9167
This commit is contained in:
Thomas Gelf 2015-04-24 15:57:01 +02:00
parent 0d0fcc973b
commit ca3398247e
7 changed files with 140 additions and 21 deletions

View File

@ -88,6 +88,11 @@ abstract class DbObject
$this->beforeInit(); $this->beforeInit();
} }
public function getTableName()
{
return $this->table;
}
/** /**
* Kann überschrieben werden, um Kreuz-Checks usw vor dem Speichern durch- * Kann überschrieben werden, um Kreuz-Checks usw vor dem Speichern durch-
* zuführen - die Funktion ist aber public und erlaubt jederzeit, die Kon- * zuführen - die Funktion ist aber public und erlaubt jederzeit, die Kon-
@ -484,6 +489,11 @@ abstract class DbObject
return $this; return $this;
} }
public function getOriginalProperties()
{
return $this->loadedProperties;
}
public function hasBeenLoadedFromDb() public function hasBeenLoadedFromDb()
{ {
return $this->loadedFromDb; return $this->loadedFromDb;

View File

@ -13,6 +13,13 @@ class Db extends DbConnection
return $this->getDbAdapter(); return $this->getDbAdapter();
} }
public function getLastActivityChecksum()
{
$select = "SELECT checksum FROM (SELECT * FROM (SELECT 1 AS pos, LOWER(HEX(checksum)) AS checksum FROM director_activity_log ORDER BY change_time DESC LIMIT 1) a UNION SELECT 2 AS pos, '' AS checksum) u ORDER BY pos LIMIT 1";
return $this->db()->fetchOne($select);
}
public function enumCheckcommands() public function enumCheckcommands()
{ {
$select = $this->db()->select()->from('icinga_command', array( $select = $this->db()->select()->from('icinga_command', array(

View File

@ -0,0 +1,93 @@
<?php
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Db;
use Icinga\Authentication\Manager as Auth;
class DirectorActivityLog extends DbObject
{
protected $table = 'director_activity_log';
protected $keyName = 'id';
protected $autoincKeyName = 'id';
protected $defaultProperties = array(
'id' => null,
'object_name' => null,
'action_name' => null,
'object_type' => null,
'old_properties' => null,
'new_properties' => null,
'author' => null,
'change_time' => null,
'checksum' => null,
'parent_checksum' => null,
);
protected static function username()
{
$auth = Auth::getInstance();
if ($auth->isAuthenticated()) {
return $auth->getUser()->getUsername();
} else {
return '<unknown>';
}
}
public static function logCreation(DbObject $object, Db $db)
{
$data = array(
'object_name' => $object->object_name,
'action_name' => 'create',
'author' => self::username(),
'object_type' => $object->getTableName(),
'new_properties' => json_encode($object->getProperties()),
'change_time' => date('Y-m-d H:i:s'), // TODO -> postgres!
'parent_checksum' => $db->getLastActivityChecksum()
);
$data['checksum'] = sha1(json_encode($data), true);
$data['parent_checksum'] = pack('H*', $data['parent_checksum']);
return self::create($data)->store($db);
}
public static function logModification(DbObject $object, Db $db)
{
$data = array(
'object_name' => $object->object_name,
'action_name' => 'modify',
'author' => self::username(),
'object_type' => $object->getTableName(),
'old_properties' => json_encode($object->getOriginalProperties()),
'new_properties' => json_encode($object->getModifiedProperties()),
'change_time' => date('Y-m-d H:i:s'), // TODO -> postgres!
'parent_checksum' => $db->getLastActivityChecksum()
);
$data['checksum'] = sha1(json_encode($data), true);
$data['parent_checksum'] = pack('H*', $data['parent_checksum']);
return self::create($data)->store($db);
}
public static function logRemoval(DbObject $object, Db $db)
{
$data = array(
'object_name' => $object->object_name,
'action_name' => 'delete',
'author' => self::username(),
'object_type' => $object->getTableName(),
'old_properties' => json_encode($object->getOriginalProperties()),
'change_time' => date('Y-m-d H:i:s'), // TODO -> postgres!
'parent_checksum' => $db->getLastActivityChecksum()
);
$data['checksum'] = sha1(json_encode($data), true);
$data['parent_checksum'] = pack('H*', $data['parent_checksum']);
return self::create($data)->store($db);
}
}

View File

@ -2,16 +2,10 @@
namespace Icinga\Module\Director\Objects; namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObject; class IcingaCommand extends IcingaObject
class IcingaCommand extends DbObject
{ {
protected $table = 'icinga_command'; protected $table = 'icinga_command';
protected $keyName = 'id';
protected $autoincKeyName = 'id';
protected $defaultProperties = array( protected $defaultProperties = array(
'id' => null, 'id' => null,
'object_name' => null, 'object_name' => null,

View File

@ -2,16 +2,10 @@
namespace Icinga\Module\Director\Objects; namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObject; class IcingaHost extends IcingaObject
class IcingaHost extends DbObject
{ {
protected $table = 'icinga_host'; protected $table = 'icinga_host';
protected $keyName = 'id';
protected $autoincKeyName = 'id';
protected $defaultProperties = array( protected $defaultProperties = array(
'id' => null, 'id' => null,
'object_name' => null, 'object_name' => null,

View File

@ -0,0 +1,27 @@
<?php
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObject;
abstract class IcingaObject extends DbObject
{
protected $keyName = 'id';
protected $autoincKeyName = 'id';
public function onInsert()
{
DirectorActivityLog::logCreation($this, $this->connection);
}
public function onUpdate()
{
DirectorActivityLog::logModification($this, $this->connection);
}
public function onDelete()
{
DirectorActivityLog::logRemoval($this, $this->connection);
}
}

View File

@ -2,16 +2,10 @@
namespace Icinga\Module\Director\Objects; namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObject; class IcingaZone extends IcingaObject
class IcingaZone extends DbObject
{ {
protected $table = 'icinga_zone'; protected $table = 'icinga_zone';
protected $keyName = 'id';
protected $autoincKeyName = 'id';
protected $defaultProperties = array( protected $defaultProperties = array(
'id' => null, 'id' => null,
'object_name' => null, 'object_name' => null,