2024-05-14 13:40:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
class AdminRemoveUser extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'admin:remove-user {email}';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Remove User from database';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$email = $this->argument('email');
|
2024-06-10 20:43:34 +00:00
|
|
|
$confirm = $this->confirm('Are you sure you want to remove user with email: '.$email.'?');
|
|
|
|
if (! $confirm) {
|
2024-05-14 13:40:45 +00:00
|
|
|
$this->info('User removal cancelled.');
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-05-14 13:40:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->info("Removing user with email: $email");
|
|
|
|
$user = User::whereEmail($email)->firstOrFail();
|
|
|
|
$teams = $user->teams;
|
|
|
|
foreach ($teams as $team) {
|
|
|
|
if ($team->members->count() > 1) {
|
|
|
|
$this->error('User is a member of a team with more than one member. Please remove user from team first.');
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-05-14 13:40:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$team->delete();
|
|
|
|
}
|
|
|
|
$user->delete();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->error('Failed to remove user.');
|
|
|
|
$this->error($e->getMessage());
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-05-14 13:40:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|