2017-07-15 16:52:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Eloquent;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Database;
|
2017-10-19 03:32:19 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2017-07-15 16:52:34 +00:00
|
|
|
use Illuminate\Foundation\Application;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Illuminate\Database\DatabaseManager;
|
2018-03-03 23:52:35 +00:00
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
2017-07-15 16:52:34 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
2017-08-27 19:55:25 +00:00
|
|
|
use Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException;
|
2017-07-15 16:52:34 +00:00
|
|
|
|
|
|
|
class DatabaseRepository extends EloquentRepository implements DatabaseRepositoryInterface
|
|
|
|
{
|
2017-10-19 03:32:19 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $connection = self::DEFAULT_CONNECTION_NAME;
|
|
|
|
|
2017-07-15 16:52:34 +00:00
|
|
|
/**
|
2017-07-22 18:55:30 +00:00
|
|
|
* @var \Illuminate\Database\DatabaseManager
|
2017-07-15 16:52:34 +00:00
|
|
|
*/
|
2017-07-22 18:55:30 +00:00
|
|
|
protected $database;
|
2017-07-15 16:52:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DatabaseRepository constructor.
|
|
|
|
*
|
2017-07-22 18:55:30 +00:00
|
|
|
* @param \Illuminate\Foundation\Application $application
|
|
|
|
* @param \Illuminate\Database\DatabaseManager $database
|
2017-07-15 16:52:34 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function __construct(Application $application, DatabaseManager $database)
|
|
|
|
{
|
2017-07-15 16:52:34 +00:00
|
|
|
parent::__construct($application);
|
|
|
|
|
2017-07-22 18:55:30 +00:00
|
|
|
$this->database = $database;
|
2017-07-15 16:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Return the model backing this repository.
|
|
|
|
*
|
|
|
|
* @return string
|
2017-07-15 16:52:34 +00:00
|
|
|
*/
|
|
|
|
public function model()
|
|
|
|
{
|
|
|
|
return Database::class;
|
|
|
|
}
|
|
|
|
|
2017-10-19 03:32:19 +00:00
|
|
|
/**
|
|
|
|
* Set the connection name to execute statements against.
|
|
|
|
*
|
|
|
|
* @param string $connection
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setConnection(string $connection)
|
|
|
|
{
|
|
|
|
$this->connection = $connection;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-13 14:50:56 +00:00
|
|
|
* Return the connection to execute statements against.
|
2017-10-19 03:32:19 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getConnection(): string
|
|
|
|
{
|
|
|
|
return $this->connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the databases belonging to a server.
|
|
|
|
*
|
|
|
|
* @param int $server
|
|
|
|
* @return \Illuminate\Support\Collection
|
|
|
|
*/
|
|
|
|
public function getDatabasesForServer(int $server): Collection
|
|
|
|
{
|
|
|
|
return $this->getBuilder()->where('server_id', $server)->get($this->getColumns());
|
|
|
|
}
|
|
|
|
|
2018-03-03 23:52:35 +00:00
|
|
|
/**
|
|
|
|
* Return all of the databases for a given host with the server relationship loaded.
|
|
|
|
*
|
|
|
|
* @param int $host
|
|
|
|
* @param int $count
|
|
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
|
|
|
*/
|
|
|
|
public function getDatabasesForHost(int $host, int $count = 25): LengthAwarePaginator
|
|
|
|
{
|
|
|
|
return $this->getBuilder()->with('server')
|
|
|
|
->where('database_host_id', $host)
|
|
|
|
->paginate($count, $this->getColumns());
|
|
|
|
}
|
|
|
|
|
2017-07-15 16:52:34 +00:00
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Create a new database if it does not already exist on the host with
|
|
|
|
* the provided details.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return \Pterodactyl\Models\Database
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException
|
2017-07-15 16:52:34 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function createIfNotExists(array $data): Database
|
2017-07-15 16:52:34 +00:00
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
$count = $this->getBuilder()->where([
|
2017-08-27 00:58:24 +00:00
|
|
|
['server_id', '=', array_get($data, 'server_id')],
|
|
|
|
['database_host_id', '=', array_get($data, 'database_host_id')],
|
|
|
|
['database', '=', array_get($data, 'database')],
|
2017-07-15 16:52:34 +00:00
|
|
|
])->count();
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
if ($count > 0) {
|
2017-08-27 00:58:24 +00:00
|
|
|
throw new DuplicateDatabaseNameException('A database with those details already exists for the specified server.');
|
2017-07-15 16:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->create($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Create a new database on a given connection.
|
|
|
|
*
|
|
|
|
* @param string $database
|
|
|
|
* @return bool
|
2017-07-15 16:52:34 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function createDatabase(string $database): bool
|
2017-07-15 16:52:34 +00:00
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
return $this->run(sprintf('CREATE DATABASE IF NOT EXISTS `%s`', $database));
|
2017-07-15 16:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Create a new database user on a given connection.
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
* @param string $remote
|
|
|
|
* @param string $password
|
|
|
|
* @return bool
|
2017-07-15 16:52:34 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function createUser(string $username, string $remote, string $password): bool
|
2017-07-15 16:52:34 +00:00
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
return $this->run(sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', $username, $remote, $password));
|
2017-07-15 16:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Give a specific user access to a given database.
|
|
|
|
*
|
|
|
|
* @param string $database
|
|
|
|
* @param string $username
|
|
|
|
* @param string $remote
|
|
|
|
* @return bool
|
2017-07-15 16:52:34 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function assignUserToDatabase(string $database, string $username, string $remote): bool
|
2017-07-15 16:52:34 +00:00
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
return $this->run(sprintf(
|
2019-08-03 21:57:01 +00:00
|
|
|
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX, LOCK TABLES, EXECUTE ON `%s`.* TO `%s`@`%s`',
|
2017-10-19 03:32:19 +00:00
|
|
|
$database,
|
|
|
|
$username,
|
|
|
|
$remote
|
|
|
|
));
|
2017-07-15 16:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Flush the privileges for a given connection.
|
|
|
|
*
|
|
|
|
* @return bool
|
2017-07-15 16:52:34 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function flush(): bool
|
2017-07-15 16:52:34 +00:00
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
return $this->run('FLUSH PRIVILEGES');
|
2017-07-15 16:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Drop a given database on a specific connection.
|
|
|
|
*
|
|
|
|
* @param string $database
|
|
|
|
* @return bool
|
2017-07-15 16:52:34 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function dropDatabase(string $database): bool
|
2017-07-15 16:52:34 +00:00
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
return $this->run(sprintf('DROP DATABASE IF EXISTS `%s`', $database));
|
2017-07-15 16:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Drop a given user on a specific connection.
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
* @param string $remote
|
|
|
|
* @return mixed
|
2017-07-15 16:52:34 +00:00
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
public function dropUser(string $username, string $remote): bool
|
2017-07-15 16:52:34 +00:00
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
return $this->run(sprintf('DROP USER IF EXISTS `%s`@`%s`', $username, $remote));
|
2017-07-15 16:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the provided statement against the database on a given connection.
|
|
|
|
*
|
2017-10-19 03:32:19 +00:00
|
|
|
* @param string $statement
|
2017-07-15 16:52:34 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
private function run(string $statement): bool
|
2017-07-15 16:52:34 +00:00
|
|
|
{
|
2017-10-19 03:32:19 +00:00
|
|
|
return $this->database->connection($this->getConnection())->statement($statement);
|
2017-07-15 16:52:34 +00:00
|
|
|
}
|
|
|
|
}
|