Skip to content

Commit

Permalink
Add endpoint for listing users with external-groups with specific prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
forevermatt committed Aug 28, 2024
1 parent 81d6473 commit bb033db
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
28 changes: 28 additions & 0 deletions application/common/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,34 @@ public function getNagState()
return $this->nagState->getState();
}

public static function listExternalGroups($appPrefix): array
{
/** @var User[] $users */
$users = User::find()->where(
['like', 'groups_external', $appPrefix . '-']
)->all();

$responseData = [];
foreach ($users as $user) {
$userIsMatch = false;
$externalGroups = explode(',', $user->groups_external);
$externalGroupsWithAppPrefix = [];
foreach ($externalGroups as $externalGroup) {
if (str_starts_with($externalGroup, $appPrefix . '-')) {
$userIsMatch = true;
$externalGroupsWithAppPrefix[] = $externalGroup;
}
}
if ($userIsMatch) {
$responseData[] = [
'email' => $user->email,
'groups' => $externalGroupsWithAppPrefix,
];
}
}
return $responseData;
}

public function loadMfaData(string $rpOrigin = '')
{
$verifiedMfaOptions = $this->getVerifiedMfaOptions($rpOrigin);
Expand Down
20 changes: 15 additions & 5 deletions application/features/bootstrap/GroupsExternalListContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Sil\SilIdBroker\Behat\Context;

use Behat\Behat\Tester\Exception\PendingException;
use Behat\Gherkin\Node\TableNode;
use Webmozart\Assert\Assert;

class GroupsExternalListContext extends GroupsExternalContext
{
Expand Down Expand Up @@ -35,18 +35,28 @@ public function iGetTheListOfUsersWithExternalGroups($appPrefix)
$this->cleanRequestBody();

$urlPath = sprintf(
'/user/external-groups/?app_prefix=%s',
'/user/external-groups?app_prefix=%s',
urlencode($appPrefix),
);

$this->iRequestTheResourceBe($urlPath, 'retrieved');
}

/**
* @Then the response body should contain only the following entries:
* @Then the response should only include the following users and groups:
*/
public function theResponseBodyShouldContainOnlyTheFollowingEntries(TableNode $table)
public function theResponseShouldOnlyIncludeTheFollowingUsersAndGroups(TableNode $table)
{
throw new PendingException();
$expected = [];
foreach ($table as $row) {
$expected[] = [
'email' => $row['email'],
'groups' => explode(',', $row['groups']),
];
}
Assert::eq(
json_encode($this->getResponseBody(), JSON_PRETTY_PRINT),
json_encode($expected, JSON_PRETTY_PRINT)
);
}
}
2 changes: 1 addition & 1 deletion application/features/groups-external-list.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Feature: Getting a list of Users with external groups with a given prefix
| bob_mcmanager@example.org | map-america,map-europe |
When I get the list of users with "wiki" external groups
Then the response status code should be 200
And the response body should contain only the following entries:
And the response should only include the following users and groups:
| email | groups |
| john_smith@example.org | wiki-one |
| jane_doe@example.org | wiki-one,wiki-two |
Expand Down
1 change: 1 addition & 0 deletions application/frontend/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'GET user' => 'user/index',
'GET user/<employeeId:\w+>' => 'user/view',
'POST user' => 'user/create',
'GET user/external-groups' => 'user/list-external-groups',
'PUT user/external-groups/<email:[a-z._-]+@[a-z._-]+>' => 'user/update-external-groups',
'PUT user/<employeeId:\w+>' => 'user/update',
'PUT user/<employeeId:\w+>/password' => 'user/update-password',
Expand Down
11 changes: 11 additions & 0 deletions application/frontend/controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ public function actionCreate(): User
return $user;
}

public function actionListExternalGroups()
{
$appPrefix = Yii::$app->request->getQueryParam('app_prefix');

if (empty($appPrefix)) {
throw new UnprocessableEntityHttpException('No app prefix provided.');
}

return User::listExternalGroups($appPrefix);
}

public function actionUpdate(string $employeeId)
{
$user = User::findOne(['employee_id' => $employeeId]);
Expand Down

0 comments on commit bb033db

Please sign in to comment.