ILIAS  Release_3_10_x_branch Revision 61812
 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 
36 {
37  private $server_settings = null;
38  private $role_assignment = null;
39  private $db = null;
40 
41  private $user_data = array();
42  private $setting = null;
43  private $mapping = null;
44 
51  public function __construct(ilLDAPServer $a_server)
52  {
53  global $ilDB,$ilSetting,$lng,$ilLog;
54 
55  // Initialise language object
56  if(!is_object($lng))
57  {
58  $lng = new ilLanguage('en');
59  }
60 
61  $this->log = $ilLog;
62 
63  $this->server_settings = $a_server;
64  $this->setting = $ilSetting;
65 
66  $this->initLDAPAttributeMapping();
67  $this->initLDAPRoleAssignments();
68  }
69 
77  public function setUserData($a_data)
78  {
79  $this->user_data = $a_data;
80  }
81 
82 
89  public function refresh()
90  {
91  global $rbacadmin;
92 
93  $this->usersToXML();
94 
95  include_once './Services/User/classes/class.ilUserImportParser.php';
96  $importParser = new ilUserImportParser();
97  $importParser->setXMLContent($this->writer->xmlDumpMem(false));
98  $importParser->setRoleAssignment($this->role_assignment->getPossibleRoles());
99  $importParser->setFolderId(7);
100  $importParser->startParsing();
101  $debug = $importParser->getProtocol();
102 
103  #var_dump("<pre>",$this->writer->xmlDumpMem(),"</pre>");
104 
105  return true;
106  }
107 
114  private function usersToXML()
115  {
116  include_once('classes/class.ilXmlWriter.php');
117  $this->writer = new ilXmlWriter();
118  $this->writer->xmlStartTag('Users');
119 
120  $cnt_update = 0;
121  $cnt_create = 0;
122 
123  // Single users
124  foreach($this->user_data as $external_account => $user)
125  {
126  // Required fields
127  if($user['ilInternalAccount'])
128  {
129  $usr_id = ilObjUser::_lookupId($user['ilInternalAccount']);
130 
131  ++$cnt_update;
132  // User exists
133  $this->writer->xmlStartTag('User',array('Id' => $usr_id,'Action' => 'Update'));
134  $this->writer->xmlElement('Login',array(),$user['ilInternalAccount']);
135  $this->writer->xmlElement('ExternalAccount',array(),$external_account);
136  $this->writer->xmlElement('AuthMode',array(type =>'ldap'),null);
137 
138  $rules = $this->mapping->getRulesForUpdate();
139  }
140  else
141  {
142  ++$cnt_create;
143  // Create user
144  $this->writer->xmlStartTag('User',array('Action' => 'Insert'));
145  $this->writer->xmlElement('Login',array(),ilAuthUtils::_generateLogin($external_account));
146 
147  $rules = $this->mapping->getRules();
148 
149  // Assign to role only for new users
150  $roles = $this->role_assignment->assignedRoles($external_account,$user);
151  foreach($roles as $role_data)
152  {
153  $this->writer->xmlElement('Role',
154  array('Id' => $role_data['id'],
155  'Type' => $role_data['type'],
156  'Action' => $role_data['action']),'');
157  }
158  }
159 
160  $this->writer->xmlElement('Active',array(),"true");
161  $this->writer->xmlElement('TimeLimitOwner',array(),7);
162  $this->writer->xmlElement('TimeLimitUnlimited',array(),1);
163  $this->writer->xmlElement('TimeLimitFrom',array(),time());
164  $this->writer->xmlElement('TimeLimitUntil',array(),time());
165 
166  // only for new users.
167  // If auth_mode is 'default' (ldap) this status should remain.
168  if(!$user['ilInternalAccount'])
169  {
170  $this->writer->xmlElement('AuthMode',array('type' => 'ldap'),'ldap');
171  $this->writer->xmlElement('ExternalAccount',array(),$external_account);
172  }
173  foreach($rules as $field => $data)
174  {
175  // Do Mapping: it is possible to assign multiple ldap attribute to one user data field
176  if(!($value = $this->doMapping($user,$data)))
177  {
178  continue;
179  }
180 
181  switch($field)
182  {
183  case 'gender':
184  switch(strtolower($value))
185  {
186  case 'm':
187  case 'male':
188  $this->writer->xmlElement('Gender',array(),'m');
189  break;
190 
191  case 'f':
192  case 'female':
193  default:
194  $this->writer->xmlElement('Gender',array(),'f');
195  break;
196 
197  }
198  break;
199 
200  case 'firstname':
201  $this->writer->xmlElement('Firstname',array(),$value);
202  break;
203 
204  case 'lastname':
205  $this->writer->xmlElement('Lastname',array(),$value);
206  break;
207 
208  case 'hobby':
209  $this->writer->xmlElement('Hobby',array(),$value);
210  break;
211 
212  case 'title':
213  $this->writer->xmlElement('Title',array(),$value);
214  break;
215 
216  case 'institution':
217  $this->writer->xmlElement('Institution',array(),$value);
218  break;
219 
220  case 'department':
221  $this->writer->xmlElement('Department',array(),$value);
222  break;
223 
224  case 'street':
225  $this->writer->xmlElement('Street',array(),$value);
226  break;
227 
228  case 'city':
229  $this->writer->xmlElement('City',array(),$value);
230  break;
231 
232  case 'zipcode':
233  $this->writer->xmlElement('PostalCode',array(),$value);
234  break;
235 
236  case 'country':
237  $this->writer->xmlElement('Country',array(),$value);
238  break;
239 
240  case 'phone_office':
241  $this->writer->xmlElement('PhoneOffice',array(),$value);
242  break;
243 
244  case 'phone_home':
245  $this->writer->xmlElement('PhoneHome',array(),$value);
246  break;
247 
248  case 'phone_mobile':
249  $this->writer->xmlElement('PhoneMobile',array(),$value);
250  break;
251 
252  case 'fax':
253  $this->writer->xmlElement('Fax',array(),$value);
254  break;
255 
256  case 'email':
257  $this->writer->xmlElement('Email',array(),$value);
258  break;
259 
260  case 'matriculation':
261  $this->writer->xmlElement('Matriculation',array(),$value);
262  break;
263 
264  /*
265  case 'photo':
266  $this->writer->xmlElement('PersonalPicture',array('encoding' => 'Base64','imagetype' => 'image/jpeg'),
267  base64_encode($this->convertInput($user[$value])));
268  break;
269  */
270  default:
271  // Handle user defined fields
272  if(substr($field,0,4) != 'udf_')
273  {
274  continue;
275  }
276  $id_data = explode('_',$field);
277  if(!isset($id_data[1]))
278  {
279  continue;
280  }
281  $this->initUserDefinedFields();
282  $definition = $this->udf->getDefinition($id_data[1]);
283  $this->writer->xmlElement('UserDefinedField',array('Id' => $definition['il_id'],
284  'Name' => $definition['field_name']),
285  $value);
286  break;
287 
288 
289  }
290  }
291  $this->writer->xmlEndTag('User');
292  }
293 
294  if($cnt_create)
295  {
296  $this->log->write('LDAP: Started creation of '.$cnt_create.' users.');
297  }
298  if($cnt_update)
299  {
300  $this->log->write('LDAP: Started update of '.$cnt_update.' users.');
301  }
302  $this->writer->xmlEndTag('Users');
303  }
304 
313  private function convertInput($a_value)
314  {
315  if(is_array($a_value))
316  {
317  return $a_value[0];
318  }
319  else
320  {
321  return $a_value;
322  }
323  }
324 
331  private function doMapping($user,$rule)
332  {
333  $mapping = trim(strtolower($rule['value']));
334 
335  if(strpos($mapping,',') === false)
336  {
337  return $this->convertInput($user[$mapping]);
338  }
339  // Is multiple mapping
340 
341  $fields = explode(',',$mapping);
342  $value = '';
343  foreach($fields as $field)
344  {
345  if(strlen($value))
346  {
347  $value .= ' ';
348  }
349  $value .= ($this->convertInput($user[trim($field)]));
350  }
351  return $value ? $value : '';
352  }
353 
360  private function initLDAPRoleAssignments()
361  {
362  include_once('Services/LDAP/classes/class.ilLDAPRoleAssignments.php');
363  $this->role_assignment = ilLDAPRoleAssignments::_getInstanceByServer($this->server_settings);
364  }
365 
366 
367  private function initLDAPAttributeMapping()
368  {
369  include_once('Services/LDAP/classes/class.ilLDAPAttributeMapping.php');
370  $this->mapping = ilLDAPAttributeMapping::_getInstanceByServerId($this->server_settings->getServerId());
371  }
372 
373  private function initUserDefinedFields()
374  {
375  include_once('Services/User/classes/class.ilUserDefinedFields.php');
376  $this->udf = ilUserDefinedFields::_getInstance();
377  }
378 }
379 
380 
381 
382 ?>