1
|
|
|
<?php |
2
|
|
|
namespace SOG\Dashboard\Authentication; |
3
|
|
|
|
4
|
|
|
use SOG\Dashboard\LdapAdapter; |
5
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
6
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
7
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
8
|
|
|
use Zend\Ldap\Exception\LdapException; |
9
|
|
|
use Zend\Ldap\Ldap; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This class maps a requested user to its LDAP entry. |
13
|
|
|
* The implementation follows https://github.com/DerManoMann/ldap-auth-service-provider - thank you! |
14
|
|
|
* |
15
|
|
|
* Class LdapUserProvider |
16
|
|
|
* @package SOG\Dashboard\Authentication |
17
|
|
|
*/ |
18
|
|
|
class LdapUserProvider implements UserProviderInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var LdapAdapter The LDAP resource |
22
|
|
|
*/ |
23
|
|
|
private $ldap; |
24
|
|
|
|
25
|
|
|
public function __construct(LdapAdapter $ldap) |
26
|
|
|
{ |
27
|
|
|
$this->ldap = $ldap; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function refreshUser(UserInterface $user) |
34
|
|
|
{ |
35
|
|
|
// This could be implemented in a different manner (session?) to reduce server load |
36
|
|
|
return $this->loadUserByUsername($user->getUsername()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function loadUserByUsername($username) |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
// first retrieve the full DN, this might throw an exception |
46
|
|
|
$dn = $this->ldap->getCanonicalAccountName($username, LDAP::ACCTNAME_FORM_DN); |
47
|
|
|
// then get all associated attributes |
48
|
|
|
$attributes = $this->ldap->getEntry($dn); |
49
|
|
|
// also get the groups the user is member of |
50
|
|
|
$memberships = $this->ldap->getMemberships($dn, ['cn', 'ou', 'mailinglistId'])->toArray(); |
51
|
|
|
// and maybe the owned groups |
52
|
|
|
$ownerships = $this->ldap->getOwnedGroups($dn)->toArray(); |
53
|
|
|
// assign the user's roles |
54
|
|
|
$roles = $this->getRoles($dn, $ownerships); |
55
|
|
|
return new LdapUser($username, null, $attributes, $roles, $memberships, $ownerships); |
|
|
|
|
56
|
|
|
} catch (LdapException $ex) { |
57
|
|
|
throw new UsernameNotFoundException($ex->getMessage().'Der Login war nicht erfolgreich, bitte überprüfe deinen Benutzernamen und Passwort.'); |
58
|
|
|
$logger = $this->get('logger'); |
|
|
|
|
59
|
|
|
$logger->error(($ex->getMessage())); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Infer the rules of the given user by checking the LDAP resource. This can be extended to accommodate for |
65
|
|
|
* ROLE_ADMIN or other cases |
66
|
|
|
* |
67
|
|
|
* @param string $user_dn The user DN for which to infer the rules |
68
|
|
|
* @param array $ownerships The owned groups for the user DN |
69
|
|
|
* @return array The roles of the given user |
70
|
|
|
*/ |
71
|
|
|
private function getRoles($user_dn, $ownerships) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$roles = []; |
74
|
|
|
if (count($ownerships) > 0) { |
75
|
|
|
$roles[] = 'ROLE_GROUP_ADMIN'; |
76
|
|
|
} else { |
77
|
|
|
$roles[] = 'ROLE_USER'; |
78
|
|
|
} |
79
|
|
|
return $roles; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function supportsClass($class) |
86
|
|
|
{ |
87
|
|
|
return ($class === '\\SOG\\Dashboard\\Authentication\\LdapUser'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.