Skip to content

Commit

Permalink
Plugin: Azure: Move methods to parent class - refs BT#21930
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Sep 13, 2024
1 parent 90588f2 commit 274f9f2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 100 deletions.
100 changes: 100 additions & 0 deletions plugin/azure_active_directory/src/AzureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,106 @@ protected function generateOrRefreshToken(?AccessTokenInterface &$token)
}
}

/**
* @throws Exception
*
* @return Generator<int, array<string, string>>
*/
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<int, array<string, string>>
*/
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<int, array<string, string>>
*
Expand Down
45 changes: 0 additions & 45 deletions plugin/azure_active_directory/src/AzureSyncUsergroupsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,49 +67,4 @@ public function __invoke(): Generator
}
}
}

/**
* @throws Exception
*
* @return Generator<int, array<string, string>>
*/
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);
}
}
55 changes: 0 additions & 55 deletions plugin/azure_active_directory/src/AzureSyncUsersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,59 +88,4 @@ function ($user) {
);
}
}

/**
* @throws Exception
*
* @return Generator<int, array<string, string>>
*/
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);
}
}

0 comments on commit 274f9f2

Please sign in to comment.