Add support for finding a user by external ID.

This commit is contained in:
Dane Everitt 2018-02-07 21:56:11 -06:00
parent 2e693067b8
commit a9c1946319
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
7 changed files with 91 additions and 6 deletions

View file

@ -10,6 +10,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
### Added ### Added
* Added ability to search the following API endpoints: list users, list servers, and list locations. * 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/<id>` or by passing it as the search term when listing all users.
## v0.7.0-rc.2 (Derelict Dermodactylus) ## v0.7.0-rc.2 (Derelict Dermodactylus)
### Fixed ### Fixed

View file

@ -0,0 +1,23 @@
<?php
namespace Pterodactyl\Http\Controllers\Api\Application\Users;
use Pterodactyl\Transformers\Api\Application\UserTransformer;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
use Pterodactyl\Http\Requests\Api\Application\Users\GetExternalUserRequest;
class ExternalUserController extends ApplicationApiController
{
/**
* Retrieve a specific user from the database using their external ID.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Users\GetExternalUserRequest $request
* @return array
*/
public function index(GetExternalUserRequest $request): array
{
return $this->fractal->item($request->getUserModel())
->transformWith($this->getTransformer(UserTransformer::class))
->toArray();
}
}

View file

@ -117,7 +117,7 @@ abstract class ApplicationApiRequest extends FormRequest
// an item exists (or does not exist) to the user until they can prove // an item exists (or does not exist) to the user until they can prove
// that they have permission to know about it. // that they have permission to know about it.
if ($this->attributes->get('is_missing_model', false) || ! $this->resourceExists()) { 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; return true;

View file

@ -0,0 +1,56 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Application\Users;
use Pterodactyl\Models\User;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
class GetExternalUserRequest extends ApplicationApiRequest
{
/**
* @var User
*/
private $userModel;
/**
* @var string
*/
protected $resource = AdminAcl::RESOURCE_USERS;
/**
* @var int
*/
protected $permission = AdminAcl::READ;
/**
* Determine if the requested external user exists.
*
* @return bool
*/
public function resourceExists(): bool
{
$repository = $this->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;
}
}

View file

@ -105,11 +105,12 @@ class User extends Model implements
* @var array * @var array
*/ */
protected $searchableColumns = [ protected $searchableColumns = [
'email' => 10, 'username' => 100,
'username' => 9, 'email' => 100,
'name_first' => 6, 'external_id' => 80,
'name_last' => 6, 'uuid' => 80,
'uuid' => 1, 'name_first' => 40,
'name_last' => 40,
]; ];
/** /**

View file

@ -60,4 +60,7 @@ return [
'no_viable_nodes' => 'No nodes satisfying the requirements specified for automatic deployment could be found.', '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.', '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.',
],
]; ];

View file

@ -11,6 +11,7 @@
Route::group(['prefix' => '/users'], function () { Route::group(['prefix' => '/users'], function () {
Route::get('/', 'Users\UserController@index')->name('api.application.users'); Route::get('/', 'Users\UserController@index')->name('api.application.users');
Route::get('/{user}', 'Users\UserController@view')->name('api.application.users.view'); 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::post('/', 'Users\UserController@store');
Route::patch('/{user}', 'Users\UserController@update'); Route::patch('/{user}', 'Users\UserController@update');