Skip to content

Commit d120abb

Browse files
committed
refactoring user provider for cosgin login, full name is now refreshed
from LDAP on every login
1 parent 71e364b commit d120abb

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

src/Legislator/LegislatorBundle/Security/LegislatorUserProvider.php

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,35 @@ public function loadUserByUsername($username)
3030
if ($this->cosign_login_enabled) {
3131
$ldapSearch = $this->container->get('legislator.teacher_search');
3232
$user_info = $ldapSearch->byLogin($username);
33+
if (!array_key_exists($username, $user_info)) {
34+
throw new UsernameNotFoundException(sprintf('Username "%s" not found in LDAP.', $username));
35+
}
3336
$user_info = $user_info[$username];
3437

3538
$org_unit = $this->container->getParameter('org_unit');
3639

3740
// checking org unit if set
38-
if ($org_unit !== null && is_array($user_info)
41+
if ($org_unit !== null
3942
&& array_search($org_unit, $user_info['orgUnits']) === FALSE) {
4043
throw new AccessDeniedException(sprintf('Username "%s" does not belong to unit "%s".', $username, $org_unit));
4144
}
4245

46+
// creating a new user when logging in for the first time
4347
if ($user === null) {
4448
$user = $this->userManager->createUser();
4549
$user->setUsername($username);
4650
$user->setEmail("$username@uniba.sk");
4751
$user->setPassword("$username@uniba.sk");
4852
$user->setEnabled(1);
49-
// set full name from LDAP
50-
$user->setFirstName($user_info['givenName']);
51-
$user->setSurname($user_info['familyName']);
52-
53-
$this->userManager->updateUser($user);
5453
}
54+
55+
// set full name from LDAP
56+
$user->setFirstName($user_info['givenName']);
57+
$user->setSurname($user_info['familyName']);
58+
59+
$this->userManager->updateUser($user);
60+
61+
5562
} else {
5663
if (!$user) {
5764
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
@@ -74,24 +81,6 @@ public function refreshUser(SecurityUserInterface $user)
7481
throw new UsernameNotFoundException(sprintf('User with ID "%d" could not be reloaded.', $user->getId()));
7582
}
7683

77-
// filling up full name from LDAP if enabled
78-
if ($this->cosign_login_enabled) {
79-
$name = $reloadedUser->getFirstName();
80-
if (empty($name)) {
81-
$username = $reloadedUser->getUsername();
82-
$ldapSearch = $this->container->get('legislator.teacher_search');
83-
$user_info = $ldapSearch->byLogin($username);
84-
if (is_array($user_info)) {
85-
$user_info = $user_info [$username];
86-
87-
$reloadedUser->setFirstName($user_info['givenName']);
88-
$reloadedUser->setSurname($user_info['familyName']);
89-
90-
$this->userManager->updateUser($reloadedUser);
91-
}
92-
}
93-
}
94-
9584
return $reloadedUser;
9685
}
9786

0 commit comments

Comments
 (0)