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