1 | <?php |
||
23 | class LdapRoleMapper |
||
24 | { |
||
25 | /** |
||
26 | * @var LdapManager |
||
27 | */ |
||
28 | protected $ldap; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $options = [ |
||
34 | 'check_groups_recursively' => true, |
||
35 | 'default_role' => 'ROLE_USER', |
||
36 | 'roles' => [], |
||
37 | 'role_ldap_type' => 'group', |
||
38 | 'role_attributes' => [ |
||
39 | 'guid' => 'guid', |
||
40 | 'sid' => 'sid', |
||
41 | 'name' => 'name', |
||
42 | 'members' => 'members', |
||
43 | ], |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * @param LdapManager $ldap |
||
48 | * @param array $options |
||
49 | */ |
||
50 | public function __construct(LdapManager $ldap, array $options) |
||
55 | |||
56 | /** |
||
57 | * Set the roles for the user based on LDAP group membership. |
||
58 | * |
||
59 | * @param LdapUserInterface $user |
||
60 | * @return LdapUserInterface |
||
61 | */ |
||
62 | public function setRoles(LdapUserInterface $user) |
||
84 | |||
85 | /** |
||
86 | * Check all of the groups that are valid for a specific role against all of the LDAP groups that the user belongs |
||
87 | * to. |
||
88 | * |
||
89 | * @param array $roleGroups |
||
90 | * @param LdapObjectCollection $ldapGroups |
||
91 | * @return bool |
||
92 | */ |
||
93 | protected function hasGroupForRoles(array $roleGroups, LdapObjectCollection $ldapGroups) |
||
113 | |||
114 | /** |
||
115 | * Check each LDAP group to see if any of them have an attribute with a specific value. |
||
116 | * |
||
117 | * @param LdapObjectCollection $groups |
||
118 | * @param string $attribute |
||
119 | * @param string $value |
||
120 | * @return bool |
||
121 | */ |
||
122 | protected function hasGroupWithAttributeValue(LdapObjectCollection $groups, $attribute, $value) |
||
135 | |||
136 | /** |
||
137 | * @param LdapUserInterface $user |
||
138 | * @return LdapObjectCollection |
||
139 | */ |
||
140 | protected function getGroupsForUser(LdapUserInterface $user) |
||
160 | } |
||
161 |
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 sub-classes 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 parent class: