Path: blob/1.0-develop/app/Repositories/Eloquent/DatabaseRepository.php
7460 views
<?php12namespace Pterodactyl\Repositories\Eloquent;34use Pterodactyl\Models\Database;5use Illuminate\Support\Collection;6use Illuminate\Foundation\Application;7use Illuminate\Database\DatabaseManager;8use Illuminate\Contracts\Pagination\LengthAwarePaginator;9use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;1011class DatabaseRepository extends EloquentRepository implements DatabaseRepositoryInterface12{13protected string $connection = self::DEFAULT_CONNECTION_NAME;1415/**16* DatabaseRepository constructor.17*/18public function __construct(Application $application, private DatabaseManager $database)19{20parent::__construct($application);21}2223/**24* Return the model backing this repository.25*/26public function model(): string27{28return Database::class;29}3031/**32* Return the connection to execute statements against.33*/34public function getConnection(): string35{36return $this->connection;37}3839/**40* Set the connection name to execute statements against.41*/42public function setConnection(string $connection): self43{44$this->connection = $connection;4546return $this;47}4849/**50* Return all the databases belonging to a server.51*/52public function getDatabasesForServer(int $server): Collection53{54return $this->getBuilder()->with('host')->where('server_id', $server)->get($this->getColumns());55}5657/**58* Return all the databases for a given host with the server relationship loaded.59*/60public function getDatabasesForHost(int $host, int $count = 25): LengthAwarePaginator61{62return $this->getBuilder()->with('server')63->where('database_host_id', $host)64->paginate($count, $this->getColumns());65}6667/**68* Create a new database on a given connection.69*/70public function createDatabase(string $database): bool71{72return $this->run(sprintf('CREATE DATABASE IF NOT EXISTS `%s`', $database));73}7475/**76* Create a new database user on a given connection.77*/78public function createUser(string $username, string $remote, string $password, ?int $max_connections): bool79{80$args = [$username, $remote, $password];81$command = 'CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'';8283if (!empty($max_connections)) {84$args[] = $max_connections;85$command .= ' WITH MAX_USER_CONNECTIONS %s';86}8788return $this->run(sprintf($command, ...$args));89}9091/**92* Give a specific user access to a given database.93*/94public function assignUserToDatabase(string $database, string $username, string $remote): bool95{96return $this->run(sprintf(97'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, REFERENCES, INDEX, LOCK TABLES, CREATE ROUTINE, ALTER ROUTINE, EXECUTE, CREATE TEMPORARY TABLES, CREATE VIEW, SHOW VIEW, EVENT, TRIGGER ON `%s`.* TO `%s`@`%s`',98$database,99$username,100$remote101));102}103104/**105* Flush the privileges for a given connection.106*/107public function flush(): bool108{109return $this->run('FLUSH PRIVILEGES');110}111112/**113* Drop a given database on a specific connection.114*/115public function dropDatabase(string $database): bool116{117return $this->run(sprintf('DROP DATABASE IF EXISTS `%s`', $database));118}119120/**121* Drop a given user on a specific connection.122*/123public function dropUser(string $username, string $remote): bool124{125return $this->run(sprintf('DROP USER IF EXISTS `%s`@`%s`', $username, $remote));126}127128/**129* Run the provided statement against the database on a given connection.130*/131private function run(string $statement): bool132{133return $this->database->connection($this->getConnection())->statement($statement);134}135}136137138