ILIAS  Release_4_0_x_branch Revision 61816
 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 $log;
40 
48  public function __construct($a_params = array())
49  {
50  parent::__construct($a_params);
51  $this->initECSServices();
52 
53  $this->log = $GLOBALS['ilLog'];
54  }
55 
63  public function getAbreviation()
64  {
65  return $this->abreviation;
66  }
67 
73  public function getMID()
74  {
75  return $this->mid;
76  }
77 
78 
87  public function fetchData($a_username,$a_pass)
88  {
89  global $ilLog;
90 
91  $ilLog->write(__METHOD__.': Starting ECS authentication.');
92 
93  if(!$this->settings->isEnabled())
94  {
95  $ilLog->write(__METHOD__.': ECS settings .');
96  return false;
97  }
98 
99  // Check if hash is valid ...
100  include_once('./Services/WebServices/ECS/classes/class.ilECSConnector.php');
101 
102  try
103  {
104  $connector = new ilECSConnector();
105  $res = $connector->getAuth($_GET['ecs_hash']);
106  $auths = $res->getResult();
107  $this->mid = $auths[0]->mid;
108  $ilLog->write(__METHOD__.': Got mid: '.$this->mid);
109  $this->abreviation = $auths[0]->abr;
110  $ilLog->write(__METHOD__.': Got abr: '.$this->abreviation);
111  /*
112  // Read abbreviation from mid
113  $res = $connector->getMemberships($this->mid);
114  $member = $res->getResult();
115  $this->abbreviation = $member[0]->participants[0]->abr;
116  */
117  return true;
118  }
119  catch(ilECSConnectorException $e)
120  {
121  $ilLog->write(__METHOD__.': Authentication failed with message: '.$e->getMessage());
122  return false;
123  }
124  }
125 
131  public function loginObserver($a_username, $a_auth)
132  {
133  include_once('./Services/WebServices/ECS/classes/class.ilECSUser.php');
134 
135  $user = new ilECSUser($_GET);
136 
137  if(!$usr_id = ilObject::_lookupObjIdByImportId($user->getImportId()))
138  {
139  $username = $this->createUser($user);
140  }
141  else
142  {
143  $username = $this->updateUser($user,$usr_id);
144  }
145 
146  $a_auth->setAuth($username);
147  $this->log->write(__METHOD__.': Login succesesful');
148  return true;
149  }
150 
156  public function failedLoginObserver()
157  {
158  $this->log->write(__METHOD__.': Login failed');
159  }
160 
161 
162 
168  protected function createUser(ilECSUser $user)
169  {
170  global $ilClientIniFile,$ilSetting,$rbacadmin,$ilLog;
171 
172  $userObj = new ilObjUser();
173 
174  include_once('./Services/Authentication/classes/class.ilAuthUtils.php');
175  $local_user = ilAuthUtils::_generateLogin($this->getAbreviation().'_'.$user->getLogin());
176 
177  $newUser["login"] = $local_user;
178  $newUser["firstname"] = $user->getFirstname();
179  $newUser["lastname"] = $user->getLastname();
180  $newUser['email'] = $user->getEmail();
181  $newUser['institution'] = $user->getInstitution();
182 
183  // set "plain md5" password (= no valid password)
184  $newUser["passwd"] = "";
185  $newUser["passwd_type"] = IL_PASSWD_MD5;
186 
187  $newUser["auth_mode"] = "ecs";
188  $newUser["profile_incomplete"] = 0;
189 
190  // system data
191  $userObj->assignData($newUser);
192  $userObj->setTitle($userObj->getFullname());
193  $userObj->setDescription($userObj->getEmail());
194 
195  // set user language to system language
196  $userObj->setLanguage($ilSetting->get("language"));
197 
198  // Time limit
199  $userObj->setTimeLimitOwner(7);
200  $userObj->setTimeLimitUnlimited(0);
201  $userObj->setTimeLimitFrom(time());
202  $userObj->setTimeLimitUntil(time() + $ilClientIniFile->readVariable("session","expire"));
203 
204  // Create user in DB
205  $userObj->setOwner(6);
206  $userObj->create();
207  $userObj->setActive(1);
208  $userObj->updateOwner();
209  $userObj->saveAsNew();
210  $userObj->writePrefs();
211 
212  $this->initSettings();
213  if($global_role = $this->settings->getGlobalRole())
214  {
215  $rbacadmin->assignUser($this->settings->getGlobalRole(),$userObj->getId(),true);
216  }
217  ilObject::_writeImportId($userObj->getId(),$user->getImportId());
218 
219  $ilLog->write(__METHOD__.': Created new remote user with usr_id: '.$user->getImportId());
220 
221  // Send Mail
222  #$this->sendNotification($userObj);
223 
224  return $userObj->getLogin();
225  }
226 
232  protected function updateUser(ilECSUser $user,$a_local_user_id)
233  {
234  global $ilClientIniFile,$ilLog,$rbacadmin;
235 
236  $user_obj = new ilObjUser($a_local_user_id);
237  $user_obj->setFirstname($user->getFirstname());
238  $user_obj->setLastname($user->getLastname());
239  $user_obj->setEmail($user->getEmail());
240  $user_obj->setInstitution($user->getInstitution());
241 
242  $until = $user_obj->getTimeLimitUntil();
243 
244  if($until < (time() + $ilClientIniFile->readVariable('session','expire')))
245  {
246  $user_obj->setTimeLimitFrom(time());
247  $user_obj->setTimeLimitUntil(time() + $ilClientIniFile->readVariable("session","expire"));
248  }
249  $user_obj->update();
250  $user_obj->refreshLogin();
251 
252  $this->initSettings();
253  if($global_role = $this->settings->getGlobalRole())
254  {
255  $rbacadmin->assignUser($this->settings->getGlobalRole(),$user_obj->getId(),true);
256  }
257 
258  $ilLog->write(__METHOD__.': Finished update of remote user with usr_id: '.$user->getImportId());
259  return $user_obj->getLogin();
260  }
261 
262 
269  private function initSettings()
270  {
271  include_once('./Services/WebServices/ECS/classes/class.ilECSSettings.php');
272  $this->settings = ilECSSettings::_getInstance();
273  }
274 
275 
282  private function initECSServices()
283  {
284  include_once('./Services/WebServices/ECS/classes/class.ilECSSettings.php');
285  $this->settings = ilECSSettings::_getInstance();
286  }
287 
295  private function sendNotification($user_obj)
296  {
297  if(!count($this->settings->getUserRecipients()))
298  {
299  return true;
300  }
301 
302  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
304  $GLOBALS['lng'] = $lang;
305  $GLOBALS['ilUser'] = $user_obj;
306  $lang->loadLanguageModule('ecs');
307 
308  include_once('./Services/Mail/classes/class.ilMail.php');
309  $mail = new ilMail(6);
310  $mail->enableSoap(false);
311  $subject = $lang->txt('ecs_new_user_subject');
312 
313  // build body
314  $body = $lang->txt('ecs_new_user_body')."\n\n";
315  $body .= $lang->txt('ecs_new_user_profile')."\n\n";
316  $body .= $user_obj->getProfileAsString($lang)."\n\n";
318 
319  $mail->sendMail($this->settings->getUserRecipientsAsString(),"","",$subject,$body,array(),array("normal"));
320  }
321 }
322 
323 
324 ?>