ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 {
36  private $server_settings = null;
37  private $role_assignment = null;
38  private $db = null;
39 
40  private $user_data = array();
41  private $setting = null;
42  private $mapping = null;
43 
44  private $new_user_auth_mode = 'ldap';
45 
52  public function __construct(ilLDAPServer $a_server)
53  {
54  global $ilDB,$ilSetting,$lng,$ilLog;
55 
56  // Initialise language object
57  if(!is_object($lng))
58  {
59  include_once './Services/Language/classes/class.ilLanguage.php';
60  $lng = new ilLanguage('en');
61  }
62 
63  $this->log = $ilLog;
64 
65  $this->server_settings = $a_server;
66  $this->setting = $ilSetting;
67 
68  $this->initLDAPAttributeMapping();
69  }
70 
71  // begin-patch ldap_multiple
76  public function getServer()
77  {
79  }
80  // end-patch ldap_multiple
81 
89  public function setUserData($a_data)
90  {
91  $this->user_data = $a_data;
92  }
93 
99  public function setNewUserAuthMode($a_authmode)
100  {
101  $this->new_user_auth_mode = $a_authmode;
102  }
103 
107  public function getNewUserAuthMode()
108  {
110  }
111 
112 
119  public function refresh()
120  {
121  global $rbacadmin;
122 
123  $this->usersToXML();
124 
125  include_once './Services/User/classes/class.ilUserImportParser.php';
126  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
127 
128  $importParser = new ilUserImportParser();
129  $importParser->setXMLContent($this->writer->xmlDumpMem(false));
130  $importParser->setRoleAssignment(ilLDAPRoleAssignmentRules::getAllPossibleRoles($this->getServer()->getServerId()));
131  $importParser->setFolderId(7);
132  $importParser->startParsing();
133  $debug = $importParser->getProtocol();
134  #var_dump("<pre>",$this->writer->xmlDumpMem(),"</pre>");
135  #print_r($this->writer->xmlDumpMem($format));
136 
137  return true;
138  }
139 
146  private function usersToXML()
147  {
148  include_once('./Services/Xml/classes/class.ilXmlWriter.php');
149  $this->writer = new ilXmlWriter();
150  $this->writer->xmlStartTag('Users');
151 
152  $cnt_update = 0;
153  $cnt_create = 0;
154 
155  // Single users
156  foreach($this->user_data as $external_account => $user)
157  {
158  $user['ilExternalAccount'] = $external_account;
159 
160  // Required fields
161  if($user['ilInternalAccount'])
162  {
163  $usr_id = ilObjUser::_lookupId($user['ilInternalAccount']);
164 
165  ++$cnt_update;
166  // User exists
167  $this->writer->xmlStartTag('User',array('Id' => $usr_id,'Action' => 'Update'));
168  $this->writer->xmlElement('Login',array(),$user['ilInternalAccount']);
169  $this->writer->xmlElement('ExternalAccount',array(),$external_account);
170  $this->writer->xmlElement('AuthMode',array(type => $this->getNewUserAuthMode()),null);
171  $rules = $this->mapping->getRulesForUpdate();
172 
173  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
175  $this->getServer()->getServerId(),
176  $usr_id,
177  $external_account,
178  $user) as $role_data)
179  {
180  $this->writer->xmlElement('Role',
181  array('Id' => $role_data['id'],
182  'Type' => $role_data['type'],
183  'Action' => $role_data['action']),'');
184  }
185  }
186  else
187  {
188  ++$cnt_create;
189  // Create user
190  $this->writer->xmlStartTag('User',array('Action' => 'Insert'));
191  $this->writer->xmlElement('Login',array(),ilAuthUtils::_generateLogin($external_account));
192 
193  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
195  $this->getServer()->getServerId(),
196  $external_account,
197  $user) as $role_data)
198  {
199  $this->writer->xmlElement('Role',
200  array('Id' => $role_data['id'],
201  'Type' => $role_data['type'],
202  'Action' => $role_data['action']),'');
203  }
204 
205  $rules = $this->mapping->getRules();
206 
207  }
208 
209  $this->writer->xmlElement('Active',array(),"true");
210  $this->writer->xmlElement('TimeLimitOwner',array(),7);
211  $this->writer->xmlElement('TimeLimitUnlimited',array(),1);
212  $this->writer->xmlElement('TimeLimitFrom',array(),time());
213  $this->writer->xmlElement('TimeLimitUntil',array(),time());
214 
215  // only for new users.
216  // If auth_mode is 'default' (ldap) this status should remain.
217  if(!$user['ilInternalAccount'])
218  {
219  $this->writer->xmlElement('AuthMode',
220  array('type' => $this->getNewUserAuthMode()),
221  $this->getNewUserAuthMode()
222  );
223  $this->writer->xmlElement('ExternalAccount',array(),$external_account);
224  }
225  foreach($rules as $field => $data)
226  {
227  // Do Mapping: it is possible to assign multiple ldap attribute to one user data field
228  if(!($value = $this->doMapping($user,$data)))
229  {
230  continue;
231  }
232 
233  switch($field)
234  {
235  case 'gender':
236  switch(strtolower($value))
237  {
238  case 'm':
239  case 'male':
240  $this->writer->xmlElement('Gender',array(),'m');
241  break;
242 
243  case 'f':
244  case 'female':
245  default:
246  $this->writer->xmlElement('Gender',array(),'f');
247  break;
248 
249  }
250  break;
251 
252  case 'firstname':
253  $this->writer->xmlElement('Firstname',array(),$value);
254  break;
255 
256  case 'lastname':
257  $this->writer->xmlElement('Lastname',array(),$value);
258  break;
259 
260  case 'hobby':
261  $this->writer->xmlElement('Hobby',array(),$value);
262  break;
263 
264  case 'title':
265  $this->writer->xmlElement('Title',array(),$value);
266  break;
267 
268  case 'institution':
269  $this->writer->xmlElement('Institution',array(),$value);
270  break;
271 
272  case 'department':
273  $this->writer->xmlElement('Department',array(),$value);
274  break;
275 
276  case 'street':
277  $this->writer->xmlElement('Street',array(),$value);
278  break;
279 
280  case 'city':
281  $this->writer->xmlElement('City',array(),$value);
282  break;
283 
284  case 'zipcode':
285  $this->writer->xmlElement('PostalCode',array(),$value);
286  break;
287 
288  case 'country':
289  $this->writer->xmlElement('Country',array(),$value);
290  break;
291 
292  case 'phone_office':
293  $this->writer->xmlElement('PhoneOffice',array(),$value);
294  break;
295 
296  case 'phone_home':
297  $this->writer->xmlElement('PhoneHome',array(),$value);
298  break;
299 
300  case 'phone_mobile':
301  $this->writer->xmlElement('PhoneMobile',array(),$value);
302  break;
303 
304  case 'fax':
305  $this->writer->xmlElement('Fax',array(),$value);
306  break;
307 
308  case 'email':
309  $this->writer->xmlElement('Email',array(),$value);
310  break;
311 
312  case 'matriculation':
313  $this->writer->xmlElement('Matriculation',array(),$value);
314  break;
315 
316  /*
317  case 'photo':
318  $this->writer->xmlElement('PersonalPicture',array('encoding' => 'Base64','imagetype' => 'image/jpeg'),
319  base64_encode($this->convertInput($user[$value])));
320  break;
321  */
322  default:
323  // Handle user defined fields
324  if(substr($field,0,4) != 'udf_')
325  {
326  continue;
327  }
328  $id_data = explode('_',$field);
329  if(!isset($id_data[1]))
330  {
331  continue;
332  }
333  $this->initUserDefinedFields();
334  $definition = $this->udf->getDefinition($id_data[1]);
335  $this->writer->xmlElement('UserDefinedField',array('Id' => $definition['il_id'],
336  'Name' => $definition['field_name']),
337  $value);
338  break;
339 
340 
341  }
342  }
343  $this->writer->xmlEndTag('User');
344  }
345 
346  if($cnt_create)
347  {
348  $this->log->write('LDAP: Started creation of '.$cnt_create.' users.');
349  }
350  if($cnt_update)
351  {
352  $this->log->write('LDAP: Started update of '.$cnt_update.' users.');
353  }
354  $this->writer->xmlEndTag('Users');
355  }
356 
365  private function convertInput($a_value)
366  {
367  if(is_array($a_value))
368  {
369  return $a_value[0];
370  }
371  else
372  {
373  return $a_value;
374  }
375  }
376 
383  private function doMapping($user,$rule)
384  {
385  $mapping = trim(strtolower($rule['value']));
386 
387  if(strpos($mapping,',') === false)
388  {
389  return $this->convertInput($user[$mapping]);
390  }
391  // Is multiple mapping
392 
393  $fields = explode(',',$mapping);
394  $value = '';
395  foreach($fields as $field)
396  {
397  if(strlen($value))
398  {
399  $value .= ' ';
400  }
401  $value .= ($this->convertInput($user[trim($field)]));
402  }
403  return $value ? $value : '';
404  }
405 
406 
407 
408  private function initLDAPAttributeMapping()
409  {
410  include_once('Services/LDAP/classes/class.ilLDAPAttributeMapping.php');
411  $this->mapping = ilLDAPAttributeMapping::_getInstanceByServerId($this->server_settings->getServerId());
412  }
413 
414  private function initUserDefinedFields()
415  {
416  include_once('Services/User/classes/class.ilUserDefinedFields.php');
417  $this->udf = ilUserDefinedFields::_getInstance();
418  }
419 }
420 
421 
422 
423 ?>
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
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.
convertInput($a_value)
A value can be an array or a string This function converts arrays to strings.
$data
_generateLogin($a_login)
generate free login by starting with a default string and adding postfix numbers
__construct(ilLDAPServer $a_server)
Construct of ilLDAPAttribute2XML Defines between LDAP and ILIAS user attributes.
refresh()
Create/Update non existing users.
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:40
global $lng
Definition: privfeed.php:40
global $ilDB
language handling
Update/create ILIAS user account by given LDAP attributes according to user attribute mapping setting...
getServer()
Get server settings.
usersToXML()
Create xml string of user according to mapping rules.