2017-12-03 20:00:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\DaemonKeys;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepository;
|
|
|
|
|
|
|
|
class RevokeMultipleDaemonKeysService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $exceptions = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $daemonRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RevokeMultipleDaemonKeysService constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface $repository
|
2017-12-03 20:00:47 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonRepository
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
DaemonKeyRepositoryInterface $repository,
|
|
|
|
DaemonServerRepository $daemonRepository
|
|
|
|
) {
|
|
|
|
$this->daemonRepository = $daemonRepository;
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grab all of the keys that exist for a single user and delete them from all
|
|
|
|
* daemon's that they are assigned to. If connection fails, this function will
|
|
|
|
* return an error.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\User $user
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param bool $ignoreConnectionErrors
|
2017-12-03 20:00:47 +00:00
|
|
|
*/
|
|
|
|
public function handle(User $user, bool $ignoreConnectionErrors = false)
|
|
|
|
{
|
|
|
|
$keys = $this->repository->getKeysForRevocation($user);
|
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$keys->groupBy('node.id')->each(function ($group, $nodeId) use ($ignoreConnectionErrors) {
|
2017-12-03 20:00:47 +00:00
|
|
|
try {
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonRepository->setNode(collect($group)->first()->getRelation('node'))->revokeAccessKey(collect($group)->pluck('secret')->toArray());
|
2017-12-03 20:00:47 +00:00
|
|
|
} catch (RequestException $exception) {
|
|
|
|
if (! $ignoreConnectionErrors) {
|
|
|
|
throw new DaemonConnectionException($exception);
|
|
|
|
}
|
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->setConnectionException($nodeId, $exception);
|
2017-12-03 20:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->repository->deleteKeys(collect($group)->pluck('id')->toArray());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of exceptions that were returned by the handle function.
|
|
|
|
*
|
|
|
|
* @return RequestException[]
|
|
|
|
*/
|
|
|
|
public function getExceptions()
|
|
|
|
{
|
|
|
|
return $this->exceptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an exception for a node to the array.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param int $node
|
2017-12-03 20:00:47 +00:00
|
|
|
* @param \GuzzleHttp\Exception\RequestException $exception
|
|
|
|
*/
|
|
|
|
protected function setConnectionException(int $node, RequestException $exception)
|
|
|
|
{
|
|
|
|
$this->exceptions[$node] = $exception;
|
|
|
|
}
|
|
|
|
}
|