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

147 lines
3.8 KiB
PHP
Raw Normal View History

2017-07-15 16:52:34 +00:00
<?php
2017-09-26 02:43:01 +00:00
/**
2017-07-15 16:52:34 +00:00
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
2017-07-15 16:52:34 +00:00
*/
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Database;
use Illuminate\Foundation\Application;
2017-08-05 22:26:30 +00:00
use Illuminate\Database\DatabaseManager;
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
{
/**
* @var \Illuminate\Database\DatabaseManager
2017-07-15 16:52:34 +00:00
*/
protected $database;
2017-07-15 16:52:34 +00:00
/**
* DatabaseRepository constructor.
*
* @param \Illuminate\Foundation\Application $application
* @param \Illuminate\Database\DatabaseManager $database
2017-07-15 16:52:34 +00:00
*/
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
}
/**
* {@inheritdoc}
*/
public function model()
{
return Database::class;
}
/**
* {@inheritdoc}
* @return bool|\Illuminate\Database\Eloquent\Model
*/
public function createIfNotExists(array $data)
{
$instance = $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();
if ($instance > 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);
}
/**
* {@inheritdoc}
*/
public function createDatabase($database, $connection = null)
{
return $this->runStatement(
2017-08-22 03:10:48 +00:00
sprintf('CREATE DATABASE IF NOT EXISTS `%s`', $database),
$connection
2017-07-15 16:52:34 +00:00
);
}
/**
* {@inheritdoc}
*/
public function createUser($username, $remote, $password, $connection = null)
{
return $this->runStatement(
2017-08-22 03:10:48 +00:00
sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', $username, $remote, $password),
$connection
2017-07-15 16:52:34 +00:00
);
}
/**
* {@inheritdoc}
*/
public function assignUserToDatabase($database, $username, $remote, $connection = null)
{
return $this->runStatement(
sprintf(
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX, EXECUTE ON `%s`.* TO `%s`@`%s`',
2017-08-22 03:10:48 +00:00
$database,
$username,
$remote
2017-07-15 16:52:34 +00:00
),
$connection
);
}
/**
* {@inheritdoc}
*/
public function flush($connection = null)
{
return $this->runStatement('FLUSH PRIVILEGES', $connection);
}
/**
* {@inheritdoc}
*/
public function dropDatabase($database, $connection = null)
{
return $this->runStatement(
2017-08-22 03:10:48 +00:00
sprintf('DROP DATABASE IF EXISTS `%s`', $database),
$connection
2017-07-15 16:52:34 +00:00
);
}
/**
* {@inheritdoc}
*/
public function dropUser($username, $remote, $connection = null)
{
return $this->runStatement(
2017-08-22 03:10:48 +00:00
sprintf('DROP USER IF EXISTS `%s`@`%s`', $username, $remote),
$connection
2017-07-15 16:52:34 +00:00
);
}
/**
* Run the provided statement against the database on a given connection.
*
2017-08-22 03:10:48 +00:00
* @param string $statement
* @param null|string $connection
2017-07-15 16:52:34 +00:00
* @return bool
*/
protected function runStatement($statement, $connection = null)
{
return $this->database->connection($connection)->statement($statement);
2017-07-15 16:52:34 +00:00
}
}