ryxeo-glpi-git / inc / rule.right.class.php @ b67d8923
Historique | Voir | Annoter | Télécharger (15,4 ko)
1 |
<?php
|
---|---|
2 |
/*
|
3 |
* @version $Id: rule.right.class.php 7875 2009-01-23 15:16:47Z moyo $
|
4 |
-------------------------------------------------------------------------
|
5 |
GLPI - Gestionnaire Libre de Parc Informatique
|
6 |
Copyright (C) 2003-2009 by the INDEPNET Development Team.
|
7 |
|
8 |
http://indepnet.net/ http://glpi-project.org
|
9 |
-------------------------------------------------------------------------
|
10 |
|
11 |
LICENSE
|
12 |
|
13 |
This file is part of GLPI.
|
14 |
|
15 |
GLPI is free software; you can redistribute it and/or modify
|
16 |
it under the terms of the GNU General Public License as published by
|
17 |
the Free Software Foundation; either version 2 of the License, or
|
18 |
(at your option) any later version.
|
19 |
|
20 |
GLPI is distributed in the hope that it will be useful,
|
21 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
22 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
23 |
GNU General Public License for more details.
|
24 |
|
25 |
You should have received a copy of the GNU General Public License
|
26 |
along with GLPI; if not, write to the Free Software
|
27 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
28 |
--------------------------------------------------------------------------
|
29 |
*/
|
30 |
|
31 |
// ----------------------------------------------------------------------
|
32 |
// Original Author of file: Walid Nouh
|
33 |
// Purpose of file:
|
34 |
// ----------------------------------------------------------------------
|
35 |
if (!defined('GLPI_ROOT')) { |
36 |
die("Sorry. You can't access directly to this file"); |
37 |
} |
38 |
|
39 |
/// Rule class for Rights management
|
40 |
class RightAffectRule extends Rule { |
41 |
|
42 |
/**
|
43 |
* Constructor
|
44 |
**/
|
45 |
function RightAffectRule() { |
46 |
global $RULES_CRITERIAS; |
47 |
|
48 |
$this->table = "glpi_rules_descriptions"; |
49 |
$this->type = -1; |
50 |
$this->rule_type = RULE_AFFECT_RIGHTS; |
51 |
|
52 |
//Dynamically add all the ldap criterias to the current list of rule's criterias
|
53 |
$this->addLdapCriteriasToArray();
|
54 |
$this->right="rule_ldap"; |
55 |
$this->orderby="name"; |
56 |
} |
57 |
|
58 |
function maxActionsCount(){ |
59 |
// Unlimited
|
60 |
return 4; |
61 |
} |
62 |
/**
|
63 |
* Display form to add rules
|
64 |
* @param $target where to post form
|
65 |
* @param $ID entity ID
|
66 |
*/
|
67 |
function showAndAddRuleForm($target, $ID) { |
68 |
global $LANG, $CFG_GLPI; |
69 |
|
70 |
$canedit = haveRight($this->right, "w"); |
71 |
|
72 |
echo "<form name='ldapaffectation_form' id='ldapaffectation_form' method='post' action=\"$target\">"; |
73 |
|
74 |
if ($canedit) { |
75 |
|
76 |
echo "<div class='center'>"; |
77 |
echo "<table class='tab_cadre_fixe'>"; |
78 |
echo "<tr class='tab_bg_1'><th colspan='4'>" .$LANG["rulesengine"][19] . "</tr><tr><td class='tab_bg_2' align='center'>"; |
79 |
echo $LANG["common"][16] . ":"; |
80 |
echo "</td><td align='center' class='tab_bg_2'>"; |
81 |
autocompletionTextField("name", "glpi_rules_descriptions", "name", "", 30); |
82 |
echo $LANG["joblist"][6] . ":"; |
83 |
autocompletionTextField("description", "glpi_rules_descriptions", "description", "", 30); |
84 |
echo "</td><td align='center' class='tab_bg_2'>"; |
85 |
echo $LANG["rulesengine"][9] . ":"; |
86 |
$this->dropdownRulesMatch("match", "AND"); |
87 |
echo "</td><td align='center' class='tab_bg_2'>"; |
88 |
echo "</td></tr>"; |
89 |
|
90 |
echo "<tr><td align='center' class='tab_bg_2'>"; |
91 |
echo "</td><td align='center' class='tab_bg_2'>"; |
92 |
echo $LANG["profiles"][22].":"; |
93 |
dropdownValue("glpi_profiles","FK_profiles"); |
94 |
echo "</td><td align='center' class='tab_bg_2'>"; |
95 |
echo $LANG["profiles"][28].":"; |
96 |
dropdownYesNo("recursive",0); |
97 |
echo "</td><td align='center' class='tab_bg_2'>"; |
98 |
echo "<input type=hidden name='rule_type' value=\"" . $this->rule_type . "\">"; |
99 |
echo "<input type=hidden name='FK_entities' value=\"-1\">"; |
100 |
echo "<input type=hidden name='affectentity' value=\"" . $ID . "\">"; |
101 |
echo "<input type='submit' name='add_user_rule' value=\"" . $LANG["buttons"][8] . "\" class='submit'>"; |
102 |
echo "</td></tr>"; |
103 |
|
104 |
echo "</table></div><br>"; |
105 |
|
106 |
} |
107 |
|
108 |
echo "<div class='center'><table class='tab_cadrehov'><tr><th colspan='3'>" . $LANG["entity"][6] . "</th></tr>"; |
109 |
|
110 |
//Get all rules and actions
|
111 |
$rules = $this->getRulesByID( $ID, 0, 1); |
112 |
|
113 |
if (!empty ($rules)) { |
114 |
|
115 |
foreach ($rules as $rule) { |
116 |
echo "<tr class='tab_bg_1'>"; |
117 |
|
118 |
if ($canedit) { |
119 |
echo "<td width='10'>"; |
120 |
$sel = ""; |
121 |
if (isset ($_GET["select"]) && $_GET["select"] == "all") |
122 |
$sel = "checked"; |
123 |
echo "<input type='checkbox' name='item[" . $rule->fields["ID"] . "]' value='1' $sel>"; |
124 |
echo "</td>"; |
125 |
} |
126 |
|
127 |
if ($canedit) |
128 |
echo "<td><a href=\"" . $CFG_GLPI["root_doc"] . "/front/rule.right.form.php?ID=" . $rule->fields["ID"] . "&onglet=1\">" . $rule->fields["name"] . "</a></td>"; |
129 |
else
|
130 |
echo "<td>" . $rule->fields["name"] . "</td>"; |
131 |
|
132 |
echo "<td>" . $rule->fields["description"] . "</td>"; |
133 |
echo "</tr>"; |
134 |
} |
135 |
} |
136 |
echo "</table></div>"; |
137 |
|
138 |
if ($canedit) { |
139 |
echo "<div class='center'>"; |
140 |
echo "<table width='80%'>"; |
141 |
echo "<tr><td><img src=\"" . $CFG_GLPI["root_doc"] . "/pics/arrow-left.png\" alt=''></td><td class='center'><a onclick= \"if ( markAllRows('ldapaffectation_form') ) return false;\" href='" . $_SERVER['PHP_SELF'] . "?ID=$ID&select=all'>" . $LANG["buttons"][18] . "</a></td>"; |
142 |
|
143 |
echo "<td>/</td><td class='center'><a onclick= \"if ( unMarkAllRows('ldapaffectation_form') ) return false;\" href='" . $_SERVER['PHP_SELF'] . "?ID=$ID&select=none'>" . $LANG["buttons"][19] . "</a>"; |
144 |
echo "</td><td align='left' width='80%'>"; |
145 |
echo "<input type='submit' name='delete_user_rule' value=\"" . $LANG["buttons"][6] . "\" class='submit'>"; |
146 |
echo "</td>"; |
147 |
echo "</table>"; |
148 |
|
149 |
echo "</div>"; |
150 |
|
151 |
} |
152 |
echo "</form>"; |
153 |
} |
154 |
|
155 |
/**
|
156 |
* Get all ldap rules criterias from the DB and add them into the RULES_CRITERIAS
|
157 |
*/
|
158 |
function addLdapCriteriasToArray() |
159 |
{ |
160 |
global $DB,$RULES_CRITERIAS; |
161 |
|
162 |
$sql = "SELECT name,value,rule_type FROM glpi_rules_ldap_parameters WHERE rule_type='".$this->rule_type."'"; |
163 |
$result = $DB->query($sql); |
164 |
while ($datas = $DB->fetch_array($result)) |
165 |
{ |
166 |
$RULES_CRITERIAS[$this->rule_type][$datas["value"]]['name']=$datas["name"]; |
167 |
$RULES_CRITERIAS[$this->rule_type][$datas["value"]]['field']=$datas["value"]; |
168 |
$RULES_CRITERIAS[$this->rule_type][$datas["value"]]['linkfield']=''; |
169 |
$RULES_CRITERIAS[$this->rule_type][$datas["value"]]['table']=''; |
170 |
} |
171 |
} |
172 |
|
173 |
/**
|
174 |
* Execute the actions as defined in the rule
|
175 |
* @param $output the result of the actions
|
176 |
* @param $params the parameters
|
177 |
* @return the fields modified
|
178 |
*/
|
179 |
function executeActions($output,$params) |
180 |
{ |
181 |
$entity=''; |
182 |
$right=''; |
183 |
$recursive = 0; |
184 |
|
185 |
if (count($this->actions)){ |
186 |
foreach ($this->actions as $action){ |
187 |
switch ($action->fields["action_type"]){ |
188 |
case "assign" : |
189 |
if ($action->fields["field"] == "FK_entities") $entity = $action->fields["value"]; |
190 |
elseif ($action->fields["field"] == "FK_profiles") $right = $action->fields["value"]; |
191 |
elseif ($action->fields["field"] == "recursive") $recursive = $action->fields["value"]; |
192 |
elseif ($action->fields["field"] == "active") $output["active"] = $action->fields["value"]; |
193 |
break;
|
194 |
} |
195 |
} |
196 |
} |
197 |
|
198 |
//Nothing to be returned by the function :
|
199 |
//Store in session the entity and/or right
|
200 |
if ($entity != '' && $right != '') |
201 |
$output["_ldap_rules"]["rules_entities_rights"][]=array($entity,$right,$recursive); |
202 |
elseif ($entity != '') |
203 |
$output["_ldap_rules"]["rules_entities"][]=array($entity,$recursive); |
204 |
elseif ($right != '') |
205 |
$output["_ldap_rules"]["rules_rights"][]=$right; |
206 |
|
207 |
return $output; |
208 |
} |
209 |
|
210 |
|
211 |
/**
|
212 |
* Return all rules from database
|
213 |
* @param $ID of rules
|
214 |
* @param $withcriterias import rules criterias too
|
215 |
* @param $withactions import rules actions too
|
216 |
*/
|
217 |
function getRulesByID($ID, $withcriterias, $withactions) { |
218 |
global $DB; |
219 |
$ldap_affect_user_rules = array (); |
220 |
// MOYO : quoi donc que ca fout la ca ?
|
221 |
// MOYO : ca correspond pas deja à un cas particulier de ca : getRuleWithCriteriasAndActions ?
|
222 |
|
223 |
|
224 |
//Get all the rules whose rule_type is $rule_type and entity is $ID
|
225 |
$sql="SELECT * FROM `glpi_rules_actions` as gra, glpi_rules_descriptions as grd WHERE gra.FK_rules=grd.ID AND gra.field='FK_entities' and grd.rule_type=".$this->rule_type." and gra.value='".$ID."'"; |
226 |
|
227 |
$result = $DB->query($sql); |
228 |
while ($rule = $DB->fetch_array($result)) { |
229 |
$affect_rule = new Rule; |
230 |
$affect_rule->getRuleWithCriteriasAndActions($rule["ID"], 0, 1); |
231 |
$ldap_affect_user_rules[] = $affect_rule; |
232 |
} |
233 |
|
234 |
return $ldap_affect_user_rules; |
235 |
} |
236 |
|
237 |
function getTitleCriteria($target) { |
238 |
global $LANG,$CFG_GLPI; |
239 |
echo "<div class='center'>"; |
240 |
echo "<table class='tab_cadrehov'>"; |
241 |
echo "<tr class='tab_bg_2'>"; |
242 |
echo "<td width='100%'>"; |
243 |
echo "<a href=\"".$CFG_GLPI["root_doc"]."/front/ldap.parameters.php\">".$LANG["setup"][142]." ".$LANG["ruleldap"][1]."</a>"; |
244 |
echo "</td></tr>"; |
245 |
echo "</table></div><br>"; |
246 |
|
247 |
} |
248 |
|
249 |
function getTitleRule($target) { |
250 |
} |
251 |
|
252 |
function getTitle() |
253 |
{ |
254 |
global $LANG; |
255 |
return $LANG["entity"][6]; |
256 |
} |
257 |
} |
258 |
|
259 |
/// Rule collection class for Rights management
|
260 |
class RightRuleCollection extends RuleCollection { |
261 |
|
262 |
/// Array containing results : entity + right
|
263 |
var $rules_entity_rights = array(); |
264 |
/// Array containing results : only entity
|
265 |
var $rules_entity = array(); |
266 |
/// Array containing results : only right
|
267 |
var $rules_rights = array(); |
268 |
|
269 |
/**
|
270 |
* Constructor
|
271 |
**/
|
272 |
function RightRuleCollection() { |
273 |
global $DB; |
274 |
$this->rule_type = RULE_AFFECT_RIGHTS; |
275 |
$this->rule_class_name = 'RightAffectRule'; |
276 |
$this->stop_on_first_match=false; |
277 |
$this->right="rule_ldap"; |
278 |
$this->orderby="name"; |
279 |
} |
280 |
|
281 |
function getTitle() { |
282 |
global $LANG; |
283 |
return $LANG["rulesengine"][19]; |
284 |
} |
285 |
|
286 |
|
287 |
function cleanTestOutputCriterias($output){ |
288 |
if (isset($output["_rule_process"])) |
289 |
unset($output["_rule_process"]); |
290 |
|
291 |
return $output; |
292 |
} |
293 |
|
294 |
function showTestResults($rule,$output,$global_result){ |
295 |
global $LANG,$RULES_ACTIONS; |
296 |
|
297 |
echo "<tr><th colspan='4'>" . $LANG["rulesengine"][81] . "</th></tr>"; |
298 |
echo "<tr class='tab_bg_2'>"; |
299 |
echo "<td class='tab_bg_2' colspan='4' align='center'>".$LANG["rulesengine"][41]." : <strong> ".getYesNo($global_result)."</strong></td>"; |
300 |
|
301 |
|
302 |
if (isset($output["_ldap_rules"]["rules_entities"])) |
303 |
{ |
304 |
echo "<tr class='tab_bg_2'>"; |
305 |
echo "<td class='tab_bg_2' colspan='4' align='center'>".$LANG["rulesengine"][111]."</td>"; |
306 |
|
307 |
foreach ($output["_ldap_rules"]["rules_entities"] as $val) |
308 |
{ |
309 |
$this->displayActionByName("entity",$val[0]); |
310 |
if (isset($val[1])) |
311 |
$this->displayActionByName("recursive",$val[1]); |
312 |
} |
313 |
} |
314 |
|
315 |
if (isset($output["_ldap_rules"]["rules_rights"])) |
316 |
{ |
317 |
echo "<tr class='tab_bg_2'>"; |
318 |
echo "<td class='tab_bg_2' colspan='4' align='center'>".$LANG["rulesengine"][110]."</td>"; |
319 |
|
320 |
foreach ($output["_ldap_rules"]["rules_rights"] as $val) |
321 |
$this->displayActionByName("profile",$val[0]); |
322 |
} |
323 |
|
324 |
if (isset($output["_ldap_rules"]["rules_entities_rights"])) |
325 |
{ |
326 |
echo "<tr class='tab_bg_2'>"; |
327 |
echo "<td class='tab_bg_2' colspan='4' align='center'>".$LANG["rulesengine"][112]."</td>"; |
328 |
|
329 |
foreach ($output["_ldap_rules"]["rules_entities_rights"] as $val) |
330 |
{ |
331 |
$this->displayActionByName("entity",$val[0]); |
332 |
if (isset($val[1])) |
333 |
$this->displayActionByName("profile",$val[1]); |
334 |
if (isset($val[2])) |
335 |
$this->displayActionByName("recursive",$val[2]); |
336 |
} |
337 |
} |
338 |
|
339 |
if (isset($output["_ldap_rules"])) |
340 |
unset($output["_ldap_rules"]); |
341 |
|
342 |
foreach ($output as $criteria => $value) |
343 |
{ |
344 |
echo "<tr class='tab_bg_2'>"; |
345 |
echo "<td class='tab_bg_2' align='center'>"; |
346 |
echo $RULES_ACTIONS[$this->rule_type][$criteria]["name"]; |
347 |
echo "</td>"; |
348 |
echo "<td class='tab_bg_2' align='center'>"; |
349 |
echo $rule->getActionValue($criteria,$value); |
350 |
echo "</td>"; |
351 |
echo "</tr>"; |
352 |
|
353 |
} |
354 |
echo "</tr>"; |
355 |
} |
356 |
/**
|
357 |
* Display action using its name
|
358 |
* @param $name action name
|
359 |
* @param $value default value
|
360 |
*/
|
361 |
function displayActionByName($name,$value){ |
362 |
global $LANG; |
363 |
echo "<tr>"; |
364 |
switch ($name){ |
365 |
case "entity": |
366 |
echo "<td class='tab_bg_2' align='center'>".$LANG["entity"][0]." </td>\n"; |
367 |
echo "<td class='tab_bg_2' align='center'>"; |
368 |
echo getDropdownName("glpi_entities",$value); |
369 |
echo "</td>"; |
370 |
break;
|
371 |
case "profile": |
372 |
echo "<td class='tab_bg_2' align='center'>".$LANG["Menu"][35]." </td>\n"; |
373 |
echo "<td class='tab_bg_2' align='center'>"; |
374 |
echo getDropdownName("glpi_profiles",$value); |
375 |
echo "</td>"; |
376 |
break;
|
377 |
case "recursive": |
378 |
echo "<td class='tab_bg_2' align='center'>".$LANG["profiles"][28]." </td>\n"; |
379 |
echo "<td class='tab_bg_2' align='center'>"; |
380 |
echo ((!$value)?$LANG["choice"][0]:$LANG["choice"][1]); |
381 |
echo "</td>"; |
382 |
break;
|
383 |
} |
384 |
echo "</tr>"; |
385 |
} |
386 |
/**
|
387 |
* Get all the fields needed to perform the rule
|
388 |
*/
|
389 |
function getFieldsToLookFor() |
390 |
{ |
391 |
global $DB; |
392 |
$params = array(); |
393 |
$sql = "SELECT DISTINCT value " . |
394 |
"FROM glpi_rules_descriptions, glpi_rules_criterias, glpi_rules_ldap_parameters " .
|
395 |
"WHERE glpi_rules_descriptions.rule_type='".$this->rule_type."' AND glpi_rules_criterias.FK_rules=glpi_rules_descriptions.ID AND glpi_rules_criterias.criteria=glpi_rules_ldap_parameters.value"; |
396 |
|
397 |
$result = $DB->query($sql); |
398 |
while ($param = $DB->fetch_array($result)) |
399 |
{ |
400 |
//Dn is alwsays retreived from ldap : don't need to ask for it !
|
401 |
if ($param["value"] != "dn") |
402 |
$params[]=strtolower($param["value"]); |
403 |
} |
404 |
return $params; |
405 |
} |
406 |
|
407 |
/**
|
408 |
* Get the attributes needed for processing the rules
|
409 |
* @param $input input datas
|
410 |
* @param $params extra parameters given
|
411 |
* @return an array of attributes
|
412 |
*/
|
413 |
function prepareInputDataForProcess($input,$params){ |
414 |
global $RULES_CRITERIAS; |
415 |
|
416 |
$rule_parameters = array(); |
417 |
|
418 |
//LDAP type method
|
419 |
if ($params["type"] == "LDAP") |
420 |
{ |
421 |
//Get all the field to retrieve to be able to process rule matching
|
422 |
$rule_fields = $this->getFieldsToLookFor(); |
423 |
|
424 |
//Get all the datas we need from ldap to process the rules
|
425 |
$sz = @ ldap_read($params["connection"], $params["userdn"], "objectClass=*", $rule_fields); |
426 |
$rule_input = ldap_get_entries($params["connection"], $sz); |
427 |
|
428 |
if (count($rule_input)) |
429 |
{ |
430 |
|
431 |
if (isset($input)) |
432 |
$groups = $input; |
433 |
else
|
434 |
$groups = array(); |
435 |
|
436 |
$rule_input = $rule_input[0]; |
437 |
|
438 |
//Get all the ldap fields
|
439 |
$fields = $this->getFieldsForQuery(); |
440 |
|
441 |
foreach ($fields as $field) |
442 |
{ |
443 |
switch(strtoupper($field)) |
444 |
{ |
445 |
case "LDAP_SERVER": |
446 |
$rule_parameters["LDAP_SERVER"] = $params["ldap_server"]; |
447 |
break;
|
448 |
case "GROUPS" : |
449 |
foreach ($groups as $group) |
450 |
$rule_parameters["GROUPS"][] = $group; |
451 |
break;
|
452 |
default :
|
453 |
if (isset($rule_input[$field])) |
454 |
{ |
455 |
if (!is_array($rule_input[$field])) |
456 |
$rule_parameters[$field] = $rule_input[$field]; |
457 |
else
|
458 |
{ |
459 |
for ($i=0;$i < count($rule_input[$field]) -1;$i++) |
460 |
$rule_parameters[$field][] = $rule_input[$field][$i]; |
461 |
break;
|
462 |
} |
463 |
} |
464 |
} |
465 |
} |
466 |
|
467 |
return $rule_parameters; |
468 |
} |
469 |
else return $rule_input; |
470 |
} |
471 |
//IMAP/POP login method
|
472 |
else
|
473 |
{ |
474 |
$rule_parameters["MAIL_SERVER"] = $params["mail_server"]; |
475 |
$rule_parameters["MAIL_EMAIL"] = $params["email"]; |
476 |
return $rule_parameters; |
477 |
} |
478 |
} |
479 |
|
480 |
/**
|
481 |
* Get the list of fields to be retreived to process rules
|
482 |
*/
|
483 |
function getFieldsForQuery() |
484 |
{ |
485 |
global $RULES_CRITERIAS; |
486 |
|
487 |
$fields = array(); |
488 |
foreach ($RULES_CRITERIAS[$this->rule_type] as $criteria){ |
489 |
if (isset($criteria['virtual']) && $criteria['virtual'] == "true") |
490 |
$fields[]=$criteria['id']; |
491 |
else
|
492 |
$fields[]=$criteria['field']; |
493 |
} |
494 |
|
495 |
return $fields; |
496 |
} |
497 |
} |
498 |
?>
|