ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAuthContainerECS.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 
24 include_once('Auth/Container.php');
25 
35 {
36  protected $mid = null;
37  protected $abreviation = null;
38 
39  protected $currentServer = null;
40  protected $servers = null;
41 
42  protected $log;
43 
51  public function __construct($a_params = array())
52  {
53  parent::__construct($a_params);
54 
55  $this->initECSServices();
56 
57  $this->log = $GLOBALS['ilLog'];
58  }
59 
67  public function getAbreviation()
68  {
69  return $this->abreviation;
70  }
71 
77  public function getMID()
78  {
79  return $this->mid;
80  }
81 
86  public function setCurrentServer(ilECSSetting $server = null)
87  {
88  $this->currentServer = $server;
89  }
90 
95  public function getCurrentServer()
96  {
97  return $this->currentServer;
98  }
99 
104  public function getServerSettings()
105  {
106  return $this->servers;
107  }
108 
114  public function fetchData($a_username,$a_pass)
115  {
116  global $ilLog;
117 
118  $ilLog->write(__METHOD__.': Starting ECS authentication.');
119 
120  if(!$this->getServerSettings()->activeServerExists())
121  {
122  $GLOBALS['ilLog']->write(__METHOD__.': no active ecs server found. Aborting');
123  return false;
124  }
125 
126  // Iterate through all active ecs instances
127  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
128  foreach($this->getServerSettings()->getServers() as $server)
129  {
130  $this->setCurrentServer($server);
131  if($this->validateHash())
132  {
133  return true;
134  }
135  }
136  $GLOBALS['ilLog']->write(__METHOD__.': Could not validate ecs hash for any server');
137  return false;
138 
139  }
140 
141 
150  public function validateHash()
151  {
152  global $ilLog;
153 
154  // Check if hash is valid ...
155  try
156  {
157  include_once('./Services/WebServices/ECS/classes/class.ilECSConnector.php');
158  $connector = new ilECSConnector($this->getCurrentServer());
159  $res = $connector->getAuth($_GET['ecs_hash']);
160  $auths = $res->getResult();
161  $this->abreviation = $auths->abbr;
162  $ilLog->write(__METHOD__.': Got abr: '.$this->abreviation);
163  return true;
164  }
165  catch(ilECSConnectorException $e)
166  {
167  $ilLog->write(__METHOD__.': Authentication failed with message: '.$e->getMessage());
168  return false;
169  }
170  }
171 
177  public function loginObserver($a_username, $a_auth)
178  {
179  include_once('./Services/WebServices/ECS/classes/class.ilECSUser.php');
180 
181  $user = new ilECSUser($_GET);
182 
183  if(!$usr_id = ilObject::_lookupObjIdByImportId($user->getImportId()))
184  {
185  $username = $this->createUser($user);
186  }
187  else
188  {
189  $username = $this->updateUser($user,$usr_id);
190  }
191 
192  $a_auth->setAuth($username);
193  $this->log->write(__METHOD__.': Login succesesful');
194  return true;
195  }
196 
202  public function failedLoginObserver()
203  {
204  $this->log->write(__METHOD__.': Login failed');
205  return false;
206  }
207 
208 
209 
215  protected function createUser(ilECSUser $user)
216  {
217  global $ilClientIniFile, $ilSetting, $rbacadmin, $ilLog;
218 
219  $userObj = new ilObjUser();
220 
221  include_once('./Services/Authentication/classes/class.ilAuthUtils.php');
222  $local_user = ilAuthUtils::_generateLogin($this->getAbreviation() . '_' . $user->getLogin());
223 
224  $newUser["login"] = $local_user;
225  $newUser["firstname"] = $user->getFirstname();
226  $newUser["lastname"] = $user->getLastname();
227  $newUser['email'] = $user->getEmail();
228  $newUser['institution'] = $user->getInstitution();
229 
230  // set "plain md5" password (= no valid password)
231  $newUser["passwd"] = "";
232  $newUser["passwd_type"] = IL_PASSWD_MD5;
233 
234  $newUser["auth_mode"] = "ecs";
235  $newUser["profile_incomplete"] = 0;
236 
237  // system data
238  $userObj->assignData($newUser);
239  $userObj->setTitle($userObj->getFullname());
240  $userObj->setDescription($userObj->getEmail());
241 
242  // set user language to system language
243  $userObj->setLanguage($ilSetting->get("language"));
244 
245  // Time limit
246  $userObj->setTimeLimitOwner(7);
247  $userObj->setTimeLimitUnlimited(0);
248  $userObj->setTimeLimitFrom(time() - 5);
249  $userObj->setTimeLimitUntil(time() + $ilClientIniFile->readVariable("session", "expire"));
250 
251  // Create user in DB
252  $userObj->setOwner(6);
253  $userObj->create();
254  $userObj->setActive(1);
255  $userObj->updateOwner();
256  $userObj->saveAsNew();
257  $userObj->writePrefs();
258 
259  if($global_role = $this->getCurrentServer()->getGlobalRole())
260  {
261  $rbacadmin->assignUser($this->getCurrentServer()->getGlobalRole(), $userObj->getId(), true);
262  }
263  ilObject::_writeImportId($userObj->getId(), $user->getImportId());
264 
265  $ilLog->write(__METHOD__ . ': Created new remote user with usr_id: ' . $user->getImportId());
266 
267  // Send Mail
268  #$this->sendNotification($userObj);
269 
270  return $userObj->getLogin();
271  }
272 
278  protected function updateUser(ilECSUser $user,$a_local_user_id)
279  {
280  global $ilClientIniFile,$ilLog,$rbacadmin;
281 
282  $user_obj = new ilObjUser($a_local_user_id);
283  $user_obj->setFirstname($user->getFirstname());
284  $user_obj->setLastname($user->getLastname());
285  $user_obj->setEmail($user->getEmail());
286  $user_obj->setInstitution($user->getInstitution());
287 
288  $until = $user_obj->getTimeLimitUntil();
289  $user_obj->setTimeLimitFrom(time() - 5);
290 
291  if($until < (time() + $ilClientIniFile->readVariable('session','expire')))
292  {
293  $user_obj->setTimeLimitUntil(time() + $ilClientIniFile->readVariable("session","expire"));
294  }
295  $user_obj->update();
296  $user_obj->refreshLogin();
297 
298  if($global_role = $this->getCurrentServer()->getGlobalRole())
299  {
300  $rbacadmin->assignUser(
301  $this->getCurrentServer()->getGlobalRole(),
302  $user_obj->getId(),
303  true
304  );
305  }
306 
307  $ilLog->write(__METHOD__.': Finished update of remote user with usr_id: '.$user->getImportId());
308  return $user_obj->getLogin();
309  }
310 
311 
318  private function initECSServices()
319  {
320  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
321  $this->servers = ilECSServerSettings::getInstance();
322  }
323 
331  private function sendNotification($user_obj)
332  {
333  if(!count($this->getCurrentServer()->getUserRecipients()))
334  {
335  return true;
336  }
337 
338  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
340  $GLOBALS['lng'] = $lang;
341  $GLOBALS['ilUser'] = $user_obj;
342  $lang->loadLanguageModule('ecs');
343 
344  include_once('./Services/Mail/classes/class.ilMail.php');
345  $mail = new ilMail(6);
346  $mail->enableSoap(false);
347  $subject = $lang->txt('ecs_new_user_subject');
348 
349  // build body
350  $body = $lang->txt('ecs_new_user_body')."\n\n";
351  $body .= $lang->txt('ecs_new_user_profile')."\n\n";
352  $body .= $user_obj->getProfileAsString($lang)."\n\n";
354 
355  $mail->sendMail(
356  $this->getCurrentServer()->getUserRecipientsAsString(),
357  "",
358  "",
359  $subject,
360  $body,
361  array(),
362  array("normal")
363  );
364  }
365 }
366 ?>