ILIAS  Release_4_1_x_branch Revision 61804
 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  return false;
160  }
161 
162 
163 
169  protected function createUser(ilECSUser $user)
170  {
171  global $ilClientIniFile,$ilSetting,$rbacadmin,$ilLog;
172 
173  $userObj = new ilObjUser();
174 
175  include_once('./Services/Authentication/classes/class.ilAuthUtils.php');
176  $local_user = ilAuthUtils::_generateLogin($this->getAbreviation().'_'.$user->getLogin());
177 
178  $newUser["login"] = $local_user;
179  $newUser["firstname"] = $user->getFirstname();
180  $newUser["lastname"] = $user->getLastname();
181  $newUser['email'] = $user->getEmail();
182  $newUser['institution'] = $user->getInstitution();
183 
184  // set "plain md5" password (= no valid password)
185  $newUser["passwd"] = "";
186  $newUser["passwd_type"] = IL_PASSWD_MD5;
187 
188  $newUser["auth_mode"] = "ecs";
189  $newUser["profile_incomplete"] = 0;
190 
191  // system data
192  $userObj->assignData($newUser);
193  $userObj->setTitle($userObj->getFullname());
194  $userObj->setDescription($userObj->getEmail());
195 
196  // set user language to system language
197  $userObj->setLanguage($ilSetting->get("language"));
198 
199  // Time limit
200  $userObj->setTimeLimitOwner(7);
201  $userObj->setTimeLimitUnlimited(0);
202  $userObj->setTimeLimitFrom(time());
203  $userObj->setTimeLimitUntil(time() + $ilClientIniFile->readVariable("session","expire"));
204 
205  // Create user in DB
206  $userObj->setOwner(6);
207  $userObj->create();
208  $userObj->setActive(1);
209  $userObj->updateOwner();
210  $userObj->saveAsNew();
211  $userObj->writePrefs();
212 
213  $this->initSettings();
214  if($global_role = $this->settings->getGlobalRole())
215  {
216  $rbacadmin->assignUser($this->settings->getGlobalRole(),$userObj->getId(),true);
217  }
218  ilObject::_writeImportId($userObj->getId(),$user->getImportId());
219 
220  $ilLog->write(__METHOD__.': Created new remote user with usr_id: '.$user->getImportId());
221 
222  // Send Mail
223  #$this->sendNotification($userObj);
224 
225  return $userObj->getLogin();
226  }
227 
233  protected function updateUser(ilECSUser $user,$a_local_user_id)
234  {
235  global $ilClientIniFile,$ilLog,$rbacadmin;
236 
237  $user_obj = new ilObjUser($a_local_user_id);
238  $user_obj->setFirstname($user->getFirstname());
239  $user_obj->setLastname($user->getLastname());
240  $user_obj->setEmail($user->getEmail());
241  $user_obj->setInstitution($user->getInstitution());
242 
243  $until = $user_obj->getTimeLimitUntil();
244 
245  if($until < (time() + $ilClientIniFile->readVariable('session','expire')))
246  {
247  $user_obj->setTimeLimitFrom(time());
248  $user_obj->setTimeLimitUntil(time() + $ilClientIniFile->readVariable("session","expire"));
249  }
250  $user_obj->update();
251  $user_obj->refreshLogin();
252 
253  $this->initSettings();
254  if($global_role = $this->settings->getGlobalRole())
255  {
256  $rbacadmin->assignUser($this->settings->getGlobalRole(),$user_obj->getId(),true);
257  }
258 
259  $ilLog->write(__METHOD__.': Finished update of remote user with usr_id: '.$user->getImportId());
260  return $user_obj->getLogin();
261  }
262 
263 
270  private function initSettings()
271  {
272  include_once('./Services/WebServices/ECS/classes/class.ilECSSettings.php');
273  $this->settings = ilECSSettings::_getInstance();
274  }
275 
276 
283  private function initECSServices()
284  {
285  include_once('./Services/WebServices/ECS/classes/class.ilECSSettings.php');
286  $this->settings = ilECSSettings::_getInstance();
287  }
288 
296  private function sendNotification($user_obj)
297  {
298  if(!count($this->settings->getUserRecipients()))
299  {
300  return true;
301  }
302 
303  include_once('./Services/Language/classes/class.ilLanguageFactory.php');
305  $GLOBALS['lng'] = $lang;
306  $GLOBALS['ilUser'] = $user_obj;
307  $lang->loadLanguageModule('ecs');
308 
309  include_once('./Services/Mail/classes/class.ilMail.php');
310  $mail = new ilMail(6);
311  $mail->enableSoap(false);
312  $subject = $lang->txt('ecs_new_user_subject');
313 
314  // build body
315  $body = $lang->txt('ecs_new_user_body')."\n\n";
316  $body .= $lang->txt('ecs_new_user_profile')."\n\n";
317  $body .= $user_obj->getProfileAsString($lang)."\n\n";
319 
320  $mail->sendMail($this->settings->getUserRecipientsAsString(),"","",$subject,$body,array(),array("normal"));
321  }
322 }
323 
324 
325 ?>