Fairly basic implementation of adding a server. Does not actually do anything yet.
Pushing this for commit sake.
This commit is contained in:
parent
94bfd24e7c
commit
b6ac96550b
3 changed files with 89 additions and 13 deletions
8
app/Exceptions/DisplayValidationException.php
Normal file
8
app/Exceptions/DisplayValidationException.php
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pterodactyl\Exceptions;
|
||||||
|
|
||||||
|
class DisplayValidationException extends \Exception
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
|
@ -54,8 +54,16 @@ class ServersController extends Controller
|
||||||
|
|
||||||
public function postNewServer(Request $request)
|
public function postNewServer(Request $request)
|
||||||
{
|
{
|
||||||
Server::addServer($request->all());
|
|
||||||
|
try {
|
||||||
|
$resp = Server::addServer($request->all());
|
||||||
|
echo $resp . '<br />';
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Debugbar::addException($e);
|
||||||
|
}
|
||||||
|
|
||||||
return json_encode($request->all());
|
return json_encode($request->all());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -3,14 +3,16 @@
|
||||||
namespace Pterodactyl\Models;
|
namespace Pterodactyl\Models;
|
||||||
|
|
||||||
use Auth;
|
use Auth;
|
||||||
use Validator;
|
use DB;
|
||||||
use Debugbar;
|
use Debugbar;
|
||||||
|
use Validator;
|
||||||
|
|
||||||
use Pterodactyl\Exceptions\DisplayException;
|
use Pterodactyl\Exceptions\DisplayException;
|
||||||
use Pterodactyl\Exceptions\AccountNotFoundException;
|
use Pterodactyl\Exceptions\AccountNotFoundException;
|
||||||
use Pterodactyl\Models\User;
|
use Pterodactyl\Exceptions\DisplayValidationException;
|
||||||
use Pterodactyl\Models\Permission;
|
|
||||||
use Pterodactyl\Models\Subuser;
|
use Pterodactyl\Models;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
class Server extends Model
|
class Server extends Model
|
||||||
|
@ -48,6 +50,18 @@ class Server extends Model
|
||||||
self::$user = Auth::user();
|
self::$user = Auth::user();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static function generateSFTPUsername($name)
|
||||||
|
{
|
||||||
|
|
||||||
|
$name = preg_replace('/\s+/', '', $name);
|
||||||
|
if (strlen($name) > 6) {
|
||||||
|
return strtolower('ptdl-' . substr($name, 0, 6) . '_' . str_random(5));
|
||||||
|
}
|
||||||
|
|
||||||
|
return strtolower('ptdl-' . $name . '_' . str_random((11 - strlen($name))));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if we need to change the server's daemonSecret value to
|
* Determine if we need to change the server's daemonSecret value to
|
||||||
* match that of the user if they are a subuser.
|
* match that of the user if they are a subuser.
|
||||||
|
@ -62,7 +76,7 @@ class Server extends Model
|
||||||
return $server->daemonSecret;
|
return $server->daemonSecret;
|
||||||
}
|
}
|
||||||
|
|
||||||
$subuser = Subuser::where('server_id', $server->id)->where('user_id', self::$user->id)->first();
|
$subuser = Models\Subuser::where('server_id', $server->id)->where('user_id', self::$user->id)->first();
|
||||||
|
|
||||||
if (is_null($subuser)) {
|
if (is_null($subuser)) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -87,7 +101,7 @@ class Server extends Model
|
||||||
->where('active', 1);
|
->where('active', 1);
|
||||||
|
|
||||||
if (self::$user->root_admin !== 1) {
|
if (self::$user->root_admin !== 1) {
|
||||||
$query->whereIn('servers.id', Subuser::accessServers());
|
$query->whereIn('servers.id', Models\Subuser::accessServers());
|
||||||
}
|
}
|
||||||
|
|
||||||
return $query->get();
|
return $query->get();
|
||||||
|
@ -110,7 +124,7 @@ class Server extends Model
|
||||||
$query = self::where('uuidShort', $uuid)->where('active', 1);
|
$query = self::where('uuidShort', $uuid)->where('active', 1);
|
||||||
|
|
||||||
if (self::$user->root_admin !== 1) {
|
if (self::$user->root_admin !== 1) {
|
||||||
$query->whereIn('servers.id', Subuser::accessServers());
|
$query->whereIn('servers.id', Models\Subuser::accessServers());
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $query->first();
|
$result = $query->first();
|
||||||
|
@ -154,7 +168,7 @@ class Server extends Model
|
||||||
// Validate Fields
|
// Validate Fields
|
||||||
$validator = Validator::make($data, [
|
$validator = Validator::make($data, [
|
||||||
'owner' => 'required|email|exists:users,email',
|
'owner' => 'required|email|exists:users,email',
|
||||||
'node' => 'required|numeric|min:1',
|
'node' => 'required|numeric|min:1|exists:nodes,id',
|
||||||
'name' => 'required|regex:([\w -]{4,35})',
|
'name' => 'required|regex:([\w -]{4,35})',
|
||||||
'memory' => 'required|numeric|min:1',
|
'memory' => 'required|numeric|min:1',
|
||||||
'disk' => 'required|numeric|min:1',
|
'disk' => 'required|numeric|min:1',
|
||||||
|
@ -167,14 +181,60 @@ class Server extends Model
|
||||||
'custom_image_name' => 'required_if:use_custom_image,on',
|
'custom_image_name' => 'required_if:use_custom_image,on',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// @TODO: Have this return a JSON response.
|
// Run validator, throw catchable and displayable exception if it fails.
|
||||||
|
// Exception includes a JSON result of failed validation rules.
|
||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
foreach($validator->errors()->all() as $error) {
|
throw new DisplayValidationException(json_encode($validator->errors()->all()));
|
||||||
Debugbar::info($error);
|
}
|
||||||
|
|
||||||
|
// Get the User ID; user exists since we passed the 'exists:users,email' part of the validation
|
||||||
|
$user = Models\User::select('id')->where('email', $data['owner'])->first();
|
||||||
|
|
||||||
|
// Verify IP & Port are a.) free and b.) assigned to the node.
|
||||||
|
// We know the node exists because of 'exists:nodes,id' in the validation
|
||||||
|
$node = Models\Node::find($data['node']);
|
||||||
|
$allocation = Models\Allocation::where('ip', $data['ip'])->where('port', $data['port'])->where('node', $data['node'])->whereNull('assigned_to')->first();
|
||||||
|
|
||||||
|
// Something failed in the query, either that combo doesn't exist, or it is in use.
|
||||||
|
if (!$allocation) {
|
||||||
|
throw new DisplayException('The selected IP/Port combination (' . $data['ip'] . ':' . $data['port'] . ') is either already in use, or unavaliable for this node.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate those Service Option Variables
|
||||||
|
// We know the service and option exists because of the validation.
|
||||||
|
// We need to verify that the option exists for the service, and then check for
|
||||||
|
// any required variable fields. (fields are labeled env_<env_variable>)
|
||||||
|
$option = Models\ServiceOptions::where('id', $data['option'])->where('parent_service', $data['service'])->first();
|
||||||
|
if (!$option) {
|
||||||
|
throw new DisplayException('The requested service option does not exist for the specified service.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check those Variables
|
||||||
|
$variables = Models\ServiceVariables::where('option_id', $data['option'])->get();
|
||||||
|
if ($variables) {
|
||||||
|
foreach($variables as $variable) {
|
||||||
|
|
||||||
|
// Is the variable required?
|
||||||
|
if (!$data['env_' . $variable->env_variable]) {
|
||||||
|
if ($variable->required === 1) {
|
||||||
|
throw new DisplayException('A required service option variable field (env_' . $variable->env_variable . ') was missing from the request.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$data['env_' . $variable->env_variable] = $variable->default_value;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check aganist Regex Pattern
|
||||||
|
if (!is_null($variable->regex) && !preg_match($variable->regex, $data['env_' . $variable->env_variable])) {
|
||||||
|
throw new DisplayException('Failed to validate service option variable field (env_' . $variable->env_variable . ') aganist regex (' . $variable->regex . ').');
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return self::generateSFTPUsername($data['name']);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue