Passed
Push — dev ( 3ad29f...97f4bd )
by Nils
08:12
created

userIsEnabled()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 14
rs 9.9332
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
 * @file      ldap.activedirectory.php
15
 * ---
16
 *
17
 * @author    Nils Laumaillé ([email protected])
18
 *
19
 * @copyright 2009-2023 Teampass.net
20
 *
21
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
22
 * ---
23
 *
24
 * @see       https://www.teampass.net
25
 */
26
27
use LdapRecord\Connection;
28
use LdapRecord\Container;
29
use LdapRecord\Models\ActiveDirectory\User;
30
31
require_once __DIR__.'/../vendor/autoload.php';
32
33
/**
34
 * Get the user's AD groups.
35
 *
36
 * @param string $userDN
37
 * @param LdapRecord\Connection $connection
38
 * @param array $SETTINGS
39
 *
40
 * @return array
41
 */
42
function getUserADGroups(string $userDN, Connection $connection, array $SETTINGS): array
43
{
44
    // init
45
    $groupsArr = [];
46
47
    try {
48
        Container::addConnection($connection);
49
        // get id attribute
50
        if (isset($SETTINGS['ldap_guid_attibute']) ===true && empty($SETTINGS['ldap_guid_attibute']) === false) {
51
            $idAttribute = $SETTINGS['ldap_guid_attibute'];
52
        } else {
53
            $idAttribute = 'objectguid';
54
        }
55
56
        // Get user groups from AD
57
        $user = User::find($userDN);
58
        $groups = $user->groups()->get();
59
        foreach ($groups as $group) {
60
            array_push(
61
                $groupsArr,
62
                $group[$idAttribute][0]
63
            );
64
        }
65
    } catch (\LdapRecord\Auth\BindException $e) {
66
        // Do nothing
67
    }
68
69
    return [
70
        'error' => false,
71
        'message' => '',
72
        'userGroups' => $groupsArr,
73
    ];
74
}
75
76
/**
77
 * Check is user is enabled
78
 *
79
 * @param string $userDN
80
 * @param Connection $connection
81
 * @return array
82
 */
83
function userIsEnabled(string $userDN, Connection $connection): array
84
{
85
    $isEnabled = false;
86
    try {
87
        Container::addConnection($connection);
88
        $user = User::find($userDN);
89
        $isEnabled = $user->isEnabled();
90
    } catch (\LdapRecord\Auth\BindException $e) {
91
        // Do nothing
92
    }
93
    return [
94
        'error' => false,
95
        'message' => '',
96
        'isEnabled' => $isEnabled,
97
    ];
98
}