1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2016 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\API; |
26
|
|
|
|
27
|
|
|
use Pterodactyl\Models; |
28
|
|
|
use Illuminate\Http\Request; |
29
|
|
|
use Dingo\Api\Exception\ResourceException; |
30
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
31
|
|
|
use Pterodactyl\Repositories\UserRepository; |
32
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
33
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
34
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
35
|
|
|
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @Resource("Users") |
39
|
|
|
*/ |
40
|
|
|
class UserController extends BaseController |
41
|
|
|
{ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* List All Users. |
48
|
|
|
* |
49
|
|
|
* Lists all users currently on the system. |
50
|
|
|
* |
51
|
|
|
* @Get("/users/{?page}") |
52
|
|
|
* @Versions({"v1"}) |
53
|
|
|
* @Parameters({ |
54
|
|
|
* @Parameter("page", type="integer", description="The page of results to view.", default=1) |
55
|
|
|
* }) |
56
|
|
|
* @Response(200) |
57
|
|
|
*/ |
58
|
|
|
public function lists(Request $request) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
return Models\User::all()->toArray(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* List Specific User. |
65
|
|
|
* |
66
|
|
|
* Lists specific fields about a user or all fields pertaining to that user. |
67
|
|
|
* |
68
|
|
|
* @Get("/users/{id}/{fields}") |
69
|
|
|
* @Versions({"v1"}) |
70
|
|
|
* @Parameters({ |
71
|
|
|
* @Parameter("id", type="integer", required=true, description="The ID of the user to get information on."), |
72
|
|
|
* @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.") |
73
|
|
|
* }) |
74
|
|
|
* @Response(200) |
75
|
|
|
*/ |
76
|
|
|
public function view(Request $request, $id) |
77
|
|
|
{ |
78
|
|
|
$query = Models\User::where('id', $id); |
79
|
|
|
|
80
|
|
View Code Duplication |
if (! is_null($request->input('fields'))) { |
|
|
|
|
81
|
|
|
foreach (explode(',', $request->input('fields')) as $field) { |
82
|
|
|
if (! empty($field)) { |
83
|
|
|
$query->addSelect($field); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
try { |
89
|
|
|
if (! $query->first()) { |
90
|
|
|
throw new NotFoundHttpException('No user by that ID was found.'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$user = $query->first(); |
94
|
|
|
$userArray = $user->toArray(); |
95
|
|
|
$userArray['servers'] = Models\Server::select('id', 'uuid', 'node', 'suspended')->where('owner', $user->id)->get(); |
96
|
|
|
|
97
|
|
|
return $userArray; |
98
|
|
|
} catch (NotFoundHttpException $ex) { |
99
|
|
|
throw $ex; |
100
|
|
|
} catch (\Exception $ex) { |
101
|
|
|
throw new BadRequestHttpException('There was an issue with the fields passed in the request.'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Create a New User. |
107
|
|
|
* |
108
|
|
|
* @Post("/users") |
109
|
|
|
* @Versions({"v1"}) |
110
|
|
|
* @Transaction({ |
111
|
|
|
* @Request({ |
112
|
|
|
* "email": "[email protected]", |
113
|
|
|
* "password": "foopassword", |
114
|
|
|
* "admin": false, |
115
|
|
|
* "custom_id": 123 |
116
|
|
|
* }, headers={"Authorization": "Bearer <token>"}), |
117
|
|
|
* @Response(201), |
118
|
|
|
* @Response(422) |
119
|
|
|
* }) |
120
|
|
|
*/ |
121
|
|
|
public function create(Request $request) |
122
|
|
|
{ |
123
|
|
|
try { |
124
|
|
|
$user = new UserRepository; |
125
|
|
|
$create = $user->create($request->input('email'), $request->input('password'), $request->input('admin'), $request->input('custom_id')); |
|
|
|
|
126
|
|
|
|
127
|
|
|
return ['id' => $create]; |
128
|
|
|
} catch (DisplayValidationException $ex) { |
129
|
|
|
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true)); |
130
|
|
|
} catch (DisplayException $ex) { |
131
|
|
|
throw new ResourceException($ex->getMessage()); |
132
|
|
|
} catch (\Exception $ex) { |
133
|
|
|
throw new ServiceUnavailableHttpException('Unable to create a user on the system due to an error.'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Update an Existing User. |
139
|
|
|
* |
140
|
|
|
* The data sent in the request will be used to update the existing user on the system. |
141
|
|
|
* |
142
|
|
|
* @Patch("/users/{id}") |
143
|
|
|
* @Versions({"v1"}) |
144
|
|
|
* @Transaction({ |
145
|
|
|
* @Request({ |
146
|
|
|
* "email": "[email protected]" |
147
|
|
|
* }, headers={"Authorization": "Bearer <token>"}), |
148
|
|
|
* @Response(200, body={"email": "[email protected]"}), |
149
|
|
|
* @Response(422) |
150
|
|
|
* }) |
151
|
|
|
* @Parameters({ |
152
|
|
|
* @Parameter("id", type="integer", required=true, description="The ID of the user to modify.") |
153
|
|
|
* }) |
154
|
|
|
*/ |
155
|
|
View Code Duplication |
public function update(Request $request, $id) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
try { |
158
|
|
|
$user = new UserRepository; |
159
|
|
|
$user->update($id, $request->all()); |
160
|
|
|
|
161
|
|
|
return Models\User::findOrFail($id); |
162
|
|
|
} catch (DisplayValidationException $ex) { |
163
|
|
|
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true)); |
164
|
|
|
} catch (DisplayException $ex) { |
165
|
|
|
throw new ResourceException($ex->getMessage()); |
166
|
|
|
} catch (\Exception $ex) { |
167
|
|
|
throw new ServiceUnavailableHttpException('Unable to update a user on the system due to an error.'); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Delete a User. |
173
|
|
|
* |
174
|
|
|
* @Delete("/users/{id}") |
175
|
|
|
* @Versions({"v1"}) |
176
|
|
|
* @Transaction({ |
177
|
|
|
* @Request(headers={"Authorization": "Bearer <token>"}), |
178
|
|
|
* @Response(204), |
179
|
|
|
* @Response(422) |
180
|
|
|
* }) |
181
|
|
|
* @Parameters({ |
182
|
|
|
* @Parameter("id", type="integer", required=true, description="The ID of the user to delete.") |
183
|
|
|
* }) |
184
|
|
|
*/ |
185
|
|
View Code Duplication |
public function delete(Request $request, $id) |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
try { |
188
|
|
|
$user = new UserRepository; |
189
|
|
|
$user->delete($id); |
190
|
|
|
|
191
|
|
|
return $this->response->noContent(); |
192
|
|
|
} catch (DisplayException $ex) { |
193
|
|
|
throw new ResourceException($ex->getMessage()); |
194
|
|
|
} catch (\Exception $ex) { |
195
|
|
|
throw new ServiceUnavailableHttpException('Unable to delete this user due to an error.'); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.