ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  return self::$instances[$a_server_id];
69  }
70  return self::$instances[$a_server_id] = new ilLDAPRoleGroupMappingSettings($a_server_id);
71  }
72 
73  public static function _deleteByRole($a_role_id)
74  {
75  global $ilDB;
76 
77  $query = "DELETE FROM ldap_rg_mapping " .
78  "WHERE role = " . $ilDB->quote($a_role_id, 'integer');
79  $res = $ilDB->manipulate($query);
80 
81  return true;
82  }
83 
84  public static function _deleteByServerId($a_server_id)
85  {
86  global $ilDB;
87 
88  $query = "DELETE FROM ldap_rg_mapping " .
89  "WHERE server_id = " . $ilDB->quote($a_server_id, 'integer');
90  $res = $ilDB->manipulate($query);
91 
92  return true;
93  }
94 
95  public static function _getAllActiveMappings()
96  {
97  global $ilDB,$rbacreview;
98 
99  $query = "SELECT rgm.* FROM ldap_rg_mapping rgm JOIN ldap_server_settings lss " .
100  "ON rgm.server_id = lss.server_id " .
101  "WHERE lss.active = 1 " .
102  "AND lss.role_sync_active = 1 ";
103  $res = $ilDB->query($query);
104  while ($row = $ilDB->fetchObject($res)) {
105  $data['server_id'] = $row->server_id;
106  $data['url'] = $row->url;
107  $data['mapping_id'] = $row->mapping_id;
108  $data['dn'] = $row->dn;
109  $data['member'] = $row->member_attribute;
110  $data['isdn'] = $row->member_isdn;
111  $data['info'] = $row->mapping_info;
112  $data['info_type'] = $row->mapping_info_type;
113  // read assigned object
114  $data['object_id'] = $rbacreview->getObjectOfRole($row->role);
115 
116 
117  $active[$row->role][] = $data;
118  }
119  return $active ? $active : array();
120  }
121 
122  public function getServerId()
123  {
124  return $this->server_id;
125  }
126 
133  public function getMappings()
134  {
135  return $this->mappings ? $this->mappings : array();
136  }
137 
138  public function loadFromPost($a_mappings)
139  {
140  global $rbacreview;
141 
142  if (!$a_mappings) {
143  return;
144  }
145 
146  $this->mappings = array();
147  foreach ($a_mappings as $mapping_id => $data) {
148  if ($mapping_id == 0) {
149  if (!$data['dn'] and !$data['member'] and !$data['memberisdn'] and !$data['role']) {
150  continue;
151  }
152  }
153  $this->mappings[$mapping_id]['dn'] = ilUtil::stripSlashes($data['dn']);
154  $this->mappings[$mapping_id]['url'] = ilUtil::stripSlashes($data['url']);
155  $this->mappings[$mapping_id]['member_attribute'] = ilUtil::stripSlashes($data['member']);
156  $this->mappings[$mapping_id]['member_isdn'] = ilUtil::stripSlashes($data['memberisdn']);
157  $this->mappings[$mapping_id]['role_name'] = ilUtil::stripSlashes($data['role']);
158  $this->mappings[$mapping_id]['role'] = $rbacreview->roleExists(ilUtil::stripSlashes($data['role']));
159  $this->mappings[$mapping_id]['info'] = ilUtil::stripSlashes($data['info']);
160  $this->mappings[$mapping_id]['info_type'] = ilUtil::stripSlashes($data['info_type']);
161  }
162  }
163 
170  public function validate()
171  {
172  global $ilErr,$rbacreview;
173 
174  $ilErr->setMessage('');
175  $found_missing = false;
176  foreach ($this->mappings as $mapping_id => $data) {
177  // Check if all required fields are available
178  if (!strlen($data['dn']) || !strlen($data['member_attribute']) || !strlen($data['role_name'])) {
179  if (!$found_missing) {
180  $found_missing = true;
181  $ilErr->appendMessage($this->lng->txt('fill_out_all_required_fields'));
182  }
183  }
184  // Check role valid
185  if (strlen($data['role_name']) and !$rbacreview->roleExists($data['role_name'])) {
186  $ilErr->appendMessage($this->lng->txt('ldap_role_not_exists') . ' ' . $data['role_name']);
187  }
188  }
189  return strlen($ilErr->getMessage()) ? false : true;
190  }
191 
199  public function save()
200  {
201  global $ilDB;
202 
203  foreach ($this->mappings as $mapping_id => $data) {
204  if (!$mapping_id) {
205  $next_id = $ilDB->nextId('ldap_rg_mapping');
206  $query = "INSERT INTO ldap_rg_mapping (mapping_id,server_id,url,dn,member_attribute,member_isdn,role,mapping_info,mapping_info_type) " .
207  "VALUES ( " .
208  $ilDB->quote($next_id, 'integer') . ", " .
209  $this->db->quote($this->getServerId(), 'integer') . ", " .
210  $this->db->quote($data['url'], 'text') . ", " .
211  $this->db->quote($data['dn'], 'text') . ", " .
212  $this->db->quote($data['member_attribute'], 'text') . ", " .
213  $this->db->quote($data['member_isdn'], 'integer') . ", " .
214  $this->db->quote($data['role'], 'integer') . ", " .
215  $this->db->quote($data['info'], 'text') . ", " .
216  $this->db->quote($data['info_type'], 'integer') .
217  ")";
218  $res = $ilDB->manipulate($query);
219  } else {
220  $query = "UPDATE ldap_rg_mapping " .
221  "SET server_id = " . $this->db->quote($this->getServerId(), 'integer') . ", " .
222  "url = " . $this->db->quote($data['url'], 'text') . ", " .
223  "dn =" . $this->db->quote($data['dn'], 'text') . ", " .
224  "member_attribute = " . $this->db->quote($data['member_attribute'], 'text') . ", " .
225  "member_isdn = " . $this->db->quote($data['member_isdn'], 'integer') . ", " .
226  "role = " . $this->db->quote($data['role'], 'integer') . ", " .
227  "mapping_info = " . $this->db->quote($data['info'], 'text') . ", " .
228  "mapping_info_type = " . $this->db->quote($data['info_type'], 'integer') . " " .
229  "WHERE mapping_id = " . $this->db->quote($mapping_id, 'integer');
230  $res = $ilDB->manipulate($query);
231  }
232  }
233  $this->read();
234  }
235 
236 
244  public function delete($a_mapping_id)
245  {
246  global $ilDB;
247 
248  $query = "DELETE FROM ldap_rg_mapping " .
249  "WHERE server_id = " . $this->db->quote($this->getServerId(), 'integer') . " " .
250  "AND mapping_id = " . $this->db->quote($a_mapping_id, 'integer');
251  $res = $ilDB->manipulate($query);
252  $this->read();
253  }
254 
255 
262  public function getMappingInfoString($a_mapping_id)
263  {
264  $role = $this->mappings[$a_mapping_id]['role_name'];
265  $dn_parts = explode(',', $this->mappings[$a_mapping_id]['dn']);
266 
267  return (array_key_exists(0, $dn_parts) ? $dn_parts[0] : "''");
268  }
269 
270 
277  private function read()
278  {
279  global $ilObjDataCache,$rbacreview,$tree;
280 
281  $this->mappings = array();
282  $query = "SELECT * FROM ldap_rg_mapping LEFT JOIN object_data " .
283  "ON role = obj_id " .
284  "WHERE server_id =" . $this->db->quote($this->getServerId(), 'integer') . ' ' .
285  "ORDER BY title,dn";
286 
287  $res = $this->db->query($query);
288  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
289  $this->mappings[$row->mapping_id]['mapping_id'] = $row->mapping_id;
290  $this->mappings[$row->mapping_id]['dn'] = $row->dn;
291  $this->mappings[$row->mapping_id]['url'] = $row->url;
292  $this->mappings[$row->mapping_id]['member_attribute'] = $row->member_attribute;
293  $this->mappings[$row->mapping_id]['member_isdn'] = $row->member_isdn;
294  $this->mappings[$row->mapping_id]['role'] = $row->role;
295  $this->mappings[$row->mapping_id]['info'] = $row->mapping_info;
296  $this->mappings[$row->mapping_id]['info_type'] = $row->mapping_info_type;
297  if ($ilObjDataCache->lookupType($row->role) == 'role') {
298  $this->mappings[$row->mapping_id]['role_name'] = $ilObjDataCache->lookupTitle($row->role);
299  } else {
300  $this->mappings[$row->mapping_id]['role_name'] = $row->role;
301  }
302  }
303  }
304 }
global $ilErr
Definition: raiseError.php:16
getMappings()
Get already configured mappings.
static _getInstanceByServerId($a_server_id)
Get instance of class.
foreach($_POST as $key=> $value) $res
$query
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
Create styles array
The data for the language used.
global $lng
Definition: privfeed.php:17
getMappingInfoString($a_mapping_id)
Create an info string for a role group mapping.
global $ilDB
__construct($a_server_id)
Private constructor (Singleton for each server_id)