assign role to user

This commit is contained in:
Viktor Scharf
2022-11-18 17:17:32 +01:00
parent 1329daffc7
commit 34e4f0f1dc
5 changed files with 336 additions and 70 deletions
@@ -687,76 +687,6 @@ class SpacesContext implements Context {
$this->setSpaceCreator($spaceName, $user);
}
/**
* @When /^the administrator has given "([^"]*)" the role "([^"]*)" using the settings api$/
*
* @param string $user
* @param string $role
*
* @return void
*
* @throws GuzzleException
* @throws Exception
*/
public function theAdministratorGivesUserTheRole(string $user, string $role): void {
$admin = $this->featureContext->getAdminUsername();
$password = $this->featureContext->getAdminPassword();
$bundles = [];
$assignment = [];
// get the roles list first
$fullUrl = $this->baseUrl . "/api/v0/settings/roles-list";
$this->featureContext->setResponse($this->sendPostRequestToUrl($fullUrl, $admin, $password, "{}"));
if ($this->featureContext->getResponse()) {
$rawBody = $this->featureContext->getResponse()->getBody()->getContents();
if (isset(\json_decode($rawBody, true, 512, JSON_THROW_ON_ERROR)["bundles"])) {
$bundles = \json_decode($rawBody, true, 512, JSON_THROW_ON_ERROR)["bundles"];
}
}
$roleToAssign = "";
foreach ($bundles as $value) {
// find the selected role
if ($value["displayName"] === $role) {
$roleToAssign = $value;
}
}
Assert::assertNotEmpty($roleToAssign, "The selected role $role could not be found");
$this->featureContext->setResponse(
GraphHelper::getUser(
$this->featureContext->getBaseUrl(),
$this->featureContext->getStepLineRef(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
$user
)
);
if ($this->featureContext->getResponse()) {
$rawBody = $this->featureContext->getResponse()->getBody()->getContents();
if (isset(\json_decode($rawBody, true, 512, JSON_THROW_ON_ERROR)["id"])) {
$accountToChange = \json_decode($rawBody, true, 512, JSON_THROW_ON_ERROR);
}
}
Assert::assertNotEmpty($accountToChange, "The selected account $user does not exist");
// set the new role
$fullUrl = $this->baseUrl . "/api/v0/settings/assignments-add";
$body = json_encode(["account_uuid" => $accountToChange["id"], "role_id" => $roleToAssign["id"]], JSON_THROW_ON_ERROR);
$this->featureContext->setResponse($this->sendPostRequestToUrl($fullUrl, $admin, $password, $body));
if ($this->featureContext->getResponse()) {
$rawBody = $this->featureContext->getResponse()->getBody()->getContents();
if (isset(\json_decode($rawBody, true, 512, JSON_THROW_ON_ERROR)["assignment"])) {
$assignment = \json_decode($rawBody, true, 512, JSON_THROW_ON_ERROR)["assignment"];
}
}
Assert::assertEquals($accountToChange["id"], $assignment["accountUuid"]);
Assert::assertEquals($roleToAssign["id"], $assignment["roleId"]);
}
/**
* Remember the available Spaces
*