2017-08-04 19:11:41 -05:00
|
|
|
<?php
|
2022-01-15 18:10:37 +02:00
|
|
|
|
2017-08-04 19:11:41 -05:00
|
|
|
namespace Pterodactyl\Services\Users;
|
|
|
|
|
2017-08-05 17:26:30 -05:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2017-08-04 19:11:41 -05:00
|
|
|
use Illuminate\Contracts\Translation\Translator;
|
2017-08-05 17:26:30 -05:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
2017-08-04 19:11:41 -05:00
|
|
|
|
2017-08-27 15:10:51 -05:00
|
|
|
class UserDeletionService
|
2017-08-04 19:11:41 -05:00
|
|
|
{
|
|
|
|
/**
|
2022-10-14 10:59:20 -06:00
|
|
|
* UserDeletionService constructor.
|
2017-08-04 19:11:41 -05:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2022-10-14 10:59:20 -06:00
|
|
|
protected ServerRepositoryInterface $serverRepository,
|
|
|
|
protected Translator $translator
|
2017-08-04 19:11:41 -05:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a user from the panel only if they have no servers attached to their account.
|
|
|
|
*
|
2022-10-22 03:59:36 -04:00
|
|
|
* @throws DisplayException
|
2017-08-04 19:11:41 -05:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function handle(int|User $user): ?bool
|
2017-08-04 19:11:41 -05:00
|
|
|
{
|
2022-10-22 03:59:36 -04:00
|
|
|
if (is_int($user)) {
|
|
|
|
$user = User::query()->findOrFail($user);
|
2017-08-04 19:11:41 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 03:59:36 -04:00
|
|
|
$servers = $this->serverRepository->setColumns('id')->findCountWhere([['owner_id', '=', $user->id]]);
|
2017-08-05 17:23:02 -05:00
|
|
|
if ($servers > 0) {
|
2022-01-15 18:10:37 +02:00
|
|
|
throw new DisplayException($this->translator->get('admin/user.exceptions.user_has_servers'));
|
2017-08-04 19:11:41 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 03:59:36 -04:00
|
|
|
return $user->delete();
|
2017-08-04 19:11:41 -05:00
|
|
|
}
|
|
|
|
}
|