Skip to content
Snippets Groups Projects

Add Keycloak/Drupal user integrity check drush command

All threads resolved!
Files
2
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;
use Stevenmaguire\OAuth2\Client\Provider\Keycloak;
@@ -281,7 +282,7 @@ class KeycloakIntegration {
private function updateUser($user_id, $uuid) {
$account = user_load($user_id);
$style = 'grid-2';
$style = 'grid-2-2x-square';
if (is_numeric($account->picture)) {
$picture_path = file_load($account->picture)->uri;
}
@@ -320,7 +321,7 @@ class KeycloakIntegration {
}
// Per https://github.com/keycloak/keycloak/discussions/8552, this has
// to be a separate REST call.
$group_uuid = variable_get('drupalorg_keycloak_trusted_group', 'e327a320-6a04-4727-8ffa-322c920fc4c5');
$group_uuid = variable_get('drupalorg_keycloak_trusted_group', '2141a3e3-dd22-48bc-9023-11233f577d61');
$method = in_array(variable_get('drupalorg_crosssite_trusted_role', 36), $account->roles) ? 'PUT' : 'DELETE';
$result = $this->invoke(sprintf('admin/realms/%s/users/%s/groups/%s', $this->authOptions()['realm'], $uuid, $group_uuid), $method);
$success = \in_array($result->getStatusCode(), range(200, 299), TRUE);
@@ -342,4 +343,49 @@ class KeycloakIntegration {
}
return NULL;
}
public function getUsers($query_parameters = []) {
static $wait_seconds;
if (!isset($wait_seconds)) {
$wait_seconds = 2;
}
$this->authenticate();
$uri = sprintf('admin/realms/%s/users', $this->authOptions()['realm']);
if (!empty($query_parameters)) {
$uri .= '?' . http_build_query($query_parameters);
}
try {
$result = $this->invoke($uri, 'GET');
}
catch (ConnectException $e) {
if ($wait_seconds <= self::EXECUTE_TIME_LIMIT_SECONDS) {
sleep($wait_seconds);
$wait_seconds *= 2;
return $this->getUsers($query_parameters);
}
throw new \Exception(sprintf('User retrieval failed: "%s"', $e->getMessage()));
}
$result_code = $result->getStatusCode();
$result_body = (string) $result->getBody();
$success = \in_array($result_code, range(200, 299), TRUE);
if ($success) {
return $result_body;
}
throw new \Exception(sprintf('User retrieval failed (%d): "%s"', $result_code, $result_body));
}
public function getGroupMembers($group_uuid, $query_parameters = []) {
$this->authenticate();
$uri = sprintf('admin/realms/%s/groups/%s/members', $this->authOptions()['realm'], $group_uuid);
$query_parameters += [
'briefRepresentation' => TRUE,
];
$uri .= '?' . http_build_query($query_parameters);
$result = $this->invoke($uri, 'GET');
$success = \in_array($result->getStatusCode(), range(200, 299), TRUE);
if ($success) {
return (string) $result->getBody();
}
}
}
Loading