Users Manager

User Class

The class User is a User Representation. To use User Manager, you need to pass the Realm that you will work with.

for specifying which namespace and source file of user
Class File
KeycloakAdmin\Users\User src/Users/User.php

List Users

This method returns a collection of Users of specific Realm.

Usage

public function listUsers()
{
    $collectionOfUsers = $this->admin
        ->user('my-realm')
        ->list();

    foreach ($collectionOfUsers as $user) {
        var_dump($user->getName());
    }
}

Save User

This method returns will save a new User on specific Realm. Must have at least username, in their API this field is optional, but in truth is required.

Usage

public function createUser(string $id, string $name, string $username)
{
    $myUser = [
        'id'   => $id,
        'name' => $name,
        'username' => 'myusername'
    ];

    $user = $this->admin->user('my-realm')
        ->createFromArray($myUser)
        ->save();
}

Show User

This method returns a User, you need to pass the parameter realmName and must pass the $id. If User was not found, you will receive an Exception.

Usage

public function showUser(string $id) : Client
{
    return $this->admin->user('my-realm')
        ->show($id);
}

Update User

This method will update a User, an instance of User must be created before call to update. This expects at least id to can update. In this Library, you can’t update the User id.

Usage

public function update(string $id, array $params = []) : Client
{
    $data = [
        'id' => $id
    ] + $params;

    return $this->admin->user('my-realm')
        ->createFromArray($data)
        ->update();
}

Delete User

This method will delete a User, id must be passed as parameter. If an error occurs you will receive an Exception otherwise, this is a void method

Usage

public function delete(string $id)
{
    try {
        $this->admin->user('my-realm')
        ->delete($id);
    } catch (\Exception $e) {
        var_dump('cant delete this client, ' . $e->getMessage());
    }
}

Reset Password

This method will reset a User Password, id must be passed as parameter. If an error occurs you will receive an Exception otherwise, this is a void method

Usage

public function changePass(string $id, string $value)
{
    $json = '{ "type": "password", "temporary": false, "value": "' . $value . '" }';

    try {
        $this->admin->user('my-realm')
        ->resetPassword($id)
        ->createFromJson($json)
        ->save()
    } catch (\Exception $e) {
        var_dump('cant change this user password, ' . $e->getMessage());
    }
}

Logout

This method will logout all User sessions. Pass Id as parameter.

Usage

public function logout(string $id)
{
    $this->admin->user('my-realm')->logout($id);
}