|
1
|
|
|
<?php |
|
2
|
|
|
namespace SOG\Dashboard\Authentication; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
5
|
|
|
use Zend\Ldap\Attribute; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* This class represents an authenticated LDAP user. |
|
9
|
|
|
* The implementation follows https://github.com/DerManoMann/ldap-auth-service-provider - thank you! |
|
10
|
|
|
* |
|
11
|
|
|
* Class LdapUser |
|
12
|
|
|
* @package SOG\Dashboard\Authentication |
|
13
|
|
|
*/ |
|
14
|
|
|
class LdapUser implements UserInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string The unique usernam (LDAP: uid) of the represented user |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $username; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string The hashed password |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $password; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array All LDAP attributes |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $attributes; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var array All roles associated with this user |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $roles; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var array All groups the user is a member of |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $memberships; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var array All groups the user is an owner of |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $ownerships; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* LdapUser constructor, set members. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $username |
|
45
|
|
|
* @param string $password |
|
46
|
|
|
* @param array $attributes |
|
47
|
|
|
* @param array $roles |
|
48
|
|
|
* @param array $memberships |
|
49
|
|
|
* @param array $ownerships |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct($username, $password, array $attributes = [], array $roles = [], array $memberships = [], array $ownerships = []) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->username = $username; |
|
54
|
|
|
$this->password = $password; |
|
55
|
|
|
$this->attributes = $attributes; |
|
56
|
|
|
$this->roles = $roles; |
|
57
|
|
|
$this->memberships = $memberships; |
|
58
|
|
|
$this->ownerships = array_map(function($group) { |
|
59
|
|
|
return $group['ou'][0]; |
|
60
|
|
|
}, $ownerships); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Retrieve the requested attribute with $index, there may be many of any given kind. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $attribName The name of the attribute |
|
67
|
|
|
* @param int $index The index. Set null, if an array containing all attributes should be returned |
|
68
|
|
|
* @return string|array Returns the specified attribute of the user or an array containing all attributes, if $index is null |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getAttribute($attribName, $index = null) |
|
71
|
|
|
{ |
|
72
|
|
|
return Attribute::getAttribute($this->attributes, $attribName, $index); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns all attributes |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getAttributes() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->attributes; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getRoles() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->roles; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getPassword() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->password; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getSalt() |
|
105
|
|
|
{ |
|
106
|
|
|
// we don't use a salt. |
|
107
|
|
|
return null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getUsername() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->username; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns the groups the user is member of. |
|
120
|
|
|
* |
|
121
|
|
|
* @return array The groups containing the user as member |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getGroups() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->memberships; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Returns the groups the user is owner of. |
|
130
|
|
|
* |
|
131
|
|
|
* @return array The groups containing the user as owner |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getOwnerships() |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->ownerships; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get an attribute of one of the associated groups. |
|
140
|
|
|
* |
|
141
|
|
|
* @param int $groupIndex |
|
142
|
|
|
* @param string $attribName |
|
143
|
|
|
* @param null $index |
|
144
|
|
|
* @return array|mixed |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getGroupAttribute($groupIndex, $attribName, $index = null) |
|
147
|
|
|
{ |
|
148
|
|
|
return Attribute::getAttribute($this->memberships[$groupIndex], $attribName, $index); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* {@inheritdoc} |
|
153
|
|
|
*/ |
|
154
|
|
|
public function eraseCredentials() |
|
155
|
|
|
{ |
|
156
|
|
|
// we don't store the plaintext password with the user object |
|
157
|
|
|
} |
|
158
|
|
|
} |