From d293cb52f80529372bdd21279bc6d1f2db8db534 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Mon, 12 Dec 2022 16:56:51 +0100 Subject: [PATCH] chg: [behavior:keycloak] Gracefully handle issues while syncing with keycloak --- src/Model/Behavior/AuthKeycloakBehavior.php | 26 +++++++++++++++------ 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Model/Behavior/AuthKeycloakBehavior.php b/src/Model/Behavior/AuthKeycloakBehavior.php index 6b8904b..60bebbb 100644 --- a/src/Model/Behavior/AuthKeycloakBehavior.php +++ b/src/Model/Behavior/AuthKeycloakBehavior.php @@ -297,14 +297,26 @@ class AuthKeycloakBehavior extends Behavior 'modified' => [], ]; foreach ($users as &$user) { - if (empty($keycloakUsersParsed[$user['username']])) { - if ($this->createUser($user, $clientId)) { - $changes['created'][] = $user['username']; - } - } else { - if ($this->checkAndUpdateUser($keycloakUsersParsed[$user['username']], $user)) { - $changes['modified'][] = $user['username']; + try { + if (empty($keycloakUsersParsed[$user['username']])) { + if ($this->createUser($user, $clientId)) { + $changes['created'][] = $user['username']; + } + } else { + if ($this->checkAndUpdateUser($keycloakUsersParsed[$user['username']], $user)) { + $changes['modified'][] = $user['username']; + } } + } catch (\Exception $e) { + $this->_table->auditLogs()->insert([ + 'request_action' => 'syncUsers', + 'model' => 'User', + 'model_id' => 0, + 'model_title' => __('Failed to create or modify user ({0}) in keycloak', $user['username']), + 'changed' => [ + 'message' => $e->getMessage(), + ] + ]); } } return $changes;