ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  // bind
49  include_once './Services/LDAP/classes/class.ilLDAPQuery.php';
50  $query = new ilLDAPQuery($this->getServer());
52  } catch (ilLDAPQueryException $e) {
53  $this->getLogger()->error('Cannot bind to LDAP server... ' . $e->getMessage());
54  $this->handleAuthenticationFail($status, 'auth_err_ldap_exception');
55  return false;
56  }
57  try {
58  // Read user data, which does ensure a sucessful authentication.
59  $users = $query->fetchUser(
60  $this->getCredentials()->getUsername()
61  );
62 
63  if (!$users) {
64  $this->handleAuthenticationFail($status, 'err_wrong_login');
65  return false;
66  }
67  if (!trim($this->getCredentials()->getPassword())) {
68  $this->handleAuthenticationFail($status, 'err_wrong_login');
69  return false;
70  }
71  if (!array_key_exists($this->changeKeyCase($this->getCredentials()->getUsername()), $users)) {
72  $this->getLogger()->warning('Cannot find user: ' . $this->changeKeyCase($this->getCredentials()->getUsername()));
73  $this->handleAuthenticationFail($status, 'auth_err_ldap_exception');
74  return false;
75  }
76 
77  // check group membership
78  if (!$query->checkGroupMembership(
79  $this->getCredentials()->getUsername(),
80  $users[$this->changeKeyCase($this->getCredentials()->getUsername())]
81  )) {
82  $this->handleAuthenticationFail($status, 'err_wrong_login');
83  return false;
84  }
85  } catch (ilLDAPQueryException $e) {
86  $this->getLogger()->error('Cannot fetch LDAP user data... ' . $e->getMessage());
87  $this->handleAuthenticationFail($status, 'auth_err_ldap_exception');
88  return false;
89  }
90  try {
91  // now bind with login credentials
92  $query->bind(IL_LDAP_BIND_AUTH, $users[$this->changeKeyCase($this->getCredentials()->getUsername())]['dn'], $this->getCredentials()->getPassword());
93  } catch (ilLDAPQueryException $e) {
94  $this->handleAuthenticationFail($status, 'err_wrong_login');
95  return false;
96  }
97 
98  // authentication success update profile
99  return $this->updateAccount($status, $users[$this->changeKeyCase($this->getCredentials()->getUsername())]);
100  }
101 
107  protected function updateAccount(ilAuthStatus $status, array $user)
108  {
109  $user = array_change_key_case($user, CASE_LOWER);
110  $this->getLogger()->dump($user, ilLogLevel::DEBUG);
111 
112  include_once './Services/LDAP/classes/class.ilLDAPUserSynchronisation.php';
113  $sync = new ilLDAPUserSynchronisation('ldap_' . $this->getServer()->getServerId(), $this->getServer()->getServerId());
114  $sync->setExternalAccount($this->getCredentials()->getUsername());
115  $sync->setUserData($user);
116  $sync->forceCreation($this->force_new_account);
117 
118  try {
119  $internal_account = $sync->sync();
120  $this->getLogger()->debug('Internal account: ' . $internal_account);
121  } catch (UnexpectedValueException $e) {
122  $this->getLogger()->info('Login failed with message: ' . $e->getMessage());
123  $this->handleAuthenticationFail($status, 'err_wrong_login');
124  return false;
126  // No syncronisation allowed => create Error
127  $this->getLogger()->info('Login failed with message: ' . $e->getMessage());
128  $this->handleAuthenticationFail($status, 'err_auth_ldap_no_ilias_user');
129  return false;
131  // Account migration required
132  $this->setExternalAccountName($this->getCredentials()->getUsername());
133  $this->getLogger()->info('Authentication failed: account migration required for external account: ' . $this->getCredentials()->getUsername());
135  return false;
136  }
138  $status->setAuthenticatedUserId(ilObjUser::_lookupId($internal_account));
139  return true;
140  }
141 
142 
143 
147  protected function initServer($a_server_id)
148  {
149  include_once './Services/LDAP/classes/class.ilLDAPServer.php';
150  $this->server = new ilLDAPServer($a_server_id);
151  }
152 
153  // Account migration
154 
159  {
160  $this->force_new_account = true;
161 
162  try {
163  include_once './Services/LDAP/classes/class.ilLDAPQuery.php';
164  $query = new ilLDAPQuery($this->getServer());
166  } catch (ilLDAPQueryException $e) {
167  $this->getLogger()->error('Cannot bind to LDAP server... ' . $e->getMessage());
168  $this->handleAuthenticationFail($status, 'auth_err_ldap_exception');
169  return false;
170  }
171  try {
172  // fetch user
173  $users = $query->fetchUser(
174  $this->getCredentials()->getUsername()
175  );
176  if (!$users) {
177  $this->handleAuthenticationFail($status, 'err_wrong_login');
178  return false;
179  }
180  if (!array_key_exists($this->changeKeyCase($this->getCredentials()->getUsername()), $users)) {
181  $this->handleAuthenticationFail($status, 'err_wrong_login');
182  return false;
183  }
184  } catch (ilLDAPQueryException $e) {
185  $this->getLogger()->error('Cannot fetch LDAP user data... ' . $e->getMessage());
186  $this->handleAuthenticationFail($status, 'auth_err_ldap_exception');
187  return false;
188  }
189 
190  // authentication success update profile
191  $this->updateAccount($status, $users[$this->changeKeyCase($this->getCredentials()->getUsername())]);
192  }
193 
194 
195 
200  {
201  $this->force_new_account = true;
202 
203  try {
204  include_once './Services/LDAP/classes/class.ilLDAPQuery.php';
205  $query = new ilLDAPQuery($this->getServer());
207  } catch (ilLDAPQueryException $e) {
208  $this->getLogger()->error('Cannot bind to LDAP server... ' . $e->getMessage());
209  $this->handleAuthenticationFail($status, 'auth_err_ldap_exception');
210  return false;
211  }
212 
213  $users = $query->fetchUser($this->getCredentials()->getUsername());
214  $this->updateAccount($status, $users[$this->changeKeyCase($this->getCredentials()->getUsername())]);
215  return true;
216  }
217 
221  public function getTriggerAuthMode()
222  {
223  return AUTH_LDAP . '_' . $this->getServer()->getServerId();
224  }
225 
229  public function getUserAuthModeName()
230  {
231  return 'ldap_' . $this->getServer()->getServerId();
232  }
233 
238  public function getExternalAccountName()
239  {
241  }
242 
247  public function setExternalAccountName($a_name)
248  {
249  $this->migration_account = $a_name;
250  }
251 
257  protected function changeKeyCase($a_string)
258  {
259  $as_array = array_change_key_case(array($a_string => $a_string));
260  foreach ($as_array as $key => $string) {
261  return $key;
262  }
263  }
264 }
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.
$sync
setExternalAccountName($a_name)
Set external account name.
$query
Create styles array
The data for the language used.
$users
Definition: authpage.php:44
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.
$key
Definition: croninfo.php:18
const STATUS_ACCOUNT_MIGRATION_REQUIRED
changeKeyCase($a_string)
Change case similar to array_change_key_case, to avoid further encoding problems. ...