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

162 lines
4.7 KiB
PHP
Raw Normal View History

2017-07-15 16:52:34 +00:00
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
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 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
}
}