class_AuditLogAttribute.inc 3.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)

  Copyright (C) 2018-2019  FusionDirectory

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

class AuditLogAttribute extends OrderedArrayAttribute
{
  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = [], $acl = '')
  {
    Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
    $this->edit_enabled = FALSE;
    $this->attribute    = FALSE;
    $this->order        = FALSE;
    $this->setInLdap(FALSE);
  }

  protected function getAttributeArrayValue ($key, $event)
  {
    try {
36
      $author = ['html' => objects::link($event['fdAuditAuthorDN'], 'user', '', NULL, TRUE, ($event['fdAuditAuthorDN'] != $this->plugin->dn))];
37
38
39
40
41
42
43
44
    } catch (FusionDirectoryException $e) {
      $author = $event['fdAuditAuthorDN'];
    }
    try {
      $time = static::formatDateDiff(LdapGeneralizedTime::fromString($event['fdAuditDateTime']));
    } catch (Exception $e) {
      $time = $event['fdAuditDateTime'];
    }
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    switch ($event['fdAuditAction']) {
      case 'security':
        if ($event['fdAuditObjectType'] == 'login') {
          $name = _('Sign in');
        } elseif ($event['fdAuditObjectType'] == 'logout') {
          $name = _('Sign out');
        } else {
          $name = $event['fdAuditObjectType'];
        }
        break;
      case 'modify':
        if (in_array('userPassword', $event['fdAuditAttributes'])) {
          $name = _('Password modification');
        } else {
          $name = sprintf(_('Account modification (%s)'), implode(',', $event['fdAuditAttributes']));
        }
        break;
      case 'create':
      case 'remove':
      case 'copy':
      case 'snapshot':
      case 'view':
      case 'debug':
      default:
        $name = $event['fdAuditAction'].' '.$event['fdAuditObjectType'];
        break;
    }
72
    return [
73
      $name,
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
      $author,
      $event['fdAuditAuthorIP'] ?? '',
      $time,
      $event['fdAuditResult'],
    ];
  }

  protected function genRowIcons ($key, $value)
  {
    return ['', 0];
  }

  public function htmlIds (): array
  {
    return [];
  }

  function renderButtons ()
  {
    return '';
  }

96
97
  public static function formatDateDiff ($date)
  {
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    $now = new DateTime();
    $interval = $now->diff($date);

    if ($interval->y > 0) {
      return sprintf(_('%s years(s) ago'), $interval->y);
    }
    if ($interval->m > 0) {
      return sprintf(_('%s month(s) ago'), $interval->m);
    }
    if ($interval->d > 0) {
      return sprintf(_('%s day(s) ago'), $interval->d);
    }
    if ($interval->h > 0) {
      return sprintf(_('%s hour(s) ago'), $interval->h);
    }
    if ($interval->i > 0) {
      return sprintf(_('%s minute(s) ago'), $interval->i);
    }
    if ($interval->s > 0) {
      return sprintf(_('%s second(s) ago'), $interval->s);
    }

    return _('Now');
  }
}