ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLDAPRoleAssignmentRule.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 
33 {
34  private static $instances = null;
35 
36  const TYPE_GROUP = 1;
37  const TYPE_ATTRIBUTE = 2;
38 
46  private function __construct($a_id = 0)
47  {
48  global $ilDB;
49 
50  $this->db = $ilDB;
51 
52  $this->rule_id = $a_id;
53  $this->read();
54  }
55 
64  public static function _getInstanceByRuleId($a_rule_id)
65  {
66  if(isset(self::$instances[$a_rule_id]))
67  {
68  return self::$instances[$a_rule_id];
69  }
70  return self::$instances[$a_rule_id] = new ilLDAPRoleAssignmentRule($a_rule_id);
71  }
72 
73 
80  public function _getRules()
81  {
82  global $ilDB;
83 
84  $query = "SELECT rule_id FROM ldap_role_assignments ";
85  $res = $ilDB->query($query);
86  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
87  {
88  $rules[] = self::_getInstanceByRuleId($row->rule_id);
89  }
90  return $rules ? $rules : array();
91  }
92 
100  public function setRoleId($a_role_id)
101  {
102  $this->role_id = $a_role_id;
103  }
104 
111  public function getRoleId()
112  {
113  return $this->role_id;
114  }
115 
122  public function getRuleId()
123  {
124  return $this->rule_id;
125  }
126 
134  public function setServerId($a_id)
135  {
136  $this->server_id = $a_id;
137  }
138 
145  public function getServerId()
146  {
147  return $this->server_id;
148  }
149 
157  public function setType($a_type)
158  {
159  $this->type = $a_type;
160  }
161 
169  public function getType()
170  {
171  return $this->type;
172  }
173 
181  public function setDN($a_dn)
182  {
183  $this->dn = $a_dn;
184  }
185 
192  public function getDN()
193  {
194  return $this->dn;
195  }
196 
204  public function setMemberAttribute($a_attribute)
205  {
206  $this->member_attribute = $a_attribute;
207  }
208 
215  public function getMemberAttribute()
216  {
217  return $this->member_attribute;
218  }
219 
227  public function setMemberIsDN($a_status)
228  {
229  $this->member_is_dn = $a_status;
230  }
231 
238  public function isMemberAttributeDN()
239  {
240  return (bool) $this->member_is_dn;
241  }
242 
250  public function setAttributeName($a_name)
251  {
252  $this->attribute_name = $a_name;
253  }
254 
261  public function getAttributeName()
262  {
263  return $this->attribute_name;
264  }
265 
273  public function setAttributeValue($a_value)
274  {
275  $this->attribute_value = $a_value;
276  }
277 
284  public function getAttributeValue()
285  {
286  return $this->attribute_value;
287  }
288 
295  public function conditionToString()
296  {
297  switch($this->getType())
298  {
299  case self::TYPE_GROUP:
300  $dn_arr = explode(',',$this->getDN());
301  return $dn_arr[0];
302 
303 
304  case self::TYPE_ATTRIBUTE:
305  return $this->getAttributeName().'='.$this->getAttributeValue();
306  }
307  }
308 
309 
317  public function create()
318  {
319  $query = "INSERT INTO ldap_role_assignments ".
320  "SET server_id = ".$this->db->quote($this->getServerId()).", ".
321  "type = ".$this->db->quote($this->getType()).", ".
322  "dn = ".$this->db->quote($this->getDN()).", ".
323  "attribute = ".$this->db->quote($this->getMemberAttribute()).", ".
324  "isdn = ".$this->db->quote($this->isMemberAttributeDN()).", ".
325  "att_name = ".$this->db->quote($this->getAttributeName()).", ".
326  "att_value = ".$this->db->quote($this->getAttributeValue()).", ".
327  "role_id = ".(int) $this->getRoleId()." ";
328  $res = $this->db->query($query);
329 
330  $this->rule_id = $this->db->getLastInsertId();
331  return true;
332  }
333 
340  public function update()
341  {
342  $query = "UPDATE ldap_role_assignments ".
343  "SET server_id = ".$this->db->quote($this->getServerId()).", ".
344  "type = ".$this->db->quote($this->getType()).", ".
345  "dn = ".$this->db->quote($this->getDN()).", ".
346  "attribute = ".$this->db->quote($this->getMemberAttribute()).", ".
347  "isdn = ".$this->db->quote($this->isMemberAttributeDN()).", ".
348  "att_name = ".$this->db->quote($this->getAttributeName()).", ".
349  "att_value = ".$this->db->quote($this->getAttributeValue()).", ".
350  "role_id = ".(int) $this->getRoleId()." ".
351  "WHERE rule_id = ".$this->db->quote($this->getRuleId())." ";
352 
353  $res = $this->db->query($query);
354 
355  $this->rule_id = $this->db->getLastInsertId();
356  return true;
357  }
358 
365  public function validate()
366  {
367  global $ilErr;
368 
369  $ilErr->setMessage('');
370 
371  if(!$this->getRoleId())
372  {
373  $ilErr->setMessage('fill_out_all_required_fields');
374  return false;
375  }
376  switch($this->getType())
377  {
378  case self::TYPE_GROUP:
379  if(!strlen($this->getDN()) or !strlen($this->getMemberAttribute()))
380  {
381  $ilErr->setMessage('fill_out_all_required_fields');
382  return false;
383  }
384  break;
385  case self::TYPE_ATTRIBUTE:
386  if(!strlen($this->getAttributeName()) or !strlen($this->getAttributeValue()))
387  {
388  $ilErr->setMessage('fill_out_all_required_fields');
389  return false;
390  }
391  break;
392 
393  default:
394  $ilErr->setMessage('ldap_no_type_given');
395  return false;
396  }
397  return true;
398  }
399 
406  public function delete()
407  {
408  $query = "DELETE FROM ldap_role_assignments ".
409  "WHERE rule_id = ".$this->db->quote($this->getRuleId())." ";
410  $this->db->query($query);
411  return true;
412 
413  }
420  private function read()
421  {
422  $query = "SELECT * FROM ldap_role_assignments ".
423  "WHERE rule_id = ".$this->db->quote($this->getRuleId())." ";
424 
425  $res = $this->db->query($query);
426  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
427  {
428  $this->setServerId($row->server_id);
429  $this->setType($row->type);
430  $this->setDN($row->dn);
431  $this->setMemberAttribute($row->attribute);
432  $this->setMemberIsDN($row->isdn);
433  $this->setAttributeName($row->att_name);
434  $this->setAttributeValue($row->att_value);
435  $this->setRoleId($row->role_id);
436  }
437  }
438 }
439 ?>