Skip to content

Commit ad5e253

Browse files
committed
Really basic initial implementation of service management
1 parent 5500dcc commit ad5e253

File tree

13 files changed

+705
-0
lines changed

13 files changed

+705
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
namespace Pterodactyl\Http\Controllers\Admin;
25+
26+
use Alert;
27+
use DB;
28+
use Log;
29+
use Validator;
30+
31+
use Pterodactyl\Models;
32+
use Pterodactyl\Repositories\ServiceRepository;
33+
use Pterodactyl\Exceptions\DisplayException;
34+
use Pterodactyl\Exceptions\DisplayValidationException;
35+
36+
use Pterodactyl\Http\Controllers\Controller;
37+
use Illuminate\Http\Request;
38+
39+
class ServiceController extends Controller
40+
{
41+
42+
public function __construct()
43+
{
44+
//
45+
}
46+
47+
public function getIndex(Request $request)
48+
{
49+
return view('admin.services.index', [
50+
'services' => Models\Service::all()
51+
]);
52+
}
53+
54+
public function getNew(Request $request)
55+
{
56+
//
57+
}
58+
59+
public function postNew(Request $request)
60+
{
61+
//
62+
}
63+
64+
public function getService(Request $request, $service)
65+
{
66+
return view('admin.services.view', [
67+
'service' => Models\Service::findOrFail($service),
68+
'options' => Models\ServiceOptions::select(
69+
'service_options.*',
70+
DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.option = service_options.id) as c_servers')
71+
)->where('parent_service', $service)->get()
72+
]);
73+
}
74+
75+
public function postService(Request $request, $service)
76+
{
77+
try {
78+
$repo = new ServiceRepository\Service;
79+
$repo->update($service, $request->except([
80+
'_token'
81+
]));
82+
Alert::success('Successfully updated this service.')->flash();
83+
} catch (DisplayValidationException $ex) {
84+
return redirect()->route('admin.services.service', $service)->withErrors(json_decode($ex->getMessage()))->withInput();
85+
} catch (DisplayException $ex) {
86+
Alert::danger($ex->getMessage())->flash();
87+
} catch (\Exception $ex) {
88+
Log::error($ex);
89+
Alert::danger('An error occurred while attempting to update this service.')->flash();
90+
}
91+
return redirect()->route('admin.services.service', $service)->withInput();
92+
}
93+
94+
public function getOption(Request $request, $option)
95+
{
96+
$opt = Models\ServiceOptions::findOrFail($option);
97+
return view('admin.services.options.view', [
98+
'service' => Models\Service::findOrFail($opt->parent_service),
99+
'option' => $opt,
100+
'variables' => Models\ServiceVariables::where('option_id', $option)->get(),
101+
'servers' => Models\Server::select('servers.*', 'users.email as a_ownerEmail')
102+
->join('users', 'users.id', '=', 'servers.owner')
103+
->where('option', $option)
104+
->paginate(10)
105+
]);
106+
}
107+
108+
public function postOption(Request $request, $option)
109+
{
110+
// editing option
111+
}
112+
113+
public function postOptionVariable(Request $request, $option, $variable)
114+
{
115+
if ($variable === 'new') {
116+
// adding new variable
117+
} else {
118+
try {
119+
$repo = new ServiceRepository\Variable;
120+
121+
// Because of the way old() works on the display side we prefix all of the variables with thier ID
122+
// We need to remove that prefix here since the repo doesn't want it.
123+
$data = [];
124+
foreach($request->except(['_token']) as $id => $val) {
125+
$data[str_replace($variable.'_', '', $id)] = $val;
126+
}
127+
$repo->update($variable, $data);
128+
Alert::success('Successfully updated variable.')->flash();
129+
} catch (DisplayValidationException $ex) {
130+
$data = [];
131+
foreach(json_decode($ex->getMessage(), true) as $id => $val) {
132+
$data[$variable.'_'.$id] = $val;
133+
}
134+
return redirect()->route('admin.services.option', $option)->withErrors((object) $data)->withInput();
135+
} catch (DisplayException $ex) {
136+
Alert::danger($ex->getMessage())->flash();
137+
} catch (\Exception $ex) {
138+
Log::error($ex);
139+
Alert::danger('An error occurred while attempting to update this service.')->flash();
140+
}
141+
return redirect()->route('admin.services.option', $option)->withInput();
142+
}
143+
}
144+
145+
}

app/Http/Routes/AdminRoutes.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,44 @@ public function map(Router $router) {
334334
]);
335335
});
336336

337+
// Service Routes
338+
$router->group([
339+
'prefix' => 'admin/services',
340+
'middleware' => [
341+
'auth',
342+
'admin',
343+
'csrf'
344+
]
345+
], function () use ($router) {
346+
$router->get('/', [
347+
'as' => 'admin.services',
348+
'uses' => 'Admin\ServiceController@getIndex'
349+
]);
350+
351+
$router->get('/service/{id}', [
352+
'as' => 'admin.services.service',
353+
'uses' => 'Admin\ServiceController@getService'
354+
]);
355+
356+
$router->post('/service/{id}', [
357+
'uses' => 'Admin\ServiceController@postService'
358+
]);
359+
360+
$router->get('/option/{id}', [
361+
'as' => 'admin.services.option',
362+
'uses' => 'Admin\ServiceController@getOption'
363+
]);
364+
365+
$router->post('/option/{id}', [
366+
'uses' => 'Admin\ServiceController@postOption'
367+
]);
368+
369+
$router->post('/option/{option}/{variable}', [
370+
'as' => 'admin.services.option.variable',
371+
'uses' => 'Admin\ServiceController@postOptionVariable'
372+
]);
373+
});
374+
337375
}
338376

339377
}

app/Models/Service.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,11 @@ class Service extends Model
3535
*/
3636
protected $table = 'services';
3737

38+
/**
39+
* Fields that are not mass assignable.
40+
*
41+
* @var array
42+
*/
43+
protected $guarded = ['id', 'created_at', 'updated_at'];
44+
3845
}

app/Models/ServiceOptions.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class ServiceOptions extends Model
3535
*/
3636
protected $table = 'service_options';
3737

38+
/**
39+
* Fields that are not mass assignable.
40+
*
41+
* @var array
42+
*/
43+
protected $guarded = ['id', 'created_at', 'updated_at'];
44+
3845
/**
3946
* Cast values to correct type.
4047
*

app/Models/ServiceVariables.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class ServiceVariables extends Model
3535
*/
3636
protected $table = 'service_variables';
3737

38+
/**
39+
* Fields that are not mass assignable.
40+
*
41+
* @var array
42+
*/
43+
protected $guarded = ['id', 'created_at', 'updated_at'];
44+
3845
/**
3946
* Cast values to correct type.
4047
*
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
namespace Pterodactyl\Repositories\ServiceRepository;
25+
26+
use DB;
27+
use Validator;
28+
29+
use Pterodactyl\Models;
30+
use Pterodactyl\Services\UuidService;
31+
32+
use Pterodactyl\Exceptions\DisplayException;
33+
use Pterodactyl\Exceptions\DisplayValidationException;
34+
35+
class Option
36+
{
37+
38+
public function __construct()
39+
{
40+
//
41+
}
42+
43+
public function update($id, array $data)
44+
{
45+
46+
}
47+
48+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
namespace Pterodactyl\Repositories\ServiceRepository;
25+
26+
use DB;
27+
use Validator;
28+
29+
use Pterodactyl\Models;
30+
use Pterodactyl\Services\UuidService;
31+
32+
use Pterodactyl\Exceptions\DisplayException;
33+
use Pterodactyl\Exceptions\DisplayValidationException;
34+
35+
class Service
36+
{
37+
38+
public function __construct()
39+
{
40+
//
41+
}
42+
43+
public function update($id, array $data)
44+
{
45+
$service = Models\Service::findOrFail($id);
46+
47+
$validator = Validator::make($data, [
48+
'name' => 'sometimes|required|string|min:1|max:255',
49+
'description' => 'sometimes|required|string',
50+
'file' => 'sometimes|required|regex:/^[\w.-]{1,50}$/',
51+
'executable' => 'sometimes|required|max:255|regex:/^(.*)$/',
52+
'startup' => 'sometimes|required|string'
53+
]);
54+
55+
if ($validator->fails()) {
56+
throw new DisplayValidationException($validator->errors());
57+
}
58+
59+
$service->fill($data);
60+
$service->save();
61+
}
62+
63+
}

0 commit comments

Comments
 (0)