Passed
Pull Request — 1.11.x (#6003)
by Angel Fernando Quiroz
09:09
created

AzureCommand   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 22
eloc 95
c 3
b 0
f 0
dl 0
loc 186
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A generateOrRefreshToken() 0 6 4
A getAzureUsers() 0 48 5
A getAzureGroupMembers() 0 42 5
B getAzureGroups() 0 46 7
1
<?php
2
3
/* For license terms, see /license.txt */
4
5
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
6
use League\OAuth2\Client\Token\AccessTokenInterface;
7
use TheNetworg\OAuth2\Client\Provider\Azure;
8
9
abstract class AzureCommand
10
{
11
    /**
12
     * @var AzureActiveDirectory
13
     */
14
    protected $plugin;
15
    /**
16
     * @var Azure
17
     */
18
    protected $provider;
19
20
    public function __construct()
21
    {
22
        $this->plugin = AzureActiveDirectory::create();
23
        $this->plugin->get_settings(true);
24
        $this->provider = $this->plugin->getProviderForApiGraph();
25
    }
26
27
    /**
28
     * @throws IdentityProviderException
29
     */
30
    protected function generateOrRefreshToken(?AccessTokenInterface &$token)
31
    {
32
        if (!$token || ($token->getExpires() && !$token->getRefreshToken())) {
33
            $token = $this->provider->getAccessToken(
34
                'client_credentials',
35
                ['resource' => $this->provider->resource]
36
            );
37
        }
38
    }
39
40
    /**
41
     * @throws Exception
42
     *
43
     * @return Generator<int, array<string, string>>
44
     */
45
    protected function getAzureUsers(): Generator
46
    {
47
        $userFields = [
48
            'givenName',
49
            'surname',
50
            'mail',
51
            'userPrincipalName',
52
            'businessPhones',
53
            'mobilePhone',
54
            'accountEnabled',
55
            'mailNickname',
56
            'id',
57
        ];
58
59
        $query = sprintf(
60
            '$top=%d&$select=%s',
61
            AzureActiveDirectory::API_PAGE_SIZE,
62
            implode(',', $userFields)
63
        );
64
65
        $token = null;
66
67
        do {
68
            $this->generateOrRefreshToken($token);
69
70
            try {
71
                $azureUsersRequest = $this->provider->request(
72
                    'get',
73
                    "users?$query",
74
                    $token
75
                );
76
            } catch (Exception $e) {
77
                throw new Exception('Exception when requesting users from Azure: '.$e->getMessage());
78
            }
79
80
            $azureUsersInfo = $azureUsersRequest['value'] ?? [];
81
82
            foreach ($azureUsersInfo as $azureUserInfo) {
83
                yield $azureUserInfo;
84
            }
85
86
            $hasNextLink = false;
87
88
            if (!empty($azureUsersRequest['@odata.nextLink'])) {
89
                $hasNextLink = true;
90
                $query = parse_url($azureUsersRequest['@odata.nextLink'], PHP_URL_QUERY);
91
            }
92
        } while ($hasNextLink);
93
    }
94
95
    /**
96
     * @throws Exception
97
     *
98
     * @return Generator<int, array<string, string>>
99
     */
100
    protected function getAzureGroups(): Generator
101
    {
102
        $groupFilter = $this->plugin->get(AzureActiveDirectory::SETTING_GROUP_FILTER);
103
104
        $groupFields = [
105
            'id',
106
            'displayName',
107
            'description',
108
        ];
109
110
        $query = sprintf(
111
            '$top=%d&$select=%s',
112
            AzureActiveDirectory::API_PAGE_SIZE,
113
            implode(',', $groupFields)
114
        );
115
116
        $token = null;
117
118
        do {
119
            $this->generateOrRefreshToken($token);
120
121
            try {
122
                $azureGroupsRequest = $this->provider->request('get', "groups?$query", $token);
123
            } catch (Exception $e) {
124
                throw new Exception('Exception when requesting groups from Azure: '.$e->getMessage());
125
            }
126
127
            $azureGroupsInfo = $azureGroupsRequest['value'] ?? [];
128
129
            foreach ($azureGroupsInfo as $azureGroupInfo) {
130
                if (!empty($groupFilter) &&
131
                    !preg_match("/$groupFilter/", $azureGroupInfo['displayName'])
132
                ) {
133
                    continue;
134
                }
135
136
                yield $azureGroupInfo;
137
            }
138
139
            $hasNextLink = false;
140
141
            if (!empty($azureGroupsRequest['@odata.nextLink'])) {
142
                $hasNextLink = true;
143
                $query = parse_url($azureGroupsRequest['@odata.nextLink'], PHP_URL_QUERY);
144
            }
145
        } while ($hasNextLink);
146
    }
147
148
    /**
149
     * @throws Exception
150
     *
151
     * @return Generator<int, array<string, string>>
152
     */
153
    protected function getAzureGroupMembers(string $groupUid): Generator
154
    {
155
        $userFields = [
156
            'mail',
157
            'mailNickname',
158
            'id',
159
        ];
160
161
        $query = sprintf(
162
            '$top=%d&$select=%s',
163
            AzureActiveDirectory::API_PAGE_SIZE,
164
            implode(',', $userFields)
165
        );
166
167
        $token = null;
168
169
        do {
170
            $this->generateOrRefreshToken($token);
171
172
            try {
173
                $azureGroupMembersRequest = $this->provider->request(
174
                    'get',
175
                    "groups/$groupUid/members?$query",
176
                    $token
177
                );
178
            } catch (Exception $e) {
179
                throw new Exception('Exception when requesting group members from Azure: '.$e->getMessage());
180
            }
181
182
            $azureGroupMembers = $azureGroupMembersRequest['value'] ?? [];
183
184
            foreach ($azureGroupMembers as $azureGroupMember) {
185
                yield $azureGroupMember;
186
            }
187
188
            $hasNextLink = false;
189
190
            if (!empty($azureGroupMembersRequest['@odata.nextLink'])) {
191
                $hasNextLink = true;
192
                $query = parse_url($azureGroupMembersRequest['@odata.nextLink'], PHP_URL_QUERY);
193
            }
194
        } while ($hasNextLink);
195
    }
196
}
197