ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilLDAPRoleGroupMappingSettings.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
26  private static array $instances = [];
27 
28  private ilDBInterface $db;
29  private ilLanguage $lng;
33 
34  private int $server_id;
35  private array $mappings = [];
36 
37  public const MAPPING_INFO_ALL = 1;
38  public const MAPPING_INFO_INFO_ONLY = 0;
39 
43  private function __construct($a_server_id)
44  {
45  global $DIC;
46 
47  $this->db = $DIC->database();
48  $this->lng = $DIC->language();
49  $this->rbacreview = $DIC->rbac()->review();
50  $this->ilErr = $DIC['ilErr'];
51  $this->ilObjDataCache = $DIC['ilObjDataCache'];
52  $this->server_id = $a_server_id;
53  $this->read();
54  }
55 
59  public static function _getInstanceByServerId(int $a_server_id): ilLDAPRoleGroupMappingSettings
60  {
61  if (array_key_exists($a_server_id, self::$instances) && is_object(self::$instances[$a_server_id])) {
62  return self::$instances[$a_server_id];
63  }
64  return self::$instances[$a_server_id] = new ilLDAPRoleGroupMappingSettings($a_server_id);
65  }
66 
67  public static function _deleteByRole(int $a_role_id): bool
68  {
69  global $DIC;
70 
71  $ilDB = $DIC['ilDB'];
72 
73  $query = "DELETE FROM ldap_rg_mapping " .
74  "WHERE role = " . $ilDB->quote($a_role_id, 'integer');
75  $ilDB->manipulate($query);
76 
77  return true;
78  }
79 
80  public static function _deleteByServerId(int $a_server_id): bool
81  {
82  global $DIC;
83 
84  $ilDB = $DIC['ilDB'];
85 
86  $query = "DELETE FROM ldap_rg_mapping " .
87  "WHERE server_id = " . $ilDB->quote($a_server_id, 'integer');
88  $ilDB->manipulate($query);
89 
90  return true;
91  }
92 
93  public static function _getAllActiveMappings(): array
94  {
95  global $DIC;
96 
97  $ilDB = $DIC['ilDB'];
99  $rbacreview = $DIC['rbacreview'];
100 
101  $query = "SELECT rgm.* FROM ldap_rg_mapping rgm JOIN ldap_server_settings lss " .
102  "ON rgm.server_id = lss.server_id " .
103  "WHERE lss.active = 1 " .
104  "AND lss.role_sync_active = 1 ";
105  $res = $ilDB->query($query);
106  $active = [];
107  while ($row = $ilDB->fetchObject($res)) {
108  $data['server_id'] = $row->server_id;
109  $data['url'] = $row->url;
110  $data['mapping_id'] = $row->mapping_id;
111  $data['dn'] = $row->dn;
112  $data['member'] = $row->member_attribute;
113  $data['isdn'] = $row->member_isdn;
114  $data['info'] = $row->mapping_info;
115  $data['info_type'] = $row->mapping_info_type;
116  // read assigned object
117  $data['object_id'] = $rbacreview->getObjectOfRole((int) $row->role);
118 
119  $active[$row->role][] = $data;
120  }
121  return $active;
122  }
123 
124  public function getServerId(): int
125  {
126  return $this->server_id;
127  }
128 
132  public function getMappings(): array
133  {
134  return $this->mappings;
135  }
136 
137  public function loadFromPost(array $a_mappings): void
138  {
139  if (!$a_mappings) {
140  return;
141  }
142 
143  $this->mappings = [];
144  foreach ($a_mappings as $mapping_id => $data) {
145  if ($mapping_id === 0 && !$data['dn'] && !$data['member'] && !$data['memberisdn'] && !$data['role']) {
146  continue;
147  }
148  $this->mappings[$mapping_id]['dn'] = ilUtil::stripSlashes($data['dn']);
149  $this->mappings[$mapping_id]['url'] = ilUtil::stripSlashes($data['url']);
150  $this->mappings[$mapping_id]['member_attribute'] = ilUtil::stripSlashes($data['member']);
151  $this->mappings[$mapping_id]['member_isdn'] = ilUtil::stripSlashes($data['memberisdn']);
152  $this->mappings[$mapping_id]['role_name'] = ilUtil::stripSlashes($data['role']);
153  $this->mappings[$mapping_id]['role'] = $this->rbacreview->roleExists(ilUtil::stripSlashes($data['role']));
154  $this->mappings[$mapping_id]['info'] = ilUtil::stripSlashes($data['info']);
155  $this->mappings[$mapping_id]['info_type'] = ilUtil::stripSlashes($data['info_type']);
156  }
157  }
158 
165  public function validate(): bool
166  {
167  $this->ilErr->setMessage('');
168  $found_missing = false;
169  foreach ($this->mappings as $data) {
170  // Check if all required fields are available
171  if ($data['dn'] === '' || $data['member_attribute'] === '' || $data['role_name'] === '') {
172  if (!$found_missing) {
173  $found_missing = true;
174  $this->ilErr->appendMessage($this->lng->txt('fill_out_all_required_fields'));
175  }
176  }
177  // Check role valid
178  if ($data['role_name'] !== '' && !$this->rbacreview->roleExists($data['role_name'])) {
179  $this->ilErr->appendMessage($this->lng->txt('ldap_role_not_exists') . ' ' . $data['role_name']);
180  }
181  }
182 
183  return $this->ilErr->getMessage() === '';
184  }
185 
193  public function save(): void
194  {
195  foreach ($this->mappings as $mapping_id => $data) {
196  if (!$mapping_id) {
197  $next_id = $this->db->nextId('ldap_rg_mapping');
198  $query = "INSERT INTO ldap_rg_mapping (mapping_id,server_id,url,dn,member_attribute,member_isdn,role,mapping_info,mapping_info_type) " .
199  "VALUES ( " .
200  $this->db->quote($next_id, 'integer') . ", " .
201  $this->db->quote($this->getServerId(), 'integer') . ", " .
202  $this->db->quote($data['url'], 'text') . ", " .
203  $this->db->quote($data['dn'], 'text') . ", " .
204  $this->db->quote($data['member_attribute'], 'text') . ", " .
205  $this->db->quote($data['member_isdn'], 'integer') . ", " .
206  $this->db->quote($data['role'], 'integer') . ", " .
207  $this->db->quote($data['info'], 'text') . ", " .
208  $this->db->quote($data['info_type'], 'integer') .
209  ")";
210  } else {
211  $query = "UPDATE ldap_rg_mapping " .
212  "SET server_id = " . $this->db->quote($this->getServerId(), 'integer') . ", " .
213  "url = " . $this->db->quote($data['url'], 'text') . ", " .
214  "dn =" . $this->db->quote($data['dn'], 'text') . ", " .
215  "member_attribute = " . $this->db->quote($data['member_attribute'], 'text') . ", " .
216  "member_isdn = " . $this->db->quote($data['member_isdn'], 'integer') . ", " .
217  "role = " . $this->db->quote($data['role'], 'integer') . ", " .
218  "mapping_info = " . $this->db->quote($data['info'], 'text') . ", " .
219  "mapping_info_type = " . $this->db->quote($data['info_type'], 'integer') . " " .
220  "WHERE mapping_id = " . $this->db->quote($mapping_id, 'integer');
221  }
222  $this->db->manipulate($query);
223  }
224  $this->read();
225  }
226 
227 
234  public function delete($a_mapping_id): void
235  {
236  $query = "DELETE FROM ldap_rg_mapping " .
237  "WHERE server_id = " . $this->db->quote($this->getServerId(), 'integer') . " " .
238  "AND mapping_id = " . $this->db->quote($a_mapping_id, 'integer');
239  $this->db->manipulate($query);
240  $this->read();
241  }
242 
243 
249  //TODO check if method gets called somewhere
250  public function getMappingInfoString(int $a_mapping_id): string
251  {
252  $dn_parts = explode(',', $this->mappings[$a_mapping_id]['dn']);
253 
254  return $dn_parts ? $dn_parts[0] : "''";
255  }
256 
257 
261  private function read(): void
262  {
263  $this->mappings = array();
264  $query = "SELECT * FROM ldap_rg_mapping LEFT JOIN object_data " .
265  "ON role = obj_id " .
266  "WHERE server_id =" . $this->db->quote($this->getServerId(), 'integer') . ' ' .
267  "ORDER BY title,dn";
268 
269  $res = $this->db->query($query);
270  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
271  $this->mappings[$row->mapping_id]['mapping_id'] = $row->mapping_id;
272  $this->mappings[$row->mapping_id]['dn'] = $row->dn;
273  $this->mappings[$row->mapping_id]['url'] = $row->url;
274  $this->mappings[$row->mapping_id]['member_attribute'] = $row->member_attribute;
275  $this->mappings[$row->mapping_id]['member_isdn'] = $row->member_isdn;
276  $this->mappings[$row->mapping_id]['role'] = $row->role;
277  $this->mappings[$row->mapping_id]['info'] = $row->mapping_info;
278  $this->mappings[$row->mapping_id]['info_type'] = $row->mapping_info_type;
279  if ($this->ilObjDataCache->lookupType((int) $row->role) === 'role') {
280  $this->mappings[$row->mapping_id]['role_name'] = $this->ilObjDataCache->lookupTitle((int) $row->role);
281  } else {
282  $this->mappings[$row->mapping_id]['role_name'] = $row->role;
283  }
284  }
285  }
286 }
$res
Definition: ltiservices.php:66
getMappings()
Get already configured mappings.
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
getMappingInfoString(int $a_mapping_id)
Create an info string for a role group mapping.
global $DIC
Definition: shib_login.php:22
static _getInstanceByServerId(int $a_server_id)
Get instance of class.
__construct($a_server_id)
Private constructor (Singleton for each server_id)
getObjectOfRole(int $a_role_id)
Get object id of objects a role is assigned to.