Skip to content

Commit

Permalink
feat: consolidate user update functionality by merging update and upd…
Browse files Browse the repository at this point in the history
…atePassword methods in AccountController
  • Loading branch information
elbacalau committed Dec 17, 2024
1 parent ecd9a18 commit 2dd34af
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 130 deletions.
56 changes: 11 additions & 45 deletions app/src/app/Controllers/Admin/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function index($queryParams)

public function update($id, $postData)

Check warning on line 26 in app/src/app/Controllers/Admin/AccountController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/AccountController.php#L26

Added line #L26 was not covered by tests
{

$user = User::find(id: $id);

Check warning on line 28 in app/src/app/Controllers/Admin/AccountController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/AccountController.php#L28

Added line #L28 was not covered by tests

if (!$user) {
Expand All @@ -34,61 +33,28 @@ public function update($id, $postData)
exit;

Check warning on line 33 in app/src/app/Controllers/Admin/AccountController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/AccountController.php#L30-L33

Added lines #L30 - L33 were not covered by tests
}


$user->name = $postData['name'];
$user->surname = $postData['surname'];

Check warning on line 38 in app/src/app/Controllers/Admin/AccountController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/AccountController.php#L37-L38

Added lines #L37 - L38 were not covered by tests

// detect password changes
if (!empty($postData['password'])) {
$user->password = password_hash($postData['password'], PASSWORD_DEFAULT);

Check warning on line 42 in app/src/app/Controllers/Admin/AccountController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/AccountController.php#L41-L42

Added lines #L41 - L42 were not covered by tests
}


$user->save();

Check warning on line 46 in app/src/app/Controllers/Admin/AccountController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/AccountController.php#L46

Added line #L46 was not covered by tests

// **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,

]);

Check warning on line 55 in app/src/app/Controllers/Admin/AccountController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/AccountController.php#L49-L55

Added lines #L49 - L55 were not covered by tests

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');

Check warning on line 58 in app/src/app/Controllers/Admin/AccountController.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Controllers/Admin/AccountController.php#L57-L58

Added lines #L57 - L58 were not covered by tests
}
}
59 changes: 20 additions & 39 deletions app/src/app/Views/Admin/AccountConfig/AccountConfig.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div class="mt-8">
<h1 class="text-5xl font-semibold">Configuración</h1>
<p class="text-md text-gray-600 mt-3">Aquí podrás configurar tu cuenta.</p>
<article>
<h2 class="text-3xl font-semibold col-span-4 mb-5">Informacíon personal</h2>
<article class="mb-10">
<h2 class="text-3xl font-semibold col-span-4 mb-5 mt-8">Informacíon personal</h2>

Check warning on line 5 in app/src/app/Views/Admin/AccountConfig/AccountConfig.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/AccountConfig/AccountConfig.php#L1-L5

Added lines #L1 - L5 were not covered by tests

<form action="/admin/configuration/<?= $user->getId() ?>/update" method="POST" class="grid grid-cols-4 gap-4">

Check warning on line 7 in app/src/app/Views/Admin/AccountConfig/AccountConfig.php

View check run for this annotation

Codecov / codecov/patch

app/src/app/Views/Admin/AccountConfig/AccountConfig.php#L7

Added line #L7 was not covered by tests
<!-- user info -->
Expand Down Expand Up @@ -73,35 +73,17 @@ class="mt-1 px-3 py-2 border rounded-md text-gray-600 bg-gray-100 cursor-not-all
</div>
</div>

<!-- save button -->
<div class="col-span-4 flex justify-end">
<button
id="button-save"
type="submit"
class="bg-gray-400 text-gray-500 py-2 px-4 rounded-lg cursor-not-allowed disabled:bg-gray-400 disabled:text-white"
disabled>
Guardar cambios
</button>
</div>
</form>
</article>



<hr class="mt-8">
<article>
<h1 class="text-3xl font-semibold col-span-4 mt-12">Cambiar contraseña</h1>

<form action="/admin/configuration/<?= $user->getId() ?>/update-password" method="POST" class="flex flex-col justify-between h-full">
<div class="mt-4 mb-10 grid grid-cols-4 gap-4">
<!-- password change info -->
<h3 class="text-3xl font-semibold col-span-4 mt-12">Cambiar contraseña</h3>
<div class="mt-4 mb-10 grid grid-cols-2 gap-4">
<div class="flex flex-col">
<label for="current-password" class="text-sm font-semibold text-gray-700">Contraseña actual</label>
<input
type="password"
id="current-password"
name="current_password"
class="mt-1 px-3 py-2 border rounded-md text-gray-600 bg-gray-100"
oninput="checkPasswordFields()">
oninput="checkChanges()">
</div>
<div class="flex flex-col">
<label for="new-password" class="text-sm font-semibold text-gray-700">Nueva contraseña</label>
Expand All @@ -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()">
</div>
<div class="flex flex-col">
<label for="confirm-password" class="text-sm font-semibold text-gray-700">Confirmar contraseña</label>
Expand All @@ -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()">
</div>

<!-- Botón Guardar -->
<div class="col-span-4 flex justify-end">
<button
id="button-save-password"
type="submit"
class="bg-gray-400 text-gray-500 py-2 px-4 rounded-lg cursor-not-allowed disabled:bg-gray-400 disabled:text-white"
disabled>
Guardar cambios
</button>
oninput="checkChanges()">
</div>
</div>
</form>

</article>


<!-- save button -->
<div class="col-span-4 flex justify-end">
<button
id="button-save"
type="submit"
class="bg-gray-400 text-gray-500 py-2 px-4 rounded-lg cursor-not-allowed disabled:bg-gray-400 disabled:text-white"
disabled>
Guardar cambios
</button>
</div>
</form>
</article>
</div>
41 changes: 0 additions & 41 deletions app/src/public/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
}
6 changes: 1 addition & 5 deletions app/src/routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
],
],
];

0 comments on commit 2dd34af

Please sign in to comment.