ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilLDAPAttributeMapping.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
28  private static array $instances = [];
29  private int $server_id;
30  private ilDBInterface $db;
31 
32  private array $mapping_rules = [];
33  private array $rules_for_update = [];
34 
35  private function __construct(int $a_server_id)
36  {
37  global $DIC;
38 
39  $this->db = $DIC->database();
40 
41  $this->server_id = $a_server_id;
42  $this->read();
43  }
44 
45  public static function _getInstanceByServerId(int $a_server_id): ilLDAPAttributeMapping
46  {
47  if (array_key_exists($a_server_id, self::$instances) && is_object(self::$instances[$a_server_id])) {
48  return self::$instances[$a_server_id];
49  }
50 
51  return self::$instances[$a_server_id] = new ilLDAPAttributeMapping($a_server_id);
52  }
53 
54  public static function _delete(int $a_server_id): void
55  {
56  global $DIC;
57 
58  $ilDB = $DIC['ilDB'];
59 
60  $query = "DELETE FROM ldap_attribute_mapping " .
61  "WHERE server_id =" . $ilDB->quote($a_server_id, 'integer');
62  $ilDB->manipulate($query);
63  }
64 
65  public static function _lookupGlobalRole(int $a_server_id): int
66  {
67  global $DIC;
68 
69  $ilDB = $DIC['ilDB'];
70 
71  $query = "SELECT value FROM ldap_attribute_mapping " .
72  "WHERE server_id =" . $ilDB->quote($a_server_id, 'integer') . " " .
73  "AND keyword = " . $ilDB->quote('global_role', 'text');
74 
75  $res = $ilDB->query($query);
76  if ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
77  return (int) $row->value;
78  }
79 
80  return 0;
81  }
82 
87  public static function hasRulesForUpdate(int $a_server_id): bool
88  {
89  global $DIC;
90 
91  $ilDB = $DIC['ilDB'];
92 
93  $query = 'SELECT perform_update FROM ldap_attribute_mapping ' .
94  'WHERE server_id = ' . $ilDB->quote($a_server_id, 'integer') . ' ' .
95  'AND perform_update = 1';
96  $res = $ilDB->query($query);
97  return $res->numRows() > 0;
98  }
99 
100  public function setRule(string $a_field_name, string $a_ldap_attribute, bool $a_perform_update): void
101  {
102  $this->mapping_rules[$a_field_name]['value'] = $a_ldap_attribute;
103  $this->mapping_rules[$a_field_name]['performUpdate'] = $a_perform_update;
104  }
105 
110  public function getRulesForUpdate(): array
111  {
113  }
114 
119  public function getFieldsForUpdate(): array
120  {
121  return self::getMappedFields($this->rules_for_update);
122  }
123 
127  public function getFields(): array
128  {
129  return self::getMappedFields($this->mapping_rules);
130  }
131 
132  private static function getMappedFields(array $rules): array
133  {
134  $fields = [];
135  foreach ($rules as $rule) {
136  if (!$rule['value']) {
137  continue;
138  }
139  if (strpos($rule['value'], ',') === false) {
140  $fields[] = strtolower($rule['value']);
141  continue;
142  }
143  $tmp_fields = explode(',', $rule['value']);
144  foreach ($tmp_fields as $tmp_field) {
145  $fields[] = strtolower(trim($tmp_field));
146  }
147  }
148  return $fields;
149  }
150 
156  public function getRules(bool $onlyApplicable = false): array
157  {
158  if (!$onlyApplicable) {
159  return $this->mapping_rules;
160  }
161  return array_filter($this->mapping_rules, static function (array $rule): bool {
162  return $rule['value'] !== '';
163  });
164  }
165 
169  public function clearRules(): void
170  {
171  $this->mapping_rules = array();
172  }
173 
177  public function save(): void
178  {
179  $this->delete();
180 
181  foreach ($this->mapping_rules as $keyword => $options) {
182  $query = "INSERT INTO ldap_attribute_mapping (server_id,keyword,value,perform_update) " .
183  "VALUES( " .
184  $this->db->quote($this->server_id, 'integer') . ", " .
185  $this->db->quote($keyword, 'text') . ", " .
186  $this->db->quote($options['value'], 'text') . ", " .
187  $this->db->quote($options['performUpdate'], 'integer') .
188  ')';
189  $this->db->manipulate($query);
190  }
191  }
192 
196  public function delete(): void
197  {
198  self::_delete($this->server_id);
199  }
200 
206  public function enabledUpdate(string $a_field_name): bool
207  {
208  if (array_key_exists($a_field_name, $this->mapping_rules)) {
209  return (bool) $this->mapping_rules[$a_field_name]['performUpdate'];
210  }
211 
212  return false;
213  }
214 
221  public function getValue(string $a_field_name): string
222  {
223  if (array_key_exists($a_field_name, $this->mapping_rules)) {
224  return $this->mapping_rules[$a_field_name]['value'];
225  }
226 
227  return '';
228  }
229 
233  private function read(): void
234  {
235  $query = "SELECT * FROM ldap_attribute_mapping " .
236  "WHERE server_id =" . $this->db->quote($this->server_id, 'integer');
237 
238  $res = $this->db->query($query);
239  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
240  if (!is_null($row->value)) {
241  $this->mapping_rules[$row->keyword]['value'] = $row->value;
242  $this->mapping_rules[$row->keyword]['performUpdate'] = (bool) $row->perform_update;
243 
244  if ($row->perform_update) {
245  $this->rules_for_update[$row->keyword]['value'] = $row->value;
246  }
247  }
248  }
249  }
250 }
$res
Definition: ltiservices.php:69
clearRules()
Clear rules => Does not perform an update.
getFieldsForUpdate()
Get field names of all mapping rules with option &#39;update&#39;.
getFields()
Get all mapping fields.
global $DIC
Definition: feed.php:28
getRulesForUpdate()
Get all mapping rules with option &#39;update&#39;.
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
getValue(string $a_field_name)
Get LDAP attribute name by given ILIAS profile field.
getRules(bool $onlyApplicable=false)
Get all rules.
static _lookupGlobalRole(int $a_server_id)
$query
This class stores the settings that define the mapping between LDAP attribute and user profile fields...
read()
Read mapping settings from db.
enabledUpdate(string $a_field_name)
Check whether an update should be performed on a specific user attribute or not.
setRule(string $a_field_name, string $a_ldap_attribute, bool $a_perform_update)
static _delete(int $a_server_id)
save()
Save mapping rules to db.
static _getInstanceByServerId(int $a_server_id)
static hasRulesForUpdate(int $a_server_id)
Check if there is ldap attribute -> user data mapping which which is updated on login.