AuditingOutputStrategy.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
17 
18 /**
19  * AuditingOutputStrategy outputs object changes to the logger category
20  * AuditingOutputStrategy, loglevel info
21  *
22  * @author ingo herwig <ingo@wemove.com>
23  */
25 
26  private static $logger = null;
27 
28  /**
29  * Constructor
30  */
31  public function __construct() {
32  if (self::$logger == null) {
33  self::$logger = LogManager::getLogger(__CLASS__);
34  }
35  }
36 
37  /**
38  * @see OutputStrategy::writeHeader
39  */
40  public function writeHeader() {
41  // do nothing
42  }
43 
44  /**
45  * @see OutputStrategy::writeFooter
46  */
47  public function writeFooter() {
48  // do nothing
49  }
50 
51  /**
52  * @see OutputStrategy::writeObject
53  */
54  public function writeObject(PersistentObject $obj) {
55  if (self::$logger->isInfoEnabled()) {
56  $session = ObjectFactory::getInstance('session');
57  $authUserLogin = $session->getAuthUser();
58 
59  switch ($state = $obj->getState()) {
60  // log insert action
62  self::$logger->info('INSERT '.$obj->getOID().': '.str_replace("\n", " ", $obj->__toString()).' USER: '.$authUserLogin);
63  break;
64  // log update action
66  // collect differences
67  $values = [];
68  $valueNames = $obj->getValueNames(true);
69  foreach($valueNames as $name) {
70  $values[$name]['name'] = $name;
71  $values[$name]['new'] = $obj->getValue($name);
72  $values[$name]['old'] = $obj->getOriginalValue($name);
73  }
74  // make diff string
75  $diff = '';
76  foreach ($values as $value) {
77  if ($value['old'] != $value['new']) {
78  $diff .= $value['name'].':'.serialize($value['old']).'->'.serialize($value['new']).' ';
79  }
80  }
81  self::$logger->info('SAVE '.$obj->getOID().': '.$diff.' USER: '.$authUserLogin);
82  break;
83  // log delete action
85  // get old object from storage
86  self::$logger->info('DELETE '.$obj->getOID().': '.str_replace("\n", " ", $obj->__toString()).' USER: '.$authUserLogin);
87  break;
88  }
89  }
90  }
91 }
92 ?>
getOriginalValue($name)
Get the original of an attribute provided to the initialize method.
OutputStrategy defines the interface for classes that write an object's content to a destination (cal...
getValueNames($excludeTransient=false)
Get the names of all attributes.
getOID()
Get the object id of the PersistentObject.
getValue($name)
Get the value of an attribute.
AuditingOutputStrategy outputs object changes to the logger category AuditingOutputStrategy,...
static getLogger($name)
Get the logger with the given name.
Definition: LogManager.php:37
static getInstance($name, $dynamicConfiguration=[])
PersistentObject defines the interface of all persistent objects.
getState()
Get the object's state:
LogManager is used to retrieve Logger instances.
Definition: LogManager.php:20
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...