Skip to content

Commit a9c1946

Browse files
committed
Add support for finding a user by external ID.
1 parent 2e69306 commit a9c1946

File tree

7 files changed

+91
-6
lines changed

7 files changed

+91
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
1010

1111
### Added
1212
* Added ability to search the following API endpoints: list users, list servers, and list locations.
13+
* 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.
1314

1415
## v0.7.0-rc.2 (Derelict Dermodactylus)
1516
### Fixed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Api\Application\Users;
4+
5+
use Pterodactyl\Transformers\Api\Application\UserTransformer;
6+
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
7+
use Pterodactyl\Http\Requests\Api\Application\Users\GetExternalUserRequest;
8+
9+
class ExternalUserController extends ApplicationApiController
10+
{
11+
/**
12+
* Retrieve a specific user from the database using their external ID.
13+
*
14+
* @param \Pterodactyl\Http\Requests\Api\Application\Users\GetExternalUserRequest $request
15+
* @return array
16+
*/
17+
public function index(GetExternalUserRequest $request): array
18+
{
19+
return $this->fractal->item($request->getUserModel())
20+
->transformWith($this->getTransformer(UserTransformer::class))
21+
->toArray();
22+
}
23+
}

app/Http/Requests/Api/Application/ApplicationApiRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ protected function passesAuthorization()
117117
// an item exists (or does not exist) to the user until they can prove
118118
// that they have permission to know about it.
119119
if ($this->attributes->get('is_missing_model', false) || ! $this->resourceExists()) {
120-
throw new NotFoundHttpException('The requested resource does not exist on this server.');
120+
throw new NotFoundHttpException(trans('exceptions.api.resource_not_found'));
121121
}
122122

123123
return true;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Application\Users;
4+
5+
use Pterodactyl\Models\User;
6+
use Pterodactyl\Services\Acl\Api\AdminAcl;
7+
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
8+
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
9+
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
10+
11+
class GetExternalUserRequest extends ApplicationApiRequest
12+
{
13+
/**
14+
* @var User
15+
*/
16+
private $userModel;
17+
18+
/**
19+
* @var string
20+
*/
21+
protected $resource = AdminAcl::RESOURCE_USERS;
22+
23+
/**
24+
* @var int
25+
*/
26+
protected $permission = AdminAcl::READ;
27+
28+
/**
29+
* Determine if the requested external user exists.
30+
*
31+
* @return bool
32+
*/
33+
public function resourceExists(): bool
34+
{
35+
$repository = $this->container->make(UserRepositoryInterface::class);
36+
37+
try {
38+
$this->userModel = $repository->findFirstWhere([
39+
['external_id', '=', $this->route()->parameter('external_id')],
40+
]);
41+
} catch (RecordNotFoundException $exception) {
42+
return false;
43+
}
44+
45+
return true;
46+
}
47+
48+
/**
49+
* Return the user model for the requested external user.
50+
* @return \Pterodactyl\Models\User
51+
*/
52+
public function getUserModel(): User
53+
{
54+
return $this->userModel;
55+
}
56+
}

app/Models/User.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,12 @@ class User extends Model implements
105105
* @var array
106106
*/
107107
protected $searchableColumns = [
108-
'email' => 10,
109-
'username' => 9,
110-
'name_first' => 6,
111-
'name_last' => 6,
112-
'uuid' => 1,
108+
'username' => 100,
109+
'email' => 100,
110+
'external_id' => 80,
111+
'uuid' => 80,
112+
'name_first' => 40,
113+
'name_last' => 40,
113114
];
114115

115116
/**

resources/lang/en/exceptions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,7 @@
6060
'no_viable_nodes' => 'No nodes satisfying the requirements specified for automatic deployment could be found.',
6161
'no_viable_allocations' => 'No allocations satisfying the requirements for automatic deployment were found.',
6262
],
63+
'api' => [
64+
'resource_not_found' => 'The requested resource does not exist on this server.',
65+
],
6366
];

routes/api-application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Route::group(['prefix' => '/users'], function () {
1212
Route::get('/', 'Users\UserController@index')->name('api.application.users');
1313
Route::get('/{user}', 'Users\UserController@view')->name('api.application.users.view');
14+
Route::get('/external/{external_id}', 'Users\ExternalUserController@index')->name('api.application.users.external');
1415

1516
Route::post('/', 'Users\UserController@store');
1617
Route::patch('/{user}', 'Users\UserController@update');

0 commit comments

Comments
 (0)