Search::invokeTest()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 21
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\monitor\TestCase\AuthSource\Ldap;
6
7
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...
8
use SimpleSAML\Error;
9
use SimpleSAML\Module\ldap\ConnectorInterface;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\ldap\ConnectorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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