Passed
Push — monitor-3.0.x ( f85cdc...51d4ea )
by Tim
03:29
created

Search::initialize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 4
nop 1
dl 0
loc 28
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestCase\AuthSource\Ldap;
4
5
use SimpleSAML\Configuration;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, SimpleSAML\Module\monito...urce\Ldap\Configuration. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use SimpleSAML\Module\ldap\Auth\Ldap;
7
use SimpleSAML\Module\monitor\State;
8
use SimpleSAML\Module\monitor\TestData;
9
use SimpleSAML\Module\monitor\TestResult;
10
use Webmozart\Assert\Assert;
11
12
final class Search extends \SimpleSAML\Module\monitor\TestCaseFactory
13
{
14
    /** @var \SimpleSAML\Module\ldap\Auth\Ldap */
15
    private $connection;
16
17
    /** @var string */
18
    private $base;
19
20
    /** @var string */
21
    private $username;
22
23
    /** @var string */
24
    private $password;
25
26
    /** @var array */
27
    private $attributes = [];
28
29
30
    /**
31
     * @param \SimpleSAML\Module\monitor\TestData $testData
32
     *
33
     * @return void
34
     */
35
    protected function initialize(TestData $testData): void
36
    {
37
        $authSourceData = $testData->getInputItem('authSourceData');
38
        Assert::isInstanceOf($authSourceData, Configuration::class);
39
40
        // Just to be on the safe side, strip off any OU's and search to whole directory
41
        $base = $authSourceData->getArrayizeString('search.base', '<< unset >>')[0];
42
        if (($i = intval(stripos($base, 'DC='))) > 0) {
43
            $base = substr($base, $i);
44
        }
45
        $this->base = $base;
46
47
        $username = $authSourceData->getString('search.username', '<< unset >>');
48
        $this->setSubject($username);
49
        if (strpos($username, 'DC=') > 0) {
50
            // We have been given a DN
51
            $username = ldap_explode_dn($username, 1);
52
            $this->username = $username[0];
53
            $this->attributes = ['cn'];
54
        } else {
55
            // We have been given a sAMAccountName
56
            $this->username = $username;
57
            $this->attributes = ['sAMAccountName'];
58
        }
59
        $this->password = $authSourceData->getString('search.password', '<< unset >>');
60
        $this->connection = $testData->getInputItem('connection');
61
62
        parent::initialize($testData);
63
    }
64
65
66
    /**
67
     * @return void
68
     */
69
    public function invokeTest(): void
70
    {
71
        try {
72
            $this->connection->searchfordn($this->base, $this->attributes, $this->username);
73
        } catch (\SimpleSAML\Error\Error $error) {
74
            // Fallthru
75
        }
76
77
        $testResult = new TestResult('LDAP Search', $this->getSubject());
78
79
        if (isset($error)) {
80
            // When you feed str_replace a string, outcome will be string too, but Psalm doesn't see it that way
81
            $msg = str_replace('Library - LDAP searchfordn(): ', '', $error->getMessage());
82
            $testResult->setState(State::ERROR);
83
            $testResult->setMessage($msg);
84
        } else {
85
            $testResult->setState(State::OK);
86
            $testResult->setMessage('Search succesful');
87
        }
88
89
        $this->setTestResult($testResult);
90
    }
91
}
92