From a9c19463199a2e89dae51d364dc9e3f14b9e9755 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Wed, 7 Feb 2018 21:56:11 -0600 Subject: [PATCH] Add support for finding a user by external ID. --- CHANGELOG.md | 1 + .../Users/ExternalUserController.php | 23 ++++++++ .../Api/Application/ApplicationApiRequest.php | 2 +- .../Users/GetExternalUserRequest.php | 56 +++++++++++++++++++ app/Models/User.php | 11 ++-- resources/lang/en/exceptions.php | 3 + routes/api-application.php | 1 + 7 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 app/Http/Controllers/Api/Application/Users/ExternalUserController.php create mode 100644 app/Http/Requests/Api/Application/Users/GetExternalUserRequest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e16dd62b5..a43948eb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. ### Added * Added ability to search the following API endpoints: list users, list servers, and list locations. +* Add support for finding a user by external ID using `/api/application/users/external/` or by passing it as the search term when listing all users. ## v0.7.0-rc.2 (Derelict Dermodactylus) ### Fixed diff --git a/app/Http/Controllers/Api/Application/Users/ExternalUserController.php b/app/Http/Controllers/Api/Application/Users/ExternalUserController.php new file mode 100644 index 000000000..f58138173 --- /dev/null +++ b/app/Http/Controllers/Api/Application/Users/ExternalUserController.php @@ -0,0 +1,23 @@ +fractal->item($request->getUserModel()) + ->transformWith($this->getTransformer(UserTransformer::class)) + ->toArray(); + } +} diff --git a/app/Http/Requests/Api/Application/ApplicationApiRequest.php b/app/Http/Requests/Api/Application/ApplicationApiRequest.php index aeb03dfbe..126e6d604 100644 --- a/app/Http/Requests/Api/Application/ApplicationApiRequest.php +++ b/app/Http/Requests/Api/Application/ApplicationApiRequest.php @@ -117,7 +117,7 @@ abstract class ApplicationApiRequest extends FormRequest // an item exists (or does not exist) to the user until they can prove // that they have permission to know about it. if ($this->attributes->get('is_missing_model', false) || ! $this->resourceExists()) { - throw new NotFoundHttpException('The requested resource does not exist on this server.'); + throw new NotFoundHttpException(trans('exceptions.api.resource_not_found')); } return true; diff --git a/app/Http/Requests/Api/Application/Users/GetExternalUserRequest.php b/app/Http/Requests/Api/Application/Users/GetExternalUserRequest.php new file mode 100644 index 000000000..b1f779183 --- /dev/null +++ b/app/Http/Requests/Api/Application/Users/GetExternalUserRequest.php @@ -0,0 +1,56 @@ +container->make(UserRepositoryInterface::class); + + try { + $this->userModel = $repository->findFirstWhere([ + ['external_id', '=', $this->route()->parameter('external_id')], + ]); + } catch (RecordNotFoundException $exception) { + return false; + } + + return true; + } + + /** + * Return the user model for the requested external user. + * @return \Pterodactyl\Models\User + */ + public function getUserModel(): User + { + return $this->userModel; + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 29754eff8..26109cc0b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -105,11 +105,12 @@ class User extends Model implements * @var array */ protected $searchableColumns = [ - 'email' => 10, - 'username' => 9, - 'name_first' => 6, - 'name_last' => 6, - 'uuid' => 1, + 'username' => 100, + 'email' => 100, + 'external_id' => 80, + 'uuid' => 80, + 'name_first' => 40, + 'name_last' => 40, ]; /** diff --git a/resources/lang/en/exceptions.php b/resources/lang/en/exceptions.php index 712ad92d7..73b910d0d 100644 --- a/resources/lang/en/exceptions.php +++ b/resources/lang/en/exceptions.php @@ -60,4 +60,7 @@ return [ 'no_viable_nodes' => 'No nodes satisfying the requirements specified for automatic deployment could be found.', 'no_viable_allocations' => 'No allocations satisfying the requirements for automatic deployment were found.', ], + 'api' => [ + 'resource_not_found' => 'The requested resource does not exist on this server.', + ], ]; diff --git a/routes/api-application.php b/routes/api-application.php index 212b75c2b..900de8c6a 100644 --- a/routes/api-application.php +++ b/routes/api-application.php @@ -11,6 +11,7 @@ Route::group(['prefix' => '/users'], function () { Route::get('/', 'Users\UserController@index')->name('api.application.users'); Route::get('/{user}', 'Users\UserController@view')->name('api.application.users.view'); + Route::get('/external/{external_id}', 'Users\ExternalUserController@index')->name('api.application.users.external'); Route::post('/', 'Users\UserController@store'); Route::patch('/{user}', 'Users\UserController@update');