ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  $GLOBALS['ilLog']->write(__METHOD__ . ': no active ecs server found. Aborting');
127  return false;
128  }
129 
130  // Iterate through all active ecs instances
131  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
132  foreach ($this->getServerSettings()->getServers() as $server) {
133  $this->setCurrentServer($server);
134  if ($this->validateHash()) {
135  return true;
136  }
137  }
138  $GLOBALS['ilLog']->write(__METHOD__ . ': Could not validate ecs hash for any server');
139  return false;
140  }
141 
142 
151  public function validateHash()
152  {
153  global $ilLog;
154 
155  // fetch hash
156  if (isset($_GET['ecs_hash']) and strlen($_GET['ecs_hash'])) {
157  $hash = $_GET['ecs_hash'];
158  }
159  if (isset($_GET['ecs_hash_url'])) {
160  $hashurl = urldecode($_GET['ecs_hash_url']);
161  $hash = basename(parse_url($hashurl, PHP_URL_PATH));
162  //$hash = urldecode($_GET['ecs_hash_url']);
163  }
164 
165  $GLOBALS['ilLog']->write(__METHOD__ . ': Using ecs hash ' . $hash);
166 
167  // Check if hash is valid ...
168  try {
169  include_once('./Services/WebServices/ECS/classes/class.ilECSConnector.php');
170  $connector = new ilECSConnector($this->getCurrentServer());
171  $res = $connector->getAuth($hash);
172  $auths = $res->getResult();
173 
174  $GLOBALS['ilLog']->write(__METHOD__ . ': Auths: ' . print_r($auths, true));
175 
176  if ($auths->pid) {
177  try {
178  include_once './Services/WebServices/ECS/classes/class.ilECSCommunityReader.php';
180  $part = $reader->getParticipantByMID($auths->pid);
181 
182  if (is_object($part) and is_object($part->getOrganisation())) {
183  $this->abreviation = $part->getOrganisation()->getAbbreviation();
184  } else {
185  $this->abreviation = $auths->abbr;
186  }
187  } catch (Exception $e) {
188  $ilLog->write(__METHOD__ . ': Authentication failed with message: ' . $e->getMessage());
189  return false;
190  }
191  } else {
192  $this->abreviation = $auths->abbr;
193  }
194 
195  $ilLog->write(__METHOD__ . ': Got abr: ' . $this->abreviation);
196  } catch (ilECSConnectorException $e) {
197  $ilLog->write(__METHOD__ . ': Authentication failed with message: ' . $e->getMessage());
198  return false;
199  }
200 
201  // read current mid
202  try {
203  include_once('./Services/WebServices/ECS/classes/class.ilECSConnector.php');
204  $connector = new ilECSConnector($this->getCurrentServer());
205  $details = $connector->getAuth($hash, true);
206 
207  $GLOBALS['ilLog']->write(__METHOD__ . ': ' . print_r($details, true));
208  $GLOBALS['ilLog']->write(__METHOD__ . ': Token created for mid ' . $details->getFirstSender());
209 
210  $this->setMID($details->getFirstSender());
211  } catch (ilECSConnectorException $e) {
212  $ilLog->write(__METHOD__ . ': Receiving mid failed with message: ' . $e->getMessage());
213  return false;
214  }
215  return true;
216  }
217 
223  public function loginObserver($a_username, $a_auth)
224  {
225  include_once('./Services/WebServices/ECS/classes/class.ilECSUser.php');
226 
227  $user = new ilECSUser($_GET);
228 
229  if (!$usr_id = ilObject::_lookupObjIdByImportId($user->getImportId())) {
230  $username = $this->createUser($user);
231  } else {
232  $username = $this->updateUser($user, $usr_id);
233  }
234 
235  // set user imported
236  include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
237  $import = new ilECSImport($this->getCurrentServer()->getServerId(), $usr_id);
238  $import->save();
239 
240  // Store remote user data
241  include_once './Services/WebServices/ECS/classes/class.ilECSRemoteUser.php';
242  $remote = new ilECSRemoteUser();
243  $remote->setServerId($this->getCurrentServer()->getServerId());
244  $remote->setMid($this->getMID());
245  $remote->setRemoteUserId($user->getImportId());
246  $remote->setUserId(ilObjUser::_lookupId($username));
247 
248  $GLOBALS['ilLog']->write(__METHOD__ . ': Current username ' . $username);
249 
250  if (!$remote->exists()) {
251  $remote->create();
252  }
253 
254  $a_auth->setAuth($username);
255  $this->log->write(__METHOD__ . ': Login succesesful');
256  return true;
257  }
258 
264  public function failedLoginObserver()
265  {
266  $this->log->write(__METHOD__ . ': Login failed');
267  return false;
268  }
269 
270 
271 
277  protected function createUser(ilECSUser $user)
278  {
279  global $ilClientIniFile, $ilSetting, $rbacadmin, $ilLog;
280 
281  $userObj = new ilObjUser();
282  $userObj->setOwner(SYSTEM_USER_ID);
283 
284  include_once('./Services/Authentication/classes/class.ilAuthUtils.php');
285  $local_user = ilAuthUtils::_generateLogin($this->getAbreviation() . '_' . $user->getLogin());
286 
287  $newUser["login"] = $local_user;
288  $newUser["firstname"] = $user->getFirstname();
289  $newUser["lastname"] = $user->getLastname();
290  $newUser['email'] = $user->getEmail();
291  $newUser['institution'] = $user->getInstitution();
292 
293  // set "plain md5" password (= no valid password)
294  $newUser["passwd"] = "";
295  $newUser["passwd_type"] = IL_PASSWD_CRYPTED;
296 
297  $newUser["auth_mode"] = "ecs";
298  $newUser["profile_incomplete"] = 0;
299 
300  // system data
301  $userObj->assignData($newUser);
302  $userObj->setTitle($userObj->getFullname());
303  $userObj->setDescription($userObj->getEmail());
304 
305  // set user language to system language
306  $userObj->setLanguage($ilSetting->get("language"));
307 
308  // Time limit
309  $userObj->setTimeLimitOwner(7);
310  $userObj->setTimeLimitUnlimited(0);
311  $userObj->setTimeLimitFrom(time() - 5);
312  $userObj->setTimeLimitUntil(time() + $ilClientIniFile->readVariable("session", "expire"));
313 
314  #$now = new ilDateTime(time(), IL_CAL_UNIX);
315  #$userObj->setAgreeDate($now->get(IL_CAL_DATETIME));
316 
317  // Create user in DB
318  $userObj->setOwner(6);
319  $userObj->create();
320  $userObj->setActive(1);
321  $userObj->updateOwner();
322  $userObj->saveAsNew();
323  $userObj->writePrefs();
324 
325  if ($global_role = $this->getCurrentServer()->getGlobalRole()) {
326  $rbacadmin->assignUser($this->getCurrentServer()->getGlobalRole(), $userObj->getId(), true);
327  }
328  ilObject::_writeImportId($userObj->getId(), $user->getImportId());
329 
330  $ilLog->write(__METHOD__ . ': Created new remote user with usr_id: ' . $user->getImportId());
331 
332  // Send Mail
333  #$this->sendNotification($userObj);
334  $this->resetMailOptions($userObj->getId());
335 
336  return $userObj->getLogin();
337  }
338 
344  protected function updateUser(ilECSUser $user, $a_local_user_id)
345  {
346  global $ilClientIniFile,$ilLog,$rbacadmin;
347 
348  $user_obj = new ilObjUser($a_local_user_id);
349  $user_obj->setFirstname($user->getFirstname());
350  $user_obj->setLastname($user->getLastname());
351  $user_obj->setEmail($user->getEmail());
352  $user_obj->setInstitution($user->getInstitution());
353  $user_obj->setActive(true);
354 
355  $until = $user_obj->getTimeLimitUntil();
356 
357  if ($until < (time() + $ilClientIniFile->readVariable('session', 'expire'))) {
358  $user_obj->setTimeLimitFrom(time() - 60);
359  $user_obj->setTimeLimitUntil(time() + $ilClientIniFile->readVariable("session", "expire"));
360  }
361  $user_obj->update();
362  $user_obj->refreshLogin();
363 
364  if ($global_role = $this->getCurrentServer()->getGlobalRole()) {
365  $rbacadmin->assignUser(
366  $this->getCurrentServer()->getGlobalRole(),
367  $user_obj->getId(),
368  true
369  );
370  }
371 
372  $this->resetMailOptions($a_local_user_id);
373 
374  $ilLog->write(__METHOD__ . ': Finished update of remote user with usr_id: ' . $user->getImportId());
375  return $user_obj->getLogin();
376  }
377 
382  protected function resetMailOptions($a_usr_id)
383  {
384  include_once './Services/Mail/classes/class.ilMailOptions.php';
385  $options = new ilMailOptions($a_usr_id);
386  $options->setIncomingType(ilMailOptions::INCOMING_LOCAL);
387  $options->updateOptions();
388  }
389 
390 
397  private function initECSServices()
398  {
399  include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
400  $this->servers = ilECSServerSettings::getInstance();
401  }
402 
410  private function sendNotification($user_obj)
411  {
412  if (!count($this->getCurrentServer()->getUserRecipients())) {
413  return true;
414  }
415 
416  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
417  include_once './Services/Language/classes/class.ilLanguage.php';
419  $GLOBALS['lng'] = $lang;
420  $GLOBALS['ilUser'] = $user_obj;
421  $lang->loadLanguageModule('ecs');
422 
423  include_once('./Services/Mail/classes/class.ilMail.php');
424  $mail = new ilMail(6);
425  $mail->enableSoap(false);
426  $subject = $lang->txt('ecs_new_user_subject');
427 
428  // build body
429  $body = $lang->txt('ecs_new_user_body') . "\n\n";
430  $body .= $lang->txt('ecs_new_user_profile') . "\n\n";
431  $body .= $user_obj->getProfileAsString($lang) . "\n\n";
433 
434  $mail->sendMail(
435  $this->getCurrentServer()->getUserRecipientsAsString(),
436  "",
437  "",
438  $subject,
439  $body,
440  array(),
441  array("normal")
442  );
443  }
444 }
updateUser(ilECSUser $user, $a_local_user_id)
update existing user
Class ilMailOptions 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.
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.
foreach($_POST as $key=> $value) $res
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.
$server
Definition: getUserInfo.php:12
getEmail()
get email
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.
static _getAutoGeneratedMessageString(ilLanguage $lang=null)
Get auto generated info string.
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)
if(!isset($_REQUEST['ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20
validateHash()
Validate ECS hash.