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 Auth; |
29
|
|
|
use Alert; |
30
|
|
|
use Pterodactyl\Models; |
31
|
|
|
use Illuminate\Http\Request; |
32
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
33
|
|
|
use Pterodactyl\Http\Controllers\Controller; |
34
|
|
|
use Pterodactyl\Repositories\SubuserRepository; |
35
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
36
|
|
|
|
37
|
|
|
class SubuserController extends Controller |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* Displays the subuser overview index. |
41
|
|
|
* |
42
|
|
|
* @param \Illuminate\Http\Request $request |
43
|
|
|
* @param string $uuid |
44
|
|
|
* @return \Illuminate\View\View |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function index(Request $request, $uuid) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$server = Models\Server::byUuid($uuid)->load('subusers.user'); |
49
|
|
|
$this->authorize('list-subusers', $server); |
50
|
|
|
|
51
|
|
|
$server->js(); |
52
|
|
|
|
53
|
|
|
return view('server.users.index', [ |
|
|
|
|
54
|
|
|
'server' => $server, |
55
|
|
|
'node' => $server->node, |
56
|
|
|
'subusers' => $server->subusers, |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Displays the a single subuser overview. |
62
|
|
|
* |
63
|
|
|
* @param \Illuminate\Http\Request $request |
64
|
|
|
* @param string $uuid |
65
|
|
|
* @param int $id |
66
|
|
|
* @return \Illuminate\View\View |
67
|
|
|
*/ |
68
|
|
|
public function view(Request $request, $uuid, $id) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$server = Models\Server::byUuid($uuid)->load('node'); |
71
|
|
|
$this->authorize('view-subuser', $server); |
72
|
|
|
|
73
|
|
|
$subuser = Models\Subuser::with('permissions', 'user') |
|
|
|
|
74
|
|
|
->where('server_id', $server->id)->findOrFail($id); |
75
|
|
|
|
76
|
|
|
$server->js(); |
77
|
|
|
|
78
|
|
|
return view('server.users.view', [ |
|
|
|
|
79
|
|
|
'server' => $server, |
80
|
|
|
'node' => $server->node, |
81
|
|
|
'subuser' => $subuser, |
82
|
|
|
'permlist' => Models\Permission::list(), |
83
|
|
|
'permissions' => $subuser->permissions->mapWithKeys(function ($item, $key) { |
|
|
|
|
84
|
|
|
return [$item->permission => true]; |
85
|
|
|
}), |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Handles editing a subuser. |
91
|
|
|
* |
92
|
|
|
* @param \Illuminate\Http\Request $request |
93
|
|
|
* @param string $uuid |
94
|
|
|
* @param int $id |
95
|
|
|
* @return \Illuminate\Http\RedirectResponse |
96
|
|
|
*/ |
97
|
|
|
public function update(Request $request, $uuid, $id) |
98
|
|
|
{ |
99
|
|
|
$server = Models\Server::byUuid($uuid); |
100
|
|
|
$this->authorize('edit-subuser', $server); |
101
|
|
|
|
102
|
|
|
$subuser = Models\Subuser::where('server_id', $server->id)->findOrFail($id); |
103
|
|
|
|
104
|
|
|
try { |
105
|
|
|
if ($subuser->user_id === Auth::user()->id) { |
106
|
|
|
throw new DisplayException('You are not authorized to edit you own account.'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$repo = new SubuserRepository; |
110
|
|
|
$repo->update($subuser->id, [ |
111
|
|
|
'permissions' => $request->input('permissions'), |
112
|
|
|
'server' => $server->id, |
113
|
|
|
'user' => $subuser->user_id, |
114
|
|
|
]); |
115
|
|
|
|
116
|
|
|
Alert::success('Subuser permissions have successfully been updated.')->flash(); |
117
|
|
|
} catch (DisplayValidationException $ex) { |
118
|
|
|
return redirect()->route('server.subusers.view', [ |
119
|
|
|
'uuid' => $uuid, |
120
|
|
|
'id' => $id, |
121
|
|
|
])->withErrors(json_decode($ex->getMessage())); |
122
|
|
|
} catch (DisplayException $ex) { |
123
|
|
|
Alert::danger($ex->getMessage())->flash(); |
124
|
|
|
} catch (\Exception $ex) { |
125
|
|
|
Log::error($ex); |
126
|
|
|
Alert::danger('An unknown error occured while attempting to update this subuser.')->flash(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return redirect()->route('server.subusers.view', [ |
130
|
|
|
'uuid' => $uuid, |
131
|
|
|
'id' => $id, |
132
|
|
|
]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Display new subuser creation page. |
137
|
|
|
* |
138
|
|
|
* @param \Illuminate\Http\Request $request |
139
|
|
|
* @param string $uuid |
140
|
|
|
* @return \Illuminate\View\View |
141
|
|
|
*/ |
142
|
|
View Code Duplication |
public function create(Request $request, $uuid) |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$server = Models\Server::byUuid($uuid); |
145
|
|
|
$this->authorize('create-subuser', $server); |
146
|
|
|
$server->js(); |
147
|
|
|
|
148
|
|
|
return view('server.users.new', [ |
|
|
|
|
149
|
|
|
'server' => $server, |
150
|
|
|
'permissions' => Models\Permission::list(), |
151
|
|
|
'node' => $server->node, |
152
|
|
|
]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Handles creating a new subuser. |
157
|
|
|
* |
158
|
|
|
* @param \Illuminate\Http\Request $request |
159
|
|
|
* @param string $uuid |
160
|
|
|
* @return \Illuminate\Http\RedirectResponse |
161
|
|
|
*/ |
162
|
|
|
public function store(Request $request, $uuid) |
163
|
|
|
{ |
164
|
|
|
$server = Models\Server::byUuid($uuid); |
165
|
|
|
$this->authorize('create-subuser', $server); |
166
|
|
|
|
167
|
|
|
try { |
168
|
|
|
$repo = new SubuserRepository; |
169
|
|
|
$subuser = $repo->create($server->id, $request->only([ |
170
|
|
|
'permissions', 'email', |
171
|
|
|
])); |
172
|
|
|
Alert::success('Successfully created new subuser.')->flash(); |
173
|
|
|
|
174
|
|
|
return redirect()->route('server.subusers.view', [ |
175
|
|
|
'uuid' => $uuid, |
176
|
|
|
'id' => $subuser->id, |
177
|
|
|
]); |
178
|
|
|
} catch (DisplayValidationException $ex) { |
179
|
|
|
return redirect()->route('server.subusers.new', $uuid)->withErrors(json_decode($ex->getMessage()))->withInput(); |
|
|
|
|
180
|
|
|
} catch (DisplayException $ex) { |
181
|
|
|
Alert::danger($ex->getMessage())->flash(); |
182
|
|
|
} catch (\Exception $ex) { |
183
|
|
|
Log::error($ex); |
184
|
|
|
Alert::danger('An unknown error occured while attempting to add a new subuser.')->flash(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return redirect()->route('server.subusers.new', $uuid)->withInput(); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Handles deleting a subuser. |
192
|
|
|
* |
193
|
|
|
* @param \Illuminate\Http\Request $request |
194
|
|
|
* @param string $uuid |
195
|
|
|
* @param int $id |
196
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response |
197
|
|
|
*/ |
198
|
|
|
public function delete(Request $request, $uuid, $id) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
$server = Models\Server::byUuid($uuid); |
201
|
|
|
$this->authorize('delete-subuser', $server); |
202
|
|
|
|
203
|
|
|
try { |
204
|
|
|
$subuser = Models\Subuser::where('server_id', $server->id)->findOrFail($id); |
205
|
|
|
|
206
|
|
|
$repo = new SubuserRepository; |
207
|
|
|
$repo->delete($subuser->id); |
208
|
|
|
|
209
|
|
|
return response('', 204); |
210
|
|
|
} catch (DisplayException $ex) { |
211
|
|
|
response()->json([ |
212
|
|
|
'error' => $ex->getMessage(), |
213
|
|
|
], 422); |
214
|
|
|
} catch (\Exception $ex) { |
215
|
|
|
Log::error($ex); |
216
|
|
|
response()->json([ |
217
|
|
|
'error' => 'An unknown error occured while attempting to delete this subuser.', |
218
|
|
|
], 503); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.