2015-04-24 15:57:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Objects;
|
|
|
|
|
|
|
|
use Icinga\Module\Director\Data\Db\DbObject;
|
|
|
|
use Icinga\Module\Director\Db;
|
2015-06-23 14:37:23 +02:00
|
|
|
use Icinga\Module\Director\Util;
|
2015-07-31 14:50:36 +02:00
|
|
|
use Icinga\Authentication\Auth;
|
2015-11-06 09:39:14 +01:00
|
|
|
use Icinga\Application\Icinga;
|
2015-04-24 15:57:01 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2015-11-06 09:39:14 +01:00
|
|
|
if (Icinga::app()->isCli()) {
|
|
|
|
return 'cli';
|
|
|
|
}
|
2015-04-24 15:57:01 +02:00
|
|
|
|
|
|
|
$auth = Auth::getInstance();
|
|
|
|
if ($auth->isAuthenticated()) {
|
|
|
|
return $auth->getUser()->getUsername();
|
|
|
|
} else {
|
|
|
|
return '<unknown>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 15:23:57 +02:00
|
|
|
public static function loadLatest(Db $connection)
|
|
|
|
{
|
|
|
|
$db = $connection->getDbAdapter();
|
|
|
|
$query = $db->select()->from($this->table, array('id' => 'MAX(id)'));
|
|
|
|
return static::load($db->fetchOne($query));
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:57:01 +02:00
|
|
|
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(),
|
2016-02-23 00:35:35 +01:00
|
|
|
'new_properties' => $object->toJson(null, true),
|
2015-04-24 15:57:01 +02:00
|
|
|
'change_time' => date('Y-m-d H:i:s'), // TODO -> postgres!
|
|
|
|
'parent_checksum' => $db->getLastActivityChecksum()
|
|
|
|
);
|
|
|
|
|
|
|
|
$data['checksum'] = sha1(json_encode($data), true);
|
2015-06-23 14:37:23 +02:00
|
|
|
$data['parent_checksum'] = Util::hex2binary($data['parent_checksum']);
|
2015-04-24 15:57:01 +02:00
|
|
|
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(),
|
2016-02-23 00:35:35 +01:00
|
|
|
'old_properties' => json_encode($object->getPlainUnmodifiedObject()),
|
|
|
|
'new_properties' => $object->toJson(null, true),
|
2015-04-24 15:57:01 +02:00
|
|
|
'change_time' => date('Y-m-d H:i:s'), // TODO -> postgres!
|
|
|
|
'parent_checksum' => $db->getLastActivityChecksum()
|
|
|
|
);
|
|
|
|
|
|
|
|
$data['checksum'] = sha1(json_encode($data), true);
|
2015-06-23 14:37:23 +02:00
|
|
|
$data['parent_checksum'] = Util::hex2binary($data['parent_checksum']);
|
2015-04-24 15:57:01 +02:00
|
|
|
return self::create($data)->store($db);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function logRemoval(DbObject $object, Db $db)
|
|
|
|
{
|
2016-03-20 12:02:45 +01:00
|
|
|
$plain = $object->getCachedUnmodifiedObject();
|
2015-04-24 15:57:01 +02:00
|
|
|
$data = array(
|
2016-03-20 12:02:45 +01:00
|
|
|
'object_name' => $plain->object_name,
|
2015-04-24 15:57:01 +02:00
|
|
|
'action_name' => 'delete',
|
|
|
|
'author' => self::username(),
|
|
|
|
'object_type' => $object->getTableName(),
|
2016-03-20 12:02:45 +01:00
|
|
|
'old_properties' => json_encode($plain),
|
2015-04-24 15:57:01 +02:00
|
|
|
'change_time' => date('Y-m-d H:i:s'), // TODO -> postgres!
|
|
|
|
'parent_checksum' => $db->getLastActivityChecksum()
|
|
|
|
);
|
|
|
|
|
|
|
|
$data['checksum'] = sha1(json_encode($data), true);
|
2015-06-23 14:37:23 +02:00
|
|
|
$data['parent_checksum'] = Util::hex2binary($data['parent_checksum']);
|
2015-04-24 15:57:01 +02:00
|
|
|
return self::create($data)->store($db);
|
|
|
|
}
|
|
|
|
}
|