ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilAuthProviderECS.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5include_once './Services/Authentication/classes/Provider/class.ilAuthProvider.php';
6include_once './Services/Authentication/interfaces/interface.ilAuthProviderInterface.php';
7
15{
16 protected $mid = null;
17 protected $abreviation = null;
18
19 protected $currentServer = null;
20 protected $servers = null;
21
22
28 {
29 parent::__construct($credentials);
30
31 $this->initECSServices();
32 }
33
41 public function getAbreviation()
42 {
43 return $this->abreviation;
44 }
45
51 public function getMID()
52 {
53 return $this->mid;
54 }
55
56 public function setMID($a_mid)
57 {
58 $this->mid = $a_mid;
59 }
60
65 public function setCurrentServer(ilECSSetting $server = null)
66 {
67 $this->currentServer = $server;
68 }
69
74 public function getCurrentServer()
75 {
77 }
78
83 public function getServerSettings()
84 {
85 return $this->servers;
86 }
87
88
95 {
96 $this->getLogger()->debug('Starting ECS authentication');
97 if (!$this->getServerSettings()->activeServerExists()) {
98 $this->getLogger()->warning('No active ecs server found. Aborting');
99 $this->handleAuthenticationFail($status, 'err_wrong_login');
100 return false;
101 }
102
103 // Iterate through all active ecs instances
104 include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
105 foreach ($this->getServerSettings()->getServers() as $server) {
106 $this->setCurrentServer($server);
107 if ($this->validateHash()) {
108 // handle successful authentication
109 $new_usr_id = $this->handleLogin();
110 $this->getLogger()->info('ECS authentication successful.');
112 $status->setAuthenticatedUserId($new_usr_id);
113 return true;
114 }
115 }
116
117 $this->getLogger()->warning('Could not validate ecs hash for any active server.');
118 $this->handleAuthenticationFail($status, 'err_wrong_login');
119 return false;
120 }
121
122
128 public function handleLogin()
129 {
130 include_once('./Services/WebServices/ECS/classes/class.ilECSUser.php');
131
132 $user = new ilECSUser($_GET);
133
134 if (!$usr_id = ilObject::_lookupObjIdByImportId($user->getImportId())) {
135 $username = $this->createUser($user);
136 } else {
137 $username = $this->updateUser($user, $usr_id);
138 }
139
140 // set user imported
141 include_once './Services/WebServices/ECS/classes/class.ilECSImport.php';
142 $import = new ilECSImport($this->getCurrentServer()->getServerId(), $usr_id);
143 $import->save();
144
145 // Store remote user data
146 include_once './Services/WebServices/ECS/classes/class.ilECSRemoteUser.php';
147 $remote = new ilECSRemoteUser();
148 $remote->setServerId($this->getCurrentServer()->getServerId());
149 $remote->setMid($this->getMID());
150 $remote->setRemoteUserId($user->getImportId());
151 $remote->setUserId(ilObjUser::_lookupId($username));
152
153 $this->getLogger()->info('Current user is: ' . $username);
154
155 if (!$remote->exists()) {
156 $remote->create();
157 }
158 return ilObjUser::_lookupId($username);
159 }
160
161
170 public function validateHash()
171 {
172 global $ilLog;
173
174 // fetch hash
175 if (isset($_GET['ecs_hash']) and strlen($_GET['ecs_hash'])) {
176 $hash = $_GET['ecs_hash'];
177 }
178 if (isset($_GET['ecs_hash_url'])) {
179 $hashurl = urldecode($_GET['ecs_hash_url']);
180 $hash = basename(parse_url($hashurl, PHP_URL_PATH));
181 //$hash = urldecode($_GET['ecs_hash_url']);
182 }
183
184 $this->getLogger()->info('Using ecs hash: ' . $hash);
185 // Check if hash is valid ...
186 try {
187 include_once('./Services/WebServices/ECS/classes/class.ilECSConnector.php');
188 $connector = new ilECSConnector($this->getCurrentServer());
189 $res = $connector->getAuth($hash);
190 $auths = $res->getResult();
191
192 $this->getLogger()->dump($auths, ilLogLevel::DEBUG);
193
194 if ($auths->pid) {
195 try {
196 include_once './Services/WebServices/ECS/classes/class.ilECSCommunityReader.php';
198 foreach ($reader->getParticipantsByPid($auths->pid) as $participant) {
199 if ($participant->getOrganisation() instanceof \ilECSOrganisation) {
200 $this->abreviation = $participant->getOrganisation()->getAbbreviation();
201 break;
202 }
203 }
204 if (!$this->abreviation) {
205 $this->abreviation = $auths->abbr;
206 }
207 } catch (Exception $e) {
208 $this->getLogger()->warning('Authentication failed with message: ' . $e->getMessage());
209 return false;
210 }
211 } else {
212 $this->abreviation = $auths->abbr;
213 }
214
215 $this->getLogger()->debug('Got abbreviation: ' . $this->abreviation);
216 } catch (ilECSConnectorException $e) {
217 $this->getLogger()->warning('Authentication failed with message: ' . $e->getMessage());
218 return false;
219 }
220
221 // read current mid
222 try {
223 include_once('./Services/WebServices/ECS/classes/class.ilECSConnector.php');
224 $connector = new ilECSConnector($this->getCurrentServer());
225 $details = $connector->getAuth($hash, true);
226
227 $this->getLogger()->dump($details, ilLogLevel::DEBUG);
228 $this->getLogger()->debug('Token create for mid: ' . $details->getFirstSender());
229
230 $this->setMID($details->getFirstSender());
231 } catch (ilECSConnectorException $e) {
232 $this->getLogger()->warning('Receiving mid failed with message: ' . $e->getMessage());
233 return false;
234 }
235 return true;
236 }
237
238
245 private function initECSServices()
246 {
247 include_once './Services/WebServices/ECS/classes/class.ilECSServerSettings.php';
248 $this->servers = ilECSServerSettings::getInstance();
249 }
250
256 protected function createUser(ilECSUser $user)
257 {
258 global $ilClientIniFile, $ilSetting, $rbacadmin, $ilLog;
259
260 $userObj = new ilObjUser();
261 $userObj->setOwner(SYSTEM_USER_ID);
262
263 include_once('./Services/Authentication/classes/class.ilAuthUtils.php');
264 $local_user = ilAuthUtils::_generateLogin($this->getAbreviation() . '_' . $user->getLogin());
265
266 $newUser["login"] = $local_user;
267 $newUser["firstname"] = $user->getFirstname();
268 $newUser["lastname"] = $user->getLastname();
269 $newUser['email'] = $user->getEmail();
270 $newUser['institution'] = $user->getInstitution();
271
272 // set "plain md5" password (= no valid password)
273 $newUser["passwd"] = "";
274 $newUser["passwd_type"] = IL_PASSWD_CRYPTED;
275
276 $newUser["auth_mode"] = "ecs";
277 $newUser["profile_incomplete"] = 0;
278
279 // system data
280 $userObj->assignData($newUser);
281 $userObj->setTitle($userObj->getFullname());
282 $userObj->setDescription($userObj->getEmail());
283
284 // set user language to system language
285 $userObj->setLanguage($ilSetting->get("language"));
286
287 // Time limit
288 $userObj->setTimeLimitOwner(7);
289 $userObj->setTimeLimitUnlimited(0);
290 $userObj->setTimeLimitFrom(time() - 5);
291 $userObj->setTimeLimitUntil(time() + $ilClientIniFile->readVariable("session", "expire"));
292
293 #$now = new ilDateTime(time(), IL_CAL_UNIX);
294 #$userObj->setAgreeDate($now->get(IL_CAL_DATETIME));
295
296 // Create user in DB
297 $userObj->setOwner(6);
298 $userObj->create();
299 $userObj->setActive(1);
300 $userObj->updateOwner();
301 $userObj->saveAsNew();
302 $userObj->writePrefs();
303
304 if ($global_role = $this->getCurrentServer()->getGlobalRole()) {
305 $rbacadmin->assignUser($this->getCurrentServer()->getGlobalRole(), $userObj->getId(), true);
306 }
307 ilObject::_writeImportId($userObj->getId(), $user->getImportId());
308
309 $this->getLogger()->info('Created new remote user with usr_id: ' . $user->getImportId());
310
311 // Send Mail
312 #$this->sendNotification($userObj);
313 $this->resetMailOptions($userObj->getId());
314
315 return $userObj->getLogin();
316 }
317
323 protected function updateUser(ilECSUser $user, $a_local_user_id)
324 {
325 global $ilClientIniFile,$ilLog,$rbacadmin;
326
327 $user_obj = new ilObjUser($a_local_user_id);
328 $user_obj->setFirstname($user->getFirstname());
329 $user_obj->setLastname($user->getLastname());
330 $user_obj->setEmail($user->getEmail());
331 $user_obj->setInstitution($user->getInstitution());
332 $user_obj->setActive(true);
333
334 $until = $user_obj->getTimeLimitUntil();
335
336 if ($until < (time() + $ilClientIniFile->readVariable('session', 'expire'))) {
337 $user_obj->setTimeLimitFrom(time() - 60);
338 $user_obj->setTimeLimitUntil(time() + $ilClientIniFile->readVariable("session", "expire"));
339 }
340 $user_obj->update();
341 $user_obj->refreshLogin();
342
343 if ($global_role = $this->getCurrentServer()->getGlobalRole()) {
344 $rbacadmin->assignUser(
345 $this->getCurrentServer()->getGlobalRole(),
346 $user_obj->getId(),
347 true
348 );
349 }
350
351 $this->resetMailOptions($a_local_user_id);
352
353 $this->getLogger()->debug('Finished update of remote user with usr_id: ' . $user->getImportId());
354 return $user_obj->getLogin();
355 }
356
361 protected function resetMailOptions($a_usr_id)
362 {
363 include_once './Services/Mail/classes/class.ilMailOptions.php';
364 $options = new ilMailOptions($a_usr_id);
365 $options->setIncomingType(ilMailOptions::INCOMING_LOCAL);
366 $options->updateOptions();
367 }
368}
if(!isset( $_REQUEST[ 'ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20
$_GET["client_id"]
An exception for terminatinating execution or to throw for unit testing.
const IL_PASSWD_CRYPTED
Auth prvider for ecs auth.
doAuthentication(\ilAuthStatus $status)
Tra ecs authentication.
__construct(\ilAuthCredentials $credentials)
Constructor.
getServerSettings()
Get server settings.
getAbreviation()
get abbreviation
createUser(ilECSUser $user)
create new user
initECSServices()
Init ECS Services @access private.
handleLogin()
Called from base class after successful login.
getCurrentServer()
Get current server.
updateUser(ilECSUser $user, $a_local_user_id)
update existing user
validateHash()
Validate ECS hash.
resetMailOptions($a_usr_id)
Reset mail options to "local only".
setCurrentServer(ilECSSetting $server=null)
Set current server.
Base class for authentication providers (radius, ldap, apache, ...)
getLogger()
Get logger.
handleAuthenticationFail(ilAuthStatus $status, $a_reason)
Handle failed authentication.
Auth status implementation.
setStatus($a_status)
Set auth status.
static _generateLogin($a_login)
generate free login by starting with a default string and adding postfix numbers
static getInstanceByServerId($a_server_id)
Get instance by server id.
Storage of ECS imported objects.
Storage of ecs remote user.
static getInstance()
Get singleton instance.
Stores relevant user data.
getFirstname()
get firstname
getLastname()
getLastname
getLogin()
get login
getImportId()
get Email
getEmail()
get email
getInstitution()
get institution
Class ilMailOptions this class handles user mails.
static _lookupId($a_user_str)
Lookup id by login.
static _writeImportId($a_obj_id, $a_import_id)
write import id to db (static)
static _lookupObjIdByImportId($a_import_id)
$server
Definition: getUserInfo.php:12
Interface of auth credentials.
Standard interface for auth provider implementations.
global $ilSetting
Definition: privfeed.php:17
foreach($_POST as $key=> $value) $res