ILIAS  release_4-3 Revision
 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 TABLE_NAME = 'shib_role_assignment';
38  const ERR_MISSING_NAME = 'shib_missing_attr_name';
39  const ERR_MISSING_VALUE = 'shib_missing_attr_value';
40  const ERR_MISSING_ROLE = 'shib_missing_role';
41  const ERR_MISSING_PLUGIN_ID = 'shib_missing_plugin_id';
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 
308  public function matches($a_data) {
309  if ($this->isPluginActive()) {
311  }
312  // No value
313  if (! isset($a_data[$this->getName()])) {
314  return false;
315  }
316  $values = $a_data[$this->getName()];
317  if (is_array($values)) {
318  return in_array($this->getValue(), $values);
319  } else {
320  return $this->wildcardCompare($this->getValue(), $values);
321  #return $this->getValue() == $values;
322  }
323  }
324 
325 
332  protected function wildcardCompare($a_str1, $a_str2) {
333  $pattern = str_replace('*', '.*?', $a_str1);
334  $GLOBALS['ilLog']->write(__METHOD__ . ': Replace pattern:' . $pattern . ' => ' . $a_str2);
335 
336  return (bool)preg_match("/" . $pattern . "/us", $a_str2);
337  // return (bool)preg_match('/^' . $pattern . '$/i', $a_str2);
338  }
339 
340 
344  private function read() {
345  if (! $this->getRuleId()) {
346  return true;
347  }
348  $query = 'SELECT * FROM ' . self::TABLE_NAME . ' ' . 'WHERE rule_id = ' . $this->db->quote($this->getRuleId(), 'integer');
349  $res = $this->db->query($query);
350  while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) {
351  $this->setRoleId($row->role_id);
352  $this->setName($row->name);
353  $this->setValue($row->value);
354  $this->enablePlugin($row->plugin);
355  $this->setPluginId($row->plugin_id);
356  $this->enableAddOnUpdate($row->add_on_update);
357  $this->enableRemoveOnUpdate($row->remove_on_update);
358  }
359  }
360 }
361 
362 ?>