ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLDAPRoleGroupMappingSettings.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 = array();
35  private $server_id = null;
36  private $db = null;
37  private $mappings = null;
38 
39  const MAPPING_INFO_ALL = 1;
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 ilLDAPRoleGroupMappingSettings($a_server_id);
72  }
73 
74  public static function _deleteByRole($a_role_id)
75  {
76  global $ilDB;
77 
78  $query = "DELETE FROM ldap_rg_mapping ".
79  "WHERE role = ".$ilDB->quote($a_role_id,'integer');
80  $res = $ilDB->manipulate($query);
81 
82  return true;
83  }
84 
85  public static function _deleteByServerId($a_server_id)
86  {
87  global $ilDB;
88 
89  $query = "DELETE FROM ldap_rg_mapping ".
90  "WHERE server_id = ".$ilDB->quote($a_server_id,'integer');
91  $res = $ilDB->manipulate($query);
92 
93  return true;
94  }
95 
96  public static function _getAllActiveMappings()
97  {
98  global $ilDB,$rbacreview;
99 
100  $query = "SELECT rgm.* FROM ldap_rg_mapping rgm JOIN ldap_server_settings lss ".
101  "ON rgm.server_id = lss.server_id ".
102  "WHERE lss.active = 1 ".
103  "AND lss.role_sync_active = 1 ";
104  $res = $ilDB->query($query);
105  while($row = $ilDB->fetchObject($res))
106  {
107  $data['server_id'] = $row->server_id;
108  $data['url'] = $row->url;
109  $data['mapping_id'] = $row->mapping_id;
110  $data['dn'] = $row->dn;
111  $data['member'] = $row->member_attribute;
112  $data['isdn'] = $row->member_isdn;
113  $data['info'] = $row->mapping_info;
114  $data['info_type'] = $row->mapping_info_type;
115  // read assigned object
116  $data['object_id'] = $rbacreview->getObjectOfRole($row->role);
117 
118 
119  $active[$row->role][] = $data;
120  }
121  return $active ? $active : array();
122  }
123 
124  public function getServerId()
125  {
126  return $this->server_id;
127  }
128 
135  public function getMappings()
136  {
137  return $this->mappings ? $this->mappings : array();
138  }
139 
140  public function loadFromPost($a_mappings)
141  {
142  global $rbacreview;
143 
144  $this->mappings = array();
145  foreach($a_mappings as $mapping_id => $data)
146  {
147  if($mapping_id == 0)
148  {
149  if(!$data['dn'] and !$data['member'] and !$data['memberisdn'] and !$data['role'])
150  {
151  continue;
152  }
153  }
154  $this->mappings[$mapping_id]['dn'] = ilUtil::stripSlashes($data['dn']);
155  $this->mappings[$mapping_id]['url'] = ilUtil::stripSlashes($data['url']);
156  $this->mappings[$mapping_id]['member_attribute'] = ilUtil::stripSlashes($data['member']);
157  $this->mappings[$mapping_id]['member_isdn'] = ilUtil::stripSlashes($data['memberisdn']);
158  $this->mappings[$mapping_id]['role_name'] = ilUtil::stripSlashes($data['role']);
159  $this->mappings[$mapping_id]['role'] = $rbacreview->roleExists(ilUtil::stripSlashes($data['role']));
160  $this->mappings[$mapping_id]['info'] = ilUtil::stripSlashes($data['info']);
161  $this->mappings[$mapping_id]['info_type'] = ilUtil::stripSlashes($data['info_type']);
162  }
163  }
164 
171  public function validate()
172  {
173  global $ilErr,$rbacreview;
174 
175  $ilErr->setMessage('');
176  $found_missing = false;
177  foreach($this->mappings as $mapping_id => $data)
178  {
179  // Check if all required fields are available
180  if(!strlen($data['dn']) || !strlen($data['member_attribute']) || !strlen($data['role_name']))
181  {
182  if(!$found_missing)
183  {
184  $found_missing = true;
185  $ilErr->appendMessage($this->lng->txt('fill_out_all_required_fields'));
186  }
187  }
188  // Check role valid
189  if(strlen($data['role_name']) and !$rbacreview->roleExists($data['role_name']))
190  {
191  $ilErr->appendMessage($this->lng->txt('ldap_role_not_exists').' '.$data['role_name']);
192  }
193  }
194  return strlen($ilErr->getMessage()) ? false : true;
195  }
196 
204  public function save()
205  {
206  global $ilDB;
207 
208  foreach($this->mappings as $mapping_id => $data)
209  {
210  if(!$mapping_id)
211  {
212  $next_id = $ilDB->nextId('ldap_rg_mapping');
213  $query = "INSERT INTO ldap_rg_mapping (mapping_id,server_id,url,dn,member_attribute,member_isdn,role,mapping_info,mapping_info_type) ".
214  "VALUES ( ".
215  $ilDB->quote($next_id,'integer').", ".
216  $this->db->quote($this->getServerId(),'integer').", ".
217  $this->db->quote($data['url'],'text').", ".
218  $this->db->quote($data['dn'],'text').", ".
219  $this->db->quote($data['member_attribute'],'text').", ".
220  $this->db->quote($data['member_isdn'],'integer').", ".
221  $this->db->quote($data['role'],'integer').", ".
222  $this->db->quote($data['info'],'text').", ".
223  $this->db->quote($data['info_type'],'integer').
224  ")";
225  $res = $ilDB->manipulate($query);
226  }
227  else
228  {
229  $query = "UPDATE ldap_rg_mapping ".
230  "SET server_id = ".$this->db->quote($this->getServerId(),'integer').", ".
231  "url = ".$this->db->quote($data['url'],'text').", ".
232  "dn =".$this->db->quote($data['dn'],'text').", ".
233  "member_attribute = ".$this->db->quote($data['member_attribute'],'text').", ".
234  "member_isdn = ".$this->db->quote($data['member_isdn'],'integer').", ".
235  "role = ".$this->db->quote($data['role'],'integer').", ".
236  "mapping_info = ".$this->db->quote($data['info'],'text').", ".
237  "mapping_info_type = ".$this->db->quote($data['info_type'],'integer')." ".
238  "WHERE mapping_id = ".$this->db->quote($mapping_id,'integer');
239  $res = $ilDB->manipulate($query);
240 
241  }
242  }
243  $this->read();
244  }
245 
246 
254  public function delete($a_mapping_id)
255  {
256  global $ilDB;
257 
258  $query = "DELETE FROM ldap_rg_mapping ".
259  "WHERE server_id = ".$this->db->quote($this->getServerId(),'integer')." ".
260  "AND mapping_id = ".$this->db->quote($a_mapping_id ,'integer');
261  $res = $ilDB->manipulate($query);
262  $this->read();
263  }
264 
265 
272  public function getMappingInfoString($a_mapping_id)
273  {
274  $role = $this->mappings[$a_mapping_id]['role_name'];
275  $dn_parts = explode(',',$this->mappings[$a_mapping_id]['dn']);
276 
277  return (array_key_exists(0,$dn_parts) ? $dn_parts[0] : "''");
278  }
279 
280 
287  private function read()
288  {
289  global $ilObjDataCache,$rbacreview,$tree;
290 
291  $this->mappings = array();
292  $query = "SELECT * FROM ldap_rg_mapping LEFT JOIN object_data ".
293  "ON role = obj_id ".
294  "WHERE server_id =".$this->db->quote($this->getServerId(),'integer').' '.
295  "ORDER BY title,dn";
296 
297  $res = $this->db->query($query);
298  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
299  {
300  $this->mappings[$row->mapping_id]['dn'] = $row->dn;
301  $this->mappings[$row->mapping_id]['url'] = $row->url;
302  $this->mappings[$row->mapping_id]['member_attribute'] = $row->member_attribute;
303  $this->mappings[$row->mapping_id]['member_isdn'] = $row->member_isdn;
304  $this->mappings[$row->mapping_id]['role'] = $row->role;
305  $this->mappings[$row->mapping_id]['info'] = $row->mapping_info;
306  $this->mappings[$row->mapping_id]['info_type'] = $row->mapping_info_type;
307  if($ilObjDataCache->lookupType($row->role) == 'role')
308  {
309  $this->mappings[$row->mapping_id]['role_name'] = $ilObjDataCache->lookupTitle($row->role);
310  }
311  else
312  {
313  $this->mappings[$row->mapping_id]['role_name'] = $row->role;
314  }
315 
316  }
317  }
318 
319 }
320 
321 ?>