misc_pterodactyl-panel/app/Repositories/Eloquent/DatabaseRepository.php

147 lines
4 KiB
PHP
Raw Permalink Normal View History

2017-07-15 16:52:34 +00:00
<?php
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Database;
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;
class DatabaseRepository extends EloquentRepository implements DatabaseRepositoryInterface
{
/**
* @var string
*/
protected $connection = self::DEFAULT_CONNECTION_NAME;
2017-07-15 16:52:34 +00:00
/**
* @var \Illuminate\Database\DatabaseManager
2017-07-15 16:52:34 +00:00
*/
protected $database;
2017-07-15 16:52:34 +00:00
/**
* DatabaseRepository constructor.
*/
public function __construct(Application $application, DatabaseManager $database)
{
2017-07-15 16:52:34 +00:00
parent::__construct($application);
$this->database = $database;
2017-07-15 16:52:34 +00:00
}
/**
* Return the model backing this repository.
*
* @return string
2017-07-15 16:52:34 +00:00
*/
public function model()
{
return Database::class;
}
/**
* Set the connection name to execute statements against.
*
* @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.
*/
public function getConnection(): string
{
return $this->connection;
}
/**
* Return all of the databases belonging to a server.
*/
public function getDatabasesForServer(int $server): Collection
{
2018-08-22 04:47:01 +00:00
return $this->getBuilder()->with('host')->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.
*/
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
/**
* Create a new database on a given connection.
2017-07-15 16:52:34 +00:00
*/
public function createDatabase(string $database): bool
2017-07-15 16:52:34 +00:00
{
return $this->run(sprintf('CREATE DATABASE IF NOT EXISTS `%s`', $database));
2017-07-15 16:52:34 +00:00
}
/**
* Create a new database user on a given connection.
2017-07-15 16:52:34 +00:00
*/
public function createUser(string $username, string $remote, string $password, int $max_connections): bool
2017-07-15 16:52:34 +00:00
{
2021-01-23 20:33:34 +00:00
if (!$max_connections) {
return $this->run(sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', $username, $remote, $password));
} else {
return $this->run(sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\' WITH MAX_USER_CONNECTIONS %s', $username, $remote, $password, $max_connections));
}
2017-07-15 16:52:34 +00:00
}
/**
* Give a specific user access to a given database.
2017-07-15 16:52:34 +00:00
*/
public function assignUserToDatabase(string $database, string $username, string $remote): bool
2017-07-15 16:52:34 +00:00
{
return $this->run(sprintf(
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, REFERENCES, INDEX, LOCK TABLES, CREATE ROUTINE, ALTER ROUTINE, EXECUTE ON `%s`.* TO `%s`@`%s`',
$database,
$username,
$remote
));
2017-07-15 16:52:34 +00:00
}
/**
* Flush the privileges for a given connection.
2017-07-15 16:52:34 +00:00
*/
public function flush(): bool
2017-07-15 16:52:34 +00:00
{
return $this->run('FLUSH PRIVILEGES');
2017-07-15 16:52:34 +00:00
}
/**
* Drop a given database on a specific connection.
2017-07-15 16:52:34 +00:00
*/
public function dropDatabase(string $database): bool
2017-07-15 16:52:34 +00:00
{
return $this->run(sprintf('DROP DATABASE IF EXISTS `%s`', $database));
2017-07-15 16:52:34 +00:00
}
/**
* Drop a given user on a specific connection.
2017-07-15 16:52:34 +00:00
*/
public function dropUser(string $username, string $remote): bool
2017-07-15 16:52:34 +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.
*/
private function run(string $statement): bool
2017-07-15 16:52:34 +00:00
{
return $this->database->connection($this->getConnection())->statement($statement);
2017-07-15 16:52:34 +00:00
}
}