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