Roles Manager

Role Class

The class Role is a Role Representation. To use Role Manager, you need to pass the Realm that you will work with. You can also manager Client Roles too, check Client documentation.

for specifying which namespace and source file of Role scope
Class File
KeycloakAdmin\Roles\Role src/Roles/Role.php

List Roles

This method returns a collection of Roles of specific Realm.

Usage

public function listRoles()
{
    $collectionOfRoles = $this->admin
        ->role('my-realm')
        ->list();

    foreach ($collectionOfRoles as $role) {
        var_dump($role->getName());
    }
}

Save Role

This method returns will save a new Role on specific Realm.

Usage

public function createRole(string $id, string $name)
{
    $myRole = [
        'id'   => $id,
        'name' => $name
    ];

    $role = $this->admin->role('my-realm')
        ->createFromArray($myRole)
        ->save();
}

Show Role

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

Usage

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

Update Role

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

Usage

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

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

Delete Role

This method will delete a Role, 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->role('my-realm')
        ->delete($id);
    } catch (\Exception $e) {
        var_dump('cant delete this client, ' . $e->getMessage());
    }
}