ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 $ilDB,$ilSetting,$lng,$ilLog;
60 
61  // Initialise language object
62  if(!is_object($lng))
63  {
64  include_once './Services/Language/classes/class.ilLanguage.php';
65  $lng = new ilLanguage('en');
66  }
67 
68  $this->log = $ilLog;
69 
70  $this->server_settings = $a_server;
71  $this->setting = $ilSetting;
72 
73  $this->initLDAPAttributeMapping();
74  }
75 
76  // begin-patch ldap_multiple
81  public function getServer()
82  {
84  }
85  // end-patch ldap_multiple
86 
94  public function setUserData($a_data)
95  {
96  $this->user_data = $a_data;
97  }
98 
104  public function setNewUserAuthMode($a_authmode)
105  {
106  $this->new_user_auth_mode = $a_authmode;
107  }
108 
112  public function getNewUserAuthMode()
113  {
115  }
116 
121  public function addMode($a_mode)
122  {
123  if(is_array($this->modes) && !in_array($a_mode, $this->modes))
124  {
125  $this->modes[] = $a_mode;
126  }
127  }
128 
134  public function isModeActive($a_mode)
135  {
136  return is_array($this->modes) && in_array($a_mode, $this->modes);
137  }
138 
139 
146  public function refresh()
147  {
148  global $rbacadmin;
149 
150  $this->usersToXML();
151 
152  include_once './Services/User/classes/class.ilUserImportParser.php';
153  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
154 
155  $importParser = new ilUserImportParser();
156  $importParser->setXMLContent($this->writer->xmlDumpMem(false));
157  $importParser->setRoleAssignment(ilLDAPRoleAssignmentRules::getAllPossibleRoles($this->getServer()->getServerId()));
158  $importParser->setFolderId(7);
159  $importParser->startParsing();
160  $debug = $importParser->getProtocol();
161  #var_dump("<pre>",$this->writer->xmlDumpMem(),"</pre>");
162  #print_r($this->writer->xmlDumpMem($format));
163 
164  return true;
165  }
166 
173  protected function parseRoleAssignmentsForUpdate($a_usr_id, $a_external_account, $user)
174  {
175  $rules = $this->mapping->getRulesForUpdate();
176 
177  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
179  $this->getServer()->getServerId(),
180  $a_usr_id,
181  $a_external_account,
182  $user) as $role_data)
183  {
184  $this->writer->xmlElement('Role',
185  array('Id' => $role_data['id'],
186  'Type' => $role_data['type'],
187  'Action' => $role_data['action']),'');
188  }
189  }
190 
196  protected function parseRoleAssignmentsForCreation($a_external_account, $a_user)
197  {
198  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
200  $this->getServer()->getServerId(),
201  $a_external_account,
202  $a_user) as $role_data)
203  {
204  $this->writer->xmlElement('Role',
205  array('Id' => $role_data['id'],
206  'Type' => $role_data['type'],
207  'Action' => $role_data['action']),'');
208  }
209 
210  }
211 
218  private function usersToXML()
219  {
220  include_once('./Services/Xml/classes/class.ilXmlWriter.php');
221  $this->writer = new ilXmlWriter();
222  $this->writer->xmlStartTag('Users');
223 
224  $cnt_update = 0;
225  $cnt_create = 0;
226 
227  // Single users
228  foreach($this->user_data as $external_account => $user)
229  {
230  $user['ilExternalAccount'] = $external_account;
231 
232  // Required fields
233  if($user['ilInternalAccount'])
234  {
235  $usr_id = ilObjUser::_lookupId($user['ilInternalAccount']);
236 
237  ++$cnt_update;
238  // User exists
239  $this->writer->xmlStartTag('User',array('Id' => $usr_id,'Action' => 'Update'));
240  $this->writer->xmlElement('Login',array(),$user['ilInternalAccount']);
241  $this->writer->xmlElement('ExternalAccount',array(),$external_account);
242  $this->writer->xmlElement('AuthMode',array(type => $this->getNewUserAuthMode()),null);
243 
244  if($this->isModeActive(self::MODE_INITIALIZE_ROLES))
245  {
246  $this->parseRoleAssignmentsForCreation($external_account, $user);
247  }
248  else
249  {
250  $this->parseRoleAssignmentsForUpdate($usr_id, $external_account, $user);
251  }
252  $rules = $this->mapping->getRulesForUpdate();
253  }
254  else
255  {
256  ++$cnt_create;
257  // Create user
258  $this->writer->xmlStartTag('User',array('Action' => 'Insert'));
259  $this->writer->xmlElement('Login',array(),ilAuthUtils::_generateLogin($external_account));
260 
261  $this->parseRoleAssignmentsForCreation($external_account, $user);
262  $rules = $this->mapping->getRules();
263  }
264 
265  $this->writer->xmlElement('Active',array(),"true");
266  $this->writer->xmlElement('TimeLimitOwner',array(),7);
267  $this->writer->xmlElement('TimeLimitUnlimited',array(),1);
268  $this->writer->xmlElement('TimeLimitFrom',array(),time());
269  $this->writer->xmlElement('TimeLimitUntil',array(),time());
270 
271  // only for new users.
272  // If auth_mode is 'default' (ldap) this status should remain.
273  if(!$user['ilInternalAccount'])
274  {
275  $this->writer->xmlElement('AuthMode',
276  array('type' => $this->getNewUserAuthMode()),
277  $this->getNewUserAuthMode()
278  );
279  $this->writer->xmlElement('ExternalAccount',array(),$external_account);
280  }
281  foreach($rules as $field => $data)
282  {
283  // Do Mapping: it is possible to assign multiple ldap attribute to one user data field
284  if(!($value = $this->doMapping($user,$data)))
285  {
286  continue;
287  }
288 
289  switch($field)
290  {
291  case 'gender':
292  switch(strtolower($value))
293  {
294  case 'm':
295  case 'male':
296  $this->writer->xmlElement('Gender',array(),'m');
297  break;
298 
299  case 'f':
300  case 'female':
301  default:
302  $this->writer->xmlElement('Gender',array(),'f');
303  break;
304 
305  }
306  break;
307 
308  case 'firstname':
309  $this->writer->xmlElement('Firstname',array(),$value);
310  break;
311 
312  case 'lastname':
313  $this->writer->xmlElement('Lastname',array(),$value);
314  break;
315 
316  case 'hobby':
317  $this->writer->xmlElement('Hobby',array(),$value);
318  break;
319 
320  case 'title':
321  $this->writer->xmlElement('Title',array(),$value);
322  break;
323 
324  case 'institution':
325  $this->writer->xmlElement('Institution',array(),$value);
326  break;
327 
328  case 'department':
329  $this->writer->xmlElement('Department',array(),$value);
330  break;
331 
332  case 'street':
333  $this->writer->xmlElement('Street',array(),$value);
334  break;
335 
336  case 'city':
337  $this->writer->xmlElement('City',array(),$value);
338  break;
339 
340  case 'zipcode':
341  $this->writer->xmlElement('PostalCode',array(),$value);
342  break;
343 
344  case 'country':
345  $this->writer->xmlElement('Country',array(),$value);
346  break;
347 
348  case 'phone_office':
349  $this->writer->xmlElement('PhoneOffice',array(),$value);
350  break;
351 
352  case 'phone_home':
353  $this->writer->xmlElement('PhoneHome',array(),$value);
354  break;
355 
356  case 'phone_mobile':
357  $this->writer->xmlElement('PhoneMobile',array(),$value);
358  break;
359 
360  case 'fax':
361  $this->writer->xmlElement('Fax',array(),$value);
362  break;
363 
364  case 'email':
365  $this->writer->xmlElement('Email',array(),$value);
366  break;
367 
368  case 'matriculation':
369  $this->writer->xmlElement('Matriculation',array(),$value);
370  break;
371 
372  /*
373  case 'photo':
374  $this->writer->xmlElement('PersonalPicture',array('encoding' => 'Base64','imagetype' => 'image/jpeg'),
375  base64_encode($this->convertInput($user[$value])));
376  break;
377  */
378  default:
379  // Handle user defined fields
380  if(substr($field,0,4) != 'udf_')
381  {
382  continue;
383  }
384  $id_data = explode('_',$field);
385  if(!isset($id_data[1]))
386  {
387  continue;
388  }
389  $this->initUserDefinedFields();
390  $definition = $this->udf->getDefinition($id_data[1]);
391  $this->writer->xmlElement('UserDefinedField',array('Id' => $definition['il_id'],
392  'Name' => $definition['field_name']),
393  $value);
394  break;
395 
396 
397  }
398  }
399  $this->writer->xmlEndTag('User');
400  }
401 
402  if($cnt_create)
403  {
404  $this->log->write('LDAP: Started creation of '.$cnt_create.' users.');
405  }
406  if($cnt_update)
407  {
408  $this->log->write('LDAP: Started update of '.$cnt_update.' users.');
409  }
410  $this->writer->xmlEndTag('Users');
411  }
412 
421  private function convertInput($a_value)
422  {
423  if(is_array($a_value))
424  {
425  return $a_value[0];
426  }
427  else
428  {
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  {
445  return $this->convertInput($user[$mapping]);
446  }
447  // Is multiple mapping
448 
449  $fields = explode(',',$mapping);
450  $value = '';
451  foreach($fields as $field)
452  {
453  if(strlen($value))
454  {
455  $value .= ' ';
456  }
457  $value .= ($this->convertInput($user[trim($field)]));
458  }
459  return $value ? $value : '';
460  }
461 
462 
463 
464  private function initLDAPAttributeMapping()
465  {
466  include_once('Services/LDAP/classes/class.ilLDAPAttributeMapping.php');
467  $this->mapping = ilLDAPAttributeMapping::_getInstanceByServerId($this->server_settings->getServerId());
468  }
469 
470  private function initUserDefinedFields()
471  {
472  include_once('Services/User/classes/class.ilUserDefinedFields.php');
473  $this->udf = ilUserDefinedFields::_getInstance();
474  }
475 }
476 
477 
478 
479 ?>
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
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.
__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.
Create styles array
The data for the language used.
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 $lng
Definition: privfeed.php:17
global $ilDB
language handling
Update/create ILIAS user account by given LDAP attributes according to user attribute mapping setting...
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
getServer()
Get server settings.
addMode($a_mode)
Add import mode.
usersToXML()
Create xml string of user according to mapping rules.