Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
AuditUtil.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
42  class AuditUtil
43  {
44  // Give the class this...
45  //public $originalAttributeValues = array();
46 
47  public static function throwNotSupportedExceptionIfNotCalledFromAnItem() // Pass __CLASS__
48  {
49  $backTrace = debug_backtrace();
50  for ($i = 0; $i < count($backTrace); $i++)
51  {
52  if (isset($backTrace[$i]['class']) &&
53  $backTrace[$i]['class'] == 'Item')
54  {
55  return;
56  }
57  }
58  throw new NotSupportedException();
59  }
60 
66  public static function saveOriginalAttributeValue($auditableModel, $attributeName, $value)
67  {
68  assert('$auditableModel instanceof Item ||
69  $auditableModel instanceof OwnedModel ||
70  $auditableModel instanceof OwnedCustomField ||
71  $auditableModel instanceof OwnedMultipleValuesCustomField');
72  assert('property_exists($auditableModel, "originalAttributeValues")');
73  if (!array_key_exists($attributeName, $auditableModel->originalAttributeValues))
74  {
75  if (!$auditableModel::isRelation($attributeName))
76  {
77  if ($auditableModel->$attributeName != $value)
78  {
79  $auditableModel->originalAttributeValues[$attributeName] = $auditableModel->$attributeName;
80  }
81  }
82  elseif (!$auditableModel::isOwnedRelation($attributeName) &&
83  !$auditableModel->$attributeName instanceof CustomFieldData)
84  {
85  assert('$auditableModel->$attributeName instanceof RedBeanModel');
86  $relatedModel = $auditableModel->$attributeName;
87  if ($value === null || !$relatedModel->isSame($value))
88  {
89  $auditableModel->originalAttributeValues[$attributeName] = array(get_class($relatedModel),
90  $relatedModel->id,
91  strval($relatedModel));
92  }
93  }
94  }
95  }
96 
102  public static function logAuditEventsListForChangedAttributeValues(Item $item, array $attributeNames = array(), RedBeanModel $ownedModel = null)
103  {
104  assert('$item->id > 0');
105  $attributeModel = $ownedModel === null ? $item : $ownedModel;
106  $noAuditAttributeNames = self::getNoAuditAttributeNames($attributeModel);
107  foreach ($attributeModel->originalAttributeValues as $attributeName => $oldValue)
108  {
109  if (!in_array($attributeName, $noAuditAttributeNames))
110  {
111  $processAuditEvent = true;
112  if (!$attributeModel::isRelation($attributeName))
113  {
114  $newValue = $attributeModel->$attributeName;
115  }
116  elseif ($attributeModel->$attributeName instanceof RedBeanOneToManyRelatedModels)
117  {
118  $newValue = $attributeModel->$attributeName->getStringifiedData();
119  assert('$oldValue != $newValue');
120  }
121  else
122  {
123  assert('$attributeModel->$attributeName instanceof RedBeanModel');
124  $relatedModel = $attributeModel->$attributeName;
125  if ($relatedModel->id < 0 && $oldValue[1] < 0)
126  {
127  $processAuditEvent = false;
128  }
129  else
130  {
131  $newValue = array(get_class($relatedModel),
132  $relatedModel->id,
133  strval($relatedModel));
134  assert('$oldValue != $newValue');
135  }
136  }
137  if ($processAuditEvent)
138  {
139  $tempAttributeNames = $attributeNames;
140  $tempAttributeNames[] = $attributeName;
141  $data = array(strval($item), $tempAttributeNames, $oldValue, $newValue);
142  AuditEvent::logAuditEvent('ZurmoModule', ZurmoModule::AUDIT_EVENT_ITEM_MODIFIED,
143  $data, $item);
144  }
145  }
146  }
147  foreach ($attributeModel->attributeNames() as $attributeName)
148  {
149  if (!in_array($attributeName, $noAuditAttributeNames) &&
150  $attributeModel::isOwnedRelation($attributeName))
151  {
152  try
153  {
154  $ownedModel = $attributeModel->$attributeName;
155  }
157  {
158  continue; // If someone doesn't have access they
159  // they can't have modified the attributes.
160  }
161  catch (NotSupportedException $e)
162  {
163  continue; // Certain attributes can't be modified, like
164  // rights on the super administrators group
165  // so we can safely ignore them.
166  }
167  if ($ownedModel instanceof OwnedModel ||
168  $ownedModel instanceof OwnedCustomField ||
169  $ownedModel instanceof OwnedMultipleValuesCustomField)
170  {
171  $ownedModels = array($ownedModel);
172  }
173  else
174  {
175  assert('$ownedModel instanceof RedBeanModels');
176  $ownedModels = array();
177  }
178  foreach ($ownedModels as $ownedModel)
179  {
180  $tempAttributeNames = $attributeNames;
181  $tempAttributeNames[] = $attributeName;
182  self::logAuditEventsListForChangedAttributeValues($item, $tempAttributeNames, $ownedModel);
183  }
184  }
185  }
186  }
187  // TODO - collections
195  public static function stringifyValue(/*RedBeanModel*/ $attributeModel, $attributeName, $value, $format = 'long')
196  {
197  assert('$format == "long" || $format == "short"');
198  if ($attributeModel instanceof RedBeanModels)
199  {
200  return 'Collection';
201  }
202  assert('is_string($attributeName) && $attributeName != ""');
203  if (!$attributeModel::isRelation($attributeName))
204  {
205  if ($value === null || $value == '')
206  {
207  $value = Zurmo::t('Core', '(None)');
208  }
209  $s = $value;
210  }
211  elseif ($attributeModel->$attributeName instanceof RedBeanOneToManyRelatedModels)
212  {
213  $s = $attributeModel->stringifyOneToManyRelatedModelsValues($value);
214  }
215  else
216  {
217  assert('is_array($value)');
218  if ($value[1] < 0)
219  {
220  $s = Zurmo::t('Core', '(None)');
221  }
222  else
223  {
224  $modelClassName = $value[0];
225  if ($format == 'long')
226  {
227  $s = $modelClassName::getModelLabelByTypeAndLanguage('Singular') .
228  '(' . $value[1] . ') ';
229  }
230  else
231  {
232  $s = null;
233  }
234  if ($value[2] === null || $value == '')
235  {
236  $s .= Zurmo::t('Core', '(None)');
237  }
238  else
239  {
240  $s .= $value[2];
241  }
242  }
243  }
244  return $s;
245  }
246 
250  public static function clearRelatedModelsOriginalAttributeValues(Item $item)
251  {
252  assert('$item->id > 0');
253  $noAuditAttributeNames = self::getNoAuditAttributeNames($item);
254  foreach ($item->attributeNames() as $attributeName)
255  {
256  if (!in_array($attributeName, $noAuditAttributeNames) &&
257  $item::isOwnedRelation($attributeName))
258  {
259  try
260  {
261  $ownedModel = $item->$attributeName;
262  }
263  catch (NotSupportedException $e)
264  {
265  continue;
266  }
267  if ($ownedModel instanceof OwnedModel ||
268  $ownedModel instanceof OwnedCustomField ||
269  $ownedModel instanceof OwnedMultipleValuesCustomField)
270  {
271  $ownedModels = array($ownedModel);
272  }
273  else
274  {
275  assert('$ownedModel instanceof RedBeanModels');
276  $ownedModels = $ownedModel;
277  }
278  for ($i = 0; $i < count($ownedModels); $i++)
279  {
280  $ownedModel = $ownedModels[$i];
281  $ownedModel->forgetOriginalAttributeValues();
282  }
283  }
284  }
285  }
286 
287  protected static function getNoAuditAttributeNames($auditableModel)
288  {
289  assert('$auditableModel instanceof Item ||
290  $auditableModel instanceof OwnedModel ||
291  $auditableModel instanceof OwnedCustomField ||
292  $auditableModel instanceof OwnedMultipleValuesCustomField');
293  $noAuditAttributes = array();
294  $metadata = $auditableModel->getMetadata();
295  foreach ($metadata as $notUsed => $classMetadata)
296  {
297  if (isset($classMetadata['noAudit']))
298  {
299  assert('is_array($classMetadata["noAudit"])');
300  $noAuditAttributes = array_merge($noAuditAttributes, $classMetadata['noAudit']);
301  }
302  }
303  return $noAuditAttributes;
304  }
305  }
306 ?>
static saveOriginalAttributeValue($auditableModel, $attributeName, $value)
Definition: AuditUtil.php:66
static stringifyValue($attributeModel, $attributeName, $value, $format= 'long')
Definition: AuditUtil.php:195
static logAuditEventsListForChangedAttributeValues(Item $item, array $attributeNames=array(), RedBeanModel $ownedModel=null)
Definition: AuditUtil.php:102
Definition: Item.php:37
static clearRelatedModelsOriginalAttributeValues(Item $item)
Definition: AuditUtil.php:250
Generated on Wed Oct 28 2020 07:10:36
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.