ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  global $ilDB;
85  $this->db = $ilDB;
86  $this->rule_id = $a_rule_id;
87  $this->read();
88  }
89 
90 
94  public function setRuleId($a_id) {
95  $this->rule_id = $a_id;
96  }
97 
98 
102  public function getRuleId() {
103  return $this->rule_id;
104  }
105 
106 
110  public function setRoleId($a_id) {
111  $this->role_id = $a_id;
112  }
113 
114 
118  public function getRoleId() {
119  return $this->role_id;
120  }
121 
122 
126  public function setName($a_name) {
127  $this->attribute_name = $a_name;
128  }
129 
130 
134  public function getName() {
135  return $this->attribute_name;
136  }
137 
138 
142  public function setValue($a_value) {
143  $this->attribute_value = $a_value;
144  }
145 
146 
150  public function getValue() {
151  return $this->attribute_value;
152  }
153 
154 
158  public function enablePlugin($a_status) {
159  $this->plugin_active = $a_status;
160  }
161 
162 
166  public function isPluginActive() {
167  return (bool)$this->plugin_active;
168  }
169 
170 
174  public function enableAddOnUpdate($a_status) {
175  $this->add_on_update = $a_status;
176  }
177 
178 
182  public function isAddOnUpdateEnabled() {
183  return (bool)$this->add_on_update;
184  }
185 
186 
190  public function enableRemoveOnUpdate($a_status) {
191  $this->remove_on_update = $a_status;
192  }
193 
194 
198  public function isRemoveOnUpdateEnabled() {
199  return (bool)$this->remove_on_update;
200  }
201 
202 
206  public function setPluginId($a_id) {
207  $this->plugin_id = $a_id;
208  }
209 
210 
214  public function getPluginId() {
215  return $this->plugin_id;
216  }
217 
218 
222  public function conditionToString() {
223  global $lng;
224  if ($this->isPluginActive()) {
225  return $lng->txt('shib_plugin_id') . ': ' . $this->getPluginId();
226  } else {
227  return $this->getName() . '=' . $this->getValue();
228  }
229  }
230 
231 
235  public function validate() {
236  if (! $this->getRoleId()) {
237  return self::ERR_MISSING_ROLE;
238  }
239  if (! $this->isPluginActive()) {
240  if (! $this->getName()) {
241  return self::ERR_MISSING_NAME;
242  }
243  if (! $this->getValue()) {
245  }
246  } else {
247  // check plugin id is given
248  if (! $this->getPluginId()) {
250  }
251  }
252 
253  return '';
254  }
255 
256 
260  public function delete() {
261  $query = 'DELETE FROM ' . self::TABLE_NAME . ' ' . 'WHERE rule_id = ' . $this->db->quote($this->getRuleId(), 'integer');
262  $this->db->manipulate($query);
263 
264  return true;
265  }
266 
267 
271  public function add() {
272  $next_id = $this->db->nextId(self::TABLE_NAME);
273  $query = 'INSERT INTO ' . self::TABLE_NAME . ' (rule_id,role_id,name,value,plugin,plugin_id,add_on_update,remove_on_update ) ' . 'VALUES( '
274  . $this->db->quote($next_id, 'integer') . ', ' . $this->db->quote($this->getRoleId(), 'integer') . ', '
275  . $this->db->quote($this->getName(), 'text') . ', ' . $this->db->quote($this->getValue(), 'text') . ', '
276  . $this->db->quote((int)$this->isPluginActive(), 'integer') . ', ' . $this->db->quote((int)$this->getPluginId(), 'integer') . ', '
277  . $this->db->quote((int)$this->isAddOnUpdateEnabled(), 'integer') . ', '
278  . $this->db->quote((int)$this->isRemoveOnUpdateEnabled(), 'integer') . ') ';
279  $this->db->manipulate($query);
280  $this->setRuleId($this->db->getLastInsertId());
281 
282  return true;
283  }
284 
285 
289  public function update() {
290  $query = 'UPDATE ' . self::TABLE_NAME . ' ' . 'SET role_id = ' . $this->db->quote($this->getRoleId(), 'integer') . ', ' . 'name = '
291  . $this->db->quote($this->getName(), 'text') . ', ' . 'value = ' . $this->db->quote($this->getValue(), 'text') . ', ' . 'plugin = '
292  . $this->db->quote((int)$this->isPluginActive(), 'integer') . ', ' . 'plugin_id = '
293  . $this->db->quote((int)$this->getPluginId(), 'integer') . ', ' . 'add_on_update = '
294  . $this->db->quote((int)$this->isAddOnUpdateEnabled(), 'integer') . ', ' . 'remove_on_update = '
295  . $this->db->quote((int)$this->isRemoveOnUpdateEnabled(), 'integer') . ' '
296  . 'WHERE rule_id = ' . $this->db->quote($this->getRuleId(), 'integer');
297  $this->db->manipulate($query);
298 
299  return true;
300  }
301 
302 
309  public function matches($a_data) {
310  if ($this->isPluginActive()) {
312  }
313  // No value
314  if (! isset($a_data[$this->getName()])) {
315  return false;
316  }
317  $values = $a_data[$this->getName()];
318  if (is_array($values)) {
319  return in_array($this->getValue(), $values);
320  } else {
321  return $this->wildcardCompare($this->getValue(), $values);
322  }
323  }
324 
325 
333  protected function wildcardCompare($a_str1, $a_str2) {
334  $pattern = str_replace('*', '.*?', $a_str1);
335 
336  return (bool)preg_match("/" . $pattern . "/us", $a_str2);
337  }
338 
339 
345  public function doesMatch(array $a_data) {
346  if ($this->isPluginActive()) {
348  }
349  if (! isset($a_data[$this->getName()])) {
350  return false;
351  }
352  $values = $a_data[$this->getName()];
353  if (is_array($values)) {
354  return in_array($this->getValue(), $values);
355  } else {
356  $pattern = str_replace('*', '.*?', $this->getValue());
357 
358  return (bool)preg_match('/^' . $pattern . '$/us', $values);
359  }
360  }
361 
362 
366  private function read() {
367  if (! $this->getRuleId()) {
368  return true;
369  }
370  $query = 'SELECT * FROM ' . self::TABLE_NAME . ' ' . 'WHERE rule_id = ' . $this->db->quote($this->getRuleId(), 'integer');
371  $res = $this->db->query($query);
372  while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) {
373  $this->setRoleId($row->role_id);
374  $this->setName($row->name);
375  $this->setValue($row->value);
376  $this->enablePlugin($row->plugin);
377  $this->setPluginId($row->plugin_id);
378  $this->enableAddOnUpdate($row->add_on_update);
379  $this->enableRemoveOnUpdate($row->remove_on_update);
380  }
381  }
382 }
383 
384 ?>