Skip to content

Commit 419031e

Browse files
committed
Add function file editing, and move all service file items into database
1 parent d585294 commit 419031e

File tree

12 files changed

+298
-404
lines changed

12 files changed

+298
-404
lines changed

app/Http/Controllers/Admin/ServiceController.php

Lines changed: 27 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
use Log;
2828
use Alert;
29-
use Storage;
3029
use Pterodactyl\Models;
3130
use Illuminate\Http\Request;
3231
use Pterodactyl\Exceptions\DisplayException;
@@ -74,6 +73,18 @@ public function view(Request $request, $id)
7473
]);
7574
}
7675

76+
/**
77+
* Return function editing view for a service.
78+
*
79+
* @param Request $request
80+
* @param int $id
81+
* @return \Illuminate\View\View
82+
*/
83+
public function viewFunctions(Request $request, $id)
84+
{
85+
return view('admin.services.functions', ['service' => Models\Service::findOrFail($id)]);
86+
}
87+
7788
/**
7889
* Handle post action for new service.
7990
*
@@ -113,11 +124,12 @@ public function create(Request $request)
113124
public function edit(Request $request, $id)
114125
{
115126
$repo = new ServiceRepository;
127+
$redirectTo = ($request->input('redirect_to')) ? 'admin.services.view.functions' : 'admin.services.view';
116128

117129
try {
118130
if ($request->input('action') !== 'delete') {
119131
$repo->update($id, $request->intersect([
120-
'name', 'description', 'folder', 'startup',
132+
'name', 'description', 'folder', 'startup', 'index_file',
121133
]));
122134
Alert::success('Service has been updated successfully.')->flash();
123135
} else {
@@ -127,201 +139,26 @@ public function edit(Request $request, $id)
127139
return redirect()->route('admin.services');
128140
}
129141
} catch (DisplayValidationException $ex) {
130-
return redirect()->route('admin.services.view', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
142+
return redirect()->route($redirectTo, $id)->withErrors(json_decode($ex->getMessage()))->withInput();
131143
} catch (DisplayException $ex) {
132144
Alert::danger($ex->getMessage())->flash();
133145
} catch (\Exception $ex) {
134146
Log::error($ex);
135147
Alert::danger('An error occurred while attempting to update this service. This error has been logged.')->flash();
136148
}
137149

138-
return redirect()->route('admin.services.view', $id);
150+
return redirect()->route($redirectTo, $id);
139151
}
140152

141-
// public function getOption(Request $request, $service, $option)
142-
// {
143-
// $option = Models\ServiceOption::with('service', 'variables')->findOrFail($option);
144-
// $option->setRelation('servers', $option->servers()->with('user')->paginate(25));
145-
//
146-
// return view('admin.services.options.view', ['option' => $option]);
147-
// }
148-
//
149-
// public function postOption(Request $request, $service, $option)
150-
// {
151-
// try {
152-
// $repo = new ServiceRepository\Option;
153-
// $repo->update($option, $request->only([
154-
// 'name', 'description', 'tag',
155-
// 'executable', 'docker_image', 'startup',
156-
// ]));
157-
// Alert::success('Option settings successfully updated.')->flash();
158-
// } catch (DisplayValidationException $ex) {
159-
// return redirect()->route('admin.services.option', [$service, $option])->withErrors(json_decode($ex->getMessage()))->withInput();
160-
// } catch (\Exception $ex) {
161-
// Log::error($ex);
162-
// Alert::danger('An error occured while attempting to modify this option.')->flash();
163-
// }
164-
//
165-
// return redirect()->route('admin.services.option', [$service, $option])->withInput();
166-
// }
167-
//
168-
// public function deleteOption(Request $request, $service, $option)
169-
// {
170-
// try {
171-
// $repo = new ServiceRepository\Option;
172-
// $repo->delete($option);
173-
//
174-
// Alert::success('Successfully deleted that option.')->flash();
175-
//
176-
// return redirect()->route('admin.services.service', $service);
177-
// } catch (DisplayException $ex) {
178-
// Alert::danger($ex->getMessage())->flash();
179-
// } catch (\Exception $ex) {
180-
// Log::error($ex);
181-
// Alert::danger('An error was encountered while attempting to delete this option.')->flash();
182-
// }
183-
//
184-
// return redirect()->route('admin.services.option', [$service, $option]);
185-
// }
186-
//
187-
// public function postOptionVariable(Request $request, $service, $option, $variable)
188-
// {
189-
// try {
190-
// $repo = new ServiceRepository\Variable;
191-
//
192-
// // Because of the way old() works on the display side we prefix all of the variables with thier ID
193-
// // We need to remove that prefix here since the repo doesn't want it.
194-
// $data = [
195-
// 'user_viewable' => '0',
196-
// 'user_editable' => '0',
197-
// 'required' => '0',
198-
// ];
199-
// foreach ($request->except(['_token']) as $id => $val) {
200-
// $data[str_replace($variable . '_', '', $id)] = $val;
201-
// }
202-
// $repo->update($variable, $data);
203-
// Alert::success('Successfully updated variable.')->flash();
204-
// } catch (DisplayValidationException $ex) {
205-
// $data = [];
206-
// foreach (json_decode($ex->getMessage(), true) as $id => $val) {
207-
// $data[$variable . '_' . $id] = $val;
208-
// }
209-
//
210-
// return redirect()->route('admin.services.option', [$service, $option])->withErrors((object) $data)->withInput();
211-
// } catch (DisplayException $ex) {
212-
// Alert::danger($ex->getMessage())->flash();
213-
// } catch (\Exception $ex) {
214-
// Log::error($ex);
215-
// Alert::danger('An error occurred while attempting to update this service.')->flash();
216-
// }
217-
//
218-
// return redirect()->route('admin.services.option', [$service, $option])->withInput();
219-
// }
220-
//
221-
// public function getNewVariable(Request $request, $service, $option)
222-
// {
223-
// return view('admin.services.options.variable', [
224-
// 'option' => Models\ServiceOption::with('service')->findOrFail($option),
225-
// ]);
226-
// }
227-
//
228-
// public function postNewVariable(Request $request, $service, $option)
229-
// {
230-
// try {
231-
// $repo = new ServiceRepository\Variable;
232-
// $repo->create($option, $request->only([
233-
// 'name', 'description', 'env_variable',
234-
// 'default_value', 'user_viewable',
235-
// 'user_editable', 'required', 'regex',
236-
// ]));
237-
// Alert::success('Successfully added new variable to this option.')->flash();
238-
//
239-
// return redirect()->route('admin.services.option', [$service, $option]);
240-
// } catch (DisplayValidationException $ex) {
241-
// return redirect()->route('admin.services.option.variable.new', [$service, $option])->withErrors(json_decode($ex->getMessage()))->withInput();
242-
// } catch (DisplayException $ex) {
243-
// Alert::danger($ex->getMessage())->flash();
244-
// } catch (\Exception $ex) {
245-
// Log::error($ex);
246-
// Alert::danger('An error occurred while attempting to add this variable.')->flash();
247-
// }
248-
//
249-
// return redirect()->route('admin.services.option.variable.new', [$service, $option])->withInput();
250-
// }
251-
//
252-
// public function newOption(Request $request, $service)
253-
// {
254-
// return view('admin.services.options.new', [
255-
// 'service' => Models\Service::findOrFail($service),
256-
// ]);
257-
// }
258-
//
259-
// public function postNewOption(Request $request, $service)
260-
// {
261-
// try {
262-
// $repo = new ServiceRepository\Option;
263-
// $id = $repo->create($service, $request->except([
264-
// '_token',
265-
// ]));
266-
// Alert::success('Successfully created new service option.')->flash();
267-
//
268-
// return redirect()->route('admin.services.option', [$service, $id]);
269-
// } catch (DisplayValidationException $ex) {
270-
// return redirect()->route('admin.services.option.new', $service)->withErrors(json_decode($ex->getMessage()))->withInput();
271-
// } catch (\Exception $ex) {
272-
// Log::error($ex);
273-
// Alert::danger('An error occured while attempting to add this service option.')->flash();
274-
// }
275-
//
276-
// return redirect()->route('admin.services.option.new', $service)->withInput();
277-
// }
278-
//
279-
// public function deleteVariable(Request $request, $service, $option, $variable)
280-
// {
281-
// try {
282-
// $repo = new ServiceRepository\Variable;
283-
// $repo->delete($variable);
284-
// Alert::success('Deleted variable.')->flash();
285-
// } catch (DisplayException $ex) {
286-
// Alert::danger($ex->getMessage())->flash();
287-
// } catch (\Exception $ex) {
288-
// Log::error($ex);
289-
// Alert::danger('An error occured while attempting to delete that variable.')->flash();
290-
// }
291-
//
292-
// return redirect()->route('admin.services.option', [$service, $option]);
293-
// }
294-
//
295-
// public function getConfiguration(Request $request, $serviceId)
296-
// {
297-
// $service = Models\Service::findOrFail($serviceId);
298-
//
299-
// return view('admin.services.config', [
300-
// 'service' => $service,
301-
// 'contents' => [
302-
// 'json' => Storage::get('services/' . $service->file . '/main.json'),
303-
// 'index' => Storage::get('services/' . $service->file . '/index.js'),
304-
// ],
305-
// ]);
306-
// }
307-
//
308-
// public function postConfiguration(Request $request, $serviceId)
309-
// {
310-
// try {
311-
// $repo = new ServiceRepository\Service;
312-
// $repo->updateFile($serviceId, $request->only(['file', 'contents']));
313-
//
314-
// return response('', 204);
315-
// } catch (DisplayException $ex) {
316-
// return response()->json([
317-
// 'error' => $ex->getMessage(),
318-
// ], 503);
319-
// } catch (\Exception $ex) {
320-
// Log::error($ex);
321-
//
322-
// return response()->json([
323-
// 'error' => 'An error occured while attempting to save the file.',
324-
// ], 503);
325-
// }
326-
// }
153+
/**
154+
* Edits function file for a service.
155+
*
156+
* @param Request $request
157+
* @param int $id
158+
* @return \Illuminate\Response\RedirectResponse
159+
*/
160+
public function editFunctions(Request $request, $id)
161+
{
162+
163+
}
327164
}

app/Http/Routes/AdminRoutes.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,11 @@ public function map(Router $router)
403403
'uses' => 'Admin\ServiceController@view',
404404
]);
405405

406-
$router->post('/view/{id}', [
407-
'uses' => 'Admin\ServiceController@edit',
406+
$router->post('/view/{id}', 'Admin\ServiceController@edit');
407+
408+
$router->get('/view/{id}/functions', [
409+
'as' => 'admin.services.view.functions',
410+
'uses' => 'Admin\ServiceController@viewFunctions',
408411
]);
409412

410413
$router->delete('/view/{id}', [

app/Models/Service.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,52 @@ class Service extends Model
4141
* @var array
4242
*/
4343
protected $fillable = [
44-
'name', 'description', 'folder', 'startup',
44+
'name', 'description', 'folder', 'startup', 'index_file',
4545
];
4646

47+
/**
48+
* Returns the default contents of the index.js file for a service.
49+
*
50+
* @return string
51+
*/
52+
public function defaultIndexFile()
53+
{
54+
return <<<EOF
55+
'use strict';
56+
57+
/**
58+
* Pterodactyl - Daemon
59+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>
60+
*
61+
* Permission is hereby granted, free of charge, to any person obtaining a copy
62+
* of this software and associated documentation files (the "Software"), to deal
63+
* in the Software without restriction, including without limitation the rights
64+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
65+
* copies of the Software, and to permit persons to whom the Software is
66+
* furnished to do so, subject to the following conditions:
67+
*
68+
* The above copyright notice and this permission notice shall be included in all
69+
* copies or substantial portions of the Software.
70+
*
71+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
72+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
73+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
74+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
75+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
76+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
77+
* SOFTWARE.
78+
*/
79+
const rfr = require('rfr');
80+
const _ = require('lodash');
81+
82+
const Core = rfr('src/services/index.js');
83+
84+
class Service extends Core {}
85+
86+
module.exports = Service;
87+
EOF;
88+
}
89+
4790
/**
4891
* Gets all service options associated with this service.
4992
*

0 commit comments

Comments
 (0)