Completed
Push — master ( 418ae6...a735c4 )
by Tim
01:51
created

Search::initialize()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 8
nop 1
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestCase\AuthSource\Ldap;
4
5
use \SimpleSAML\Module\monitor\State as State;
6
use \SimpleSAML\Module\monitor\TestData as TestData;
7
use \SimpleSAML\Module\monitor\TestResult as TestResult;
8
use \SimpleSAML\Module\monitor\TestSuite as TestSuite;
9
10
final class Search extends \SimpleSAML\Module\monitor\TestCaseFactory
11
{
12
    /*
13
     * @var \SimpleSAML_Auth_LDAP|null
14
     */
15
    private $connection = null;
16
17
    /*
18
     * @var string|null
19
     */
20
    private $base = null;
21
22
    /*
23
     * @var string|null
24
     */
25
    private $username = null;
26
27
    /*
28
     * @var string|null
29
     */
30
    private $password = null;
31
32
    /*
33
     * @var array
34
     */
35
    private $attributes = array();
36
37
    /*
38
     * @param TestData $testData
39
     *
40
     * @return void
41
     */
42
    protected function initialize($testData)
43
    {
44
        $authSourceData = $testData->getInput('authSourceData');
45
46
        $base = $authSourceData['search.base'];
47
        $base = is_array($base) ? $base[0] : $base;
48
        if (($i = stripos($base, 'DC=')) > 0) {
49
            $base = substr($base, $i);
50
        }
51
        $this->base = $base;
52
53
        $username = $authSourceData['search.username'];
54
        $this->setSubject($username);
55
        if (strpos($username, 'DC=') > 0) {
56
            // We have been given a DN
57
            $username = ldap_explode_dn($username, 1);
58
            $this->username = $username[0];
59
            $this->attributes = array('cn');
60
        } else {
61
            // We have been given a sAMAccountName
62
            $this->username = $username;
63
            $this->attributes = array('sAMAccountName');
64
        }
65
        $this->password = $authSourceData['search.password'];
66
        $this->connection = $testData->getInput('connection');
67
68
        parent::initialize($testData);
69
    }
70
71
    /*
72
     * @return void
73
     */
74
    protected function invokeTest()
75
    {
76
        $connection = $this->connection;
77
        $testResult = new TestResult('LDAP Search', $this->getSubject());
78
79
        try {
80
            $distinguishedName = $connection->searchfordn($this->base, $this->attributes, $this->username);
81
        } catch (\Exception $e) {
82
            $distinguishedName = false;
83
        }
84
85
        if (is_string($distinguishedName)) {
86
            $msg = 'Search succesful';
87
            $state = State::OK;
88
        } elseif ($distinguishedName === false) {
89
            $msg = str_replace('Library - LDAP searchfordn(): ', '', $e->getMessage());
90
            $state = State::ERROR;
91
        } else ($distinguishedName === null) {
92
            // Search for configured search.username returned no results; Shouldn't happen!!
93
            $msg = 'Invalid search result';
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ';'
Loading history...
94
            $state = State::WARNING;
95
        }
96
97
        $testResult->setState($state);
98
        $testResult->setMessage($msg);
99
        $this->setTestResult($testResult);
100
    }
101
}
102