From 2dd34afaf06b9cd57dcb17995d90effcbee6db9e Mon Sep 17 00:00:00 2001 From: elbacalau Date: Tue, 17 Dec 2024 21:07:10 +0100 Subject: [PATCH] feat: consolidate user update functionality by merging update and updatePassword methods in AccountController --- .../Controllers/Admin/AccountController.php | 56 ++++-------------- .../Admin/AccountConfig/AccountConfig.php | 59 +++++++------------ app/src/public/assets/js/app.js | 41 ------------- app/src/routes/admin.php | 6 +- 4 files changed, 32 insertions(+), 130 deletions(-) diff --git a/app/src/app/Controllers/Admin/AccountController.php b/app/src/app/Controllers/Admin/AccountController.php index 6af631f..985dd19 100644 --- a/app/src/app/Controllers/Admin/AccountController.php +++ b/app/src/app/Controllers/Admin/AccountController.php @@ -25,7 +25,6 @@ public function index($queryParams) public function update($id, $postData) { - $user = User::find(id: $id); if (!$user) { @@ -34,61 +33,28 @@ public function update($id, $postData) exit; } + $user->name = $postData['name']; $user->surname = $postData['surname']; + + // detect password changes + if (!empty($postData['password'])) { + $user->password = password_hash($postData['password'], PASSWORD_DEFAULT); + } + + $user->save(); - // **warning: recharge the user session with the new data + Session::set('user', [ 'id' => $user->getId(), 'name' => $user->name, - 'surname' => $user->surname[0], + 'surname' => $user->surname, 'email' => $user->email, 'role' => $user->role, - ]); - Session::set('success', 'Usuario actualizado correctamente'); - - - - header('Location: /admin/configuration'); - } - - // method to update the password - public function updatePassword($id, $postData) - { - - $user = User::find(id: $id); - - - if (!$user) { - Session::set('error', 'Usuario no encontrado'); - header('Location: /admin/configuration'); - exit; - } - - if (!password_verify($postData['current_password'], $user->password)) { - Session::set('error', 'La contraseña actual es incorrecta'); - header('Location: /admin/configuration'); - exit; - } - - - if ($postData['password'] !== $postData['password_confirmation']) { - Session::set('error', 'Las contraseñas no coinciden'); - header('Location: /admin/configuration'); - exit; - } - - - $user->password = password_hash($postData['password'], PASSWORD_DEFAULT); - $user->save(); - - session_unset(); - session_destroy(); - - Session::set('success', 'Contraseña actualizada correctamente'); + Session::set('success', 'Usuario y/o contraseña actualizados correctamente'); header('Location: /admin/configuration'); } } diff --git a/app/src/app/Views/Admin/AccountConfig/AccountConfig.php b/app/src/app/Views/Admin/AccountConfig/AccountConfig.php index 909c809..baa5589 100644 --- a/app/src/app/Views/Admin/AccountConfig/AccountConfig.php +++ b/app/src/app/Views/Admin/AccountConfig/AccountConfig.php @@ -1,8 +1,8 @@

Configuración

Aquí podrás configurar tu cuenta.

-
-

Informacíon personal

+
+

Informacíon personal

@@ -73,27 +73,9 @@ class="mt-1 px-3 py-2 border rounded-md text-gray-600 bg-gray-100 cursor-not-all
- -
- -
- - - - - -
-
-

Cambiar contraseña

- -
-
+ +

Cambiar contraseña

+
+ oninput="checkChanges()">
@@ -110,7 +92,7 @@ class="mt-1 px-3 py-2 border rounded-md text-gray-600 bg-gray-100" id="new-password" name="password" class="mt-1 px-3 py-2 border rounded-md text-gray-600 bg-gray-100" - oninput="checkPasswordFields()"> + oninput="checkChanges()">
@@ -119,23 +101,22 @@ class="mt-1 px-3 py-2 border rounded-md text-gray-600 bg-gray-100" id="confirm-password" name="password_confirmation" class="mt-1 px-3 py-2 border rounded-md text-gray-600 bg-gray-100" - oninput="checkPasswordFields()"> -
- - -
- + oninput="checkChanges()">
- -
+ +
+ +
+ + \ No newline at end of file diff --git a/app/src/public/assets/js/app.js b/app/src/public/assets/js/app.js index eeb0124..800fd42 100644 --- a/app/src/public/assets/js/app.js +++ b/app/src/public/assets/js/app.js @@ -420,44 +420,3 @@ function checkChanges() { ); } } - -function checkPasswordFields() { - const currentPassword = document - .getElementById("current-password") - .value.trim(); - const newPassword = document.getElementById("new-password").value.trim(); - const confirmPassword = document - .getElementById("confirm-password") - .value.trim(); - const button = document.getElementById("button-save-password"); - - if ( - currentPassword !== "" && - newPassword !== "" && - confirmPassword !== "" - ) { - button.disabled = false; - button.classList.remove( - "bg-gray-300", - "cursor-not-allowed", - "text-gray-500" - ); - button.classList.add( - "bg-green-500", - "hover:bg-green-600", - "text-white" - ); - } else { - button.disabled = true; - button.classList.add( - "bg-gray-300", - "cursor-not-allowed", - "text-gray-500" - ); - button.classList.remove( - "bg-green-500", - "hover:bg-green-600", - "text-white" - ); - } -} diff --git a/app/src/routes/admin.php b/app/src/routes/admin.php index 6770922..4384543 100644 --- a/app/src/routes/admin.php +++ b/app/src/routes/admin.php @@ -304,15 +304,11 @@ ], // === Config POST Routes + '/admin/configuration/:id/update' => [ 'controller' => AccountController::class, 'method' => 'update', 'middlewares' => [AdminMiddleware::class], ], - '/admin/configuration/:id/update-password' => [ - 'controller' => AccountController::class, - 'method' => 'updatePassword', - 'middlewares' => [AdminMiddleware::class], - ], ], ];