1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>. |
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
|
|
|
|
25
|
|
|
namespace Pterodactyl\Http\Controllers\Server; |
26
|
|
|
|
27
|
|
|
use Log; |
28
|
|
|
use Alert; |
29
|
|
|
use Illuminate\Http\Request; |
30
|
|
|
use Pterodactyl\Models\Server; |
31
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
32
|
|
|
use Pterodactyl\Http\Controllers\Controller; |
33
|
|
|
use Pterodactyl\Repositories\TaskRepository; |
34
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
35
|
|
|
|
36
|
|
|
class TaskController extends Controller |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Display task index page. |
40
|
|
|
* |
41
|
|
|
* @param \Illuminate\Http\Request $request |
42
|
|
|
* @param string $uuid |
43
|
|
|
* @return \Illuminate\View\View |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function index(Request $request, $uuid) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$server = Server::byUuid($uuid)->load('tasks'); |
48
|
|
|
$this->authorize('list-tasks', $server); |
49
|
|
|
$server->js(); |
50
|
|
|
|
51
|
|
|
return view('server.tasks.index', [ |
|
|
|
|
52
|
|
|
'server' => $server, |
53
|
|
|
'node' => $server->node, |
54
|
|
|
'tasks' => $server->tasks, |
55
|
|
|
'actions' => [ |
56
|
|
|
'command' => trans('server.tasks.actions.command'), |
57
|
|
|
'power' => trans('server.tasks.actions.power'), |
58
|
|
|
], |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Display new task page. |
64
|
|
|
* |
65
|
|
|
* @param \Illuminate\Http\Request $request |
66
|
|
|
* @param string $uuid |
67
|
|
|
* @return \Illuminate\View\View |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function create(Request $request, $uuid) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$server = Server::byUuid($uuid); |
72
|
|
|
$this->authorize('create-task', $server); |
73
|
|
|
$server->js(); |
74
|
|
|
|
75
|
|
|
return view('server.tasks.new', [ |
|
|
|
|
76
|
|
|
'server' => $server, |
77
|
|
|
'node' => $server->node, |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Handle creation of new task. |
83
|
|
|
* |
84
|
|
|
* @param \Illuminate\Http\Request $request |
85
|
|
|
* @param string $uuid |
86
|
|
|
* @return \Illuminate\Http\RedirectResponse |
87
|
|
|
*/ |
88
|
|
|
public function store(Request $request, $uuid) |
89
|
|
|
{ |
90
|
|
|
$server = Server::byUuid($uuid); |
91
|
|
|
$this->authorize('create-task', $server); |
92
|
|
|
|
93
|
|
|
$repo = new TaskRepository; |
94
|
|
|
try { |
95
|
|
|
$repo->create($server->id, $request->user()->id, $request->except([ |
96
|
|
|
'_token', |
97
|
|
|
])); |
98
|
|
|
|
99
|
|
|
return redirect()->route('server.tasks', $uuid); |
|
|
|
|
100
|
|
|
} catch (DisplayValidationException $ex) { |
101
|
|
|
return redirect()->route('server.tasks.new', $uuid)->withErrors(json_decode($ex->getMessage()))->withInput(); |
|
|
|
|
102
|
|
|
} catch (DisplayException $ex) { |
103
|
|
|
Alert::danger($ex->getMessage())->flash(); |
104
|
|
|
} catch (\Exception $ex) { |
105
|
|
|
Log::error($ex); |
106
|
|
|
Alert::danger('An unknown error occured while attempting to create this task.')->flash(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return redirect()->route('server.tasks.new', $uuid); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Handle deletion of a task. |
114
|
|
|
* |
115
|
|
|
* @param \Illuminate\Http\Request $request |
116
|
|
|
* @param string $uuid |
117
|
|
|
* @param int $id |
118
|
|
|
* @return \Illuminate\Http\JsonResponse |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function delete(Request $request, $uuid, $id) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$server = Server::byUuid($uuid)->load('tasks'); |
123
|
|
|
$this->authorize('delete-task', $server); |
124
|
|
|
|
125
|
|
|
$task = $server->tasks->where('id', $id)->first(); |
126
|
|
|
if (! $task) { |
127
|
|
|
return response()->json([ |
128
|
|
|
'error' => 'No task by that ID was found associated with this server.', |
129
|
|
|
], 404); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$repo = new TaskRepository; |
133
|
|
|
try { |
134
|
|
|
$repo->delete($id); |
135
|
|
|
|
136
|
|
|
return response()->json([], 204); |
137
|
|
|
} catch (\Exception $ex) { |
138
|
|
|
Log::error($ex); |
139
|
|
|
|
140
|
|
|
return response()->json([ |
141
|
|
|
'error' => 'A server error occured while attempting to delete this task.', |
142
|
|
|
], 503); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Toggle the status of a task. |
148
|
|
|
* |
149
|
|
|
* @param \Illuminate\Http\Request $request |
150
|
|
|
* @param string $uuid |
151
|
|
|
* @param int $id |
152
|
|
|
* @return \Illuminate\Http\JsonResponse |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
public function toggle(Request $request, $uuid, $id) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$server = Server::byUuid($uuid)->load('tasks'); |
157
|
|
|
$this->authorize('toggle-task', $server); |
158
|
|
|
|
159
|
|
|
$task = $server->tasks->where('id', $id)->first(); |
160
|
|
|
if (! $task) { |
161
|
|
|
return response()->json([ |
162
|
|
|
'error' => 'No task by that ID was found associated with this server.', |
163
|
|
|
], 404); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$repo = new TaskRepository; |
167
|
|
|
try { |
168
|
|
|
$resp = $repo->toggle($id); |
169
|
|
|
|
170
|
|
|
return response()->json([ |
171
|
|
|
'status' => $resp, |
172
|
|
|
]); |
173
|
|
|
} catch (\Exception $ex) { |
174
|
|
|
Log::error($ex); |
175
|
|
|
|
176
|
|
|
return response()->json([ |
177
|
|
|
'error' => 'A server error occured while attempting to toggle this task.', |
178
|
|
|
], 503); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.