|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2017 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 | + |
| 25 | +namespace Pterodactyl\Http\Controllers\API\Admin; |
| 26 | + |
| 27 | +use Fractal; |
| 28 | +use Illuminate\Http\Request; |
| 29 | +use Pterodactyl\Models\User; |
| 30 | +use Pterodactyl\Http\Controllers\Controller; |
| 31 | +use Pterodactyl\Exceptions\DisplayException; |
| 32 | +use Pterodactyl\Repositories\UserRepository; |
| 33 | +use Pterodactyl\Transformers\Admin\UserTransformer; |
| 34 | +use Pterodactyl\Exceptions\DisplayValidationException; |
| 35 | + |
| 36 | +class UserController extends Controller |
| 37 | +{ |
| 38 | + /** |
| 39 | + * Controller to handle returning all users on the system. |
| 40 | + * |
| 41 | + * @param \Illuminate\Http\Request $request |
| 42 | + * @return array |
| 43 | + */ |
| 44 | + public function index(Request $request) |
| 45 | + { |
| 46 | + $this->authorize('user-list', $request->apiKey()); |
| 47 | + |
| 48 | + $fractal = Fractal::create()->collection(User::all()); |
| 49 | + if ($request->input('include')) { |
| 50 | + $fractal->parseIncludes(explode(',', $request->input('include'))); |
| 51 | + } |
| 52 | + |
| 53 | + return $fractal->transformWith(new UserTransformer($request)) |
| 54 | + ->withResourceName('user') |
| 55 | + ->toArray(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Display information about a single user on the system. |
| 60 | + * |
| 61 | + * @param \Illuminate\Http\Request $request |
| 62 | + * @param int $id |
| 63 | + * @return array |
| 64 | + */ |
| 65 | + public function view(Request $request, $id) |
| 66 | + { |
| 67 | + $this->authorize('user-view', $request->apiKey()); |
| 68 | + |
| 69 | + $fractal = Fractal::create()->item(User::findOrFail($id)); |
| 70 | + if ($request->input('include')) { |
| 71 | + $fractal->parseIncludes(explode(',', $request->input('include'))); |
| 72 | + } |
| 73 | + |
| 74 | + return $fractal->transformWith(new UserTransformer($request)) |
| 75 | + ->withResourceName('user') |
| 76 | + ->toArray(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Create a new user on the system. |
| 81 | + * |
| 82 | + * @param \Illuminate\Http\Request $request |
| 83 | + * @return \Illuminate\Http\JsonResponse|array |
| 84 | + */ |
| 85 | + public function store(Request $request) |
| 86 | + { |
| 87 | + $this->authorize('user-create', $request->apiKey()); |
| 88 | + |
| 89 | + $repo = new UserRepository; |
| 90 | + try { |
| 91 | + $user = $repo->create($request->only([ |
| 92 | + 'custom_id', 'email', 'password', 'name_first', |
| 93 | + 'name_last', 'username', 'root_admin', |
| 94 | + ])); |
| 95 | + |
| 96 | + $fractal = Fractal::create()->item($user)->transformWith(new UserTransformer($request)); |
| 97 | + if ($request->input('include')) { |
| 98 | + $fractal->parseIncludes(explode(',', $request->input('include'))); |
| 99 | + } |
| 100 | + |
| 101 | + return $fractal->withResourceName('user')->toArray(); |
| 102 | + } catch (DisplayValidationException $ex) { |
| 103 | + return response()->json([ |
| 104 | + 'error' => json_decode($ex->getMessage()), |
| 105 | + ], 400); |
| 106 | + } catch (\Exception $ex) { |
| 107 | + Log::error($ex); |
| 108 | + return response()->json([ |
| 109 | + 'error' => 'An unhandled exception occured while attemping to create this user. Please try again.', |
| 110 | + ], 500); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Update a user. |
| 116 | + * |
| 117 | + * @param \Illuminate\Http\Request $request |
| 118 | + * @param int $user |
| 119 | + * @return \Illuminate\Http\RedirectResponse |
| 120 | + */ |
| 121 | + public function update(Request $request, $user) |
| 122 | + { |
| 123 | + $this->authorize('user-edit', $request->apiKey()); |
| 124 | + |
| 125 | + $repo = new UserRepository; |
| 126 | + try { |
| 127 | + $user = $repo->update($user, $request->intersect([ |
| 128 | + 'email', 'password', 'name_first', |
| 129 | + 'name_last', 'username', 'root_admin', |
| 130 | + ])); |
| 131 | + |
| 132 | + $fractal = Fractal::create()->item($user)->transformWith(new UserTransformer($request)); |
| 133 | + if ($request->input('include')) { |
| 134 | + $fractal->parseIncludes(explode(',', $request->input('include'))); |
| 135 | + } |
| 136 | + |
| 137 | + return $fractal->withResourceName('user')->toArray(); |
| 138 | + } catch (DisplayValidationException $ex) { |
| 139 | + return response()->json([ |
| 140 | + 'error' => json_decode($ex->getMessage()), |
| 141 | + ], 400); |
| 142 | + } catch (\Exception $ex) { |
| 143 | + Log::error($ex); |
| 144 | + return response()->json([ |
| 145 | + 'error' => 'An unhandled exception occured while attemping to update this user. Please try again.', |
| 146 | + ], 500); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Delete a user from the system. |
| 152 | + * |
| 153 | + * @param \Illuminate\Http\Request $request |
| 154 | + * @param int $id |
| 155 | + * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse |
| 156 | + */ |
| 157 | + public function delete(Request $request, $id) |
| 158 | + { |
| 159 | + $this->authorize('user-delete', $request->apiKey()); |
| 160 | + |
| 161 | + $repo = new UserRepository; |
| 162 | + try { |
| 163 | + $repo->delete($id); |
| 164 | + |
| 165 | + return response('', 204); |
| 166 | + } catch (DisplayException $ex) { |
| 167 | + return response()->json([ |
| 168 | + 'error' => $ex->getMessage(), |
| 169 | + ], 400); |
| 170 | + } catch (\Exception $ex) { |
| 171 | + Log::error($ex); |
| 172 | + return response()->json([ |
| 173 | + 'error' => 'An unhandled exception occured while attemping to delete this user. Please try again.', |
| 174 | + ], 500); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments