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-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()
|
|
|
|
{
|
|
|
|
|
|
|
|
$auth = Auth::getInstance();
|
|
|
|
if ($auth->isAuthenticated()) {
|
|
|
|
return $auth->getUser()->getUsername();
|
|
|
|
} else {
|
|
|
|
return '<unknown>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 11:46:27 +02:00
|
|
|
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();
|
|
|
|
}
|
2015-07-31 14:50:36 +02:00
|
|
|
if ($object->supportsCustomVars()) {
|
|
|
|
$props['vars'] = $object->getVars();
|
|
|
|
}
|
2015-08-28 17:14:59 +02:00
|
|
|
if ($object->supportsImports()) {
|
|
|
|
$props['imports'] = $object->imports()->listImportNames();
|
|
|
|
}
|
2015-07-31 14:50:36 +02:00
|
|
|
|
|
|
|
return json_encode($props);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function prepareModifiedProperties(DbObject $object)
|
|
|
|
{
|
|
|
|
$props = $object->getModifiedProperties();
|
|
|
|
if ($object->supportsCustomVars()) {
|
|
|
|
$mod = array();
|
|
|
|
foreach ($object->vars() as $name => $var) {
|
|
|
|
if ($var->hasBeenModified()) {
|
|
|
|
$mod[$name] = $var->getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (! empty($mod)) {
|
|
|
|
$props['vars'] = (object) $mod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($object->supportsGroups()) {
|
2015-08-28 17:14:59 +02:00
|
|
|
$old = $object->groups()->listOriginalGroupNames();
|
|
|
|
$new = $object->groups()->listGroupNames();
|
|
|
|
if ($old !== $new) {
|
|
|
|
$props['groups'] = $new;
|
|
|
|
}
|
2015-07-31 14:50:36 +02:00
|
|
|
}
|
2015-08-28 17:14:59 +02:00
|
|
|
if ($object->supportsImports()) {
|
|
|
|
$old = $object->imports()->listOriginalImportNames();
|
|
|
|
$new = $object->imports()->listImportNames();
|
|
|
|
if ($old !== $new) {
|
|
|
|
$props['imports'] = $new;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 14:50:36 +02:00
|
|
|
|
|
|
|
return json_encode($props);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function prepareOriginalProperties(DbObject $object)
|
|
|
|
{
|
2015-08-28 17:14:59 +02:00
|
|
|
$props = $object->getOriginalProperties();
|
2015-07-31 14:50:36 +02:00
|
|
|
if ($object->supportsCustomVars()) {
|
|
|
|
$props['vars'] = (object) array();
|
|
|
|
foreach ($object->vars()->getOriginalVars() as $name => $var) {
|
|
|
|
$props['vars']->$name = $var->getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($object->supportsGroups()) {
|
2015-08-28 17:14:59 +02:00
|
|
|
$groups = $object->groups()->listOriginalGroupNames();
|
|
|
|
if (! empty($groups)) {
|
|
|
|
$props['groups'] = $groups;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($object->supportsImports()) {
|
2015-10-26 11:42:38 +01:00
|
|
|
$imports = $object->imports()->listOriginalImportNames();
|
2015-08-28 17:14:59 +02:00
|
|
|
if (! empty($imports)) {
|
|
|
|
$props['imports'] = $imports;
|
|
|
|
}
|
2015-07-31 14:50:36 +02:00
|
|
|
}
|
2015-07-30 11:46:27 +02:00
|
|
|
|
|
|
|
return json_encode($props);
|
|
|
|
}
|
|
|
|
|
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(),
|
2015-07-30 11:46:27 +02:00
|
|
|
'new_properties' => self::prepareNewObjectProperties($object),
|
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(),
|
2015-07-31 14:50:36 +02:00
|
|
|
'old_properties' => self::prepareOriginalProperties($object),
|
|
|
|
'new_properties' => self::prepareModifiedProperties($object),
|
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)
|
|
|
|
{
|
|
|
|
$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']);
|
2015-04-24 15:57:01 +02:00
|
|
|
return self::create($data)->store($db);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|