Add support for node management actions using new services
This commit is contained in:
parent
4391defb9f
commit
c1a078bdcf
33 changed files with 1375 additions and 745 deletions
|
@ -51,6 +51,7 @@ class BaseRepository implements BaseRepositoryInterface
|
|||
|
||||
public function setNode($id)
|
||||
{
|
||||
// @todo accept a model
|
||||
$this->node = $this->nodeRepository->find($id);
|
||||
|
||||
return $this;
|
||||
|
|
63
app/Repositories/Daemon/ConfigurationRepository.php
Normal file
63
app/Repositories/Daemon/ConfigurationRepository.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?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\Daemon;
|
||||
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface;
|
||||
|
||||
class ConfigurationRepository extends BaseRepository implements ConfigurationRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function update(array $overrides = [])
|
||||
{
|
||||
$node = $this->getNode();
|
||||
$structure = [
|
||||
'web' => [
|
||||
'listen' => $node->daemonListen,
|
||||
'ssl' => [
|
||||
'enabled' => (! $node->behind_proxy && $node->scheme === 'https'),
|
||||
],
|
||||
],
|
||||
'sftp' => [
|
||||
'path' => $node->daemonBase,
|
||||
'port' => $node->daemonSFTP,
|
||||
],
|
||||
'remote' => [
|
||||
'base' => $this->config->get('app.url'),
|
||||
],
|
||||
'uploads' => [
|
||||
'size_limit' => $node->upload_size,
|
||||
],
|
||||
'keys' => [
|
||||
$node->daemonSecret,
|
||||
],
|
||||
];
|
||||
|
||||
return $this->getHttpClient()->request('PATCH', '/config', [
|
||||
'json' => array_merge($structure, $overrides),
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -105,6 +105,14 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
|
|||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}.
|
||||
*/
|
||||
public function findCountWhere(array $fields)
|
||||
{
|
||||
return $this->getBuilder()->where($fields)->count($this->getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
@ -28,8 +28,9 @@ use Pterodactyl\Models\Location;
|
|||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
|
||||
use Pterodactyl\Repositories\Eloquent\Attributes\SearchableRepository;
|
||||
|
||||
class LocationRepository extends EloquentRepository implements LocationRepositoryInterface
|
||||
class LocationRepository extends SearchableRepository implements LocationRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
|
@ -44,21 +45,6 @@ class LocationRepository extends EloquentRepository implements LocationRepositor
|
|||
return Location::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function search($term)
|
||||
{
|
||||
if (empty($term)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$clone = clone $this;
|
||||
$clone->searchTerm = $term;
|
||||
|
||||
return $clone;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
namespace Pterodactyl\Repositories\Eloquent;
|
||||
|
||||
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Pterodactyl\Models\Node;
|
||||
use Pterodactyl\Repositories\Eloquent\Attributes\SearchableRepository;
|
||||
|
||||
|
@ -38,6 +39,104 @@ class NodeRepository extends SearchableRepository implements NodeRepositoryInter
|
|||
return Node::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getUsageStats($id)
|
||||
{
|
||||
$node = $this->getBuilder()->select(
|
||||
'nodes.disk_overallocate', 'nodes.memory_overallocate', 'nodes.disk', 'nodes.memory',
|
||||
$this->getBuilder()->raw('SUM(servers.memory) as sum_memory, SUM(servers.disk) as sum_disk')
|
||||
)->join('servers', 'servers.node_id', '=', 'nodes.id')
|
||||
->where('nodes.id', $id)
|
||||
->first();
|
||||
|
||||
return collect(['disk' => $node->sum_disk, 'memory' => $node->sum_memory])
|
||||
->mapWithKeys(function ($value, $key) use ($node) {
|
||||
$maxUsage = $node->{$key};
|
||||
if ($node->{$key . '_overallocate'} > 0) {
|
||||
$maxUsage = $node->{$key} * (1 + ($node->{$key . '_overallocate'} / 100));
|
||||
}
|
||||
|
||||
$percent = ($value / $maxUsage) * 100;
|
||||
|
||||
return [
|
||||
$key => [
|
||||
'value' => number_format($value),
|
||||
'max' => number_format($maxUsage),
|
||||
'percent' => $percent,
|
||||
'css' => ($percent <= 75) ? 'green' : (($percent > 90) ? 'red' : 'yellow'),
|
||||
],
|
||||
];
|
||||
})
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getNodeListingData($count = 25)
|
||||
{
|
||||
$instance = $this->getBuilder()->with('location')->withCount('servers');
|
||||
|
||||
if ($this->searchTerm) {
|
||||
$instance->search($this->searchTerm);
|
||||
}
|
||||
|
||||
return $instance->paginate($count, $this->getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSingleNode($id)
|
||||
{
|
||||
$instance = $this->getBuilder()->with('location')->withCount('servers')->find($id, $this->getColumns());
|
||||
|
||||
if (! $instance) {
|
||||
throw new RecordNotFoundException();
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getNodeAllocations($id)
|
||||
{
|
||||
$instance = $this->getBuilder()->find($id, $this->getColumns());
|
||||
|
||||
if (! $instance) {
|
||||
throw new RecordNotFoundException();
|
||||
}
|
||||
|
||||
$instance->setRelation(
|
||||
'allocations',
|
||||
$this->getModel()->allocations()->orderBy('ip', 'asc')
|
||||
->orderBy('port', 'asc')
|
||||
->with('server')
|
||||
->paginate(50)
|
||||
);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getNodeServers($id)
|
||||
{
|
||||
$instance = $this->getBuilder()->with('servers.user', 'servers.service', 'servers.option')
|
||||
->find($id, $this->getColumns());
|
||||
|
||||
if (! $instance) {
|
||||
throw new RecordNotFoundException();
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
@ -1,207 +0,0 @@
|
|||
<?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;
|
||||
|
||||
use DB;
|
||||
use Auth;
|
||||
use Crypt;
|
||||
use Validator;
|
||||
use IPTools\Network;
|
||||
use Pterodactyl\Models\User;
|
||||
use Pterodactyl\Models\APIKey as Key;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Models\APIPermission as Permission;
|
||||
use Pterodactyl\Exceptions\DisplayValidationException;
|
||||
|
||||
class APIRepository
|
||||
{
|
||||
/**
|
||||
* Holder for listing of allowed IPs when creating a new key.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $allowed = [];
|
||||
|
||||
/**
|
||||
* The eloquent model for a user.
|
||||
*
|
||||
* @var \Pterodactyl\Models\User
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* Constructor for API Repository.
|
||||
*
|
||||
* @param null|\Pterodactyl\Models\User $user
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(User $user = null)
|
||||
{
|
||||
$this->user = is_null($user) ? Auth::user() : $user;
|
||||
if (is_null($this->user)) {
|
||||
throw new \Exception('Unable to initialize user for API repository instance.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a New API Keypair on the system.
|
||||
*
|
||||
* @param array $data
|
||||
* @return string
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\DisplayValidationException
|
||||
*/
|
||||
public function create(array $data)
|
||||
{
|
||||
$validator = Validator::make($data, [
|
||||
'memo' => 'string|max:500',
|
||||
'allowed_ips' => 'sometimes|string',
|
||||
'permissions' => 'sometimes|required|array',
|
||||
'admin_permissions' => 'sometimes|required|array',
|
||||
]);
|
||||
|
||||
$validator->after(function ($validator) use ($data) {
|
||||
if (array_key_exists('allowed_ips', $data) && ! empty($data['allowed_ips'])) {
|
||||
foreach (explode("\n", $data['allowed_ips']) as $ip) {
|
||||
$ip = trim($ip);
|
||||
try {
|
||||
Network::parse($ip);
|
||||
array_push($this->allowed, $ip);
|
||||
} catch (\Exception $ex) {
|
||||
$validator->errors()->add('allowed_ips', 'Could not parse IP <' . $ip . '> because it is in an invalid format.');
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Run validator, throw catchable and displayable exception if it fails.
|
||||
// Exception includes a JSON result of failed validation rules.
|
||||
if ($validator->fails()) {
|
||||
throw new DisplayValidationException(json_encode($validator->errors()));
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$secretKey = str_random(16) . '.' . str_random(7) . '.' . str_random(7);
|
||||
$key = Key::create([
|
||||
'user_id' => $this->user->id,
|
||||
'public' => str_random(16),
|
||||
'secret' => Crypt::encrypt($secretKey),
|
||||
'allowed_ips' => empty($this->allowed) ? null : json_encode($this->allowed),
|
||||
'memo' => $data['memo'],
|
||||
'expires_at' => null,
|
||||
]);
|
||||
|
||||
$totalPermissions = 0;
|
||||
$pNodes = Permission::permissions();
|
||||
|
||||
if (isset($data['permissions'])) {
|
||||
foreach ($data['permissions'] as $permission) {
|
||||
$parts = explode('-', $permission);
|
||||
|
||||
if (count($parts) !== 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
list($block, $search) = $parts;
|
||||
|
||||
if (! array_key_exists($block, $pNodes['_user'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! in_array($search, $pNodes['_user'][$block])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$totalPermissions++;
|
||||
Permission::create([
|
||||
'key_id' => $key->id,
|
||||
'permission' => 'user.' . $permission,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->user->isRootAdmin() && isset($data['admin_permissions'])) {
|
||||
unset($pNodes['_user']);
|
||||
|
||||
foreach ($data['admin_permissions'] as $permission) {
|
||||
$parts = explode('-', $permission);
|
||||
|
||||
if (count($parts) !== 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
list($block, $search) = $parts;
|
||||
|
||||
if (! array_key_exists($block, $pNodes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! in_array($search, $pNodes[$block])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$totalPermissions++;
|
||||
Permission::create([
|
||||
'key_id' => $key->id,
|
||||
'permission' => $permission,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($totalPermissions < 1) {
|
||||
throw new DisplayException('No valid permissions were passed.');
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $secretKey;
|
||||
} catch (\Exception $ex) {
|
||||
DB::rollBack();
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Revokes an API key and associated permissions.
|
||||
*
|
||||
* @param string $key
|
||||
* @return void
|
||||
*
|
||||
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||
*/
|
||||
public function revoke($key)
|
||||
{
|
||||
DB::transaction(function () use ($key) {
|
||||
$model = Key::with('permissions')->where('public', $key)->where('user_id', $this->user->id)->firstOrFail();
|
||||
foreach ($model->permissions as &$permission) {
|
||||
$permission->delete();
|
||||
}
|
||||
|
||||
$model->delete();
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,173 +0,0 @@
|
|||
<?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;
|
||||
|
||||
use DB;
|
||||
use Crypt;
|
||||
use Validator;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Models\Database;
|
||||
use Pterodactyl\Models\DatabaseHost;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Exceptions\DisplayValidationException;
|
||||
|
||||
class DatabaseRepository
|
||||
{
|
||||
/**
|
||||
* Adds a new database to a specified database host server.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
* @return \Pterodactyl\Models\Database
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\DisplayValidationException
|
||||
*/
|
||||
public function create($id, array $data)
|
||||
{
|
||||
$server = Server::findOrFail($id);
|
||||
|
||||
$validator = Validator::make($data, [
|
||||
'host' => 'required|exists:database_hosts,id',
|
||||
'database' => 'required|regex:/^\w{1,100}$/',
|
||||
'connection' => 'required|regex:/^[0-9%.]{1,15}$/',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
throw new DisplayValidationException(json_encode($validator->errors()));
|
||||
}
|
||||
|
||||
$host = DatabaseHost::findOrFail($data['host']);
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
$database = Database::firstOrNew([
|
||||
'server_id' => $server->id,
|
||||
'database_host_id' => $data['host'],
|
||||
'database' => sprintf('s%d_%s', $server->id, $data['database']),
|
||||
]);
|
||||
|
||||
if ($database->exists) {
|
||||
throw new DisplayException('A database with those details already exists in the system.');
|
||||
}
|
||||
|
||||
$database->username = sprintf('s%d_%s', $server->id, str_random(10));
|
||||
$database->remote = $data['connection'];
|
||||
$database->password = Crypt::encrypt(str_random(20));
|
||||
|
||||
$database->save();
|
||||
} catch (\Exception $ex) {
|
||||
DB::rollBack();
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
try {
|
||||
$host->setDynamicConnection();
|
||||
|
||||
DB::connection('dynamic')->statement(sprintf('CREATE DATABASE IF NOT EXISTS `%s`', $database->database));
|
||||
DB::connection('dynamic')->statement(sprintf(
|
||||
'CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'',
|
||||
$database->username, $database->remote, Crypt::decrypt($database->password)
|
||||
));
|
||||
DB::connection('dynamic')->statement(sprintf(
|
||||
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX ON `%s`.* TO `%s`@`%s`',
|
||||
$database->database, $database->username, $database->remote
|
||||
));
|
||||
|
||||
DB::connection('dynamic')->statement('FLUSH PRIVILEGES');
|
||||
|
||||
// Save Everything
|
||||
DB::commit();
|
||||
|
||||
return $database;
|
||||
} catch (\Exception $ex) {
|
||||
try {
|
||||
DB::connection('dynamic')->statement(sprintf('DROP DATABASE IF EXISTS `%s`', $database->database));
|
||||
DB::connection('dynamic')->statement(sprintf('DROP USER IF EXISTS `%s`@`%s`', $database->username, $database->remote));
|
||||
DB::connection('dynamic')->statement('FLUSH PRIVILEGES');
|
||||
} catch (\Exception $ex) {
|
||||
}
|
||||
|
||||
DB::rollBack();
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the password for a given database.
|
||||
*
|
||||
* @param int $id
|
||||
* @param string $password
|
||||
* @return void
|
||||
*
|
||||
* @todo Fix logic behind resetting passwords.
|
||||
*/
|
||||
public function password($id, $password)
|
||||
{
|
||||
$database = Database::with('host')->findOrFail($id);
|
||||
$database->host->setDynamicConnection();
|
||||
|
||||
DB::transaction(function () use ($database, $password) {
|
||||
$database->password = Crypt::encrypt($password);
|
||||
|
||||
// We have to do the whole delete user, create user thing rather than
|
||||
// SET PASSWORD ... because MariaDB and PHP statements ends up inserting
|
||||
// a corrupted password. A way around this is strtoupper(sha1(sha1($password, true)))
|
||||
// but no garuntees that will work correctly with every system.
|
||||
DB::connection('dynamic')->statement(sprintf('DROP USER IF EXISTS `%s`@`%s`', $database->username, $database->remote));
|
||||
DB::connection('dynamic')->statement(sprintf(
|
||||
'CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'',
|
||||
$database->username, $database->remote, $password
|
||||
));
|
||||
DB::connection('dynamic')->statement(sprintf(
|
||||
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX ON `%s`.* TO `%s`@`%s`',
|
||||
$database->database, $database->username, $database->remote
|
||||
));
|
||||
DB::connection('dynamic')->statement('FLUSH PRIVILEGES');
|
||||
|
||||
$database->save();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops a database from the associated database host.
|
||||
*
|
||||
* @param int $id
|
||||
* @return void
|
||||
*/
|
||||
public function drop($id)
|
||||
{
|
||||
$database = Database::with('host')->findOrFail($id);
|
||||
$database->host->setDynamicConnection();
|
||||
|
||||
DB::transaction(function () use ($database) {
|
||||
DB::connection('dynamic')->statement(sprintf('DROP DATABASE IF EXISTS `%s`', $database->database));
|
||||
DB::connection('dynamic')->statement(sprintf('DROP USER IF EXISTS `%s`@`%s`', $database->username, $database->remote));
|
||||
DB::connection('dynamic')->statement('FLUSH PRIVILEGES');
|
||||
|
||||
$database->delete();
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,104 +0,0 @@
|
|||
<?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;
|
||||
|
||||
use Validator;
|
||||
use Pterodactyl\Models\Location;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Exceptions\DisplayValidationException;
|
||||
|
||||
class LocationRepository
|
||||
{
|
||||
/**
|
||||
* Creates a new location on the system.
|
||||
*
|
||||
* @param array $data
|
||||
* @return \Pterodactyl\Models\Location
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayValidationException
|
||||
*/
|
||||
public function create(array $data)
|
||||
{
|
||||
$validator = Validator::make($data, [
|
||||
'short' => 'required|string|between:1,60|unique:locations,short',
|
||||
'long' => 'required|string|between:1,255',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
throw new DisplayValidationException(json_encode($validator->errors()));
|
||||
}
|
||||
|
||||
return Location::create([
|
||||
'long' => $data['long'],
|
||||
'short' => $data['short'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies a location.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
* @return \Pterodactyl\Models\Location
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayValidationException
|
||||
*/
|
||||
public function update($id, array $data)
|
||||
{
|
||||
$location = Location::findOrFail($id);
|
||||
|
||||
$validator = Validator::make($data, [
|
||||
'short' => 'sometimes|required|string|between:1,60|unique:locations,short,' . $location->id,
|
||||
'long' => 'sometimes|required|string|between:1,255',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
throw new DisplayValidationException(json_encode($validator->errors()));
|
||||
}
|
||||
|
||||
$location->fill($data)->save();
|
||||
|
||||
return $location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a location from the system.
|
||||
*
|
||||
* @param int $id
|
||||
* @return void
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$location = Location::withCount('nodes')->findOrFail($id);
|
||||
|
||||
if ($location->nodes_count > 0) {
|
||||
throw new DisplayException('Cannot delete a location that has nodes assigned to it.');
|
||||
}
|
||||
|
||||
$location->delete();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue