diff --git a/src/Model/Behavior/AuthKeycloakBehavior.php b/src/Model/Behavior/AuthKeycloakBehavior.php index d958ee5..6550dae 100644 --- a/src/Model/Behavior/AuthKeycloakBehavior.php +++ b/src/Model/Behavior/AuthKeycloakBehavior.php @@ -150,15 +150,12 @@ class AuthKeycloakBehavior extends Behavior foreach ($roles as $role) { $rolesParsed[$role['name']] = $role['id']; } - $this->createUser($user, $clientId, $rolesParsed); - $logChange = [ - 'username' => $user['username'], - 'individual_id' => $user['individual']['id'], - 'role_id' => $user['role']['id'] - ]; - if (!$response->isOk()) { - $logChange['code'] = $response->getStatusCode(); - $logChange['error_body'] = $response->getStringBody(); + if ($this->createUser($user, $clientId, $rolesParsed)) { + $logChange = [ + 'username' => $user['username'], + 'individual_id' => $user['individual']['id'], + 'role_id' => $user['role']['id'] + ]; $this->_table->auditLogs()->insert([ 'request_action' => 'enrollUser', 'model' => 'User', @@ -167,6 +164,11 @@ class AuthKeycloakBehavior extends Behavior 'changed' => $logChange ]); } else { + $logChange = [ + 'username' => $user['username'], + 'individual_id' => $user['individual']['id'], + 'role_id' => $user['role']['id'] + ]; $this->_table->auditLogs()->insert([ 'request_action' => 'enrollUser', 'model' => 'User', @@ -406,10 +408,14 @@ class AuthKeycloakBehavior extends Behavior ]); } $newUser = $this->restApiRequest('%s/admin/realms/%s/users?username=' . urlencode($user['username']), [], 'get'); - $user['id'] = json_decode($newUser->getStringBody(), true); - if (empty($user['id'])) { + $users = json_decode($newUser->getStringBody(), true); + if (empty($users[0]['id'])) { return false; } + if (is_array($users[0]['id'])) { + $users[0]['id'] = $users[0]['id'][0]; + } + $user['id'] = $users[0]['id']; $this->assignRolesToUser($user, $rolesParsed, $clientId); return true; }