• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Services/LDAP/classes/class.ilLDAPAttributeToUser.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2006 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00035 class ilLDAPAttributeToUser
00036 {
00037         private $server_settings = null;
00038         private $role_assignment = null;
00039         private $db = null;
00040         
00041         private $user_data = array();
00042         private $setting = null;
00043         private $mapping = null;
00044         
00051         public function __construct(ilLDAPServer $a_server)
00052         {
00053                 global $ilDB,$ilSetting,$lng,$ilLog;
00054                 
00055                 // Initialise language object
00056                 if(!is_object($lng))
00057                 {
00058                         $lng = new ilLanguage('en');
00059                 }
00060                 
00061                 $this->log = $ilLog;
00062 
00063                 $this->server_settings = $a_server;
00064                 $this->setting = $ilSetting;
00065                 
00066                 $this->initLDAPAttributeMapping();
00067                 $this->initLDAPRoleAssignments();
00068         }
00069         
00077         public function setUserData($a_data)
00078         {
00079                 $this->user_data = $a_data;
00080         }
00081         
00082         
00089         public function refresh()
00090         {
00091                 global $rbacadmin;
00092                 
00093                 $this->usersToXML();
00094                 
00095                 include_once './Services/User/classes/class.ilUserImportParser.php';
00096                 $importParser = new ilUserImportParser();
00097                 $importParser->setXMLContent($this->writer->xmlDumpMem(false));
00098                 $importParser->setRoleAssignment($this->role_assignment->getPossibleRoles());
00099                 $importParser->setFolderId(7);
00100                 $importParser->startParsing();
00101                 $debug = $importParser->getProtocol();
00102                 
00103                 #var_dump("<pre>",$this->writer->xmlDumpMem(),"</pre>");
00104                 
00105                 return true;
00106         }
00107         
00114         private function usersToXML()
00115         {
00116                 include_once('classes/class.ilXmlWriter.php');
00117                 $this->writer = new ilXmlWriter();
00118                 $this->writer->xmlStartTag('Users');
00119                 
00120                 $cnt_update = 0;
00121                 $cnt_create = 0;
00122                 
00123                 // Single users
00124                 foreach($this->user_data as $external_account => $user)
00125                 {
00126                         // Required fields
00127                         if($user['ilInternalAccount'])
00128                         {
00129                                 $usr_id = ilObjUser::_lookupId($user['ilInternalAccount']);
00130                                 
00131                                 ++$cnt_update;
00132                                 // User exists
00133                                 $this->writer->xmlStartTag('User',array('Id' => $usr_id,'Action' => 'Update'));
00134                                 $this->writer->xmlElement('Login',array(),$user['ilInternalAccount']);
00135                                 $this->writer->xmlElement('ExternalAccount',array(),$external_account);
00136                                 $this->writer->xmlElement('AuthMode',array(type =>'ldap'),null);
00137                                 
00138                                 $rules = $this->mapping->getRulesForUpdate();
00139                         }
00140                         else
00141                         {
00142                                 ++$cnt_create;
00143                                 // Create user
00144                                 $this->writer->xmlStartTag('User',array('Action' => 'Insert'));
00145                                 $this->writer->xmlElement('Login',array(),ilAuthUtils::_generateLogin($external_account));
00146                                 
00147                                 $rules = $this->mapping->getRules();
00148 
00149                                 // Assign to role only for new users
00150                                 $roles = $this->role_assignment->assignedRoles($external_account,$user);
00151                                 foreach($roles as $role_data)
00152                                 {
00153                                         $this->writer->xmlElement('Role',
00154                                                 array('Id' => $role_data['id'],
00155                                                                 'Type' => $role_data['type'],
00156                                                                 'Action' => $role_data['action']),'');
00157                                 }
00158                         }
00159 
00160                         $this->writer->xmlElement('Active',array(),"true");
00161                         $this->writer->xmlElement('TimeLimitOwner',array(),7);
00162                         $this->writer->xmlElement('TimeLimitUnlimited',array(),1);
00163                         $this->writer->xmlElement('TimeLimitFrom',array(),time());
00164                         $this->writer->xmlElement('TimeLimitUntil',array(),time());
00165                         
00166                         // only for new users. 
00167                         // If auth_mode is 'default' (ldap) this status should remain.
00168                         if(!$user['ilInternalAccount'])
00169                         {
00170                                 $this->writer->xmlElement('AuthMode',array('type' => 'ldap'),'ldap');
00171                                 $this->writer->xmlElement('ExternalAccount',array(),$external_account);
00172                         }
00173                         foreach($rules as $field => $data)
00174                         {
00175                                 // Do Mapping: it is possible to assign multiple ldap attribute to one user data field
00176                                 if(!($value = $this->doMapping($user,$data)))
00177                                 {
00178                                         continue;
00179                                 }
00180                 
00181                                 switch($field)
00182                                 {
00183                                         case 'gender':
00184                                                 switch(strtolower($value))
00185                                                 {
00186                                                         case 'm':
00187                                                         case 'male':
00188                                                                 $this->writer->xmlElement('Gender',array(),'m');
00189                                                                 break;
00190                                                         
00191                                                         case 'f':
00192                                                         case 'female':
00193                                                         default:
00194                                                                 $this->writer->xmlElement('Gender',array(),'f');
00195                                                                 break;
00196                                                                 
00197                                                 }
00198                                                 break;
00199                                         
00200                                         case 'firstname':
00201                                                 $this->writer->xmlElement('Firstname',array(),$value);
00202                                                 break;
00203                                                 
00204                                         case 'lastname':
00205                                                 $this->writer->xmlElement('Lastname',array(),$value);
00206                                                 break;
00207                                         
00208                                         case 'hobby':
00209                                                 $this->writer->xmlElement('Hobby',array(),$value);
00210                                                 break;
00211                                                 
00212                                         case 'title':
00213                                                 $this->writer->xmlElement('Title',array(),$value);
00214                                                 break;
00215                                                 
00216                                         case 'institution':
00217                                                 $this->writer->xmlElement('Institution',array(),$value);
00218                                                 break;
00219 
00220                                         case 'department':
00221                                                 $this->writer->xmlElement('Department',array(),$value);
00222                                                 break;
00223 
00224                                         case 'street':
00225                                                 $this->writer->xmlElement('Street',array(),$value);
00226                                                 break;
00227                                                 
00228                                         case 'city':
00229                                                 $this->writer->xmlElement('City',array(),$value);
00230                                                 break;
00231 
00232                                         case 'zipcode':
00233                                                 $this->writer->xmlElement('PostalCode',array(),$value);
00234                                                 break;
00235 
00236                                         case 'country':
00237                                                 $this->writer->xmlElement('Country',array(),$value);
00238                                                 break;
00239                                                 
00240                                         case 'phone_office':
00241                                                 $this->writer->xmlElement('PhoneOffice',array(),$value);
00242                                                 break;
00243 
00244                                         case 'phone_home':
00245                                                 $this->writer->xmlElement('PhoneHome',array(),$value);
00246                                                 break;
00247 
00248                                         case 'phone_mobile':
00249                                                 $this->writer->xmlElement('PhoneMobile',array(),$value);
00250                                                 break;
00251                                                 
00252                                         case 'fax':
00253                                                 $this->writer->xmlElement('Fax',array(),$value);
00254                                                 break;
00255 
00256                                         case 'email':
00257                                                 $this->writer->xmlElement('Email',array(),$value);
00258                                                 break;
00259                                                 
00260                                         case 'matriculation':
00261                                                 $this->writer->xmlElement('Matriculation',array(),$value);
00262                                                 break;
00263                                                 
00264                                         /*                                              
00265                                         case 'photo':
00266                                                 $this->writer->xmlElement('PersonalPicture',array('encoding' => 'Base64','imagetype' => 'image/jpeg'),
00267                                                         base64_encode($this->convertInput($user[$value])));
00268                                                 break;
00269                                         */
00270                                         default:
00271                                                 // Handle user defined fields
00272                                                 if(substr($field,0,4) != 'udf_')
00273                                                 {
00274                                                         continue;
00275                                                 }
00276                                                 $id_data = explode('_',$field);
00277                                                 if(!isset($id_data[1]))
00278                                                 {
00279                                                         continue;
00280                                                 }
00281                                                 $this->initUserDefinedFields();
00282                                                 $definition = $this->udf->getDefinition($id_data[1]);
00283                                                 $this->writer->xmlElement('UserDefinedField',array('Id' => $definition['il_id'],
00284                                                                                                                                                         'Name' => $definition['field_name']),
00285                                                                                                                                         $value);
00286                                                 break;
00287                                                                                                                                                         
00288                                                 
00289                                 }
00290                         }
00291                         $this->writer->xmlEndTag('User');
00292                 }
00293                 
00294                 if($cnt_create)
00295                 {
00296                         $this->log->write('LDAP: Started creation of '.$cnt_create.' users.');
00297                 }
00298                 if($cnt_update)
00299                 {
00300                         $this->log->write('LDAP: Started update of '.$cnt_update.' users.');
00301                 }
00302                 $this->writer->xmlEndTag('Users');
00303         }
00304         
00313         private function convertInput($a_value)
00314         {
00315                 if(is_array($a_value))
00316                 {
00317                         return $a_value[0];
00318                 }
00319                 else
00320                 {
00321                         return $a_value;
00322                 }
00323         }
00324         
00331         private function doMapping($user,$rule)
00332         {
00333                 $mapping = trim(strtolower($rule['value']));
00334                 
00335                 if(strpos($mapping,',') === false)
00336                 {
00337                         return $this->convertInput($user[$mapping]);
00338                 }
00339                 // Is multiple mapping
00340                 
00341                 $fields = explode(',',$mapping);
00342                 $value = '';
00343                 foreach($fields as $field)
00344                 {
00345                         if(strlen($value))
00346                         {
00347                                 $value .= ' ';
00348                         }
00349                         $value .= ($this->convertInput($user[trim($field)]));
00350                 }
00351                 return $value ? $value : '';
00352         }
00353         
00360         private function initLDAPRoleAssignments()
00361         {
00362                 include_once('Services/LDAP/classes/class.ilLDAPRoleAssignments.php');
00363                 $this->role_assignment = ilLDAPRoleAssignments::_getInstanceByServer($this->server_settings);
00364         }
00365         
00366         
00367         private function initLDAPAttributeMapping()
00368         {
00369                 include_once('Services/LDAP/classes/class.ilLDAPAttributeMapping.php');
00370                 $this->mapping = ilLDAPAttributeMapping::_getInstanceByServerId($this->server_settings->getServerId());
00371         }
00372         
00373         private function initUserDefinedFields()
00374         {
00375                 include_once('Services/User/classes/class.ilUserDefinedFields.php');
00376                 $this->udf = ilUserDefinedFields::_getInstance();
00377         }
00378 }
00379 
00380 
00381 
00382 ?>

Generated on Fri Dec 13 2013 17:56:57 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1