ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilShibbolethRoleAssignmentRule.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 require_once('./Services/AuthShibboleth/classes/class.ilShibbolethRoleAssignmentRules.php');
24 
36 {
37  const ERR_MISSING_NAME = 'shib_missing_attr_name';
38  const ERR_MISSING_VALUE = 'shib_missing_attr_value';
39  const ERR_MISSING_ROLE = 'shib_missing_role';
40  const ERR_MISSING_PLUGIN_ID = 'shib_missing_plugin_id';
41  const TABLE_NAME = 'shib_role_assignment';
45  protected $db;
49  private $rule_id = 0;
53  private $role_id = 0;
57  private $attribute_name = '';
61  private $attribute_value = '';
65  private $plugin_active = false;
69  private $add_on_update = false;
73  private $remove_on_update = false;
77  private $plugin_id = 0;
78 
79 
83  public function __construct($a_rule_id = 0)
84  {
85  global $DIC;
86  $ilDB = $DIC['ilDB'];
87  $this->db = $ilDB;
88  $this->rule_id = $a_rule_id;
89  $this->read();
90  }
91 
92 
96  public function setRuleId($a_id)
97  {
98  $this->rule_id = $a_id;
99  }
100 
101 
105  public function getRuleId()
106  {
107  return $this->rule_id;
108  }
109 
110 
114  public function setRoleId($a_id)
115  {
116  $this->role_id = $a_id;
117  }
118 
119 
123  public function getRoleId()
124  {
125  return $this->role_id;
126  }
127 
128 
132  public function setName($a_name)
133  {
134  $this->attribute_name = $a_name;
135  }
136 
137 
141  public function getName()
142  {
143  return $this->attribute_name;
144  }
145 
146 
150  public function setValue($a_value)
151  {
152  $this->attribute_value = $a_value;
153  }
154 
155 
159  public function getValue()
160  {
161  return $this->attribute_value;
162  }
163 
164 
168  public function enablePlugin($a_status)
169  {
170  $this->plugin_active = $a_status;
171  }
172 
173 
177  public function isPluginActive()
178  {
179  return (bool) $this->plugin_active;
180  }
181 
182 
186  public function enableAddOnUpdate($a_status)
187  {
188  $this->add_on_update = $a_status;
189  }
190 
191 
195  public function isAddOnUpdateEnabled()
196  {
197  return (bool) $this->add_on_update;
198  }
199 
200 
204  public function enableRemoveOnUpdate($a_status)
205  {
206  $this->remove_on_update = $a_status;
207  }
208 
209 
213  public function isRemoveOnUpdateEnabled()
214  {
215  return (bool) $this->remove_on_update;
216  }
217 
218 
222  public function setPluginId($a_id)
223  {
224  $this->plugin_id = $a_id;
225  }
226 
227 
231  public function getPluginId()
232  {
233  return $this->plugin_id;
234  }
235 
236 
240  public function conditionToString()
241  {
242  global $DIC;
243  $lng = $DIC['lng'];
244  if ($this->isPluginActive()) {
245  return $lng->txt('shib_plugin_id') . ': ' . $this->getPluginId();
246  } else {
247  return $this->getName() . '=' . $this->getValue();
248  }
249  }
250 
251 
255  public function validate()
256  {
257  if (!$this->getRoleId()) {
258  return self::ERR_MISSING_ROLE;
259  }
260  if (!$this->isPluginActive()) {
261  if (!$this->getName()) {
262  return self::ERR_MISSING_NAME;
263  }
264  if (!$this->getValue()) {
265  return self::ERR_MISSING_VALUE;
266  }
267  } else {
268  // check plugin id is given
269  if (!$this->getPluginId()) {
270  return self::ERR_MISSING_PLUGIN_ID;
271  }
272  }
273 
274  return '';
275  }
276 
277 
281  public function delete()
282  {
283  $query = 'DELETE FROM ' . self::TABLE_NAME . ' ' . 'WHERE rule_id = ' . $this->db->quote($this->getRuleId(), 'integer');
284  $this->db->manipulate($query);
285 
286  return true;
287  }
288 
289 
293  public function add()
294  {
295  $next_id = $this->db->nextId(self::TABLE_NAME);
296  $query = 'INSERT INTO ' . self::TABLE_NAME . ' (rule_id,role_id,name,value,plugin,plugin_id,add_on_update,remove_on_update ) ' . 'VALUES( '
297  . $this->db->quote($next_id, 'integer') . ', ' . $this->db->quote($this->getRoleId(), 'integer') . ', '
298  . $this->db->quote($this->getName(), 'text') . ', ' . $this->db->quote($this->getValue(), 'text') . ', '
299  . $this->db->quote((int) $this->isPluginActive(), 'integer') . ', ' . $this->db->quote((int) $this->getPluginId(), 'integer') . ', '
300  . $this->db->quote((int) $this->isAddOnUpdateEnabled(), 'integer') . ', '
301  . $this->db->quote((int) $this->isRemoveOnUpdateEnabled(), 'integer') . ') ';
302  $this->db->manipulate($query);
303  $this->setRuleId($this->db->getLastInsertId());
304 
305  return true;
306  }
307 
308 
312  public function update()
313  {
314  $query = 'UPDATE ' . self::TABLE_NAME . ' ' . 'SET role_id = ' . $this->db->quote($this->getRoleId(), 'integer') . ', ' . 'name = '
315  . $this->db->quote($this->getName(), 'text') . ', ' . 'value = ' . $this->db->quote($this->getValue(), 'text') . ', ' . 'plugin = '
316  . $this->db->quote((int) $this->isPluginActive(), 'integer') . ', ' . 'plugin_id = '
317  . $this->db->quote((int) $this->getPluginId(), 'integer') . ', ' . 'add_on_update = '
318  . $this->db->quote((int) $this->isAddOnUpdateEnabled(), 'integer') . ', ' . 'remove_on_update = '
319  . $this->db->quote((int) $this->isRemoveOnUpdateEnabled(), 'integer') . ' '
320  . 'WHERE rule_id = ' . $this->db->quote($this->getRuleId(), 'integer');
321  $this->db->manipulate($query);
322 
323  return true;
324  }
325 
326 
333  public function matches($a_data)
334  {
335  if ($this->isPluginActive()) {
337  }
338  // No value
339  if (!isset($a_data[$this->getName()])) {
340  return false;
341  }
342  $values = $a_data[$this->getName()];
343  if (is_array($values)) {
344  return in_array($this->getValue(), $values);
345  } else {
346  return $this->wildcardCompare($this->getValue(), $values);
347  }
348  }
349 
350 
358  protected function wildcardCompare($a_str1, $a_str2)
359  {
360  $pattern = str_replace('*', '.*?', $a_str1);
361 
362  return (bool) preg_match("/" . $pattern . "/us", $a_str2);
363  }
364 
365 
371  public function doesMatch(array $a_data)
372  {
373  if ($this->isPluginActive()) {
375  }
376  if (!isset($a_data[$this->getName()])) {
377  return false;
378  }
379  $values = $a_data[$this->getName()];
380  if (is_array($values)) {
381  return in_array($this->getValue(), $values);
382  } else {
383  $pattern = str_replace('*', '.*?', $this->getValue());
384 
385  return (bool) preg_match('/^' . $pattern . '$/us', $values);
386  }
387  }
388 
389 
393  private function read()
394  {
395  if (!$this->getRuleId()) {
396  return true;
397  }
398  $query = 'SELECT * FROM ' . self::TABLE_NAME . ' ' . 'WHERE rule_id = ' . $this->db->quote($this->getRuleId(), 'integer');
399  $res = $this->db->query($query);
400  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
401  $this->setRoleId($row->role_id);
402  $this->setName($row->name);
403  $this->setValue($row->value);
404  $this->enablePlugin($row->plugin);
405  $this->setPluginId($row->plugin_id);
406  $this->enableAddOnUpdate($row->add_on_update);
407  $this->enableRemoveOnUpdate($row->remove_on_update);
408  }
409  }
410 }
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res
$lng
$values
$query
$row
global $ilDB