Replace database repository

This commit is contained in:
Lance Pioch 2022-10-24 00:19:14 -04:00
parent e49ba65709
commit 483c081d7f
9 changed files with 102 additions and 248 deletions

View file

@ -10,7 +10,6 @@ use Pterodactyl\Helpers\Utilities;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
use Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException;
use Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException;
use Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException;
@ -36,8 +35,7 @@ class DatabaseManagementService
public function __construct(
protected ConnectionInterface $connection,
protected DynamicDatabaseConnection $dynamic,
protected Encrypter $encrypter,
protected DatabaseRepository $repository
protected Encrypter $encrypter
) {
}
@ -105,26 +103,26 @@ class DatabaseManagementService
$this->dynamic->set('dynamic', $data['database_host_id']);
$this->repository->createDatabase($database->database);
$this->repository->createUser(
$database->createDatabase($database->database);
$database->createUser(
$database->username,
$database->remote,
$this->encrypter->decrypt($database->password),
$database->max_connections
);
$this->repository->assignUserToDatabase($database->database, $database->username, $database->remote);
$this->repository->flush();
$database->assignUserToDatabase($database->database, $database->username, $database->remote);
$database->flush();
return $database;
});
} catch (Exception $exception) {
try {
if ($database instanceof Database) {
$this->repository->dropDatabase($database->database);
$this->repository->dropUser($database->username, $database->remote);
$this->repository->flush();
$database->dropDatabase($database->database);
$database->dropUser($database->username, $database->remote);
$database->flush();
}
} catch (Exception $deletionException) {
} catch (Exception) {
// Do nothing here. We've already encountered an issue before this point so no
// reason to prioritize this error over the initial one.
}
@ -142,9 +140,9 @@ class DatabaseManagementService
{
$this->dynamic->set('dynamic', $database->database_host_id);
$this->repository->dropDatabase($database->database);
$this->repository->dropUser($database->username, $database->remote);
$this->repository->flush();
$database->dropDatabase($database->database);
$database->dropUser($database->username, $database->remote);
$database->flush();
return $database->delete();
}

View file

@ -7,7 +7,6 @@ use Pterodactyl\Helpers\Utilities;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
class DatabasePasswordService
{
@ -17,8 +16,7 @@ class DatabasePasswordService
public function __construct(
private ConnectionInterface $connection,
private DynamicDatabaseConnection $dynamic,
private Encrypter $encrypter,
private DatabaseRepositoryInterface $repository
private Encrypter $encrypter
) {
}
@ -34,14 +32,14 @@ class DatabasePasswordService
$this->connection->transaction(function () use ($database, $password) {
$this->dynamic->set('dynamic', $database->database_host_id);
$this->repository->withoutFreshModel()->update($database->id, [
$database->update([
'password' => $this->encrypter->encrypt($password),
]);
$this->repository->dropUser($database->username, $database->remote);
$this->repository->createUser($database->username, $database->remote, $password, $database->max_connections);
$this->repository->assignUserToDatabase($database->database, $database->username, $database->remote);
$this->repository->flush();
$database->dropUser($database->username, $database->remote);
$database->createUser($database->username, $database->remote, $password, $database->max_connections);
$database->assignUserToDatabase($database->database, $database->username, $database->remote);
$database->flush();
});
return $password;

View file

@ -3,33 +3,25 @@
namespace Pterodactyl\Services\Databases\Hosts;
use Pterodactyl\Exceptions\Service\HasActiveServersException;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
use Pterodactyl\Models\DatabaseHost;
class HostDeletionService
{
/**
* HostDeletionService constructor.
*/
public function __construct(
private DatabaseRepositoryInterface $databaseRepository,
private DatabaseHostRepositoryInterface $repository
) {
}
/**
* Delete a specified host from the Panel if no databases are
* attached to it.
*
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
* @throws HasActiveServersException
*/
public function handle(int $host): int
{
$count = $this->databaseRepository->findCountWhere([['database_host_id', '=', $host]]);
if ($count > 0) {
/** @var DatabaseHost $host */
$host = DatabaseHost::query()->findOrFail($host);
if ($host->databases()->count() > 0) {
throw new HasActiveServersException(trans('exceptions.databases.delete_has_databases'));
}
return $this->repository->delete($host);
return $host->delete();
}
}