ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilAuthProviderLDAP.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 
5 include_once './Services/Authentication/classes/Provider/class.ilAuthProvider.php';
6 include_once './Services/Authentication/interfaces/interface.ilAuthProviderInterface.php';
7 include_once './Services/Authentication/interfaces/interface.ilAuthProviderAccountMigrationInterface.php';
8 
16 {
17  private $server = null;
18  private $migration_account = '';
19  private $force_new_account = false;
20 
25  public function __construct(\ilAuthCredentials $credentials, $a_server_id = 0)
26  {
27  parent::__construct($credentials);
28  $this->initServer($a_server_id);
29  }
30 
35  public function getServer()
36  {
37  return $this->server;
38  }
39 
40 
46  {
47  try
48  {
49  // bind
50  include_once './Services/LDAP/classes/class.ilLDAPQuery.php';
51  $query = new ilLDAPQuery($this->getServer());
53  }
54  catch(ilLDAPQueryException $e)
55  {
56  $this->getLogger()->error('Cannot bind to LDAP server... '. $e->getMessage());
57  $this->handleAuthenticationFail($status, 'auth_err_ldap_exception');
58  return false;
59  }
60  try
61  {
62  // Read user data, which does ensure a sucessful authentication.
63  $users = $query->fetchUser(
64  $this->getCredentials()->getUsername()
65  );
66 
67  if(!$users)
68  {
69  $this->handleAuthenticationFail($status, 'err_wrong_login');
70  return false;
71  }
72  if(!trim($this->getCredentials()->getPassword()))
73  {
74  $this->handleAuthenticationFail($status, 'err_wrong_login');
75  return false;
76  }
77  if(!array_key_exists($this->changeKeyCase($this->getCredentials()->getUsername()), $users))
78  {
79  $this->getLogger()->warning('Cannot find user: '. $this->changeKeyCase($this->getCredentials()->getUsername()));
80  $this->handleAuthenticationFail($status, 'auth_err_ldap_exception');
81  return false;
82  }
83 
84  // check group membership
85  if(!$query->checkGroupMembership(
86  $this->getCredentials()->getUsername(),
87  $users[$this->changeKeyCase($this->getCredentials()->getUsername())]
88  ))
89  {
90  $this->handleAuthenticationFail($status, 'err_wrong_login');
91  return false;
92  }
93  }
94  catch (ilLDAPQueryException $e) {
95  $this->getLogger()->error('Cannot fetch LDAP user data... '. $e->getMessage());
96  $this->handleAuthenticationFail($status, 'auth_err_ldap_exception');
97  return false;
98  }
99  try
100  {
101  // now bind with login credentials
102  $query->bind(IL_LDAP_BIND_AUTH, $users[$this->changeKeyCase($this->getCredentials()->getUsername())]['dn'], $this->getCredentials()->getPassword());
103  }
104  catch (ilLDAPQueryException $e) {
105  $this->handleAuthenticationFail($status, 'err_wrong_login');
106  return false;
107  }
108 
109  // authentication success update profile
110  return $this->updateAccount($status, $users[$this->changeKeyCase($this->getCredentials()->getUsername())]);
111  }
112 
118  protected function updateAccount(ilAuthStatus $status, array $user)
119  {
120  $user = array_change_key_case($user,CASE_LOWER);
121  $this->getLogger()->dump($user, ilLogLevel::DEBUG);
122 
123  include_once './Services/LDAP/classes/class.ilLDAPUserSynchronisation.php';
124  $sync = new ilLDAPUserSynchronisation('ldap_'.$this->getServer()->getServerId(), $this->getServer()->getServerId());
125  $sync->setExternalAccount($this->getCredentials()->getUsername());
126  $sync->setUserData($user);
127  $sync->forceCreation($this->force_new_account);
128 
129  try {
130  $internal_account = $sync->sync();
131  $this->getLogger()->debug('Internal account: ' . $internal_account);
132  }
133  catch(UnexpectedValueException $e) {
134  $this->getLogger()->info('Login failed with message: ' . $e->getMessage());
135  $this->handleAuthenticationFail($status, 'err_wrong_login');
136  return false;
137  }
139  // No syncronisation allowed => create Error
140  $this->getLogger()->info('Login failed with message: ' . $e->getMessage());
141  $this->handleAuthenticationFail($status, 'err_auth_ldap_no_ilias_user');
142  return false;
143  }
145  // Account migration required
146  $this->setExternalAccountName($this->getCredentials()->getUsername());
147  $this->getLogger()->info('Authentication failed: account migration required for external account: ' . $this->getCredentials()->getUsername());
149  return false;
150  }
152  $status->setAuthenticatedUserId(ilObjUser::_lookupId($internal_account));
153  return true;
154 
155  }
156 
157 
158 
162  protected function initServer($a_server_id)
163  {
164  include_once './Services/LDAP/classes/class.ilLDAPServer.php';
165  $this->server = new ilLDAPServer($a_server_id);
166  }
167 
168  // Account migration
169 
174  {
175  $this->force_new_account = true;
176 
177  try
178  {
179  include_once './Services/LDAP/classes/class.ilLDAPQuery.php';
180  $query = new ilLDAPQuery($this->getServer());
182  }
183  catch(ilLDAPQueryException $e)
184  {
185  $this->getLogger()->error('Cannot bind to LDAP server... '. $e->getMessage());
186  $this->handleAuthenticationFail($status, 'auth_err_ldap_exception');
187  return false;
188  }
189  try
190  {
191  // fetch user
192  $users = $query->fetchUser(
193  $this->getCredentials()->getUsername()
194  );
195  if(!$users)
196  {
197  $this->handleAuthenticationFail($status, 'err_wrong_login');
198  return false;
199  }
200  if(!array_key_exists($this->changeKeyCase($this->getCredentials()->getUsername()), $users))
201  {
202  $this->handleAuthenticationFail($status, 'err_wrong_login');
203  return false;
204  }
205  }
206  catch (ilLDAPQueryException $e) {
207  $this->getLogger()->error('Cannot fetch LDAP user data... '. $e->getMessage());
208  $this->handleAuthenticationFail($status, 'auth_err_ldap_exception');
209  return false;
210  }
211 
212  // authentication success update profile
213  $this->updateAccount($status, $users[$this->changeKeyCase($this->getCredentials()->getUsername())]);
214  }
215 
216 
217 
222  {
223  $this->force_new_account = true;
224 
225  try
226  {
227  include_once './Services/LDAP/classes/class.ilLDAPQuery.php';
228  $query = new ilLDAPQuery($this->getServer());
230  }
231  catch(ilLDAPQueryException $e)
232  {
233  $this->getLogger()->error('Cannot bind to LDAP server... '. $e->getMessage());
234  $this->handleAuthenticationFail($status, 'auth_err_ldap_exception');
235  return false;
236  }
237 
238  $users = $query->fetchUser($this->getCredentials()->getUsername());
239  $this->updateAccount($status, $users[$this->changeKeyCase($this->getCredentials()->getUsername())]);
240  return true;
241  }
242 
246  public function getTriggerAuthMode()
247  {
248  return AUTH_LDAP.'_'.$this->getServer()->getServerId();
249  }
250 
254  public function getUserAuthModeName()
255  {
256  return 'ldap_'.$this->getServer()->getServerId();
257  }
258 
263  public function getExternalAccountName()
264  {
266  }
267 
272  public function setExternalAccountName($a_name)
273  {
274  $this->migration_account = $a_name;
275  }
276 
282  protected function changeKeyCase($a_string)
283  {
284  $as_array = array_change_key_case(array($a_string => $a_string));
285  foreach($as_array as $key => $string)
286  {
287  return $key;
288  }
289  }
290 
291 }
292 ?>
Interface of auth credentials.
updateAccount(ilAuthStatus $status, array $user)
Update Account.
getTriggerAuthMode()
Get trigger auth mode.
createNewAccount(ilAuthStatus $status)
Create new ILIAS account for external_account.
doAuthentication(\ilAuthStatus $status)
Do authentication.
const IL_LDAP_BIND_AUTH
const IL_LDAP_BIND_DEFAULT
const AUTH_LDAP
Synchronization of user accounts used in auth container ldap, radius , cas,...
static _lookupId($a_user_str)
Lookup id by login.
Description of ilLDAPAccountMigrationRequiredException.
setAuthenticatedUserId($a_id)
Description of class class.
Base class for authentication providers (radius, ldap, apache, ...)
Standard interface for auth provider implementations.
__construct(\ilAuthCredentials $credentials, $a_server_id=0)
Constructor.
setStatus($a_status)
Set auth status.
getUserAuthModeName()
Get user auth mode name.
setExternalAccountName($a_name)
Set external account name.
Create styles array
The data for the language used.
getLogger()
Get logger.
migrateAccount(ilAuthStatus $status)
Create new account.
getExternalAccountName()
Get external account name.
initServer($a_server_id)
Init Server.
handleAuthenticationFail(ilAuthStatus $status, $a_reason)
Handle failed authentication.
Auth status implementation.
const STATUS_ACCOUNT_MIGRATION_REQUIRED
changeKeyCase($a_string)
Change case similar to array_change_key_case, to avoid further encoding problems. ...