PHP code example of saifulferoz / keycloak-admin-bundle

1. Go to this page and download the library: Download saifulferoz/keycloak-admin-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

saifulferoz / keycloak-admin-bundle example snippets


return [
    // ...
    Feroz\KeycloakAdminBundle\KeycloakAdminBundle::class => ['all' => true],
];

namespace App\Controller;

use Feroz\KeycloakAdmin\KeycloakClientInterface;
use Feroz\KeycloakAdmin\Model\User\UserRepresentation;
use Feroz\KeycloakAdminBundle\Factory\KeycloakClientRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;

final class UserController extends AbstractController
{
    #[Route('/api/users', methods: ['POST'])]
    public function createUser(KeycloakClientInterface $keycloak): JsonResponse
    {
        $user = UserRepresentation::create('[email protected]', 'username')
            ->withName('First', 'Last');

        $userId = $keycloak->users()->create($user);

        return new JsonResponse(['id' => $userId], 201);
    }

    #[Route('/api/partner/users', methods: ['GET'])]
    public function listPartnerUsers(KeycloakClientRegistry $registry): JsonResponse
    {
        $partnerClient = $registry->getClient('partner_tenant');
        $users = $partnerClient->users()->list();

        return new JsonResponse(array_map(fn($u) => $u->toArray(), $users->getItems()));
    }
}