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 24/** 25* @author Stefan Meyer <meyer@leifos.com> 26* @version $Id$ 27* 28* 29* @ingroup ServicesLDAP 30*/ 31 32class ilLDAPRoleGroupMappingSettings 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; 40 const MAPPING_INFO_INFO_ONLY = 0; 41 42 /** 43 * Private constructor (Singleton for each server_id) 44 * 45 * @access private 46 * 47 */ 48 private function __construct($a_server_id) 49 { 50 global $DIC; 51 52 $ilDB = $DIC['ilDB']; 53 $lng = $DIC['lng']; 54 55 $this->db = $ilDB; 56 $this->lng = $lng; 57 $this->server_id = $a_server_id; 58 $this->read(); 59 } 60 61 /** 62 * Get instance of class 63 * 64 * @access public 65 * @param int server_id 66 * @return ilLDAPRoleGroupMappingSettings 67 */ 68 public static function _getInstanceByServerId($a_server_id) 69 { 70 if (array_key_exists($a_server_id, self::$instances) and is_object(self::$instances[$a_server_id])) { 71 return self::$instances[$a_server_id]; 72 } 73 return self::$instances[$a_server_id] = new ilLDAPRoleGroupMappingSettings($a_server_id); 74 } 75 76 public static function _deleteByRole($a_role_id) 77 { 78 global $DIC; 79 80 $ilDB = $DIC['ilDB']; 81 82 $query = "DELETE FROM ldap_rg_mapping " . 83 "WHERE role = " . $ilDB->quote($a_role_id, 'integer'); 84 $res = $ilDB->manipulate($query); 85 86 return true; 87 } 88 89 public static function _deleteByServerId($a_server_id) 90 { 91 global $DIC; 92 93 $ilDB = $DIC['ilDB']; 94 95 $query = "DELETE FROM ldap_rg_mapping " . 96 "WHERE server_id = " . $ilDB->quote($a_server_id, 'integer'); 97 $res = $ilDB->manipulate($query); 98 99 return true; 100 } 101 102 public static function _getAllActiveMappings() 103 { 104 global $DIC; 105 106 $ilDB = $DIC['ilDB']; 107 $rbacreview = $DIC['rbacreview']; 108 109 $query = "SELECT rgm.* FROM ldap_rg_mapping rgm JOIN ldap_server_settings lss " . 110 "ON rgm.server_id = lss.server_id " . 111 "WHERE lss.active = 1 " . 112 "AND lss.role_sync_active = 1 "; 113 $res = $ilDB->query($query); 114 while ($row = $ilDB->fetchObject($res)) { 115 $data['server_id'] = $row->server_id; 116 $data['url'] = $row->url; 117 $data['mapping_id'] = $row->mapping_id; 118 $data['dn'] = $row->dn; 119 $data['member'] = $row->member_attribute; 120 $data['isdn'] = $row->member_isdn; 121 $data['info'] = $row->mapping_info; 122 $data['info_type'] = $row->mapping_info_type; 123 // read assigned object 124 $data['object_id'] = $rbacreview->getObjectOfRole($row->role); 125 126 127 $active[$row->role][] = $data; 128 } 129 return $active ? $active : array(); 130 } 131 132 public function getServerId() 133 { 134 return $this->server_id; 135 } 136 137 /** 138 * Get already configured mappings 139 * 140 * @access public 141 * 142 */ 143 public function getMappings() 144 { 145 return $this->mappings ? $this->mappings : array(); 146 } 147 148 public function loadFromPost($a_mappings) 149 { 150 global $DIC; 151 152 $rbacreview = $DIC['rbacreview']; 153 154 if (!$a_mappings) { 155 return; 156 } 157 158 $this->mappings = array(); 159 foreach ($a_mappings as $mapping_id => $data) { 160 if ($mapping_id == 0) { 161 if (!$data['dn'] and !$data['member'] and !$data['memberisdn'] and !$data['role']) { 162 continue; 163 } 164 } 165 $this->mappings[$mapping_id]['dn'] = ilUtil::stripSlashes($data['dn']); 166 $this->mappings[$mapping_id]['url'] = ilUtil::stripSlashes($data['url']); 167 $this->mappings[$mapping_id]['member_attribute'] = ilUtil::stripSlashes($data['member']); 168 $this->mappings[$mapping_id]['member_isdn'] = ilUtil::stripSlashes($data['memberisdn']); 169 $this->mappings[$mapping_id]['role_name'] = ilUtil::stripSlashes($data['role']); 170 $this->mappings[$mapping_id]['role'] = $rbacreview->roleExists(ilUtil::stripSlashes($data['role'])); 171 $this->mappings[$mapping_id]['info'] = ilUtil::stripSlashes($data['info']); 172 $this->mappings[$mapping_id]['info_type'] = ilUtil::stripSlashes($data['info_type']); 173 } 174 } 175 176 /** 177 * Validate mappings 178 * 179 * @access public 180 * 181 */ 182 public function validate() 183 { 184 global $DIC; 185 186 $ilErr = $DIC['ilErr']; 187 $rbacreview = $DIC['rbacreview']; 188 189 $ilErr->setMessage(''); 190 $found_missing = false; 191 foreach ($this->mappings as $mapping_id => $data) { 192 // Check if all required fields are available 193 if (!strlen($data['dn']) || !strlen($data['member_attribute']) || !strlen($data['role_name'])) { 194 if (!$found_missing) { 195 $found_missing = true; 196 $ilErr->appendMessage($this->lng->txt('fill_out_all_required_fields')); 197 } 198 } 199 // Check role valid 200 if (strlen($data['role_name']) and !$rbacreview->roleExists($data['role_name'])) { 201 $ilErr->appendMessage($this->lng->txt('ldap_role_not_exists') . ' ' . $data['role_name']); 202 } 203 } 204 return strlen($ilErr->getMessage()) ? false : true; 205 } 206 207 /** 208 * Save mappings 209 * 210 * @access public 211 * @param 212 * 213 */ 214 public function save() 215 { 216 global $DIC; 217 218 $ilDB = $DIC['ilDB']; 219 220 foreach ($this->mappings as $mapping_id => $data) { 221 if (!$mapping_id) { 222 $next_id = $ilDB->nextId('ldap_rg_mapping'); 223 $query = "INSERT INTO ldap_rg_mapping (mapping_id,server_id,url,dn,member_attribute,member_isdn,role,mapping_info,mapping_info_type) " . 224 "VALUES ( " . 225 $ilDB->quote($next_id, 'integer') . ", " . 226 $this->db->quote($this->getServerId(), 'integer') . ", " . 227 $this->db->quote($data['url'], 'text') . ", " . 228 $this->db->quote($data['dn'], 'text') . ", " . 229 $this->db->quote($data['member_attribute'], 'text') . ", " . 230 $this->db->quote($data['member_isdn'], 'integer') . ", " . 231 $this->db->quote($data['role'], 'integer') . ", " . 232 $this->db->quote($data['info'], 'text') . ", " . 233 $this->db->quote($data['info_type'], 'integer') . 234 ")"; 235 $res = $ilDB->manipulate($query); 236 } else { 237 $query = "UPDATE ldap_rg_mapping " . 238 "SET server_id = " . $this->db->quote($this->getServerId(), 'integer') . ", " . 239 "url = " . $this->db->quote($data['url'], 'text') . ", " . 240 "dn =" . $this->db->quote($data['dn'], 'text') . ", " . 241 "member_attribute = " . $this->db->quote($data['member_attribute'], 'text') . ", " . 242 "member_isdn = " . $this->db->quote($data['member_isdn'], 'integer') . ", " . 243 "role = " . $this->db->quote($data['role'], 'integer') . ", " . 244 "mapping_info = " . $this->db->quote($data['info'], 'text') . ", " . 245 "mapping_info_type = " . $this->db->quote($data['info_type'], 'integer') . " " . 246 "WHERE mapping_id = " . $this->db->quote($mapping_id, 'integer'); 247 $res = $ilDB->manipulate($query); 248 } 249 } 250 $this->read(); 251 } 252 253 254 /** 255 * Delete a mapping 256 * 257 * @access public 258 * @param int mapping_id 259 * 260 */ 261 public function delete($a_mapping_id) 262 { 263 global $DIC; 264 265 $ilDB = $DIC['ilDB']; 266 267 $query = "DELETE FROM ldap_rg_mapping " . 268 "WHERE server_id = " . $this->db->quote($this->getServerId(), 'integer') . " " . 269 "AND mapping_id = " . $this->db->quote($a_mapping_id, 'integer'); 270 $res = $ilDB->manipulate($query); 271 $this->read(); 272 } 273 274 275 /** 276 * Create an info string for a role group mapping 277 * 278 * @access public 279 * @param int mapping_id 280 */ 281 public function getMappingInfoString($a_mapping_id) 282 { 283 $role = $this->mappings[$a_mapping_id]['role_name']; 284 $dn_parts = explode(',', $this->mappings[$a_mapping_id]['dn']); 285 286 return (array_key_exists(0, $dn_parts) ? $dn_parts[0] : "''"); 287 } 288 289 290 /** 291 * Read mappings 292 * 293 * @access private 294 * 295 */ 296 private function read() 297 { 298 global $DIC; 299 300 $ilObjDataCache = $DIC['ilObjDataCache']; 301 $rbacreview = $DIC['rbacreview']; 302 $tree = $DIC['tree']; 303 304 $this->mappings = array(); 305 $query = "SELECT * FROM ldap_rg_mapping LEFT JOIN object_data " . 306 "ON role = obj_id " . 307 "WHERE server_id =" . $this->db->quote($this->getServerId(), 'integer') . ' ' . 308 "ORDER BY title,dn"; 309 310 $res = $this->db->query($query); 311 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) { 312 $this->mappings[$row->mapping_id]['mapping_id'] = $row->mapping_id; 313 $this->mappings[$row->mapping_id]['dn'] = $row->dn; 314 $this->mappings[$row->mapping_id]['url'] = $row->url; 315 $this->mappings[$row->mapping_id]['member_attribute'] = $row->member_attribute; 316 $this->mappings[$row->mapping_id]['member_isdn'] = $row->member_isdn; 317 $this->mappings[$row->mapping_id]['role'] = $row->role; 318 $this->mappings[$row->mapping_id]['info'] = $row->mapping_info; 319 $this->mappings[$row->mapping_id]['info_type'] = $row->mapping_info_type; 320 if ($ilObjDataCache->lookupType($row->role) == 'role') { 321 $this->mappings[$row->mapping_id]['role_name'] = $ilObjDataCache->lookupTitle($row->role); 322 } else { 323 $this->mappings[$row->mapping_id]['role_name'] = $row->role; 324 } 325 } 326 } 327} 328