ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
82  public function setMID($a_mid)
83  {
84  $this->mid = $a_mid;
85  }
86 
91  public function setCurrentServer(ilECSSetting $server = null)
92  {
93  $this->currentServer = $server;
94  }
95 
100  public function getCurrentServer()
101  {
102  return $this->currentServer;
103  }
104 
109  public function getServerSettings()
110  {
111  return $this->servers;
112  }
113 
119  public function fetchData($a_username,$a_pass)
120  {
121  global $ilLog;
122 
123  $ilLog->write(__METHOD__.': Starting ECS authentication.');
124 
125  if(!$this->getServerSettings()->activeServerExists())
126  {
127  $GLOBALS['ilLog']->write(__METHOD__.': no active ecs server found. Aborting');
128  return false;
129  }
130 
131  // Iterate through all active ecs instances
132  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
133  foreach($this->getServerSettings()->getServers() as $server)
134  {
135  $this->setCurrentServer($server);
136  if($this->validateHash())
137  {
138  return true;
139  }
140  }
141  $GLOBALS['ilLog']->write(__METHOD__.': Could not validate ecs hash for any server');
142  return false;
143 
144  }
145 
146 
155  public function validateHash()
156  {
157  global $ilLog;
158 
159  // fetch hash
160  if(isset($_GET['ecs_hash']) and strlen($_GET['ecs_hash']))
161  {
162  $hash = $_GET['ecs_hash'];
163  }
164  if(isset($_GET['ecs_hash_url']))
165  {
166  $hashurl = urldecode($_GET['ecs_hash_url']);
167  $hash = basename(parse_url($hashurl,PHP_URL_PATH));
168  //$hash = urldecode($_GET['ecs_hash_url']);
169  }
170 
171  $GLOBALS['ilLog']->write(__METHOD__.': Using ecs hash '. $hash);
172 
173  // Check if hash is valid ...
174  try
175  {
176  include_once('./Services/WebServices/ECS/classes/class.ilECSConnector.php');
177  $connector = new ilECSConnector($this->getCurrentServer());
178  $res = $connector->getAuth($hash);
179  $auths = $res->getResult();
180 
181  $GLOBALS['ilLog']->write(__METHOD__.': Auths: '.print_r($auths,TRUE));
182 
183  if($auths->pid)
184  {
185  try
186  {
187  include_once './Services/WebServices/ECS/classes/class.ilECSCommunityReader.php';
189  $part = $reader->getParticipantByMID($auths->pid);
190 
191  if(is_object($part) and is_object($part->getOrganisation()))
192  {
193  $this->abreviation = $part->getOrganisation()->getAbbreviation();
194  }
195  else
196  {
197  $this->abreviation = $auths->abbr;
198  }
199  }
200  catch(Exception $e)
201  {
202  $ilLog->write(__METHOD__.': Authentication failed with message: '.$e->getMessage());
203  return false;
204  }
205  }
206  else
207  {
208  $this->abreviation = $auths->abbr;
209  }
210 
211  $ilLog->write(__METHOD__.': Got abr: '.$this->abreviation);
212  }
213  catch(ilECSConnectorException $e)
214  {
215  $ilLog->write(__METHOD__.': Authentication failed with message: '.$e->getMessage());
216  return false;
217  }
218 
219  // read current mid
220  try
221  {
222  include_once('./Services/WebServices/ECS/classes/class.ilECSConnector.php');
223  $connector = new ilECSConnector($this->getCurrentServer());
224  $details = $connector->getAuth($hash,TRUE);
225 
226  $GLOBALS['ilLog']->write(__METHOD__.': '.print_r($details,TRUE));
227  $GLOBALS['ilLog']->write(__METHOD__.': Token created for mid '. $details->getFirstSender());
228 
229  $this->setMID($details->getFirstSender());
230  }
231  catch(ilECSConnectorException $e)
232  {
233  $ilLog->write(__METHOD__.': Receiving mid failed with message: '.$e->getMessage());
234  return false;
235  }
236  return TRUE;
237  }
238 
244  public function loginObserver($a_username, $a_auth)
245  {
246  include_once('./Services/WebServices/ECS/classes/class.ilECSUser.php');
247 
248  $user = new ilECSUser($_GET);
249 
250  if(!$usr_id = ilObject::_lookupObjIdByImportId($user->getImportId()))
251  {
252  $username = $this->createUser($user);
253  }
254  else
255  {
256  $username = $this->updateUser($user,$usr_id);
257  }
258 
259  // set user imported
260  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
261  $import = new ilECSImport($this->getCurrentServer()->getServerId(), $usr_id);
262  $import->save();
263 
264  // Store remote user data
265  include_once './Services/WebServices/ECS/classes/class.ilECSRemoteUser.php';
266  $remote = new ilECSRemoteUser();
267  $remote->setServerId($this->getCurrentServer()->getServerId());
268  $remote->setMid($this->getMID());
269  $remote->setRemoteUserId($user->getImportId());
270  $remote->setUserId(ilObjUser::_lookupId($username));
271 
272  $GLOBALS['ilLog']->write(__METHOD__.': Current username '.$username);
273 
274  if(!$remote->exists())
275  {
276  $remote->create();
277  }
278 
279  $a_auth->setAuth($username);
280  $this->log->write(__METHOD__.': Login succesesful');
281  return true;
282  }
283 
289  public function failedLoginObserver()
290  {
291  $this->log->write(__METHOD__.': Login failed');
292  return false;
293  }
294 
295 
296 
302  protected function createUser(ilECSUser $user)
303  {
304  global $ilClientIniFile, $ilSetting, $rbacadmin, $ilLog;
305 
306  $userObj = new ilObjUser();
307  $userObj->setOwner(SYSTEM_USER_ID);
308 
309  include_once('./Services/Authentication/classes/class.ilAuthUtils.php');
310  $local_user = ilAuthUtils::_generateLogin($this->getAbreviation() . '_' . $user->getLogin());
311 
312  $newUser["login"] = $local_user;
313  $newUser["firstname"] = $user->getFirstname();
314  $newUser["lastname"] = $user->getLastname();
315  $newUser['email'] = $user->getEmail();
316  $newUser['institution'] = $user->getInstitution();
317 
318  // set "plain md5" password (= no valid password)
319  $newUser["passwd"] = "";
320  $newUser["passwd_type"] = IL_PASSWD_CRYPTED;
321 
322  $newUser["auth_mode"] = "ecs";
323  $newUser["profile_incomplete"] = 0;
324 
325  // system data
326  $userObj->assignData($newUser);
327  $userObj->setTitle($userObj->getFullname());
328  $userObj->setDescription($userObj->getEmail());
329 
330  // set user language to system language
331  $userObj->setLanguage($ilSetting->get("language"));
332 
333  // Time limit
334  $userObj->setTimeLimitOwner(7);
335  $userObj->setTimeLimitUnlimited(0);
336  $userObj->setTimeLimitFrom(time() - 5);
337  $userObj->setTimeLimitUntil(time() + $ilClientIniFile->readVariable("session", "expire"));
338 
339  #$now = new ilDateTime(time(), IL_CAL_UNIX);
340  #$userObj->setAgreeDate($now->get(IL_CAL_DATETIME));
341 
342  // Create user in DB
343  $userObj->setOwner(6);
344  $userObj->create();
345  $userObj->setActive(1);
346  $userObj->updateOwner();
347  $userObj->saveAsNew();
348  $userObj->writePrefs();
349 
350  if($global_role = $this->getCurrentServer()->getGlobalRole())
351  {
352  $rbacadmin->assignUser($this->getCurrentServer()->getGlobalRole(), $userObj->getId(), true);
353  }
354  ilObject::_writeImportId($userObj->getId(), $user->getImportId());
355 
356  $ilLog->write(__METHOD__ . ': Created new remote user with usr_id: ' . $user->getImportId());
357 
358  // Send Mail
359  #$this->sendNotification($userObj);
360  $this->resetMailOptions($userObj->getId());
361 
362  return $userObj->getLogin();
363  }
364 
370  protected function updateUser(ilECSUser $user,$a_local_user_id)
371  {
372  global $ilClientIniFile,$ilLog,$rbacadmin;
373 
374  $user_obj = new ilObjUser($a_local_user_id);
375  $user_obj->setFirstname($user->getFirstname());
376  $user_obj->setLastname($user->getLastname());
377  $user_obj->setEmail($user->getEmail());
378  $user_obj->setInstitution($user->getInstitution());
379  $user_obj->setActive(true);
380 
381  $until = $user_obj->getTimeLimitUntil();
382 
383  if($until < (time() + $ilClientIniFile->readVariable('session','expire')))
384  {
385  $user_obj->setTimeLimitFrom(time() - 60);
386  $user_obj->setTimeLimitUntil(time() + $ilClientIniFile->readVariable("session","expire"));
387  }
388  $user_obj->update();
389  $user_obj->refreshLogin();
390 
391  if($global_role = $this->getCurrentServer()->getGlobalRole())
392  {
393  $rbacadmin->assignUser(
394  $this->getCurrentServer()->getGlobalRole(),
395  $user_obj->getId(),
396  true
397  );
398  }
399 
400  $this->resetMailOptions($a_local_user_id);
401 
402  $ilLog->write(__METHOD__.': Finished update of remote user with usr_id: '.$user->getImportId());
403  return $user_obj->getLogin();
404  }
405 
410  protected function resetMailOptions($a_usr_id)
411  {
412  include_once './Services/Mail/classes/class.ilMailOptions.php';
413  $options = new ilMailOptions($a_usr_id);
414  $options->updateOptions(
415  $options->getSignature(),
416  $options->getLinebreak(),
418  $options->getCronjobNotification()
419  );
420  }
421 
422 
429  private function initECSServices()
430  {
431  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
432  $this->servers = ilECSServerSettings::getInstance();
433  }
434 
442  private function sendNotification($user_obj)
443  {
444  if(!count($this->getCurrentServer()->getUserRecipients()))
445  {
446  return true;
447  }
448 
449  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
450  include_once './Services/Language/classes/class.ilLanguage.php';
452  $GLOBALS['lng'] = $lang;
453  $GLOBALS['ilUser'] = $user_obj;
454  $lang->loadLanguageModule('ecs');
455 
456  include_once('./Services/Mail/classes/class.ilMail.php');
457  $mail = new ilMail(6);
458  $mail->enableSoap(false);
459  $subject = $lang->txt('ecs_new_user_subject');
460 
461  // build body
462  $body = $lang->txt('ecs_new_user_body')."\n\n";
463  $body .= $lang->txt('ecs_new_user_profile')."\n\n";
464  $body .= $user_obj->getProfileAsString($lang)."\n\n";
465  $body .= ilMail::_getAutoGeneratedMessageString($lang);
466 
467  $mail->sendMail(
468  $this->getCurrentServer()->getUserRecipientsAsString(),
469  "",
470  "",
471  $subject,
472  $body,
473  array(),
474  array("normal")
475  );
476  }
477 }
478 ?>
updateUser(ilECSUser $user, $a_local_user_id)
update existing user
Class UserMail this class handles user mails.
getFirstname()
get firstname
getAbreviation()
get abbreviation
$_GET["client_id"]
static getInstance()
Get singleton instance.
const IL_PASSWD_CRYPTED
static _generateLogin($a_login)
generate free login by starting with a default string and adding postfix numbers
Storage of ecs remote user.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
static _lookupId($a_user_str)
Lookup id by login.
const IL_MAIL_LOCAL
getServerSettings()
Get server settings.
createUser(ilECSUser $user)
create new user
static getInstanceByServerId($a_server_id)
Get instance by server id.
sendNotification($user_obj)
Send notification.
static _getLanguage($a_lang_key='')
Get langauge object.
if(!is_array($argv)) $options
getLastname()
getLastname
This class handles base functions for mail handling.
failedLoginObserver()
Called from base class after failed login.
getCurrentServer()
Get current server.
Storage of ECS imported objects.
Create styles array
The data for the language used.
getEmail()
get email
$server
getLogin()
get login
resetMailOptions($a_usr_id)
Reset mail options to "local only".
getImportId()
get Email
global $ilSetting
Definition: privfeed.php:17
static _writeImportId($a_obj_id, $a_import_id)
write import id to db (static)
loginObserver($a_username, $a_auth)
Called from base class after successful login.
for($i=1; $i<=count($kw_cases_sel); $i+=1) $lang
Definition: langwiz.php:349
fetchData($a_username, $a_pass)
Check for valid ecs_hash.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
setCurrentServer(ilECSSetting $server=null)
Set current server.
initECSServices()
Init ECS Services private.
__construct($a_params=array())
Constructor.
getInstitution()
get institution
Custom PEAR Auth Container for ECS auth checks.
Stores relevant user data.
static _lookupObjIdByImportId($a_import_id)
validateHash()
Validate ECS hash.