icingaweb2-module-director/library/Director/Objects/DirectorActivityLog.php

108 lines
3.6 KiB
PHP
Raw Normal View History

<?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;
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>';
}
}
protected static function prepareNewObjectProperties(DbObject $object)
{
$props = $object->getProperties();
if ($object->supportsCustomVars()) {
// $props->vars = $object->vars()->toJson();
}
if ($object->supportsGroups()) {
$props['groups'] = $object->groups()->listGroupNames();
}
return json_encode($props);
}
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' => self::prepareNewObjectProperties($object),
'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']);
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);
2015-06-23 14:37:23 +02:00
$data['parent_checksum'] = Util::hex2binary($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);
2015-06-23 14:37:23 +02:00
$data['parent_checksum'] = Util::hex2binary($data['parent_checksum']);
return self::create($data)->store($db);
}
}