From 69c2e89fe0b6810181245ffc4e807557937b01b6 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 15 Jan 2016 00:08:50 -0500 Subject: [PATCH] Fix some missing exceptions and validation handling for users --- app/Http/Controllers/API/UserController.php | 15 ++++++++++++++- app/Repositories/UserRepository.php | 8 +++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/API/UserController.php b/app/Http/Controllers/API/UserController.php index 0ab62f2c8..edae69474 100644 --- a/app/Http/Controllers/API/UserController.php +++ b/app/Http/Controllers/API/UserController.php @@ -9,8 +9,11 @@ use Dingo\Api\Exception\StoreResourceFailedException; use Pterodactyl\Models; use Pterodactyl\Transformers\UserTransformer; use Pterodactyl\Repositories\UserRepository; + use Pterodactyl\Exceptions\DisplayValidationException; use Pterodactyl\Exceptions\DisplayException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; /** * @Resource("Users") @@ -61,7 +64,17 @@ class UserController extends BaseController } } - return $query->first(); + try { + if (!$query->first()) { + throw new NotFoundHttpException('No user by that ID was found.'); + } + return $query->first(); + } catch (NotFoundHttpException $ex) { + throw $ex; + } catch (\Exception $ex) { + throw new BadRequestHttpException('There was an issue with the fields passed in the request.'); + } + } /** diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php index cc0e771b7..7f5b8ade1 100644 --- a/app/Repositories/UserRepository.php +++ b/app/Repositories/UserRepository.php @@ -80,11 +80,17 @@ class UserRepository 'totp_secret' => 'size:16' ]); + // Run validator, throw catchable and displayable exception if it fails. + // Exception includes a JSON result of failed validation rules. + if ($validator->fails()) { + throw new DisplayValidationException($validator->errors()); + } + if(array_key_exists('password', $data)) { $user['password'] = Hash::make($data['password']); } - return Models\User::find($id)->update($data); + return Models\User::findOrFail($id)->update($data); } /**