ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilLDAPAttributeToUser.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 
35 {
37 
38  private $modes = [];
39 
40 
41  private $server_settings = null;
42  private $role_assignment = null;
43  private $db = null;
44 
45  private $user_data = array();
46  private $setting = null;
47  private $mapping = null;
48 
49  private $new_user_auth_mode = 'ldap';
50 
57  public function __construct(ilLDAPServer $a_server)
58  {
59  global $DIC;
60 
61  $ilDB = $DIC['ilDB'];
62  $ilSetting = $DIC['ilSetting'];
63  $lng = $DIC['lng'];
64  $ilLog = $DIC['ilLog'];
65 
66  // Initialise language object
67  if (!is_object($lng)) {
68  include_once './Services/Language/classes/class.ilLanguage.php';
69  $lng = new ilLanguage('en');
70  }
71 
72  $this->log = $ilLog;
73 
74  $this->server_settings = $a_server;
75  $this->setting = $ilSetting;
76 
77  $this->initLDAPAttributeMapping();
78  }
79 
80  // begin-patch ldap_multiple
85  public function getServer()
86  {
88  }
89  // end-patch ldap_multiple
90 
98  public function setUserData($a_data)
99  {
100  $this->user_data = $a_data;
101  }
102 
108  public function setNewUserAuthMode($a_authmode)
109  {
110  $this->new_user_auth_mode = $a_authmode;
111  }
112 
116  public function getNewUserAuthMode()
117  {
119  }
120 
125  public function addMode($a_mode)
126  {
127  if (is_array($this->modes) && !in_array($a_mode, $this->modes)) {
128  $this->modes[] = $a_mode;
129  }
130  }
131 
137  public function isModeActive($a_mode)
138  {
139  return is_array($this->modes) && in_array($a_mode, $this->modes);
140  }
141 
142 
149  public function refresh()
150  {
151  global $DIC;
152 
153  $rbacadmin = $DIC['rbacadmin'];
154 
155  $this->usersToXML();
156 
157  include_once './Services/User/classes/class.ilUserImportParser.php';
158  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
159 
160  $importParser = new ilUserImportParser();
161  $importParser->setXMLContent($this->writer->xmlDumpMem(false));
162  $importParser->setRoleAssignment(ilLDAPRoleAssignmentRules::getAllPossibleRoles($this->getServer()->getServerId()));
163  $importParser->setFolderId(7);
164  $importParser->startParsing();
165  $debug = $importParser->getProtocol();
166  #var_dump("<pre>",$this->writer->xmlDumpMem(),"</pre>");
167  #print_r($this->writer->xmlDumpMem($format));
168 
169  return true;
170  }
171 
178  protected function parseRoleAssignmentsForUpdate($a_usr_id, $a_external_account, $user)
179  {
180  $rules = $this->mapping->getRulesForUpdate();
181 
182  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
184  $this->getServer()->getServerId(),
185  $a_usr_id,
186  $a_external_account,
187  $user
188  ) as $role_data) {
189  $this->writer->xmlElement(
190  'Role',
191  array('Id' => $role_data['id'],
192  'Type' => $role_data['type'],
193  'Action' => $role_data['action']),
194  ''
195  );
196  }
197  }
198 
204  protected function parseRoleAssignmentsForCreation($a_external_account, $a_user)
205  {
206  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
208  $this->getServer()->getServerId(),
209  $a_external_account,
210  $a_user
211  ) as $role_data) {
212  $this->writer->xmlElement(
213  'Role',
214  array('Id' => $role_data['id'],
215  'Type' => $role_data['type'],
216  'Action' => $role_data['action']),
217  ''
218  );
219  }
220  }
221 
228  private function usersToXML()
229  {
230  include_once('./Services/Xml/classes/class.ilXmlWriter.php');
231  $this->writer = new ilXmlWriter();
232  $this->writer->xmlStartTag('Users');
233 
234  $cnt_update = 0;
235  $cnt_create = 0;
236 
237  // Single users
238  foreach ($this->user_data as $external_account => $user) {
239  $user['ilExternalAccount'] = $external_account;
240 
241  // Required fields
242  if ($user['ilInternalAccount']) {
243  $usr_id = ilObjUser::_lookupId($user['ilInternalAccount']);
244 
245  ++$cnt_update;
246  // User exists
247  $this->writer->xmlStartTag('User', array('Id' => $usr_id,'Action' => 'Update'));
248  $this->writer->xmlElement('Login', array(), $user['ilInternalAccount']);
249  $this->writer->xmlElement('ExternalAccount', array(), $external_account);
250  $this->writer->xmlElement('AuthMode', array('type' => $this->getNewUserAuthMode()), null);
251 
252  if ($this->isModeActive(self::MODE_INITIALIZE_ROLES)) {
253  $this->parseRoleAssignmentsForCreation($external_account, $user);
254  } else {
255  $this->parseRoleAssignmentsForUpdate($usr_id, $external_account, $user);
256  }
257  $rules = $this->mapping->getRulesForUpdate();
258  } else {
259  ++$cnt_create;
260  // Create user
261  $this->writer->xmlStartTag('User', array('Action' => 'Insert'));
262  $this->writer->xmlElement('Login', array(), ilAuthUtils::_generateLogin($external_account));
263 
264  $this->parseRoleAssignmentsForCreation($external_account, $user);
265  $rules = $this->mapping->getRules();
266  }
267 
268  $this->writer->xmlElement('Active', array(), "true");
269  $this->writer->xmlElement('TimeLimitOwner', array(), 7);
270  $this->writer->xmlElement('TimeLimitUnlimited', array(), 1);
271  $this->writer->xmlElement('TimeLimitFrom', array(), time());
272  $this->writer->xmlElement('TimeLimitUntil', array(), time());
273 
274  // only for new users.
275  // If auth_mode is 'default' (ldap) this status should remain.
276  if (!$user['ilInternalAccount']) {
277  $this->writer->xmlElement(
278  'AuthMode',
279  array('type' => $this->getNewUserAuthMode()),
280  $this->getNewUserAuthMode()
281  );
282  $this->writer->xmlElement('ExternalAccount', array(), $external_account);
283  }
284  foreach ($rules as $field => $data) {
285  // Do Mapping: it is possible to assign multiple ldap attribute to one user data field
286  if (!($value = $this->doMapping($user, $data))) {
287  continue;
288  }
289 
290  switch ($field) {
291  case 'gender':
292  switch (strtolower($value)) {
293  case 'n':
294  case 'neutral':
295  $this->writer->xmlElement('Gender', array(), 'n');
296  break;
297 
298  case 'm':
299  case 'male':
300  $this->writer->xmlElement('Gender', array(), 'm');
301  break;
302 
303  case 'f':
304  case 'female':
305  default:
306  $this->writer->xmlElement('Gender', array(), 'f');
307  break;
308 
309  }
310  break;
311 
312  case 'firstname':
313  $this->writer->xmlElement('Firstname', array(), $value);
314  break;
315 
316  case 'lastname':
317  $this->writer->xmlElement('Lastname', array(), $value);
318  break;
319 
320  case 'hobby':
321  $this->writer->xmlElement('Hobby', array(), $value);
322  break;
323 
324  case 'title':
325  $this->writer->xmlElement('Title', array(), $value);
326  break;
327 
328  case 'institution':
329  $this->writer->xmlElement('Institution', array(), $value);
330  break;
331 
332  case 'department':
333  $this->writer->xmlElement('Department', array(), $value);
334  break;
335 
336  case 'street':
337  $this->writer->xmlElement('Street', array(), $value);
338  break;
339 
340  case 'city':
341  $this->writer->xmlElement('City', array(), $value);
342  break;
343 
344  case 'zipcode':
345  $this->writer->xmlElement('PostalCode', array(), $value);
346  break;
347 
348  case 'country':
349  $this->writer->xmlElement('Country', array(), $value);
350  break;
351 
352  case 'phone_office':
353  $this->writer->xmlElement('PhoneOffice', array(), $value);
354  break;
355 
356  case 'phone_home':
357  $this->writer->xmlElement('PhoneHome', array(), $value);
358  break;
359 
360  case 'phone_mobile':
361  $this->writer->xmlElement('PhoneMobile', array(), $value);
362  break;
363 
364  case 'fax':
365  $this->writer->xmlElement('Fax', array(), $value);
366  break;
367 
368  case 'email':
369  $this->writer->xmlElement('Email', array(), $value);
370  break;
371 
372  case 'matriculation':
373  $this->writer->xmlElement('Matriculation', array(), $value);
374  break;
375 
376  /*
377  case 'photo':
378  $this->writer->xmlElement('PersonalPicture',array('encoding' => 'Base64','imagetype' => 'image/jpeg'),
379  base64_encode($this->convertInput($user[$value])));
380  break;
381  */
382  default:
383  // Handle user defined fields
384  if (substr($field, 0, 4) != 'udf_') {
385  continue 2;
386  }
387  $id_data = explode('_', $field);
388  if (!isset($id_data[1])) {
389  continue 2;
390  }
391  $this->initUserDefinedFields();
392  $definition = $this->udf->getDefinition($id_data[1]);
393  $this->writer->xmlElement(
394  'UserDefinedField',
395  array('Id' => $definition['il_id'],
396  'Name' => $definition['field_name']),
397  $value
398  );
399  break;
400 
401 
402  }
403  }
404  $this->writer->xmlEndTag('User');
405  }
406 
407  if ($cnt_create) {
408  $this->log->write('LDAP: Started creation of ' . $cnt_create . ' users.');
409  }
410  if ($cnt_update) {
411  $this->log->write('LDAP: Started update of ' . $cnt_update . ' users.');
412  }
413  $this->writer->xmlEndTag('Users');
414  }
415 
424  private function convertInput($a_value)
425  {
426  if (is_array($a_value)) {
427  return $a_value[0];
428  } else {
429  return $a_value;
430  }
431  }
432 
439  private function doMapping($user, $rule)
440  {
441  $mapping = trim(strtolower($rule['value']));
442 
443  if (strpos($mapping, ',') === false) {
444  return $this->convertInput($user[$mapping]);
445  }
446  // Is multiple mapping
447 
448  $fields = explode(',', $mapping);
449  $value = '';
450  foreach ($fields as $field) {
451  if (strlen($value)) {
452  $value .= ' ';
453  }
454  $value .= ($this->convertInput($user[trim($field)]));
455  }
456  return $value ? $value : '';
457  }
458 
459 
460 
461  private function initLDAPAttributeMapping()
462  {
463  include_once('Services/LDAP/classes/class.ilLDAPAttributeMapping.php');
464  $this->mapping = ilLDAPAttributeMapping::_getInstanceByServerId($this->server_settings->getServerId());
465  }
466 
467  private function initUserDefinedFields()
468  {
469  include_once('Services/User/classes/class.ilUserDefinedFields.php');
470  $this->udf = ilUserDefinedFields::_getInstance();
471  }
472 }
static getAssignmentsForCreation($a_server_id, $a_usr_name, $a_usr_data)
static _getInstance()
Get instance.
static _getInstanceByServerId($a_server_id)
Get instance of class.
doMapping($user, $rule)
doMapping
global $DIC
Definition: saml.php:7
static _generateLogin($a_login)
generate free login by starting with a default string and adding postfix numbers
XML writer class.
static _lookupId($a_user_str)
Lookup id by login.
static getAssignmentsForUpdate($a_server_id, $a_usr_id, $a_usr_name, $a_usr_data)
type $ilDB type $rbacadmin type $rbacreview type $ilSetting type $ilLog
setUserData($a_data)
Set user data received from pear auth or by ldap_search.
isModeActive($a_mode)
Check if mode is active.
convertInput($a_value)
A value can be an array or a string This function converts arrays to strings.
$rule
Definition: showstats.php:43
$lng
__construct(ilLDAPServer $a_server)
Construct of ilLDAPAttribute2XML Defines between LDAP and ILIAS user attributes.
parseRoleAssignmentsForCreation($a_external_account, $a_user)
Parse role assignments for update of user account.
$user
Definition: migrateto20.php:57
refresh()
Create/Update non existing users.
parseRoleAssignmentsForUpdate($a_usr_id, $a_external_account, $user)
Parse role assignments for update of user account.
getNewUserAuthMode()
Get auth mode for new users.
static getAllPossibleRoles($a_server_id)
Get all assignable roles (used for import parser)
setNewUserAuthMode($a_authmode)
Set auth mode for new users.
global $ilSetting
Definition: privfeed.php:17
global $ilDB
language handling
Update/create ILIAS user account by given LDAP attributes according to user attribute mapping setting...
getServer()
Get server settings.
addMode($a_mode)
Add import mode.
$data
Definition: bench.php:6
usersToXML()
Create xml string of user according to mapping rules.