ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  static public function isActive()
26  {
27  include_once "Services/PrivacySecurity/classes/class.ilPrivacySettings.php";
28  $settings = ilPrivacySettings::_getInstance();
29  if($settings->enabledRbacLog())
30  {
31  return true;
32  }
33  return false;
34  }
35 
36  static public function gatherFaPa($a_ref_id, array $a_role_ids)
37  {
38  global $rbacreview;
39 
40  $result = array();
41 
42  // roles
43  foreach($a_role_ids as $role_id)
44  {
45  if ($role_id != SYSTEM_ROLE_ID)
46  {
47  $result["ops"][$role_id] = $rbacreview->getRoleOperationsOnObject($role_id, $a_ref_id);
48  }
49  }
50 
51  // inheritance
52  $rolf_data = $rbacreview->getRoleFolderOfObject($a_ref_id);
53  $rolf_id = $rolf_data["child"];
54  if($rolf_id && $rolf_id != ROLE_FOLDER_ID)
55  {
56  $result["inht"] = $rbacreview->getRolesOfRoleFolder($rolf_id);
57  }
58 
59  return $result;
60  }
61 
62  static public function diffFaPa(array $a_old, array $a_new)
63  {
64  $result = array();
65 
66  // roles
67  foreach((array) $a_old["ops"] as $role_id => $ops)
68  {
69  $diff = array_diff($ops, $a_new["ops"][$role_id]);
70  if(sizeof($diff))
71  {
72  $result["ops"][$role_id]["rmv"] = array_values($diff);
73  }
74  $diff = array_diff($a_new["ops"][$role_id], $ops);
75  if(sizeof($diff))
76  {
77  $result["ops"][$role_id]["add"] = array_values($diff);
78  }
79  }
80 
81  if(isset($a_old["int"]) || isset($a_new["inht"]))
82  {
83  if(isset($a_old["inht"]) && !isset($a_new["inht"]))
84  {
85  $result["inht"]["rmv"] = $a_old["inht"];
86  }
87  else if(!isset($a_old["inht"]) && isset($a_new["inht"]))
88  {
89  $result["inht"]["add"] = $a_new["inht"];
90  }
91  else
92  {
93  $diff = array_diff($a_old["inht"], $a_new["inht"]);
94  if(sizeof($diff))
95  {
96  $result["inht"]["rmv"] = array_values($diff);
97  }
98  $diff = array_diff($a_new["inht"], $a_old["inht"]);
99  if(sizeof($diff))
100  {
101  $result["inht"]["add"] = array_values($diff);
102  }
103  }
104  }
105 
106  return $result;
107  }
108 
109  static public function gatherTemplate($a_role_folder_ref_id, $a_role_id)
110  {
111  global $rbacreview;
112 
113  return $rbacreview->getAllOperationsOfRole($a_role_id, $a_role_folder_ref_id);
114  }
115 
116  static public function diffTemplate(array $a_old, array $a_new)
117  {
118  $result = array();
119  $types = array_unique(array_merge(array_keys($a_old), array_keys($a_new)));
120  foreach($types as $type)
121  {
122  if(!isset($a_old[$type]))
123  {
124  $result[$type]["add"] = $a_new[$type];
125  }
126  else if(!isset($a_new[$type]))
127  {
128  $result[$type]["rmv"] = $a_old[$type];
129  }
130  else
131  {
132  $diff = array_diff($a_old[$type], $a_new[$type]);
133  if(sizeof($diff))
134  {
135  $result[$type]["rmv"] = array_values($diff);
136  }
137  $diff = array_diff($a_new[$type], $a_old[$type]);
138  if(sizeof($diff))
139  {
140  $result[$type]["add"] = array_values($diff);
141  }
142  }
143  }
144  return $result;
145  }
146 
147  static public function add($a_action, $a_ref_id, array $a_diff, $a_source_ref_id = false)
148  {
149  global $ilUser, $ilDB;
150 
151  if(self::isValidAction($a_action) && sizeof($a_diff))
152  {
153  if($a_source_ref_id)
154  {
155  $a_diff["src"] = $a_source_ref_id;
156  }
157 
158  $ilDB->query("INSERT INTO rbac_log (user_id, created, ref_id, action, data)".
159  " VALUES (".$ilDB->quote($ilUser->getId(), "integer").",".$ilDB->quote(time(), "integer").
160  ",".$ilDB->quote($a_ref_id, "integer").",".$ilDB->quote($a_action, "integer").
161  ",".$ilDB->quote(serialize($a_diff), "text").")");
162  return true;
163  }
164  return false;
165  }
166 
167  static protected function isValidAction($a_action)
168  {
169  if(in_array($a_action, array(self::EDIT_PERMISSIONS, self::MOVE_OBJECT, self::LINK_OBJECT,
170  self::COPY_OBJECT, self::CREATE_OBJECT, self::EDIT_TEMPLATE, self::EDIT_TEMPLATE_EXISTING,
171  self::CHANGE_OWNER)))
172  {
173  return true;
174  }
175  return false;
176  }
177 
178  static public function getLogItems($a_ref_id, $a_limit, $a_offset, array $a_filter = NULL)
179  {
180  global $ilDB, $rbacreview;
181 
182  if($a_filter)
183  {
184  $where = NULL;
185  if($a_filter["action"])
186  {
187  $where[] = "action = ".$ilDB->quote($a_filter["action"], "integer");
188  }
189  if($a_filter["date"]["from"])
190  {
191  $from = $a_filter["date"]["from"]->get(IL_CAL_UNIX);
192  $from = strtotime("00:00:00", $from);
193  $where[] = "created >= ".$ilDB->quote($from, "integer");
194  }
195  if($a_filter["date"]["to"])
196  {
197  $to = $a_filter["date"]["to"]->get(IL_CAL_UNIX);
198  $to = strtotime("23:59:59", $to);
199  $where[] = "created <= ".$ilDB->quote($to, "integer");
200  }
201  if(sizeof($where))
202  {
203  $where = " AND ".implode(" AND ", $where);
204  }
205  }
206 
207  $set = $ilDB->query("SELECT COUNT(*) FROM rbac_log WHERE ref_id = ".$ilDB->quote($a_ref_id, "integer").$where);
208  $count = array_pop($ilDB->fetchAssoc($set));
209 
210  $ilDB->setLimit($a_limit, $a_offset);
211  $set = $ilDB->query("SELECT * FROM rbac_log WHERE ref_id = ".$ilDB->quote($a_ref_id, "integer").
212  $where." ORDER BY created DESC");
213  $result = array();
214  while($row = $ilDB->fetchAssoc($set))
215  {
216  $row["data"] = unserialize($row["data"]);
217  $result[] = $row;
218  }
219  return array("cnt"=>$count, "set"=>$result);
220  }
221 
222  static function delete($a_ref_id)
223  {
224  global $ilDB;
225 
226  $ilDB->query("DELETE FROM rbac_log WHERE ref_id = ".$ilDB->quote($a_ref_id, "integer"));
227 
229  }
230 
231  static function garbageCollection()
232  {
233  global $ilDB;
234 
235  include_once "Services/PrivacySecurity/classes/class.ilPrivacySettings.php";
236  $settings = ilPrivacySettings::_getInstance();
237  $max = $settings->getRbacLogAge();
238 
239  $ilDB->query("DELETE FROM rbac_log WHERE created < ".$ilDB->quote(strtotime("-".$max."months"), "integer"));
240  }
241 }
242 
243 ?>