misc_pterodactyl-panel/app/Services/Databases/DatabaseManagementService.php

126 lines
3.8 KiB
PHP
Raw Normal View History

2017-07-15 16:52:34 +00:00
<?php
namespace Pterodactyl\Services\Databases;
2017-07-15 16:52:34 +00:00
2017-10-24 01:12:15 +00:00
use Pterodactyl\Models\Database;
use Illuminate\Database\DatabaseManager;
2017-07-15 16:52:34 +00:00
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
class DatabaseManagementService
2017-07-15 16:52:34 +00:00
{
/**
* @var \Illuminate\Database\DatabaseManager
2017-07-15 16:52:34 +00:00
*/
protected $database;
/**
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection
*/
protected $dynamic;
/**
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
protected $encrypter;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
*/
protected $repository;
/**
* CreationService constructor.
*
* @param \Illuminate\Database\DatabaseManager $database
2017-07-15 16:52:34 +00:00
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
*/
public function __construct(
DatabaseManager $database,
2017-07-15 16:52:34 +00:00
DynamicDatabaseConnection $dynamic,
DatabaseRepositoryInterface $repository,
Encrypter $encrypter
) {
$this->database = $database;
$this->dynamic = $dynamic;
$this->encrypter = $encrypter;
$this->repository = $repository;
}
/**
* Create a new database that is linked to a specific host.
*
2017-08-22 03:10:48 +00:00
* @param int $server
* @param array $data
2017-07-15 16:52:34 +00:00
* @return \Illuminate\Database\Eloquent\Model
*
* @throws \Exception
*/
public function create($server, array $data)
2017-07-15 16:52:34 +00:00
{
$data['server_id'] = $server;
$data['database'] = sprintf('s%d_%s', $server, $data['database']);
$data['username'] = sprintf('u%d_%s', $server, str_random(10));
2017-07-15 16:52:34 +00:00
$data['password'] = $this->encrypter->encrypt(str_random(16));
$this->database->beginTransaction();
try {
$database = $this->repository->createIfNotExists($data);
$this->dynamic->set('dynamic', $data['database_host_id']);
$this->repository->createDatabase($database->database);
2017-07-15 16:52:34 +00:00
$this->repository->createUser(
2017-08-22 03:10:48 +00:00
$database->username,
$database->remote,
$this->encrypter->decrypt($database->password)
2017-07-15 16:52:34 +00:00
);
$this->repository->assignUserToDatabase(
2017-08-22 03:10:48 +00:00
$database->database,
$database->username,
$database->remote
2017-07-15 16:52:34 +00:00
);
$this->repository->flush();
2017-07-15 16:52:34 +00:00
$this->database->commit();
} catch (\Exception $ex) {
try {
2017-10-24 01:12:15 +00:00
if (isset($database) && $database instanceof Database) {
$this->repository->dropDatabase($database->database);
$this->repository->dropUser($database->username, $database->remote);
$this->repository->flush();
2017-07-15 16:52:34 +00:00
}
} catch (\Exception $exTwo) {
2017-07-15 16:52:34 +00:00
// ignore an exception
}
$this->database->rollBack();
throw $ex;
}
return $database;
}
/**
* Delete a database from the given host server.
*
2017-08-22 03:10:48 +00:00
* @param int $id
2017-07-15 16:52:34 +00:00
* @return bool|null
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2017-07-15 16:52:34 +00:00
*/
public function delete($id)
{
$database = $this->repository->find($id);
$this->dynamic->set('dynamic', $database->database_host_id);
$this->repository->dropDatabase($database->database);
$this->repository->dropUser($database->username, $database->remote);
$this->repository->flush();
2017-07-15 16:52:34 +00:00
return $this->repository->delete($id);
}
}