From ca3398247ea951c7ba6ed045207ba89c440250cb Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 24 Apr 2015 15:57:01 +0200 Subject: [PATCH] Activity log: initial basic implementation fixes #9167 --- library/Director/Data/Db/DbObject.php | 10 ++ library/Director/Db.php | 7 ++ .../Director/Objects/DirectorActivityLog.php | 93 +++++++++++++++++++ library/Director/Objects/IcingaCommand.php | 8 +- library/Director/Objects/IcingaHost.php | 8 +- library/Director/Objects/IcingaObject.php | 27 ++++++ library/Director/Objects/IcingaZone.php | 8 +- 7 files changed, 140 insertions(+), 21 deletions(-) create mode 100644 library/Director/Objects/DirectorActivityLog.php create mode 100644 library/Director/Objects/IcingaObject.php diff --git a/library/Director/Data/Db/DbObject.php b/library/Director/Data/Db/DbObject.php index c356ca7c..2f4ed816 100644 --- a/library/Director/Data/Db/DbObject.php +++ b/library/Director/Data/Db/DbObject.php @@ -88,6 +88,11 @@ abstract class DbObject $this->beforeInit(); } + public function getTableName() + { + return $this->table; + } + /** * Kann überschrieben werden, um Kreuz-Checks usw vor dem Speichern durch- * zuführen - die Funktion ist aber public und erlaubt jederzeit, die Kon- @@ -484,6 +489,11 @@ abstract class DbObject return $this; } + public function getOriginalProperties() + { + return $this->loadedProperties; + } + public function hasBeenLoadedFromDb() { return $this->loadedFromDb; diff --git a/library/Director/Db.php b/library/Director/Db.php index 2b165807..98639b75 100644 --- a/library/Director/Db.php +++ b/library/Director/Db.php @@ -13,6 +13,13 @@ class Db extends DbConnection 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() { $select = $this->db()->select()->from('icinga_command', array( diff --git a/library/Director/Objects/DirectorActivityLog.php b/library/Director/Objects/DirectorActivityLog.php new file mode 100644 index 00000000..612c19b6 --- /dev/null +++ b/library/Director/Objects/DirectorActivityLog.php @@ -0,0 +1,93 @@ + 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 ''; + } + } + + 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); + } + +} diff --git a/library/Director/Objects/IcingaCommand.php b/library/Director/Objects/IcingaCommand.php index 56dadeb4..e3e38ca7 100644 --- a/library/Director/Objects/IcingaCommand.php +++ b/library/Director/Objects/IcingaCommand.php @@ -2,16 +2,10 @@ namespace Icinga\Module\Director\Objects; -use Icinga\Module\Director\Data\Db\DbObject; - -class IcingaCommand extends DbObject +class IcingaCommand extends IcingaObject { protected $table = 'icinga_command'; - protected $keyName = 'id'; - - protected $autoincKeyName = 'id'; - protected $defaultProperties = array( 'id' => null, 'object_name' => null, diff --git a/library/Director/Objects/IcingaHost.php b/library/Director/Objects/IcingaHost.php index 31f55537..807178f0 100644 --- a/library/Director/Objects/IcingaHost.php +++ b/library/Director/Objects/IcingaHost.php @@ -2,16 +2,10 @@ namespace Icinga\Module\Director\Objects; -use Icinga\Module\Director\Data\Db\DbObject; - -class IcingaHost extends DbObject +class IcingaHost extends IcingaObject { protected $table = 'icinga_host'; - protected $keyName = 'id'; - - protected $autoincKeyName = 'id'; - protected $defaultProperties = array( 'id' => null, 'object_name' => null, diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php new file mode 100644 index 00000000..488e062d --- /dev/null +++ b/library/Director/Objects/IcingaObject.php @@ -0,0 +1,27 @@ +connection); + } + + public function onUpdate() + { + DirectorActivityLog::logModification($this, $this->connection); + } + + public function onDelete() + { + DirectorActivityLog::logRemoval($this, $this->connection); + } +} diff --git a/library/Director/Objects/IcingaZone.php b/library/Director/Objects/IcingaZone.php index eb837b35..3d1fa406 100644 --- a/library/Director/Objects/IcingaZone.php +++ b/library/Director/Objects/IcingaZone.php @@ -2,16 +2,10 @@ namespace Icinga\Module\Director\Objects; -use Icinga\Module\Director\Data\Db\DbObject; - -class IcingaZone extends DbObject +class IcingaZone extends IcingaObject { protected $table = 'icinga_zone'; - protected $keyName = 'id'; - - protected $autoincKeyName = 'id'; - protected $defaultProperties = array( 'id' => null, 'object_name' => null,