From 274f9f28cd6deb461c8da12a0ce746b9ebdefb6a Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos <1697880+AngelFQC@users.noreply.github.com> Date: Fri, 13 Sep 2024 16:53:13 -0500 Subject: [PATCH] Plugin: Azure: Move methods to parent class - refs BT#21930 --- .../src/AzureCommand.php | 100 ++++++++++++++++++ .../src/AzureSyncUsergroupsCommand.php | 45 -------- .../src/AzureSyncUsersCommand.php | 55 ---------- 3 files changed, 100 insertions(+), 100 deletions(-) diff --git a/plugin/azure_active_directory/src/AzureCommand.php b/plugin/azure_active_directory/src/AzureCommand.php index 6a14c94e076..1c0ccd3e7b3 100644 --- a/plugin/azure_active_directory/src/AzureCommand.php +++ b/plugin/azure_active_directory/src/AzureCommand.php @@ -37,6 +37,106 @@ protected function generateOrRefreshToken(?AccessTokenInterface &$token) } } + /** + * @throws Exception + * + * @return Generator> + */ + protected function getAzureUsers(): Generator + { + $userFields = [ + 'givenName', + 'surname', + 'mail', + 'userPrincipalName', + 'businessPhones', + 'mobilePhone', + 'accountEnabled', + 'mailNickname', + 'id', + ]; + + $query = sprintf( + '$top=%d&$select=%s', + AzureActiveDirectory::API_PAGE_SIZE, + implode(',', $userFields) + ); + + $token = null; + + do { + $this->generateOrRefreshToken($token); + + try { + $azureUsersRequest = $this->provider->request( + 'get', + "users?$query", + $token + ); + } catch (Exception $e) { + throw new Exception('Exception when requesting users from Azure: '.$e->getMessage()); + } + + $azureUsersInfo = $azureUsersRequest['value'] ?? []; + + foreach ($azureUsersInfo as $azureUserInfo) { + yield $azureUserInfo; + } + + $hasNextLink = false; + + if (!empty($azureUsersRequest['@odata.nextLink'])) { + $hasNextLink = true; + $query = parse_url($azureUsersRequest['@odata.nextLink'], PHP_URL_QUERY); + } + } while ($hasNextLink); + } + + /** + * @throws Exception + * + * @return Generator> + */ + protected function getAzureGroups(): Generator + { + $groupFields = [ + 'id', + 'displayName', + 'description', + ]; + + $query = sprintf( + '$top=%d&$select=%s', + AzureActiveDirectory::API_PAGE_SIZE, + implode(',', $groupFields) + ); + + $token = null; + + do { + $this->generateOrRefreshToken($token); + + try { + $azureGroupsRequest = $this->provider->request('get', "groups?$query", $token); + } catch (Exception $e) { + throw new Exception('Exception when requesting groups from Azure: '.$e->getMessage()); + } + + $azureGroupsInfo = $azureGroupsRequest['value'] ?? []; + + foreach ($azureGroupsInfo as $azureGroupInfo) { + yield $azureGroupInfo; + } + + $hasNextLink = false; + + if (!empty($azureGroupsRequest['@odata.nextLink'])) { + $hasNextLink = true; + $query = parse_url($azureGroupsRequest['@odata.nextLink'], PHP_URL_QUERY); + } + } while ($hasNextLink); + } + /** * @return Generator> * diff --git a/plugin/azure_active_directory/src/AzureSyncUsergroupsCommand.php b/plugin/azure_active_directory/src/AzureSyncUsergroupsCommand.php index 2fe7c0508f6..3f51db2736b 100644 --- a/plugin/azure_active_directory/src/AzureSyncUsergroupsCommand.php +++ b/plugin/azure_active_directory/src/AzureSyncUsergroupsCommand.php @@ -67,49 +67,4 @@ public function __invoke(): Generator } } } - - /** - * @throws Exception - * - * @return Generator> - */ - private function getAzureGroups(): Generator - { - $groupFields = [ - 'id', - 'displayName', - 'description', - ]; - - $query = sprintf( - '$top=%d&$select=%s', - AzureActiveDirectory::API_PAGE_SIZE, - implode(',', $groupFields) - ); - - $token = null; - - do { - $this->generateOrRefreshToken($token); - - try { - $azureGroupsRequest = $this->provider->request('get', "groups?$query", $token); - } catch (Exception $e) { - throw new Exception('Exception when requesting groups from Azure: '.$e->getMessage()); - } - - $azureGroupsInfo = $azureGroupsRequest['value'] ?? []; - - foreach ($azureGroupsInfo as $azureGroupInfo) { - yield $azureGroupInfo; - } - - $hasNextLink = false; - - if (!empty($azureGroupsRequest['@odata.nextLink'])) { - $hasNextLink = true; - $query = parse_url($azureGroupsRequest['@odata.nextLink'], PHP_URL_QUERY); - } - } while ($hasNextLink); - } } diff --git a/plugin/azure_active_directory/src/AzureSyncUsersCommand.php b/plugin/azure_active_directory/src/AzureSyncUsersCommand.php index 97bf5f1bb44..748204680ba 100644 --- a/plugin/azure_active_directory/src/AzureSyncUsersCommand.php +++ b/plugin/azure_active_directory/src/AzureSyncUsersCommand.php @@ -88,59 +88,4 @@ function ($user) { ); } } - - /** - * @throws Exception - * - * @return Generator> - */ - private function getAzureUsers(): Generator - { - $userFields = [ - 'givenName', - 'surname', - 'mail', - 'userPrincipalName', - 'businessPhones', - 'mobilePhone', - 'accountEnabled', - 'mailNickname', - 'id', - ]; - - $query = sprintf( - '$top=%d&$select=%s', - AzureActiveDirectory::API_PAGE_SIZE, - implode(',', $userFields) - ); - - $token = null; - - do { - $this->generateOrRefreshToken($token); - - try { - $azureUsersRequest = $this->provider->request( - 'get', - "users?$query", - $token - ); - } catch (Exception $e) { - throw new Exception('Exception when requesting users from Azure: '.$e->getMessage()); - } - - $azureUsersInfo = $azureUsersRequest['value'] ?? []; - - foreach ($azureUsersInfo as $azureUserInfo) { - yield $azureUserInfo; - } - - $hasNextLink = false; - - if (!empty($azureUsersRequest['@odata.nextLink'])) { - $hasNextLink = true; - $query = parse_url($azureUsersRequest['@odata.nextLink'], PHP_URL_QUERY); - } - } while ($hasNextLink); - } }