ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAuthECS.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 
34 include_once('Auth/Auth.php');
35 
36 
37 class ilAuthECS extends Auth
38 {
39  protected $container = null;
40  protected $settings = null;
41  protected $ilLog = null;
42 
49  public function __construct($a_options,$ecs_hash)
50  {
51  global $ilLog;
52 
53  $this->log = $ilLog;
54  $this->log->write(__METHOD__.': Called constructor...');
55 
56  $_POST['username'] = 'dummy';
57 
58 
59  include_once('./Services/WebServices/ECS/classes/class.ilAuthContainerECS.php');
60  parent::__construct($this->container = new ilAuthContainerECS(array('ecs_hash' => $ecs_hash)),$a_options);
61 
62  $this->initLogObserver();
63  $this->setCallbacks();
64  }
65 
71  protected function loginObserver($a_username)
72  {
73  include_once('./Services/WebServices/ECS/classes/class.ilECSUser.php');
74 
75  $user = new ilECSUser($_GET);
76 
77  if(!$usr_id = ilObject::_lookupObjIdByImportId($user->getImportId()))
78  {
79  $username = $this->createUser($user);
80  }
81  else
82  {
83  $username = $this->updateUser($user,$usr_id);
84  }
85 
86  $this->setAuth($username);
87  $this->log->write(__METHOD__.': Login succesesful');
88  return;
89  }
90 
91 
97  protected function createUser(ilECSUser $user)
98  {
99  global $ilClientIniFile,$ilSetting,$rbacadmin,$ilLog;
100 
101  $userObj = new ilObjUser();
102 
103  include_once('./Services/Authentication/classes/class.ilAuthUtils.php');
104  $local_user = ilAuthUtils::_generateLogin($this->container->getAbreviation().'_'.$user->getLogin());
105 
106  $newUser["login"] = $local_user;
107  $newUser["firstname"] = $user->getFirstname();
108  $newUser["lastname"] = $user->getLastname();
109  $newUser['email'] = $user->getEmail();
110  $newUser['institution'] = $user->getInstitution();
111 
112  // set "plain md5" password (= no valid password)
113  $newUser["passwd"] = "";
114  $newUser["passwd_type"] = IL_PASSWD_MD5;
115 
116  $newUser["auth_mode"] = "ecs";
117  $newUser["profile_incomplete"] = 0;
118 
119  // system data
120  $userObj->assignData($newUser);
121  $userObj->setTitle($userObj->getFullname());
122  $userObj->setDescription($userObj->getEmail());
123 
124  // set user language to system language
125  $userObj->setLanguage($ilSetting->get("language"));
126 
127  // Time limit
128  $userObj->setTimeLimitOwner(7);
129  $userObj->setTimeLimitUnlimited(0);
130  $userObj->setTimeLimitFrom(time());
131  $userObj->setTimeLimitUntil(time() + $ilClientIniFile->readVariable("session","expire"));
132 
133  // Create user in DB
134  $userObj->setOwner(6);
135  $userObj->create();
136  $userObj->setActive(1);
137  $userObj->updateOwner();
138  $userObj->saveAsNew();
139  $userObj->writePrefs();
140 
141  $this->initSettings();
142  if($global_role = $this->settings->getGlobalRole())
143  {
144  $rbacadmin->assignUser($this->settings->getGlobalRole(),$userObj->getId(),true);
145  }
146  ilObject::_writeImportId($userObj->getId(),$user->getImportId());
147 
148  $ilLog->write(__METHOD__.': Created new remote user with usr_id: '.$user->getImportId());
149 
150  // Send Mail
151  #$this->sendNotification($userObj);
152 
153  return $userObj->getLogin();
154  }
155 
161  protected function updateUser(ilECSUser $user,$a_local_user_id)
162  {
163  global $ilClientIniFile,$ilLog,$rbacadmin;
164 
165  $user_obj = new ilObjUser($a_local_user_id);
166  $user_obj->setFirstname($user->getFirstname());
167  $user_obj->setLastname($user->getLastname());
168  $user_obj->setEmail($user->getEmail());
169  $user_obj->setInstitution($user->getInstitution());
170 
171  $until = $user_obj->getTimeLimitUntil();
172 
173  if($until < (time() + $ilClientIniFile->readVariable('session','expire')))
174  {
175  $user_obj->setTimeLimitFrom(time());
176  $user_obj->setTimeLimitUntil(time() + $ilClientIniFile->readVariable("session","expire"));
177  }
178  $user_obj->update();
179  $user_obj->refreshLogin();
180 
181  $this->initSettings();
182  if($global_role = $this->settings->getGlobalRole())
183  {
184  $rbacadmin->assignUser($this->settings->getGlobalRole(),$user_obj->getId(),true);
185  }
186 
187  $ilLog->write(__METHOD__.': Finished update of remote user with usr_id: '.$user->getImportId());
188  return $user_obj->getLogin();
189  }
190 
196  protected function failedLoginObserver()
197  {
198  $this->log->write(__METHOD__.': Login failed');
199  }
200 
201 
206  private function setCallbacks()
207  {
208  $this->setLoginCallback(array($this,'loginObserver'));
209  $this->setFailedLoginCallback(array($this,'failedLoginObserver'));
210  }
211 
219  private function initLogObserver()
220  {
221  global $ilLog;
222 
223  if(!method_exists($this,'attachLogObserver'))
224  {
225  $ilLog->write(__METHOD__.': PEAR Auth < 1.5 => disabling logging.');
226  return false;
227  }
228 
229  if(@include_once('Log.php'))
230  {
231  if(@include_once('Log/observer.php'))
232  {
233  $ilLog->write(__METHOD__.': Attached Logging observer.');
234  include_once('Services/LDAP/classes/class.ilAuthLDAPLogObserver.php');
235  $this->attachLogObserver(new ilAuthLDAPLogObserver(AUTH_LOG_DEBUG));
236  return true;
237  }
238  }
239  $ilLog->write(__METHOD__.': PEAR Log not installed. Logging disabled');
240  }
241 
248  private function initSettings()
249  {
250  include_once('./Services/WebServices/ECS/classes/class.ilECSSettings.php');
251  $this->settings = ilECSSettings::_getInstance();
252  }
253 
261  private function sendNotification($user_obj)
262  {
263  if(!count($this->settings->getUserRecipients()))
264  {
265  return true;
266  }
267 
268  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
270  $GLOBALS['lng'] = $lang;
271  $GLOBALS['ilUser'] = $user_obj;
272  $lang->loadLanguageModule('ecs');
273 
274  include_once('./Services/Mail/classes/class.ilMail.php');
275  $mail = new ilMail(6);
276  $mail->enableSoap(false);
277  $subject = $lang->txt('ecs_new_user_subject');
278 
279  // build body
280  $body = $lang->txt('ecs_new_user_body')."\n\n";
281  $body .= $lang->txt('ecs_new_user_profile')."\n\n";
282  $body .= $user_obj->getProfileAsString($lang)."\n\n";
284 
285  $mail->sendMail($this->settings->getUserRecipientsAsString(),"","",$subject,$body,array(),array("normal"));
286  }
287 }
288 
289 ?>