ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLDAPAttributeMapping.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 
34 {
35  private static $instances = array();
36  private $server_id = null;
37  private $db = null;
38  private $mapping_rules = array();
39  private $rules_for_update = array();
40  private $lng;
41 
48  private function __construct($a_server_id)
49  {
50  global $ilDB,$lng;
51 
52  $this->db = $ilDB;
53  $this->lng = $lng;
54  $this->server_id = $a_server_id;
55  $this->read();
56  }
57 
65  public static function _getInstanceByServerId($a_server_id)
66  {
67  if(array_key_exists($a_server_id,self::$instances) and is_object(self::$instances[$a_server_id]))
68  {
69  return self::$instances[$a_server_id];
70  }
71  return self::$instances[$a_server_id] = new ilLDAPAttributeMapping($a_server_id);
72  }
73 
74 
82  public static function _delete($a_server_id)
83  {
84  global $ilDB;
85 
86  $query = "DELETE FROM ldap_attribute_mapping ".
87  "WHERE server_id =".$ilDB->quote($a_server_id);
88  $res = $ilDB->query($query);
89  }
90 
98  public static function _lookupGlobalRole($a_server_id)
99  {
100  global $ilDB;
101 
102  $query = "SELECT value FROM ldap_attribute_mapping ".
103  "WHERE server_id =".$ilDB->quote($a_server_id)." ".
104  "AND keyword = ".$ilDB->quote('global_role');
105 
106  $res = $ilDB->query($query);
107  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
108  {
109  return (int) $row->value;
110  }
111  return 0;
112  }
113 
120  public static function hasRulesForUpdate($a_server_id)
121  {
122  global $ilDB;
123 
124  $query = 'SELECT perform_update FROM ldap_attribute_mapping '.
125  'WHERE server_id = '.$ilDB->quote($a_server_id).' '.
126  'AND perform_update = 1';
127  $res = $ilDB->query($query);
128  return $res->numRows() ? true : false;
129  }
130 
140  public function setRule($a_field_name,$a_ldap_attribute,$a_perform_update)
141  {
142  $this->mapping_rules[$a_field_name]['value'] = $a_ldap_attribute;
143  $this->mapping_rules[$a_field_name]['performUpdate'] = $a_perform_update;
144  }
145 
153  public function getRulesForUpdate()
154  {
155  return $this->rules_for_update ? $this->rules_for_update : array();
156  }
157 
165  public function getFieldsForUpdate()
166  {
167  foreach($this->rules_for_update as $field => $rule)
168  {
169  if(!strlen($rule['value']))
170  {
171  continue;
172  }
173  if(strpos($rule['value'],',') === false)
174  {
175  $fields[] = strtolower($rule['value']);
176  continue;
177  }
178  $tmp_fields = explode(',',$rule['value']);
179  $value = '';
180  foreach($tmp_fields as $tmp_field)
181  {
182  $fields[] = trim(strtolower($tmp_field));
183  }
184  }
185  return $fields ? $fields : array();
186  }
187 
195  public function getFields()
196  {
197  foreach($this->mapping_rules as $field => $rule)
198  {
199  if(!strlen($rule['value']))
200  {
201  continue;
202  }
203  if(strpos($rule['value'],',') === false)
204  {
205  $fields[] = strtolower($rule['value']);
206  continue;
207  }
208  $tmp_fields = explode(',',$rule['value']);
209  $value = '';
210  foreach($tmp_fields as $tmp_field)
211  {
212  $fields[] = trim(strtolower($tmp_field));
213  }
214  }
215  return $fields ? $fields : array();
216  }
217 
225  public function getRules()
226  {
227  return $this->mapping_rules;
228  }
229 
236  public function clearRules()
237  {
238  $this->mapping_rules = array();
239  }
240 
247  public function save()
248  {
249  $this->delete();
250 
251  foreach($this->mapping_rules as $keyword => $options)
252  {
253  $query = "INSERT INTO ldap_attribute_mapping SET ".
254  "server_id =".$this->db->quote($this->server_id).", ".
255  "keyword = ".$this->db->quote($keyword).", ".
256  "value = ".$this->db->quote($options['value']).", ".
257  "perform_update = ".$this->db->quote($options['performUpdate']);
258  $this->db->query($query);
259  }
260  }
261 
268  public function delete()
269  {
270  self::_delete($this->server_id);
271  }
272 
281  public function enabledUpdate($a_field_name)
282  {
283  if(array_key_exists($a_field_name,$this->mapping_rules))
284  {
285  return (bool) $this->mapping_rules[$a_field_name]['performUpdate'];
286  }
287  return false;
288  }
289 
297  public function getValue($a_field_name)
298  {
299  if(array_key_exists($a_field_name,$this->mapping_rules))
300  {
301  return $this->mapping_rules[$a_field_name]['value'];
302  }
303  return '';
304  }
305 
313  private function read()
314  {
315  $query = "SELECT * FROM ldap_attribute_mapping ".
316  "WHERE server_id =".$this->db->quote($this->server_id)." ";
317 
318  $res = $this->db->query($query);
319  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
320  {
321  $this->mapping_rules[$row->keyword]['value'] = $row->value;
322  $this->mapping_rules[$row->keyword]['performUpdate'] = (bool) $row->perform_update;
323 
324  if($row->perform_update)
325  {
326  $this->rules_for_update[$row->keyword]['value'] = $row->value;
327  }
328  }
329  }
330 }
331 ?>