ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
78  public function setUserData($a_data)
79  {
80  $this->user_data = $a_data;
81  }
82 
88  public function setNewUserAuthMode($a_authmode)
89  {
90  $this->new_user_auth_mode = $a_authmode;
91  }
92 
96  public function getNewUserAuthMode()
97  {
99  }
100 
101 
108  public function refresh()
109  {
110  global $rbacadmin;
111 
112  $this->usersToXML();
113 
114  include_once './Services/User/classes/class.ilUserImportParser.php';
115  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
116 
117  $importParser = new ilUserImportParser();
118  $importParser->setXMLContent($this->writer->xmlDumpMem(false));
119  $importParser->setRoleAssignment(ilLDAPRoleAssignmentRules::getAllPossibleRoles());
120  $importParser->setFolderId(7);
121  $importParser->startParsing();
122  $debug = $importParser->getProtocol();
123  #var_dump("<pre>",$this->writer->xmlDumpMem(),"</pre>");
124  #print_r($this->writer->xmlDumpMem($format));
125 
126  return true;
127  }
128 
135  private function usersToXML()
136  {
137  include_once('./Services/Xml/classes/class.ilXmlWriter.php');
138  $this->writer = new ilXmlWriter();
139  $this->writer->xmlStartTag('Users');
140 
141  $cnt_update = 0;
142  $cnt_create = 0;
143 
144  // Single users
145  foreach($this->user_data as $external_account => $user)
146  {
147  $user['ilExternalAccount'] = $external_account;
148 
149  // Required fields
150  if($user['ilInternalAccount'])
151  {
152  $usr_id = ilObjUser::_lookupId($user['ilInternalAccount']);
153 
154  ++$cnt_update;
155  // User exists
156  $this->writer->xmlStartTag('User',array('Id' => $usr_id,'Action' => 'Update'));
157  $this->writer->xmlElement('Login',array(),$user['ilInternalAccount']);
158  $this->writer->xmlElement('ExternalAccount',array(),$external_account);
159  $this->writer->xmlElement('AuthMode',array(type => $this->getNewUserAuthMode()),null);
160  $rules = $this->mapping->getRulesForUpdate();
161 
162  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
163  foreach(ilLDAPRoleAssignmentRules::getAssignmentsForUpdate($usr_id,$external_account, $user) as $role_data)
164  {
165  $this->writer->xmlElement('Role',
166  array('Id' => $role_data['id'],
167  'Type' => $role_data['type'],
168  'Action' => $role_data['action']),'');
169  }
170  }
171  else
172  {
173  ++$cnt_create;
174  // Create user
175  $this->writer->xmlStartTag('User',array('Action' => 'Insert'));
176  $this->writer->xmlElement('Login',array(),ilAuthUtils::_generateLogin($external_account));
177 
178  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
179  foreach(ilLDAPRoleAssignmentRules::getAssignmentsForCreation($external_account, $user) as $role_data)
180  {
181  $this->writer->xmlElement('Role',
182  array('Id' => $role_data['id'],
183  'Type' => $role_data['type'],
184  'Action' => $role_data['action']),'');
185  }
186 
187  $rules = $this->mapping->getRules();
188 
189  }
190 
191  $this->writer->xmlElement('Active',array(),"true");
192  $this->writer->xmlElement('TimeLimitOwner',array(),7);
193  $this->writer->xmlElement('TimeLimitUnlimited',array(),1);
194  $this->writer->xmlElement('TimeLimitFrom',array(),time());
195  $this->writer->xmlElement('TimeLimitUntil',array(),time());
196 
197  // only for new users.
198  // If auth_mode is 'default' (ldap) this status should remain.
199  if(!$user['ilInternalAccount'])
200  {
201  $this->writer->xmlElement('AuthMode',
202  array('type' => $this->getNewUserAuthMode()),
203  $this->getNewUserAuthMode()
204  );
205  $this->writer->xmlElement('ExternalAccount',array(),$external_account);
206  }
207  foreach($rules as $field => $data)
208  {
209  // Do Mapping: it is possible to assign multiple ldap attribute to one user data field
210  if(!($value = $this->doMapping($user,$data)))
211  {
212  continue;
213  }
214 
215  switch($field)
216  {
217  case 'gender':
218  switch(strtolower($value))
219  {
220  case 'm':
221  case 'male':
222  $this->writer->xmlElement('Gender',array(),'m');
223  break;
224 
225  case 'f':
226  case 'female':
227  default:
228  $this->writer->xmlElement('Gender',array(),'f');
229  break;
230 
231  }
232  break;
233 
234  case 'firstname':
235  $this->writer->xmlElement('Firstname',array(),$value);
236  break;
237 
238  case 'lastname':
239  $this->writer->xmlElement('Lastname',array(),$value);
240  break;
241 
242  case 'hobby':
243  $this->writer->xmlElement('Hobby',array(),$value);
244  break;
245 
246  case 'title':
247  $this->writer->xmlElement('Title',array(),$value);
248  break;
249 
250  case 'institution':
251  $this->writer->xmlElement('Institution',array(),$value);
252  break;
253 
254  case 'department':
255  $this->writer->xmlElement('Department',array(),$value);
256  break;
257 
258  case 'street':
259  $this->writer->xmlElement('Street',array(),$value);
260  break;
261 
262  case 'city':
263  $this->writer->xmlElement('City',array(),$value);
264  break;
265 
266  case 'zipcode':
267  $this->writer->xmlElement('PostalCode',array(),$value);
268  break;
269 
270  case 'country':
271  $this->writer->xmlElement('Country',array(),$value);
272  break;
273 
274  case 'phone_office':
275  $this->writer->xmlElement('PhoneOffice',array(),$value);
276  break;
277 
278  case 'phone_home':
279  $this->writer->xmlElement('PhoneHome',array(),$value);
280  break;
281 
282  case 'phone_mobile':
283  $this->writer->xmlElement('PhoneMobile',array(),$value);
284  break;
285 
286  case 'fax':
287  $this->writer->xmlElement('Fax',array(),$value);
288  break;
289 
290  case 'email':
291  $this->writer->xmlElement('Email',array(),$value);
292  break;
293 
294  case 'matriculation':
295  $this->writer->xmlElement('Matriculation',array(),$value);
296  break;
297 
298  /*
299  case 'photo':
300  $this->writer->xmlElement('PersonalPicture',array('encoding' => 'Base64','imagetype' => 'image/jpeg'),
301  base64_encode($this->convertInput($user[$value])));
302  break;
303  */
304  default:
305  // Handle user defined fields
306  if(substr($field,0,4) != 'udf_')
307  {
308  continue;
309  }
310  $id_data = explode('_',$field);
311  if(!isset($id_data[1]))
312  {
313  continue;
314  }
315  $this->initUserDefinedFields();
316  $definition = $this->udf->getDefinition($id_data[1]);
317  $this->writer->xmlElement('UserDefinedField',array('Id' => $definition['il_id'],
318  'Name' => $definition['field_name']),
319  $value);
320  break;
321 
322 
323  }
324  }
325  $this->writer->xmlEndTag('User');
326  }
327 
328  if($cnt_create)
329  {
330  $this->log->write('LDAP: Started creation of '.$cnt_create.' users.');
331  }
332  if($cnt_update)
333  {
334  $this->log->write('LDAP: Started update of '.$cnt_update.' users.');
335  }
336  $this->writer->xmlEndTag('Users');
337  }
338 
347  private function convertInput($a_value)
348  {
349  if(is_array($a_value))
350  {
351  return $a_value[0];
352  }
353  else
354  {
355  return $a_value;
356  }
357  }
358 
365  private function doMapping($user,$rule)
366  {
367  $mapping = trim(strtolower($rule['value']));
368 
369  if(strpos($mapping,',') === false)
370  {
371  return $this->convertInput($user[$mapping]);
372  }
373  // Is multiple mapping
374 
375  $fields = explode(',',$mapping);
376  $value = '';
377  foreach($fields as $field)
378  {
379  if(strlen($value))
380  {
381  $value .= ' ';
382  }
383  $value .= ($this->convertInput($user[trim($field)]));
384  }
385  return $value ? $value : '';
386  }
387 
388 
389 
390  private function initLDAPAttributeMapping()
391  {
392  include_once('Services/LDAP/classes/class.ilLDAPAttributeMapping.php');
393  $this->mapping = ilLDAPAttributeMapping::_getInstanceByServerId($this->server_settings->getServerId());
394  }
395 
396  private function initUserDefinedFields()
397  {
398  include_once('Services/User/classes/class.ilUserDefinedFields.php');
399  $this->udf = ilUserDefinedFields::_getInstance();
400  }
401 }
402 
403 
404 
405 ?>