php-tenant/View/Modal/Web/ConfirmDeletion.php
Snider d0ad2737cb refactor: rename namespace from Core\Mod\Tenant to Core\Tenant
Simplifies the namespace hierarchy by removing the intermediate Mod
segment. Updates all 118 files including models, services, controllers,
middleware, tests, and composer.json autoload configuration.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 16:30:46 +00:00

116 lines
2.9 KiB
PHP

<?php
namespace Core\Tenant\View\Modal\Web;
use Core\Tenant\Models\AccountDeletionRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Livewire\Attributes\Layout;
use Livewire\Component;
#[Layout('components.layouts.minimal')]
class ConfirmDeletion extends Component
{
public string $token = '';
public string $password = '';
public string $step = 'verify'; // verify, confirm, deleting, goodbye, invalid
public string $error = '';
public ?AccountDeletionRequest $deletionRequest = null;
public ?string $userName = null;
public function mount(string $token): void
{
$this->token = $token;
$this->deletionRequest = AccountDeletionRequest::findValidByToken($token);
if (! $this->deletionRequest) {
$this->step = 'invalid';
return;
}
$this->userName = $this->deletionRequest->user->name;
// Even if logged in, require re-authentication for security
$this->step = 'verify';
}
public function verifyPassword(): void
{
$this->error = '';
if (! $this->deletionRequest || ! $this->deletionRequest->isActive()) {
$this->step = 'invalid';
return;
}
$user = $this->deletionRequest->user;
if (! Hash::check($this->password, $user->password)) {
$this->error = 'The password you entered is incorrect.';
return;
}
// Log the user in for this session
Auth::login($user);
$this->step = 'confirm';
}
public function confirmDeletion(): void
{
if (! $this->deletionRequest || ! $this->deletionRequest->isActive()) {
$this->step = 'invalid';
return;
}
$this->step = 'deleting';
// Process deletion in background after animation starts
$this->dispatch('start-deletion');
}
public function executeDelete(): void
{
if (! $this->deletionRequest || ! $this->deletionRequest->isActive()) {
return;
}
$user = $this->deletionRequest->user;
DB::transaction(function () use ($user) {
// Mark request as confirmed and completed
$this->deletionRequest->confirm();
$this->deletionRequest->complete();
// Delete all workspaces owned by the user
if (method_exists($user, 'ownedWorkspaces')) {
$user->ownedWorkspaces()->each(function ($workspace) {
$workspace->delete();
});
}
// Hard delete user account
$user->forceDelete();
});
Auth::logout();
session()->invalidate();
session()->regenerateToken();
$this->step = 'goodbye';
}
public function render()
{
return view('tenant::web.account.confirm-deletion');
}
}