Skip to content

Commit 058ee0f

Browse files
committed
feat(account): implement signed account deletion workflow and refined 4-step wizard
1 parent eebf5aa commit 058ee0f

9 files changed

Lines changed: 1158 additions & 243 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Mail\AccountDeletedMail;
6+
use App\Models\User;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Auth;
9+
use Illuminate\Support\Facades\Mail;
10+
use Inertia\Inertia;
11+
12+
class AccountDeletionController extends Controller
13+
{
14+
/**
15+
* Show the signed account deletion confirmation page.
16+
*/
17+
public function showConfirm(Request $request, User $user)
18+
{
19+
if (! hash_equals(sha1($user->email), (string) $request->query('hash'))) {
20+
abort(403, 'Invalid deletion token.');
21+
}
22+
23+
return Inertia::render('Account/ConfirmDeletion', [
24+
'user' => [
25+
'id' => $user->id,
26+
'name' => $user->name,
27+
'email' => $user->email,
28+
],
29+
'confirmUrl' => $request->fullUrl(),
30+
]);
31+
}
32+
33+
/**
34+
* Process the permanent deletion requested via signed URL.
35+
*/
36+
public function confirmDelete(Request $request, User $user)
37+
{
38+
if (! hash_equals(sha1($user->email), (string) $request->query('hash'))) {
39+
abort(403, 'Invalid deletion token.');
40+
}
41+
42+
// If currently authenticated as this user, log them out and invalidate session
43+
if (Auth::id() === $user->id) {
44+
Auth::logout();
45+
$request->session()->invalidate();
46+
$request->session()->regenerateToken();
47+
}
48+
49+
$email = $user->email;
50+
$name = $user->name;
51+
52+
if (! empty($email)) {
53+
Mail::to($email)->queue(new AccountDeletedMail($name));
54+
}
55+
56+
$user->delete();
57+
58+
return redirect('/')
59+
->with('success', 'আপনার অ্যাকাউন্টটি সফলভাবে মুছে ফেলা হয়েছে।');
60+
}
61+
}

‎app/Http/Controllers/Admin/UserController.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
use App\Http\Requests\User\StoreUserRequest;
77
use App\Http\Requests\User\UpdateUserRequest;
88
use App\Mail\AccountDeletedMail;
9+
use App\Mail\AccountDeletionLinkMail;
910
use App\Models\User;
1011
use App\Notifications\WelcomeNotification;
1112
use Illuminate\Http\Request;
1213
use Illuminate\Support\Facades\Auth;
1314
use Illuminate\Support\Facades\Mail;
1415
use Illuminate\Support\Facades\Storage;
16+
use Illuminate\Support\Facades\URL;
1517
use Inertia\Inertia;
1618
use Spatie\Permission\Models\Permission;
1719

@@ -170,6 +172,29 @@ public function destroy(User $user)
170172
->with('success', 'User deleted successfully.');
171173
}
172174

175+
/**
176+
* Send a temporary signed account deletion link to the user.
177+
*/
178+
public function sendDeleteLink(User $user)
179+
{
180+
if (empty($user->email)) {
181+
return back()->with('error', 'This user does not have an email address.');
182+
}
183+
184+
$signedUrl = URL::temporarySignedRoute(
185+
'account.delete.verify',
186+
now()->addDays(7),
187+
[
188+
'user' => $user->id,
189+
'hash' => sha1($user->email),
190+
]
191+
);
192+
193+
Mail::to($user->email)->queue(new AccountDeletionLinkMail($user, $signedUrl));
194+
195+
return back()->with('success', "Deletion link sent successfully to {$user->email}.");
196+
}
197+
173198
/**
174199
* Log in as the specified user.
175200
*/
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Mail;
4+
5+
use App\Models\User;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Mail\Mailable;
9+
use Illuminate\Mail\Mailables\Content;
10+
use Illuminate\Mail\Mailables\Envelope;
11+
use Illuminate\Queue\SerializesModels;
12+
13+
class AccountDeletionLinkMail extends Mailable implements ShouldQueue
14+
{
15+
use Queueable, SerializesModels;
16+
17+
public function __construct(
18+
public User $user,
19+
public string $signedUrl,
20+
) {}
21+
22+
public function envelope(): Envelope
23+
{
24+
return new Envelope(
25+
subject: 'আপনার অ্যাকাউন্ট মুছে ফেলার কনফার্মেশন লিংক',
26+
);
27+
}
28+
29+
public function content(): Content
30+
{
31+
$name = $this->user->name ? "Hello {$this->user->name}," : 'প্রিয় শিক্ষার্থী,';
32+
33+
return new Content(
34+
view: 'emails.default',
35+
with: [
36+
'subject' => 'আপনার অ্যাকাউন্ট মুছে ফেলার কনফার্মেশন লিংক',
37+
'greeting' => $name,
38+
'lines' => [
39+
'অ্যাডমিন আপনার অ্যাকাউন্ট স্থায়ীভাবে মুছে ফেলার জন্য একটি কনফার্মেশন লিংক পাঠিয়েছেন।',
40+
'আপনি যদি আপনার HSCStack অ্যাকাউন্টটি ডিলিট করতে চান, তাহলে নিচের বাটনে ক্লিক করে কনফার্মেশন সম্পন্ন করুন।',
41+
'<strong>সতর্কতা:</strong> এই লিংকের মেয়াদ আগামী <strong>৭ দিন</strong> পর্যন্ত কার্যকর থাকবে। অ্যাকাউন্ট মুছে ফেলার পর আপনার সব ডাটা স্থায়ীভাবে ডিলিট হয়ে যাবে এবং তা আর পুনরুদ্ধার করা সম্ভব হবে না।',
42+
'আপনি যদি অ্যাকাউন্ট ডিলিট করতে না চান, তবে এই ইমেইলটি উপেক্ষা করুন। আপনার অ্যাকাউন্ট সুরক্ষিত থাকবে।',
43+
],
44+
'actionText' => 'অ্যাকাউন্ট ডিলিট করুন (Confirm Deletion)',
45+
'actionUrl' => $this->signedUrl,
46+
],
47+
);
48+
}
49+
}

‎resources/js/components/admin/UserRow.vue‎

Lines changed: 149 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<script setup lang="ts">
22
import { Link, router, usePage } from '@inertiajs/vue3';
3-
import { LogIn, Pencil, Trash2, Ban } from 'lucide-vue-next';
3+
import { LogIn, Pencil, Trash2, Ban, Mail } from 'lucide-vue-next';
44
import { ref } from 'vue';
5+
import BaseModal from '@/components/BaseModal.vue';
56
import ChatBanModal from '@/components/ChatBanModal.vue';
67
import StatusBadge from '@/components/StatusBadge.vue';
78
import VerifiedBadge from '@/components/VerifiedBadge.vue';
@@ -16,6 +17,8 @@ defineProps({
1617
const page = usePage();
1718
const userId = page.props.auth.user.id;
1819
const isBanModalOpen = ref(false);
20+
const isDeleteModalOpen = ref(false);
21+
const isSendingDeleteLink = ref(false);
1922
2023
const isBanned = (user: any) => {
2124
const banUntil = user?.banned_until;
@@ -41,11 +44,42 @@ const loginAsUser = (targetUser: any) => {
4144
}
4245
};
4346
44-
const deleteUser = (id: number) => {
45-
if (confirm('Are you sure you want to delete this user?')) {
47+
const openDeleteModal = () => {
48+
isDeleteModalOpen.value = true;
49+
};
50+
51+
const handleHardDelete = (id: number) => {
52+
if (
53+
confirm(
54+
'Are you absolutely sure you want to permanently delete this user now? This action cannot be undone.',
55+
)
56+
) {
57+
isDeleteModalOpen.value = false;
4658
router.delete(`/admin/users/${id}`);
4759
}
4860
};
61+
62+
const handleSendDeleteLink = (id: number) => {
63+
if (
64+
!confirm(
65+
'Are you sure you want to send a signed account deletion link to this user? They will receive an email to confirm deletion.',
66+
)
67+
) {
68+
return;
69+
}
70+
71+
isSendingDeleteLink.value = true;
72+
router.post(
73+
`/admin/users/${id}/send-delete-link`,
74+
{},
75+
{
76+
onFinish: () => {
77+
isSendingDeleteLink.value = false;
78+
isDeleteModalOpen.value = false;
79+
},
80+
},
81+
);
82+
};
4983
</script>
5084

5185
<template>
@@ -178,7 +212,7 @@ const deleteUser = (id: number) => {
178212

179213
<button
180214
v-if="user.id !== userId && can('delete users')"
181-
@click="deleteUser(user.id)"
215+
@click="openDeleteModal"
182216
type="button"
183217
class="rounded-lg p-1.5 text-slate-400 transition-colors hover:bg-rose-50 hover:text-rose-600 dark:text-gray-500 dark:hover:bg-rose-500/10 dark:hover:text-rose-400"
184218
title="Delete user"
@@ -193,5 +227,116 @@ const deleteUser = (id: number) => {
193227
:user="user"
194228
@close="isBanModalOpen = false"
195229
/>
230+
231+
<!-- Account Deletion Action Modal -->
232+
<BaseModal
233+
:is-open="isDeleteModalOpen"
234+
title="Delete User Account"
235+
description="Choose how you want to handle the deletion of this account."
236+
maxWidth="md"
237+
@close="isDeleteModalOpen = false"
238+
>
239+
<div class="space-y-4 p-4 sm:p-6">
240+
<div
241+
class="rounded-xl border border-slate-100 bg-slate-50 p-3.5 dark:border-gray-800 dark:bg-gray-800/50"
242+
>
243+
<p
244+
class="text-xs font-medium tracking-wider text-slate-500 uppercase dark:text-gray-400"
245+
>
246+
Target User
247+
</p>
248+
<p
249+
class="text-sm font-semibold text-slate-900 dark:text-white"
250+
>
251+
{{ user.name }}
252+
</p>
253+
<p
254+
v-if="user.email"
255+
class="font-mono text-xs text-slate-600 dark:text-gray-400"
256+
>
257+
{{ user.email }}
258+
</p>
259+
</div>
260+
261+
<div class="space-y-3">
262+
<!-- Option 1: Send Deletion Link -->
263+
<button
264+
type="button"
265+
:disabled="!user.email || isSendingDeleteLink"
266+
@click="handleSendDeleteLink(user.id)"
267+
class="group flex w-full items-start gap-3 rounded-xl border border-indigo-100 bg-indigo-50/50 p-3.5 text-left transition hover:border-indigo-300 hover:bg-indigo-50 disabled:cursor-not-allowed disabled:opacity-50 dark:border-indigo-950/50 dark:bg-indigo-950/20 dark:hover:border-indigo-800"
268+
>
269+
<div
270+
class="flex h-9 w-9 shrink-0 items-center justify-center rounded-lg bg-indigo-100 text-indigo-600 group-hover:bg-indigo-200 dark:bg-indigo-900/40 dark:text-indigo-400"
271+
>
272+
<Mail class="h-5 w-5" />
273+
</div>
274+
<div class="min-w-0 flex-1">
275+
<div class="flex items-center justify-between">
276+
<span
277+
class="text-sm font-bold text-indigo-950 dark:text-indigo-200"
278+
>Send Deletion Link to User</span
279+
>
280+
<span
281+
class="rounded-full bg-indigo-100 px-2 py-0.5 text-[10px] font-semibold text-indigo-700 uppercase dark:bg-indigo-900/60 dark:text-indigo-300"
282+
>Recommended</span
283+
>
284+
</div>
285+
<p
286+
class="mt-0.5 text-xs leading-relaxed text-slate-600 dark:text-gray-400"
287+
>
288+
Sends a secure, 7-day signed authorization link
289+
to their email address. The user confirms and
290+
deletes the account themselves.
291+
</p>
292+
<p
293+
v-if="!user.email"
294+
class="mt-1 text-[11px] font-medium text-amber-600 dark:text-amber-400"
295+
>
296+
User has no email address configured.
297+
</p>
298+
</div>
299+
</button>
300+
301+
<!-- Option 2: Delete Instantly (Hard Delete) -->
302+
<button
303+
type="button"
304+
@click="handleHardDelete(user.id)"
305+
class="group flex w-full items-start gap-3 rounded-xl border border-rose-100 bg-rose-50/50 p-3.5 text-left transition hover:border-rose-300 hover:bg-rose-50 dark:border-rose-950/50 dark:bg-rose-950/20 dark:hover:border-rose-800"
306+
>
307+
<div
308+
class="flex h-9 w-9 shrink-0 items-center justify-center rounded-lg bg-rose-100 text-rose-600 group-hover:bg-rose-200 dark:bg-rose-900/40 dark:text-rose-400"
309+
>
310+
<Trash2 class="h-5 w-5" />
311+
</div>
312+
<div class="min-w-0 flex-1">
313+
<span
314+
class="text-sm font-bold text-rose-950 dark:text-rose-200"
315+
>Delete Instantly (Hard Delete)</span
316+
>
317+
<p
318+
class="mt-0.5 text-xs leading-relaxed text-slate-600 dark:text-gray-400"
319+
>
320+
Permanently wipe the account and all associated
321+
data immediately. Sends a farewell notification
322+
email if email is present.
323+
</p>
324+
</div>
325+
</button>
326+
</div>
327+
</div>
328+
329+
<template #footer>
330+
<div class="flex justify-end">
331+
<button
332+
type="button"
333+
@click="isDeleteModalOpen = false"
334+
class="rounded-lg border border-slate-200 bg-white px-4 py-2 text-xs font-semibold text-slate-700 transition hover:bg-slate-50 dark:border-gray-800 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700"
335+
>
336+
Cancel
337+
</button>
338+
</div>
339+
</template>
340+
</BaseModal>
196341
</div>
197342
</template>

0 commit comments

Comments
 (0)