ILIAS  Release_3_10_x_branch Revision 61812
 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_role_group_mapping ".
79  "WHERE role = ".$ilDB->quote($a_role_id);
80  $ilDB->query($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_role_group_mapping ".
90  "WHERE server_id = ".$ilDB->quote($a_server_id);
91 
92  $ilDB->query($query);
93  return true;
94  }
95 
96  public static function _getAllActiveMappings()
97  {
98  global $ilDB,$rbacreview;
99 
100  $query = "SELECT rgm.* FROM ldap_role_group_mapping as rgm JOIN ldap_server_settings as 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 = $res->fetchRow(DB_FETCHMODE_OBJECT))
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
141 
142  public function loadFromPost($a_mappings)
143  {
144  global $rbacreview;
145 
146  $this->mappings = array();
147  foreach($a_mappings as $mapping_id => $data)
148  {
149  if($mapping_id == 0)
150  {
151  if(!$data['dn'] and !$data['member'] and !$data['memberisdn'] and !$data['role'])
152  {
153  continue;
154  }
155  }
156  $this->mappings[$mapping_id]['dn'] = ilUtil::stripSlashes($data['dn']);
157  $this->mappings[$mapping_id]['url'] = ilUtil::stripSlashes($data['url']);
158  $this->mappings[$mapping_id]['member_attribute'] = ilUtil::stripSlashes($data['member']);
159  $this->mappings[$mapping_id]['member_isdn'] = ilUtil::stripSlashes($data['memberisdn']);
160  $this->mappings[$mapping_id]['role_name'] = ilUtil::stripSlashes($data['role']);
161  $this->mappings[$mapping_id]['role'] = $rbacreview->roleExists(ilUtil::stripSlashes($data['role']));
162  $this->mappings[$mapping_id]['info'] = ilUtil::stripSlashes($data['info']);
163  $this->mappings[$mapping_id]['info_type'] = ilUtil::stripSlashes($data['info_type']);
164  }
165  }
166 
173  public function validate()
174  {
175  global $ilErr,$rbacreview;
176 
177  $ilErr->setMessage('');
178  $found_missing = false;
179  foreach($this->mappings as $mapping_id => $data)
180  {
181  // Check if all required fields are available
182  if(!strlen($data['dn']) || !strlen($data['member_attribute']) || !strlen($data['role_name']))
183  {
184  if(!$found_missing)
185  {
186  $found_missing = true;
187  $ilErr->appendMessage($this->lng->txt('fill_out_all_required_fields'));
188  }
189  }
190  // Check role valid
191  if(strlen($data['role_name']) and !$rbacreview->roleExists($data['role_name']))
192  {
193  $ilErr->appendMessage($this->lng->txt('ldap_role_not_exists').' '.$data['role_name']);
194  }
195  }
196  return strlen($ilErr->getMessage()) ? false : true;
197  }
198 
206  public function save()
207  {
208  foreach($this->mappings as $mapping_id => $data)
209  {
210  if(!$mapping_id)
211  {
212  $query = "INSERT INTO ldap_role_group_mapping ".
213  "SET server_id = ".$this->db->quote($this->getServerId()).", ".
214  "url = ".$this->db->quote($data['url']).", ".
215  "dn = ".$this->db->quote($data['dn']).", ".
216  "member_attribute = ".$this->db->quote($data['member_attribute']).", ".
217  "member_isdn = ".$this->db->quote($data['member_isdn']).", ".
218  "role = ".$this->db->quote($data['role']).", ".
219  "mapping_info = ".$this->db->quote($data['info']).", ".
220  "mapping_info_type = ".$this->db->quote($data['info_type']);
221 
222 
223  $this->db->query($query);
224  }
225  else
226  {
227  $query = "UPDATE ldap_role_group_mapping ".
228  "SET server_id = ".$this->db->quote($this->getServerId()).", ".
229  "url = ".$this->db->quote($data['url']).", ".
230  "dn =".$this->db->quote($data['dn']).", ".
231  "member_attribute = ".$this->db->quote($data['member_attribute']).", ".
232  "member_isdn = ".$this->db->quote($data['member_isdn']).", ".
233  "role = ".$this->db->quote($data['role']).", ".
234  "mapping_info = ".$this->db->quote($data['info']).", ".
235  "mapping_info_type = ".$this->db->quote($data['info_type'])." ".
236  "WHERE mapping_id = ".$this->db->quote($mapping_id);
237 
238  $this->db->query($query);
239  }
240  }
241  $this->read();
242  }
243 
244 
252  public function delete($a_mapping_id)
253  {
254  $query = "DELETE FROM ldap_role_group_mapping ".
255  "WHERE server_id = ".$this->db->quote($this->getServerId())." ".
256  "AND mapping_id = ".$this->db->quote($a_mapping_id);
257  $this->db->query($query);
258 
259  $this->read();
260  }
261 
262 
269  public function getMappingInfoString($a_mapping_id)
270  {
271  $role = $this->mappings[$a_mapping_id]['role_name'];
272  $dn_parts = explode(',',$this->mappings[$a_mapping_id]['dn']);
273 
274  return (array_key_exists(0,$dn_parts) ? $dn_parts[0] : "''");
275  }
276 
277 
284  private function read()
285  {
286  global $ilObjDataCache,$rbacreview,$tree;
287 
288  $this->mappings = array();
289  $query = "SELECT * FROM ldap_role_group_mapping LEFT JOIN object_data ".
290  "ON role = obj_id ".
291  "WHERE server_id =".$this->db->quote($this->getServerId()).' '.
292  "ORDER BY title,dn";
293 
294  $res = $this->db->query($query);
295  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
296  {
297  $this->mappings[$row->mapping_id]['dn'] = $row->dn;
298  $this->mappings[$row->mapping_id]['url'] = $row->url;
299  $this->mappings[$row->mapping_id]['member_attribute'] = $row->member_attribute;
300  $this->mappings[$row->mapping_id]['member_isdn'] = $row->member_isdn;
301  $this->mappings[$row->mapping_id]['role'] = $row->role;
302  $this->mappings[$row->mapping_id]['info'] = $row->mapping_info;
303  $this->mappings[$row->mapping_id]['info_type'] = $row->mapping_info_type;
304  if($ilObjDataCache->lookupType($row->role) == 'role')
305  {
306  $this->mappings[$row->mapping_id]['role_name'] = $ilObjDataCache->lookupTitle($row->role);
307  }
308  else
309  {
310  $this->mappings[$row->mapping_id]['role_name'] = $row->role;
311  }
312 
313  }
314  }
315 
316 }
317 
318 ?>