Skip to content

Commit

Permalink
feat: add organizations resource
Browse files Browse the repository at this point in the history
  • Loading branch information
fschmtt committed Dec 7, 2024
1 parent e865fcb commit 558bda2
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Http/CommandExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Fschmtt\Keycloak\Serializer\Serializer;

use function is_array;

/**
* @internal
*/
Expand All @@ -18,6 +20,20 @@ public function __construct(

public function executeCommand(Command $command): void
{
$payload = $command->getPayload();

if (is_array($payload)) {
$this->client->request(
$command->getMethod()->value,
$command->getPath(),
[
'form_params' => $payload,
],
);

return;
}

$this->client->request(
$command->getMethod()->value,
$command->getPath(),
Expand Down
8 changes: 8 additions & 0 deletions src/Keycloak.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Fschmtt\Keycloak\Resource\AttackDetection;
use Fschmtt\Keycloak\Resource\Clients;
use Fschmtt\Keycloak\Resource\Groups;
use Fschmtt\Keycloak\Resource\Organizations;
use Fschmtt\Keycloak\Resource\Realms;
use Fschmtt\Keycloak\Resource\Resource;
use Fschmtt\Keycloak\Resource\Roles;
Expand Down Expand Up @@ -114,6 +115,13 @@ public function roles(): Roles
return new Roles($this->commandExecutor, $this->queryExecutor);
}

public function organizations(): Organizations
{
$this->fetchVersion();

return new Organizations($this->commandExecutor, $this->queryExecutor);
}

/**
* @param class-string<Resource> $resource
* @return Resource
Expand Down
65 changes: 65 additions & 0 deletions src/Resource/Organizations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Fschmtt\Keycloak\Resource;

use Fschmtt\Keycloak\Collection\OrganizationCollection;
use Fschmtt\Keycloak\Http\Command;
use Fschmtt\Keycloak\Http\Criteria;
use Fschmtt\Keycloak\Http\Method;
use Fschmtt\Keycloak\Http\Query;
use Fschmtt\Keycloak\Representation\Organization;

class Organizations extends Resource
{
public function all(string $realm, Criteria $criteria): OrganizationCollection
{
return $this->queryExecutor->executeQuery(
new Query(
'/admin/realms/{realm}/organizations',
OrganizationCollection::class,
['realm' => $realm],
$criteria,
),
);
}

public function get(string $realm, string $id): Organization
{
return $this->queryExecutor->executeQuery(
new Query(
'/admin/realms/{realm}/organizations/{id}',
Organization::class,
['realm' => $realm, 'id' => $id],
),
);
}

public function delete(string $realm, string $id): void
{
$this->commandExecutor->executeCommand(
new Command(
'/admin/realms/{realm}/organizations/{id}',
Method::DELETE,
['realm' => $realm, 'id' => $id],
),
);
}

public function inviteUser(string $realm, string $id, string $email, string $firstName, string $lastName): void
{
$this->commandExecutor->executeCommand(
new Command(
'/admin/realms/{realm}/organizations/{id}/members/invite-user',
Method::POST,
['realm' => $realm, 'id' => $id],
payload: [
'email' => $email,
'firstName' => $firstName,
'lastName' => $lastName,
],
),
);
}
}

0 comments on commit 558bda2

Please sign in to comment.