Skip to content

Commit d0e4592

Browse files
committed
Initial validator implementation for server creation
1 parent 692825e commit d0e4592

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

app/Http/Controllers/Admin/ServersController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function getView(Request $request, $id)
5454

5555
public function postNewServer(Request $request)
5656
{
57+
Server::addServer($request->all());
5758
return json_encode($request->all());
5859
}
5960

app/Models/Server.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
namespace Pterodactyl\Models;
44

55
use Auth;
6+
use Validator;
7+
use Debugbar;
8+
9+
use Pterodactyl\Exceptions\DisplayException;
10+
use Pterodactyl\Exceptions\AccountNotFoundException;
11+
use Pterodactyl\Models\User;
612
use Pterodactyl\Models\Permission;
713
use Pterodactyl\Models\Subuser;
814
use Illuminate\Database\Eloquent\Model;
@@ -138,4 +144,38 @@ public static function getGuzzleHeaders($uuid)
138144

139145
}
140146

147+
/**
148+
* Adds a new server to the system.
149+
* @param array $data An array of data descriptors for creating the server. These should align to the columns in the database.
150+
*/
151+
public static function addServer(array $data)
152+
{
153+
154+
// Validate Fields
155+
$validator = Validator::make($data, [
156+
'owner' => 'required|email|exists:users,email',
157+
'node' => 'required|numeric|min:1',
158+
'name' => 'required|regex:([\w -]{4,35})',
159+
'memory' => 'required|numeric|min:1',
160+
'disk' => 'required|numeric|min:1',
161+
'cpu' => 'required|numeric|min:0',
162+
'io' => 'required|numeric|min:10|max:1000',
163+
'ip' => 'required|ip',
164+
'port' => 'required|numeric|min:1|max:65535',
165+
'service' => 'required|numeric|min:1|exists:services,id',
166+
'option' => 'required|numeric|min:1|exists:service_options,id',
167+
'custom_image_name' => 'required_if:use_custom_image,on',
168+
]);
169+
170+
// @TODO: Have this return a JSON response.
171+
if ($validator->fails()) {
172+
foreach($validator->errors()->all() as $error) {
173+
Debugbar::info($error);
174+
}
175+
}
176+
177+
return;
178+
179+
}
180+
141181
}

resources/views/admin/servers/new.blade.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
<div class="well">
2525
<div class="row">
2626
<div class="form-group col-md-6">
27-
<label for="server_name" class="control-label">Server Name</label>
27+
<label for="name" class="control-label">Server Name</label>
2828
<div>
29-
<input type="text" autocomplete="off" name="server_name" class="form-control" />
29+
<input type="text" autocomplete="off" name="name" class="form-control" />
3030
<p class="text-muted"><small><em>Character limits: <code>a-zA-Z0-9_-</code> and <code>[Space]</code> (max 35 characters)</em></small></p>
3131
</div>
3232
</div>
3333
<div class="form-group col-md-6">
34-
<label for="server_name" class="control-label">Owner Email</label>
34+
<label for="owner" class="control-label">Owner Email</label>
3535
<div>
36-
<input type="text" autocomplete="off" name="owner_email" class="form-control" />
36+
<input type="text" autocomplete="off" name="owner" class="form-control" />
3737
</div>
3838
</div>
3939
</div>
@@ -129,7 +129,7 @@
129129
<div class="form-group col-md-12">
130130
<label for="service" class="control-label">Service Type</label>
131131
<div>
132-
<select name="node" id="getService" class="form-control">
132+
<select name="service" id="getService" class="form-control">
133133
<option></option>
134134
@foreach($services as $service)
135135
<option value="{{ $service->id }}">{{ $service->name }}</option>
@@ -139,9 +139,9 @@
139139
</div>
140140
</div>
141141
<div class="form-group col-md-12 hidden">
142-
<label for="service_option" class="control-label">Service Option</label>
142+
<label for="option" class="control-label">Service Option</label>
143143
<div>
144-
<select name="node" id="getOption" class="form-control">
144+
<select name="option" id="getOption" class="form-control">
145145
<option></option>
146146
</select>
147147
<p class="text-muted"><small>Select the type of service that this server will be running.</small></p>
@@ -181,7 +181,7 @@
181181
<div class="row">
182182
<div class="col-md-12 text-center">
183183
{!! csrf_field() !!}
184-
<input type="submit" name="submit" class="btn btn-primary btn-sm" value="Create New Server" />
184+
<input type="submit" class="btn btn-primary btn-sm" value="Create New Server" />
185185
</div>
186186
</div>
187187
</div>
@@ -350,7 +350,7 @@
350350
<div class="form-group col-md-6">\
351351
<label for="var_ref_' + item.id + '" class="control-label">' + item.name + '</label> ' + isRequired + '\
352352
<div>\
353-
<input type="text" autocomplete="off" name="var_ref_' + item.id + '" class="form-control" value="' + item.default_value + '" />\
353+
<input type="text" autocomplete="off" name="env_' + item.env_variable + '" class="form-control" value="' + item.default_value + '" />\
354354
<p class="text-muted"><small>' + item.description + '</small></p>\
355355
<p class="text-muted"><small>Regex Requirements for Input: <code>' + item.regex + '</code></small></p>\
356356
</div>\

0 commit comments

Comments
 (0)