Passed
Push — master ( 785c70...b3d31e )
by Angel Fernando Quiroz
09:13
created

LdapAuthenticatorHelper::queryAllUsers()   B

Complexity

Conditions 8
Paths 33

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 25
c 2
b 0
f 0
nc 33
nop 0
dl 0
loc 42
rs 8.4444
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Helpers;
8
9
use Chamilo\CoreBundle\Exception\NotAllowedException;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
use Symfony\Component\Ldap\Entry;
12
use Symfony\Component\Ldap\Exception\InvalidCredentialsException;
13
use Symfony\Component\Ldap\Ldap;
14
15
readonly class LdapAuthenticatorHelper
16
{
17
    protected array $ldapConfig;
18
19
    public function __construct(
20
        private RequestStack $requestStack,
21
        private Ldap $ldap,
22
        AuthenticationConfigHelper $authConfigHelper,
23
    ) {
24
        $this->ldapConfig = $authConfigHelper->getLdapConfig();
25
    }
26
27
    /**
28
     * @return array<int, Entry>
29
     */
30
    private function queryAllUsers(): array
31
    {
32
        try {
33
            $this->ldap->bind($this->ldapConfig['search_dn'], $this->ldapConfig['search_password']);
34
        } catch (InvalidCredentialsException) {
35
            throw new NotAllowedException();
36
        }
37
38
        $request = $this->requestStack->getCurrentRequest();
39
        $dataCorrespondence = $this->ldapConfig['data_correspondence'];
40
41
        $keywordFirstname = trim($request->query->get('keyword_firstname', ''));
42
        $keywordLastname = trim($request->query->get('keyword_lastname', ''));
43
        $keywordUsername = trim($request->query->get('keyword_username', ''));
44
        $keywordType = trim($request->query->get('keyword_type', ''));
45
46
        $ldapQuery = [
47
            '(objectClass=inetOrgPerson)',
48
        ];
49
50
        if ($keywordUsername) {
51
            $ldapQuery[] = "(uid=$keywordUsername)";
52
        }
53
54
        if ($keywordLastname) {
55
            $ldapQuery[] = "({$dataCorrespondence['lastname']}=$keywordLastname*)";
56
        }
57
58
        if ($keywordFirstname) {
59
            $ldapQuery[] = "({$dataCorrespondence['firstname']}=$keywordFirstname*)";
60
        }
61
62
        if ($keywordType && 'all' !== $keywordType) {
63
            $ldapQuery[] = "(employeeType=$keywordType)";
64
        }
65
66
        $query = count($ldapQuery) > 1 ? '(& '.implode(' ', $ldapQuery).' )' : $ldapQuery[0];
67
68
        return $this->ldap
69
            ->query($this->ldapConfig['base_dn'], $query)
70
            ->execute()
71
            ->toArray();
72
    }
73
74
    /**
75
     * @return array<int, Entry>
76
     */
77
    private function queryByOu(string $ou): array
78
    {
79
        try {
80
            $this->ldap->bind($this->ldapConfig['search_dn'], $this->ldapConfig['search_password']);
81
        } catch (InvalidCredentialsException) {
82
            throw new NotAllowedException();
83
        }
84
85
        return $this->ldap
86
            ->query($this->ldapConfig['base_dn'], "(ou=*$ou)")
87
            ->execute()
88
            ->toArray();
89
    }
90
91
    public function countUsers(array $params): int
92
    {
93
        return count($this->queryAllUsers());
94
    }
95
96
    public function getAllUsers(int $from, int $numberOfItems, int $column, string $direction, array $params): array
97
    {
98
        $isWesternNameOrder = api_is_western_name_order();
99
        $ldapUsers = $this->queryAllUsers();
100
        $userIdentifier = $this->ldapConfig['uid_key'];
101
        $dataCorrespondence = $this->ldapConfig['data_correspondence'];
102
103
        $users = [];
104
105
        foreach ($ldapUsers as $ldapUser) {
106
            $user = [];
107
108
            $user[] = $ldapUser->getAttribute($userIdentifier)[0];
109
            $user[] = $ldapUser->getAttribute($userIdentifier)[0];
110
111
            if ($isWesternNameOrder) {
112
                $user[] = $ldapUser->getAttribute($dataCorrespondence['firstname'])[0];
113
                $user[] = $ldapUser->getAttribute($dataCorrespondence['lastname'])[0];
114
            } else {
115
                $user[] = $ldapUser->getAttribute($dataCorrespondence['lastname'])[0];
116
                $user[] = $ldapUser->getAttribute($dataCorrespondence['firstname'])[0];
117
            }
118
119
            $user[] = $ldapUser->getAttribute($dataCorrespondence['email'])[0];
120
            $user[] = $ldapUser->getAttribute($userIdentifier)[0];
121
122
            $users[] = $user;
123
        }
124
125
        return $users;
126
    }
127
128
    public function getUsersByOu(string $ou): array
129
    {
130
        $ldapUsers = $this->queryByOu($ou);
131
        $userIdentifier = $this->ldapConfig['uid_key'];
132
        $passwordAttribute = $this->ldapConfig['password_attribute'];
133
        $dataCorrespondence = $this->ldapConfig['data_correspondence'];
134
135
        $users = [];
136
137
        foreach ($ldapUsers as $ldapUser) {
138
            $users[] = [
139
                'username' => $ldapUser->getAttribute($userIdentifier)[0],
140
                'firstname' => $ldapUser->getAttribute($dataCorrespondence['firstname'])[0],
141
                'lastname' => $ldapUser->getAttribute($dataCorrespondence['lastname'])[0],
142
                'email' => $ldapUser->getAttribute($dataCorrespondence['email'])[0],
143
                'password' => $ldapUser->getAttribute($passwordAttribute)[0],
144
            ];
145
        }
146
147
        return $users;
148
    }
149
}