ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAuthContainerCAS.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once 'Auth/Container.php';
5 
6 
15 {
16 
17 
18  protected $server_version = null;
19  protected $server_hostname = null;
20  protected $server_port = null;
21  protected $server_uri = null;
22 
23 
26  public function __construct()
27  {
29  $this->initCAS();
30  }
31 
39  public function forceAuthentication($username,$status,$auth)
40  {
41  global $PHPCAS_CLIENT,$ilLog;
42 
43  if(!$PHPCAS_CLIENT->isAuthenticated())
44  {
45  $PHPCAS_CLIENT->forceAuthentication();
46  }
47  }
48 
52  public function loginObserver($a_username, $a_auth)
53  {
54  global $ilias, $rbacadmin, $ilSetting,$ilLog,$PHPCAS_CLIENT;
55 
56  $ilLog->write(__METHOD__.': Successful CAS login.');
57 
58  // Radius with ldap as data source
59  include_once './Services/LDAP/classes/class.ilLDAPServer.php';
61  {
62  return $this->handleLDAPDataSource($a_auth,$a_username);
63  }
64 
65  include_once("./Services/CAS/lib/CAS.php");
66  if ($PHPCAS_CLIENT->getUser() != "")
67  {
68  $username = $PHPCAS_CLIENT->getUser();
69  $ilLog->write(__METHOD__.': Username: '.$username);
70 
71  // Authorize this user
72  include_once('./Services/User/classes/class.ilObjUser.php');
73  $local_user = ilObjUser::_checkExternalAuthAccount("cas", $username);
74 
75  if ($local_user != "")
76  {
77  $a_auth->setAuth($local_user);
78  }
79  else
80  {
81  if (!$ilSetting->get("cas_create_users"))
82  {
83  $a_auth->status = AUTH_CAS_NO_ILIAS_USER;
84  $a_auth->logout();
85  return false;
86  }
87 
88  $userObj = new ilObjUser();
89 
90  $local_user = ilAuthUtils::_generateLogin($username);
91 
92  $newUser["firstname"] = $local_user;
93  $newUser["lastname"] = "";
94 
95  $newUser["login"] = $local_user;
96 
97  // set "plain md5" password (= no valid password)
98  $newUser["passwd"] = "";
99  $newUser["passwd_type"] = IL_PASSWD_MD5;
100 
101  //$newUser["gender"] = "m";
102  $newUser["auth_mode"] = "cas";
103  $newUser["ext_account"] = $username;
104  $newUser["profile_incomplete"] = 1;
105 
106  // system data
107  $userObj->assignData($newUser);
108  $userObj->setTitle($userObj->getFullname());
109  $userObj->setDescription($userObj->getEmail());
110 
111  // set user language to system language
112  $userObj->setLanguage($ilSetting->get("language"));
113 
114  // Time limit
115  $userObj->setTimeLimitOwner(7);
116  $userObj->setTimeLimitUnlimited(1);
117  $userObj->setTimeLimitFrom(time());
118  $userObj->setTimeLimitUntil(time());
119 
120  // Create user in DB
121  $userObj->setOwner(0);
122  $userObj->create();
123  $userObj->setActive(1);
124 
125  $userObj->updateOwner();
126 
127  //insert user data in table user_data
128  $userObj->saveAsNew();
129 
130  // setup user preferences
131  $userObj->writePrefs();
132 
133  // to do: test this
134  $rbacadmin->assignUser($ilSetting->get('cas_user_default_role'), $userObj->getId(),true);
135  unset($userObj);
136 
137  $a_auth->setAuth($local_user);
138  return true;
139  }
140  }
141  else
142  {
143  $ilLog->write(__METHOD__.': Login failed.');
144 
145  // This should never occur unless CAS is not configured properly
146  $a_auth->status = AUTH_WRONG_LOGIN;
147  return false;
148  }
149  return false;
150  }
151 
157  protected function handleLDAPDataSource($a_auth,$ext_account)
158  {
159  include_once './Services/LDAP/classes/class.ilLDAPServer.php';
162  );
163 
164  $GLOBALS['ilLog']->write(__METHOD__.' Using ldap data source for user: '.$ext_account);
165 
166  include_once './Services/LDAP/classes/class.ilLDAPUserSynchronisation.php';
167  $sync = new ilLDAPUserSynchronisation('cas', $server->getServerId());
168  $sync->setExternalAccount($ext_account);
169  $sync->setUserData(array());
170  #$sync->forceCreation($this->force_creation);
171  // TODO: Check this
172  $sync->forceCreation(true);
173 
174  try {
175  $internal_account = $sync->sync();
176  }
177  catch(UnexpectedValueException $e) {
178  $GLOBALS['ilLog']->write(__METHOD__.': Login failed with message: '. $e->getMessage());
179  $a_auth->status = AUTH_WRONG_LOGIN;
180  $a_auth->logout();
181  return false;
182  }
184  // No syncronisation allowed => create Error
185  $GLOBALS['ilLog']->write(__METHOD__.': Login failed with message: '. $e->getMessage());
186  $a_auth->status = AUTH_CAS_NO_ILIAS_USER;
187  $a_auth->logout();
188  return false;
189  }
191  $GLOBALS['ilLog']->write(__METHOD__.': Starting account migration.');
192  $a_auth->logout();
193  ilUtil::redirect('ilias.php?baseClass=ilStartUpGUI&cmdClass=ilstartupgui&cmd=showAccountMigration');
194  }
195  $a_auth->setAuth($internal_account);
196  return true;
197  }
198 
199 
200 
208  public function fetchData($a_username,$a_password,$isChallengeResponse = false)
209  {
210  global $PHPCAS_CLIENT,$ilLog;
211 
212  $ilLog->write(__METHOD__.': Fetch Data called');
213  return $PHPCAS_CLIENT->isAuthenticated();
214  }
215 
216  protected function initCAS()
217  {
218  global $ilSetting;
219 
220  include_once("./Services/CAS/lib/CAS.php");
221 
222  $this->server_version = CAS_VERSION_2_0;
223  $this->server_hostname = $ilSetting->get('cas_server');
224  $this->server_port = (int) $ilSetting->get('cas_port');
225  $this->server_uri = (string) $ilSetting->get('cas_uri');
226 
229  $this->server_version,
230  $this->server_hostname,
231  $this->server_port,
232  $this->server_uri
233  );
235  }
236 
237 }
238 ?>