1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ActiveDirectory\Tests\Services; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ActiveDirectory\Extensions\LDAPGroupExtension; |
6
|
|
|
use SilverStripe\ActiveDirectory\Extensions\LDAPMemberExtension; |
7
|
|
|
use SilverStripe\ActiveDirectory\Model\LDAPGateway; |
8
|
|
|
use SilverStripe\ActiveDirectory\Services\LDAPService; |
9
|
|
|
use SilverStripe\ActiveDirectory\Tests\FakeGatewayTest; |
10
|
|
|
use SilverStripe\Core\Config\Config; |
11
|
|
|
use SilverStripe\Security\Group; |
12
|
|
|
use SilverStripe\Security\Member; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @coversDefaultClass \SilverStripe\ActiveDirectory\Services\LDAPService |
16
|
|
|
* @package activedirectory |
17
|
|
|
*/ |
18
|
|
|
class LDAPServiceTest extends FakeGatewayTest |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $usesDatabase = true; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritDoc} |
28
|
|
|
*/ |
29
|
|
|
public function setUp() |
30
|
|
|
{ |
31
|
|
|
parent::setUp(); |
32
|
|
|
|
33
|
|
|
Config::modify()->set(LDAPGateway::class, 'options', ['host' => '1.2.3.4']); |
34
|
|
|
Config::modify()->set(LDAPService::class, 'groups_search_locations', [ |
35
|
|
|
'CN=Users,DC=playpen,DC=local', |
36
|
|
|
'CN=Others,DC=playpen,DC=local' |
37
|
|
|
]); |
38
|
|
|
// Prevent other module extension hooks from executing during write() etc. |
39
|
|
|
Config::modify()->remove(Member::class, 'extensions'); |
40
|
|
|
Config::modify()->remove(Group::class, 'extensions'); |
41
|
|
|
Config::modify()->set(Member::class, 'update_ldap_from_local', false); |
42
|
|
|
Config::modify()->set(Member::class, 'create_users_in_ldap', false); |
43
|
|
|
Config::modify()->set( |
44
|
|
|
Group::class, |
45
|
|
|
'extensions', |
46
|
|
|
[LDAPGroupExtension::class] |
47
|
|
|
); |
48
|
|
|
Config::modify()->set( |
49
|
|
|
Member::class, |
50
|
|
|
'extensions', |
51
|
|
|
[LDAPMemberExtension::class] |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
// Disable Monolog logging to stderr by default if you don't give it a handler |
55
|
|
|
$this->service->getLogger()->pushHandler(new \Monolog\Handler\NullHandler); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testGroups() |
59
|
|
|
{ |
60
|
|
|
$expected = [ |
61
|
|
|
'CN=Group1,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group1,CN=Users,DC=playpen,DC=local'], |
62
|
|
|
'CN=Group2,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group2,CN=Users,DC=playpen,DC=local'], |
63
|
|
|
'CN=Group3,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group3,CN=Users,DC=playpen,DC=local'], |
64
|
|
|
'CN=Group4,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group4,CN=Users,DC=playpen,DC=local'], |
65
|
|
|
'CN=Group5,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group5,CN=Users,DC=playpen,DC=local'], |
66
|
|
|
'CN=Group6,CN=Others,DC=playpen,DC=local' => ['dn' => 'CN=Group6,CN=Others,DC=playpen,DC=local'], |
67
|
|
|
'CN=Group7,CN=Others,DC=playpen,DC=local' => ['dn' => 'CN=Group7,CN=Others,DC=playpen,DC=local'], |
68
|
|
|
'CN=Group8,CN=Others,DC=playpen,DC=local' => ['dn' => 'CN=Group8,CN=Others,DC=playpen,DC=local'] |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
$results = $this->service->getGroups(); |
72
|
|
|
|
73
|
|
|
$this->assertEquals($expected, $results); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testUpdateMemberFromLDAP() |
77
|
|
|
{ |
78
|
|
|
Config::modify()->set( |
79
|
|
|
Member::class, |
80
|
|
|
'ldap_field_mappings', |
81
|
|
|
[ |
82
|
|
|
'givenname' => 'FirstName', |
83
|
|
|
'sn' => 'Surname', |
84
|
|
|
'mail' => 'Email', |
85
|
|
|
] |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$member = new Member(); |
89
|
|
|
$member->GUID = '123'; |
90
|
|
|
|
91
|
|
|
$this->service->updateMemberFromLDAP($member); |
92
|
|
|
|
93
|
|
|
$this->assertTrue($member->ID > 0, 'updateMemberFromLDAP writes the member'); |
94
|
|
|
$this->assertEquals('123', $member->GUID, 'GUID remains the same'); |
95
|
|
|
$this->assertEquals('Joe', $member->FirstName, 'FirstName updated from LDAP'); |
96
|
|
|
$this->assertEquals('Bloggs', $member->Surname, 'Surname updated from LDAP'); |
97
|
|
|
$this->assertEquals('[email protected]', $member->Email, 'Email updated from LDAP'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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: