Passed
Push — master ( 61bd0d...285557 )
by Nils
04:23
created

getUserADGroups()   A

Complexity

Conditions 5
Paths 17

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 23
c 1
b 0
f 0
nc 17
nop 3
dl 0
loc 38
rs 9.2408
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Teampass - a collaborative passwords manager.
7
 * ---
8
 * This library is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 * ---
12
 *
13
 * @project   Teampass
14
 * @version   3.0.0.23
15
 * @file      ldap.activedirectory.php
16
 * ---
17
 *
18
 * @author    Nils Laumaillé ([email protected])
19
 *
20
 * @copyright 2009-2023 Teampass.net
21
 *
22
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
23
 * ---
24
 *
25
 * @see       https://www.teampass.net
26
 */
27
28
use LdapRecord\Connection;
29
use LdapRecord\Container;
30
use LdapRecord\Models\ActiveDirectory\User;
31
32
require_once 'SecureHandler.php';
33
session_name('teampass_session');
34
session_start();
35
if (isset($_SESSION['CPM']) === false || (int) $_SESSION['CPM'] !== 1) {
36
    //die('Hacking attempt...');
37
}
38
39
/**
40
 * Get the user's AD groups.
41
 *
42
 * @param string $userDN
43
 * @param LdapRecord\Connection $connection
44
 * @param array $SETTINGS
45
 *
46
 * @return array
47
 */
48
function getUserADGroups(string $userDN, LdapRecord\Connection $connection, array $SETTINGS): array
49
{
50
    try {
51
        Container::addConnection($connection);
52
53
        // init
54
        $groupsArr = [];
55
        
56
        // get id attribute
57
        if (isset($SETTINGS['ldap_guid_attibute']) ===true && empty($SETTINGS['ldap_guid_attibute']) === false) {
58
            $idAttribute = $SETTINGS['ldap_guid_attibute'];
59
        } else {
60
            $idAttribute = 'objectguid';
61
        }
62
63
        // Get user groups from AD
64
        require_once '../includes/libraries/LdapRecord/Models/ActiveDirectory/User.php';
65
        $user = User::find($userDN);
66
        $groups = $user->groups()->get();
0 ignored issues
show
Bug introduced by
The call to Tightenco\Collect\Support\Collection::get() has too few arguments starting with key. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        $groups = $user->groups()->/** @scrutinizer ignore-call */ get();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
67
        foreach ($groups as $group) {
68
            array_push(
69
                $groupsArr,
70
                $group[$idAttribute][0]
71
            );
72
        }
73
    } catch (\LdapRecord\Auth\BindException $e) {
74
        $error = $e->getDetailedError();
75
        return [
76
            'error' => true,
77
            'message' => langHdl('error').' : '.$error->getErrorCode().' - '.$error->getErrorMessage(). '<br>'.$error->getDiagnosticMessage(),
78
79
        ];
80
    }
81
82
    return [
83
        'error' => false,
84
        'message' => '',
85
        'userGroups' => $groupsArr,
86
    ];
87
}