From 21403995e3f30a46e0569fc65028ff810ae7cbec Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Wed, 21 Sep 2022 10:11:09 +0200 Subject: [PATCH] new: [user:edit] Added keycloak updates when a user gets modified --- src/Model/Behavior/AuthKeycloakBehavior.php | 24 +++++++++++++++++++++ src/Model/Table/UsersTable.php | 15 +++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/Model/Behavior/AuthKeycloakBehavior.php b/src/Model/Behavior/AuthKeycloakBehavior.php index 57e947f..1d76e13 100644 --- a/src/Model/Behavior/AuthKeycloakBehavior.php +++ b/src/Model/Behavior/AuthKeycloakBehavior.php @@ -145,6 +145,30 @@ class AuthKeycloakBehavior extends Behavior return true; } + /** + * handleUserUpdate + * + * @param \App\Model\Entity\User $user + * @return boolean If the update was a success + */ + public function handleUserUpdate(\App\Model\Entity\User $user): bool + { + $user['individual'] = $this->_table->Individuals->find()->where([ + 'id' => $user['individual_id'] + ])->first(); + $user['role'] = $this->_table->Roles->find()->where([ + 'id' => $user['role_id'] + ])->first(); + $user['organisation'] = $this->_table->Organisations->find()->where([ + 'id' => $user['organisation_id'] + ])->first(); + + $users = [$user->toArray()]; + $clientId = $this->getClientId(); + $changes = $this->syncUsers($users, $clientId); + return !empty($changes); + } + private function getAdminAccessToken() { $keycloakConfig = Configure::read('keycloak'); diff --git a/src/Model/Table/UsersTable.php b/src/Model/Table/UsersTable.php index 3a642bb..7584216 100644 --- a/src/Model/Table/UsersTable.php +++ b/src/Model/Table/UsersTable.php @@ -61,6 +61,12 @@ class UsersTable extends AppTable $data['username'] = trim(mb_strtolower($data['username'])); } + public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options) + { + $success = $this->handleUserUpdateRouter($entity); + return $success; + } + private function initAuthBehaviors() { if (!empty(Configure::read('keycloak'))) { @@ -208,4 +214,13 @@ class UsersTable extends AppTable $this->enrollUser($data); } } + + public function handleUserUpdateRouter(\App\Model\Entity\User $user): bool + { + if (!empty(Configure::read('keycloak'))) { + $success = $this->handleUserUpdate($user); + return $success; + } + return true; + } }