ILIAS  Release_4_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 
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  }
68 
76  public function setUserData($a_data)
77  {
78  $this->user_data = $a_data;
79  }
80 
81 
88  public function refresh()
89  {
90  global $rbacadmin;
91 
92  $this->usersToXML();
93 
94  include_once './Services/User/classes/class.ilUserImportParser.php';
95  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
96 
97  $importParser = new ilUserImportParser();
98  $importParser->setXMLContent($this->writer->xmlDumpMem(false));
99  $importParser->setRoleAssignment(ilLDAPRoleAssignmentRules::getAllPossibleRoles());
100  $importParser->setFolderId(7);
101  $importParser->startParsing();
102  $debug = $importParser->getProtocol();
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  $user['ilExternalAccount'] = $external_account;
127 
128  // Required fields
129  if($user['ilInternalAccount'])
130  {
131  $usr_id = ilObjUser::_lookupId($user['ilInternalAccount']);
132 
133  ++$cnt_update;
134  // User exists
135  $this->writer->xmlStartTag('User',array('Id' => $usr_id,'Action' => 'Update'));
136  $this->writer->xmlElement('Login',array(),$user['ilInternalAccount']);
137  $this->writer->xmlElement('ExternalAccount',array(),$external_account);
138  $this->writer->xmlElement('AuthMode',array(type =>'ldap'),null);
139 
140  $rules = $this->mapping->getRulesForUpdate();
141 
142  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
143  foreach(ilLDAPRoleAssignmentRules::getAssignmentsForUpdate($usr_id,$external_account, $user) as $role_data)
144  {
145  $this->writer->xmlElement('Role',
146  array('Id' => $role_data['id'],
147  'Type' => $role_data['type'],
148  'Action' => $role_data['action']),'');
149  }
150  }
151  else
152  {
153  ++$cnt_create;
154  // Create user
155  $this->writer->xmlStartTag('User',array('Action' => 'Insert'));
156  $this->writer->xmlElement('Login',array(),ilAuthUtils::_generateLogin($external_account));
157 
158  include_once './Services/LDAP/classes/class.ilLDAPRoleAssignmentRules.php';
159  foreach(ilLDAPRoleAssignmentRules::getAssignmentsForCreation($external_account, $user) as $role_data)
160  {
161  $this->writer->xmlElement('Role',
162  array('Id' => $role_data['id'],
163  'Type' => $role_data['type'],
164  'Action' => $role_data['action']),'');
165  }
166 
167  $rules = $this->mapping->getRules();
168 
169  }
170 
171  $this->writer->xmlElement('Active',array(),"true");
172  $this->writer->xmlElement('TimeLimitOwner',array(),7);
173  $this->writer->xmlElement('TimeLimitUnlimited',array(),1);
174  $this->writer->xmlElement('TimeLimitFrom',array(),time());
175  $this->writer->xmlElement('TimeLimitUntil',array(),time());
176 
177  // only for new users.
178  // If auth_mode is 'default' (ldap) this status should remain.
179  if(!$user['ilInternalAccount'])
180  {
181  $this->writer->xmlElement('AuthMode',array('type' => 'ldap'),'ldap');
182  $this->writer->xmlElement('ExternalAccount',array(),$external_account);
183  }
184  foreach($rules as $field => $data)
185  {
186  // Do Mapping: it is possible to assign multiple ldap attribute to one user data field
187  if(!($value = $this->doMapping($user,$data)))
188  {
189  continue;
190  }
191 
192  switch($field)
193  {
194  case 'gender':
195  switch(strtolower($value))
196  {
197  case 'm':
198  case 'male':
199  $this->writer->xmlElement('Gender',array(),'m');
200  break;
201 
202  case 'f':
203  case 'female':
204  default:
205  $this->writer->xmlElement('Gender',array(),'f');
206  break;
207 
208  }
209  break;
210 
211  case 'firstname':
212  $this->writer->xmlElement('Firstname',array(),$value);
213  break;
214 
215  case 'lastname':
216  $this->writer->xmlElement('Lastname',array(),$value);
217  break;
218 
219  case 'hobby':
220  $this->writer->xmlElement('Hobby',array(),$value);
221  break;
222 
223  case 'title':
224  $this->writer->xmlElement('Title',array(),$value);
225  break;
226 
227  case 'institution':
228  $this->writer->xmlElement('Institution',array(),$value);
229  break;
230 
231  case 'department':
232  $this->writer->xmlElement('Department',array(),$value);
233  break;
234 
235  case 'street':
236  $this->writer->xmlElement('Street',array(),$value);
237  break;
238 
239  case 'city':
240  $this->writer->xmlElement('City',array(),$value);
241  break;
242 
243  case 'zipcode':
244  $this->writer->xmlElement('PostalCode',array(),$value);
245  break;
246 
247  case 'country':
248  $this->writer->xmlElement('Country',array(),$value);
249  break;
250 
251  case 'phone_office':
252  $this->writer->xmlElement('PhoneOffice',array(),$value);
253  break;
254 
255  case 'phone_home':
256  $this->writer->xmlElement('PhoneHome',array(),$value);
257  break;
258 
259  case 'phone_mobile':
260  $this->writer->xmlElement('PhoneMobile',array(),$value);
261  break;
262 
263  case 'fax':
264  $this->writer->xmlElement('Fax',array(),$value);
265  break;
266 
267  case 'email':
268  $this->writer->xmlElement('Email',array(),$value);
269  break;
270 
271  case 'matriculation':
272  $this->writer->xmlElement('Matriculation',array(),$value);
273  break;
274 
275  /*
276  case 'photo':
277  $this->writer->xmlElement('PersonalPicture',array('encoding' => 'Base64','imagetype' => 'image/jpeg'),
278  base64_encode($this->convertInput($user[$value])));
279  break;
280  */
281  default:
282  // Handle user defined fields
283  if(substr($field,0,4) != 'udf_')
284  {
285  continue;
286  }
287  $id_data = explode('_',$field);
288  if(!isset($id_data[1]))
289  {
290  continue;
291  }
292  $this->initUserDefinedFields();
293  $definition = $this->udf->getDefinition($id_data[1]);
294  $this->writer->xmlElement('UserDefinedField',array('Id' => $definition['il_id'],
295  'Name' => $definition['field_name']),
296  $value);
297  break;
298 
299 
300  }
301  }
302  $this->writer->xmlEndTag('User');
303  }
304 
305  if($cnt_create)
306  {
307  $this->log->write('LDAP: Started creation of '.$cnt_create.' users.');
308  }
309  if($cnt_update)
310  {
311  $this->log->write('LDAP: Started update of '.$cnt_update.' users.');
312  }
313  $this->writer->xmlEndTag('Users');
314  }
315 
324  private function convertInput($a_value)
325  {
326  if(is_array($a_value))
327  {
328  return $a_value[0];
329  }
330  else
331  {
332  return $a_value;
333  }
334  }
335 
342  private function doMapping($user,$rule)
343  {
344  $mapping = trim(strtolower($rule['value']));
345 
346  if(strpos($mapping,',') === false)
347  {
348  return $this->convertInput($user[$mapping]);
349  }
350  // Is multiple mapping
351 
352  $fields = explode(',',$mapping);
353  $value = '';
354  foreach($fields as $field)
355  {
356  if(strlen($value))
357  {
358  $value .= ' ';
359  }
360  $value .= ($this->convertInput($user[trim($field)]));
361  }
362  return $value ? $value : '';
363  }
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 ?>