All checks were successful
Build MapleDeploy Coolify Image / build (push) Successful in 1m26s
61 lines
1.7 KiB
PHP
61 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\Mapledeploy;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class UserDelete extends Command
|
|
{
|
|
protected $signature = 'mapledeploy:user:delete {user_id : Coolify user id}';
|
|
|
|
protected $description = 'Delete a Coolify user for MapleDeploy dashboard access management';
|
|
|
|
public function handle(): int
|
|
{
|
|
$rawUserId = $this->argument('user_id');
|
|
$userId = filter_var($rawUserId, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]);
|
|
if ($userId === false) {
|
|
return $this->failWith('INVALID_USER_ID');
|
|
}
|
|
|
|
if ($userId === 0) {
|
|
return $this->failWith('CANNOT_DELETE_ROOT_USER');
|
|
}
|
|
|
|
$user = User::find($userId);
|
|
if (! $user) {
|
|
$this->line(json_encode([
|
|
'deleted' => null,
|
|
'alreadyDeleted' => true,
|
|
'id' => $userId,
|
|
], JSON_THROW_ON_ERROR));
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
$deleted = [
|
|
'id' => $user->id,
|
|
'email' => $user->email,
|
|
];
|
|
|
|
DB::transaction(function () use ($user) {
|
|
$user->tokens()->delete();
|
|
// MapleDeploy branding: deletion must end any active browser sessions.
|
|
DB::table('sessions')->where('user_id', $user->id)->delete();
|
|
$user->delete();
|
|
});
|
|
|
|
$this->line(json_encode(['deleted' => $deleted], JSON_THROW_ON_ERROR));
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
private function failWith(string $code): int
|
|
{
|
|
$this->line(json_encode(['error' => $code], JSON_THROW_ON_ERROR));
|
|
|
|
return self::FAILURE;
|
|
}
|
|
}
|