Prevent deletion of options that have children attached to them.

closes #562
This commit is contained in:
Dane Everitt 2017-09-30 12:54:09 -05:00
parent d5bf8734ef
commit 1216f950e2
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 52 additions and 6 deletions

View file

@ -0,0 +1,16 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Exceptions\Service\ServiceOption;
use Pterodactyl\Exceptions\DisplayException;
class HasChildrenException extends DisplayException
{
}

View file

@ -9,9 +9,11 @@
namespace Pterodactyl\Services\Services\Options;
use Webmozart\Assert\Assert;
use Pterodactyl\Exceptions\Service\HasActiveServersException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServiceOptionRepositoryInterface;
use Pterodactyl\Exceptions\Service\ServiceOption\HasChildrenException;
class OptionDeletionService
{
@ -46,17 +48,22 @@ class OptionDeletionService
* @return int
*
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
* @throws \Pterodactyl\Exceptions\Service\ServiceOption\HasChildrenException
*/
public function handle($option)
{
$servers = $this->serverRepository->findCountWhere([
['option_id', '=', $option],
]);
Assert::integerish($option, 'First argument passed to handle must be integer, received %s.');
$servers = $this->serverRepository->findCountWhere([['option_id', '=', $option]]);
if ($servers > 0) {
throw new HasActiveServersException(trans('exceptions.service.options.delete_has_servers'));
}
$children = $this->repository->findCountWhere([['config_from', '=', $option]]);
if ($children > 0) {
throw new HasChildrenException(trans('exceptions.service.options.has_children'));
}
return $this->repository->delete($option);
}
}