1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CCDNForum ForumBundle |
5
|
|
|
* |
6
|
|
|
* (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/> |
7
|
|
|
* |
8
|
|
|
* Available on github <http://www.github.com/codeconsortium/> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace CCDNForum\ForumBundle\Component\Helper; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
17
|
|
|
use Symfony\Component\Security\Core\SecurityContextInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
* @category CCDNForum |
22
|
|
|
* @package ForumBundle |
23
|
|
|
* |
24
|
|
|
* @author Reece Fowell <[email protected]> |
25
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
26
|
|
|
* @version Release: 2.0 |
27
|
|
|
* @link https://github.com/codeconsortium/CCDNForumForumBundle |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
class RoleHelper |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @access protected |
35
|
|
|
* @var \Symfony\Component\Security\Core\SecurityContextInterface $securityContext |
36
|
|
|
*/ |
37
|
|
|
protected $securityContext; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
* @access protected |
42
|
|
|
* @var array $availableRoles |
43
|
|
|
*/ |
44
|
|
|
protected $availableRoles; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
* @access protected |
49
|
|
|
* @var array $availableRoleKeys |
50
|
|
|
*/ |
51
|
|
|
protected $availableRoleKeys; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
* @access public |
56
|
|
|
* @param \Symfony\Component\Security\Core\SecurityContextInterface $securityContext |
57
|
|
|
* @param array $availableRoles |
58
|
|
|
*/ |
59
|
|
|
public function __construct(SecurityContextInterface $securityContext, $availableRoles) |
60
|
|
|
{ |
61
|
|
|
$this->securityContext = $securityContext; |
62
|
|
|
|
63
|
|
|
// default role is array is empty. |
64
|
|
|
if (empty($availableRoles)) { |
65
|
|
|
$availableRoles[] = 'ROLE_USER'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->availableRoles = $availableRoles; |
69
|
|
|
|
70
|
|
|
// Remove the associate arrays. |
71
|
|
|
$this->availableRoleKeys = array_keys($availableRoles); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* |
76
|
|
|
* @access public |
77
|
|
|
* @return Array |
78
|
|
|
*/ |
79
|
|
|
public function getRoleHierarchy() |
80
|
|
|
{ |
81
|
|
|
$roles = array(); |
82
|
|
|
|
83
|
|
|
foreach ($this->availableRoles as $roleName => $roleSubs) { |
84
|
|
|
$subs = '<ul><li>' . implode('</li><li>', $roleSubs) . '</li></ul>'; |
85
|
|
|
$roles[$roleName] = '<strong>' . $roleName . '</strong>' . ($subs != '<ul><li>' . $roleName . '</li></ul>' ? "\n" . $subs:''); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $roles; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* |
93
|
|
|
* @access public |
94
|
|
|
* @return array $availableRoles |
95
|
|
|
*/ |
96
|
|
|
public function getAvailableRoles() |
97
|
|
|
{ |
98
|
|
|
return $this->availableRoles; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* |
103
|
|
|
* @access public |
104
|
|
|
* @return array $availableRoles |
105
|
|
|
*/ |
106
|
|
|
public function getAvailableRoleKeys() |
107
|
|
|
{ |
108
|
|
|
return $this->availableRoleKeys; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* |
113
|
|
|
* @access public |
114
|
|
|
* @param \Symfony\Component\Security\Core\User\UserInterface $user |
115
|
|
|
* @param string $role |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function hasRole(UserInterface $user, $role) |
119
|
|
|
{ |
120
|
|
|
foreach ($this->availableRoles as $aRoleKey => $aRole) { |
121
|
|
|
if ($user->hasRole($aRoleKey)) { |
|
|
|
|
122
|
|
|
if (in_array($role, $aRole) || $role == $aRoleKey) { |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* |
133
|
|
|
* @access public |
134
|
|
|
* @param array $userRoles |
|
|
|
|
135
|
|
|
* @return int $highestUsersRoleKey |
|
|
|
|
136
|
|
|
*/ |
137
|
|
|
public function getUsersHighestRole($usersRoles) |
138
|
|
|
{ |
139
|
|
|
$usersHighestRoleKey = 0; |
140
|
|
|
|
141
|
|
|
// Compare (A)vailable roles against (U)sers roles. |
142
|
|
|
foreach ($this->availableRoleKeys as $aRoleKey => $aRole) { |
143
|
|
|
foreach ($usersRoles as $uRole) { |
144
|
|
|
if ($uRole == $aRole && $aRoleKey > $usersHighestRoleKey) { |
145
|
|
|
$usersHighestRoleKey = $aRoleKey; |
146
|
|
|
|
147
|
|
|
break; // break because once uRole == aRole we know we cannot match anything else. |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $usersHighestRoleKey; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* |
157
|
|
|
* @access public |
158
|
|
|
* @param array $userRoles |
|
|
|
|
159
|
|
|
* @return string $role |
160
|
|
|
*/ |
161
|
|
|
public function getUsersHighestRoleAsName($usersRoles) |
162
|
|
|
{ |
163
|
|
|
$usersHighestRoleKey = $this->getUsersHighestRole($usersRoles); |
164
|
|
|
|
165
|
|
|
$roles = $this->availableRoleKeys; |
166
|
|
|
|
167
|
|
|
if (array_key_exists($usersHighestRoleKey, $roles)) { |
168
|
|
|
return $roles[$usersHighestRoleKey]; |
169
|
|
|
} else { |
170
|
|
|
return 'ROLE_USER'; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: