AuditingOutputStrategy.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 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  private $_session = null;
28 
29  /**
30  * Constructor
31  * @param $session
32  */
33  public function __construct(Session $session) {
34  $this->_session = $session;
35  if (self::$_logger == null) {
36  self::$_logger = LogManager::getLogger(__CLASS__);
37  }
38  }
39 
40  /**
41  * @see OutputStrategy::writeHeader
42  */
43  public function writeHeader() {
44  // do nothing
45  }
46 
47  /**
48  * @see OutputStrategy::writeFooter
49  */
50  public function writeFooter() {
51  // do nothing
52  }
53 
54  /**
55  * @see OutputStrategy::writeObject
56  */
57  public function writeObject(PersistentObject $obj) {
58  if (self::$_logger->isInfoEnabled()) {
59  $user = $this->_session->getAuthUser();
60 
61  switch ($state = $obj->getState()) {
62  // log insert action
64  self::$_logger->info('INSERT '.$obj->getOID().': '.str_replace("\n", " ", $obj->__toString()).' USER: '.$user->getLogin());
65  break;
66  // log update action
68  // get original values
69  $orignialValues = $obj->getOriginalValues();
70  // collect differences
71  $values = array();
72  $valueNames = $obj->getValueNames(true);
73  foreach($valueNames as $name) {
74  $values[$name]['name'] = $name;
75  $values[$name]['new'] = $obj->getValue($name);
76  $values[$name]['old'] = isset($orignialValues[$name]) ? $orignialValues[$name] : null;
77  }
78  // make diff string
79  $diff = '';
80  foreach ($values as $value) {
81  if ($value['old'] != $value['new']) {
82  $diff .= $value['name'].':'.serialize($value['old']).'->'.serialize($value['new']).' ';
83  }
84  }
85  self::$_logger->info('SAVE '.$obj->getOID().': '.$diff.' USER: '.$user->getLogin());
86  break;
87  // log delete action
89  // get old object from storage
90  self::$_logger->info('DELETE '.$obj->getOID().': '.str_replace("\n", " ", $obj->__toString()).' USER: '.$user->getLogin());
91  break;
92  }
93  }
94  }
95 }
96 ?>
getOID()
Get the object id of the PersistentObject.
AuditingOutputStrategy outputs object changes to the logger category AuditingOutputStrategy, loglevel info.
getOriginalValues()
Get the original data 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 items.
static getLogger($name)
Get the logger with the given name.
Definition: LogManager.php:35
Session is the interface for session implementations and defines access to session variables...
Definition: Session.php:21
getValue($name)
Get the value of a named item.
getState()
Get the object's state:
PersistentObject defines the interface of all persistent objects.