ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilRbacLog.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
14 class ilRbacLog
15 {
16  const EDIT_PERMISSIONS = 1;
17  const MOVE_OBJECT = 2;
18  const LINK_OBJECT = 3;
19  const COPY_OBJECT = 4;
20  const CREATE_OBJECT = 5;
21  const EDIT_TEMPLATE = 6;
23  const CHANGE_OWNER = 8;
24 
25  public static function isActive()
26  {
27  include_once "Services/PrivacySecurity/classes/class.ilPrivacySettings.php";
28  $settings = ilPrivacySettings::_getInstance();
29  if ($settings->enabledRbacLog()) {
30  return true;
31  }
32  return false;
33  }
34 
35  public static function gatherFaPa($a_ref_id, array $a_role_ids, $a_add_action = false)
36  {
37  global $DIC;
38 
39  $rbacreview = $DIC['rbacreview'];
40 
41  $result = array();
42 
43  // #10946 - if result is written to log directly we need to add an "action" dimension
44  // if result is used as input to diffFaPa() we need "raw" data
45 
46  // roles
47  foreach ($a_role_ids as $role_id) {
48  if ($role_id != SYSTEM_ROLE_ID) {
49  if ($a_add_action) {
50  $result["ops"][$role_id]["add"] = $rbacreview->getRoleOperationsOnObject($role_id, $a_ref_id);
51  } else {
52  $result["ops"][$role_id] = $rbacreview->getRoleOperationsOnObject($role_id, $a_ref_id);
53  }
54  }
55  }
56 
57  // inheritance
58  if ($a_ref_id && $a_ref_id != ROLE_FOLDER_ID) {
59  if ($a_add_action) {
60  $result["inht"]["add"] = $rbacreview->getRolesOfRoleFolder($a_ref_id);
61  } else {
62  $result["inht"] = $rbacreview->getRolesOfRoleFolder($a_ref_id);
63  }
64  }
65 
66  return $result;
67  }
68 
69  public static function diffFaPa(array $a_old, array $a_new)
70  {
71  $result = array();
72 
73  // roles
74  foreach ((array) $a_old["ops"] as $role_id => $ops) {
75  $diff = array_diff($ops, $a_new["ops"][$role_id]);
76  if (sizeof($diff)) {
77  $result["ops"][$role_id]["rmv"] = array_values($diff);
78  }
79  $diff = array_diff($a_new["ops"][$role_id], $ops);
80  if (sizeof($diff)) {
81  $result["ops"][$role_id]["add"] = array_values($diff);
82  }
83  }
84 
85  if (isset($a_old["inht"]) || isset($a_new["inht"])) {
86  if (isset($a_old["inht"]) && !isset($a_new["inht"])) {
87  $result["inht"]["rmv"] = $a_old["inht"];
88  } elseif (!isset($a_old["inht"]) && isset($a_new["inht"])) {
89  $result["inht"]["add"] = $a_new["inht"];
90  } else {
91  $diff = array_diff($a_old["inht"], $a_new["inht"]);
92  if (sizeof($diff)) {
93  $result["inht"]["rmv"] = array_values($diff);
94  }
95  $diff = array_diff($a_new["inht"], $a_old["inht"]);
96  if (sizeof($diff)) {
97  $result["inht"]["add"] = array_values($diff);
98  }
99  }
100  }
101 
102  return $result;
103  }
104 
105  public static function gatherTemplate($a_role_ref_id, $a_role_id)
106  {
107  global $DIC;
108 
109  $rbacreview = $DIC['rbacreview'];
110 
111  return $rbacreview->getAllOperationsOfRole($a_role_id, $a_role_ref_id);
112  }
113 
114  public static function diffTemplate(array $a_old, array $a_new)
115  {
116  $result = array();
117  $types = array_unique(array_merge(array_keys($a_old), array_keys($a_new)));
118  foreach ($types as $type) {
119  if (!isset($a_old[$type])) {
120  $result[$type]["add"] = $a_new[$type];
121  } elseif (!isset($a_new[$type])) {
122  $result[$type]["rmv"] = $a_old[$type];
123  } else {
124  $diff = array_diff($a_old[$type], $a_new[$type]);
125  if (sizeof($diff)) {
126  $result[$type]["rmv"] = array_values($diff);
127  }
128  $diff = array_diff($a_new[$type], $a_old[$type]);
129  if (sizeof($diff)) {
130  $result[$type]["add"] = array_values($diff);
131  }
132  }
133  }
134  return $result;
135  }
136 
137  public static function add($a_action, $a_ref_id, array $a_diff, $a_source_ref_id = false)
138  {
139  global $DIC;
140 
141  $ilUser = $DIC['ilUser'];
142  $ilDB = $DIC['ilDB'];
143 
144  if (self::isValidAction($a_action) && sizeof($a_diff)) {
145  if ($a_source_ref_id) {
146  $a_diff["src"] = $a_source_ref_id;
147  }
148  $id = $ilDB->nextId('rbac_log');
149 
150  $ilDB->query("INSERT INTO rbac_log (log_id, user_id, created, ref_id, action, data)" .
151  " VALUES (" . $ilDB->quote($id, "integer") . "," . $ilDB->quote($ilUser->getId(), "integer") .
152  "," . $ilDB->quote(time(), "integer") .
153  "," . $ilDB->quote($a_ref_id, "integer") . "," . $ilDB->quote($a_action, "integer") .
154  "," . $ilDB->quote(serialize($a_diff), "text") . ")");
155  return true;
156  }
157  return false;
158  }
159 
160  protected static function isValidAction($a_action)
161  {
162  if (in_array($a_action, array(self::EDIT_PERMISSIONS, self::MOVE_OBJECT, self::LINK_OBJECT,
163  self::COPY_OBJECT, self::CREATE_OBJECT, self::EDIT_TEMPLATE, self::EDIT_TEMPLATE_EXISTING,
164  self::CHANGE_OWNER))) {
165  return true;
166  }
167  return false;
168  }
169 
170  public static function getLogItems($a_ref_id, $a_limit, $a_offset, array $a_filter = null)
171  {
172  global $DIC;
173 
174  $ilDB = $DIC['ilDB'];
175  $rbacreview = $DIC['rbacreview'];
176 
177  $where = [];
178  if ($a_filter) {
179  if ($a_filter["action"]) {
180  $where[] = "action = " . $ilDB->quote($a_filter["action"], "integer");
181  }
182  if ($a_filter["date"]["from"]) {
183  $from = $a_filter["date"]["from"]->get(IL_CAL_UNIX);
184  $from = strtotime("00:00:00", $from);
185  $where[] = "created >= " . $ilDB->quote($from, "integer");
186  }
187  if ($a_filter["date"]["to"]) {
188  $to = $a_filter["date"]["to"]->get(IL_CAL_UNIX);
189  $to = strtotime("23:59:59", $to);
190  $where[] = "created <= " . $ilDB->quote($to, "integer");
191  }
192 
193  if (count($where) > 0) {
194  $where = array_merge([' AND '], [implode(' AND ', $where)]);
195  }
196  }
197 
198  $set = $ilDB->query("SELECT COUNT(*) FROM rbac_log WHERE ref_id = " . $ilDB->quote($a_ref_id, "integer") . implode('', $where));
199  $count = array_pop($ilDB->fetchAssoc($set));
200 
201  $ilDB->setLimit($a_limit, $a_offset);
202  $set = $ilDB->query("SELECT * FROM rbac_log WHERE ref_id = " . $ilDB->quote($a_ref_id, "integer") .
203  implode('', $where) . " ORDER BY created DESC");
204  $result = array();
205  while ($row = $ilDB->fetchAssoc($set)) {
206  $row["data"] = unserialize($row["data"]);
207  $result[] = $row;
208  }
209  return array("cnt" => $count, "set" => $result);
210  }
211 
212  public static function delete($a_ref_id)
213  {
214  global $DIC;
215 
216  $ilDB = $DIC['ilDB'];
217 
218  $ilDB->query("DELETE FROM rbac_log WHERE ref_id = " . $ilDB->quote($a_ref_id, "integer"));
219 
220  self::garbageCollection();
221  }
222 
223  public static function garbageCollection()
224  {
225  global $DIC;
226 
227  $ilDB = $DIC['ilDB'];
228 
229  include_once "Services/PrivacySecurity/classes/class.ilPrivacySettings.php";
230  $settings = ilPrivacySettings::_getInstance();
231  $max = $settings->getRbacLogAge();
232 
233  $ilDB->query("DELETE FROM rbac_log WHERE created < " . $ilDB->quote(strtotime("-" . $max . "months"), "integer"));
234  }
235 }
$result
$type
global $DIC
Definition: saml.php:7
static garbageCollection()
if(!array_key_exists('StateId', $_REQUEST)) $id
const CHANGE_OWNER
$from
static isActive()
const IL_CAL_UNIX
static gatherFaPa($a_ref_id, array $a_role_ids, $a_add_action=false)
static diffFaPa(array $a_old, array $a_new)
const EDIT_PERMISSIONS
const CREATE_OBJECT
const COPY_OBJECT
$ilUser
Definition: imgupload.php:18
const LINK_OBJECT
static getLogItems($a_ref_id, $a_limit, $a_offset, array $a_filter=null)
$row
static gatherTemplate($a_role_ref_id, $a_role_id)
static add($a_action, $a_ref_id, array $a_diff, $a_source_ref_id=false)
global $ilDB
static diffTemplate(array $a_old, array $a_new)
const EDIT_TEMPLATE_EXISTING
const EDIT_TEMPLATE
const MOVE_OBJECT
static _getInstance()
Get instance of ilPrivacySettings.
static isValidAction($a_action)
class ilRbacLog Log changes in Rbac-related settings