Skip to content

Commit 2ec76d2

Browse files
committed
Fix bad API behavior
1 parent d4d9eda commit 2ec76d2

File tree

10 files changed

+86
-68
lines changed

10 files changed

+86
-68
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ This file is a running track of new features and fixes to each version of the pa
33

44
This project follows [Semantic Versioning](http://semver.org) guidelines.
55

6+
## v0.7.0-rc.3 (Derelict Dermodactylus)
7+
### Fixed
8+
* `[rc.2]` — Fixes bad API behavior on `/user` routes.
9+
610
## v0.7.0-rc.2 (Derelict Dermodactylus)
711
### Fixed
812
* `[rc.1]` — Fixes exception thrown when revoking user sessions.

app/Http/Controllers/Api/Application/Users/UserController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Pterodactyl\Services\Users\UserDeletionService;
1111
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
1212
use Pterodactyl\Transformers\Api\Application\UserTransformer;
13-
use Pterodactyl\Http\Requests\Api\Application\Users\GetUserRequest;
1413
use Pterodactyl\Http\Requests\Api\Application\Users\GetUsersRequest;
1514
use Pterodactyl\Http\Requests\Api\Application\Users\StoreUserRequest;
1615
use Pterodactyl\Http\Requests\Api\Application\Users\DeleteUserRequest;
@@ -82,10 +81,10 @@ public function index(GetUsersRequest $request): array
8281
* Handle a request to view a single user. Includes any relations that
8382
* were defined in the request.
8483
*
85-
* @param \Pterodactyl\Http\Requests\Api\Application\Users\GetUserRequest $request
84+
* @param \Pterodactyl\Http\Requests\Api\Application\Users\GetUsersRequest $request
8685
* @return array
8786
*/
88-
public function view(GetUserRequest $request): array
87+
public function view(GetUsersRequest $request): array
8988
{
9089
return $this->fractal->item($request->getModel(User::class))
9190
->transformWith($this->getTransformer(UserTransformer::class))

app/Http/Middleware/Api/ApiSubstituteBindings.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Pterodactyl\Models\Egg;
77
use Pterodactyl\Models\Nest;
88
use Pterodactyl\Models\Node;
9+
use Pterodactyl\Models\User;
910
use Pterodactyl\Models\Server;
1011
use Pterodactyl\Models\Database;
1112
use Pterodactyl\Models\Location;
@@ -28,6 +29,7 @@ class ApiSubstituteBindings extends SubstituteBindings
2829
'nest' => Nest::class,
2930
'node' => Node::class,
3031
'server' => Server::class,
32+
'user' => User::class,
3133
];
3234

3335
/**

app/Http/Requests/Api/Application/Nodes/UpdateNodeRequest.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,6 @@
66

77
class UpdateNodeRequest extends StoreNodeRequest
88
{
9-
/**
10-
* Determine if the node being requested for editing exists
11-
* on the Panel before validating the data.
12-
*
13-
* @return bool
14-
*/
15-
public function resourceExists(): bool
16-
{
17-
$node = $this->route()->parameter('node');
18-
19-
return $node instanceof Node && $node->exists;
20-
}
21-
229
/**
2310
* Apply validation rules to this request. Uses the parent class rules()
2411
* function but passes in the rules for updating rather than creating.
@@ -28,7 +15,7 @@ public function resourceExists(): bool
2815
*/
2916
public function rules(array $rules = null): array
3017
{
31-
$nodeId = $this->route()->parameter('node')->id;
18+
$nodeId = $this->getModel(Node::class)->id;
3219

3320
return parent::rules(Node::getUpdateRulesForId($nodeId));
3421
}

app/Http/Requests/Api/Application/Users/GetUserRequest.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

app/Http/Requests/Api/Application/Users/StoreUserRequest.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,41 @@ class StoreUserRequest extends ApplicationApiRequest
2121
/**
2222
* Return the validation rules for this request.
2323
*
24+
* @param array|null $rules
2425
* @return array
2526
*/
26-
public function rules(): array
27+
public function rules(array $rules = null): array
2728
{
28-
return collect(User::getCreateRules())->only([
29+
$rules = $rules ?? User::getCreateRules();
30+
31+
$response = collect($rules)->only([
2932
'external_id',
3033
'email',
3134
'username',
32-
'name_first',
33-
'name_last',
3435
'password',
3536
'language',
3637
'root_admin',
3738
])->toArray();
39+
40+
$response['first_name'] = $rules['name_first'];
41+
$response['last_name'] = $rules['name_last'];
42+
43+
return $response;
44+
}
45+
46+
/**
47+
* @return array
48+
*/
49+
public function validated()
50+
{
51+
$data = parent::validated();
52+
53+
$data['name_first'] = $data['first_name'];
54+
$data['name_last'] = $data['last_name'];
55+
56+
unset($data['first_name'], $data['last_name']);
57+
58+
return $data;
3859
}
3960

4061
/**

app/Http/Requests/Api/Application/Users/UpdateUserRequest.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,16 @@
66

77
class UpdateUserRequest extends StoreUserRequest
88
{
9-
/**
10-
* Determine if the requested user exists on the Panel.
11-
*
12-
* @return bool
13-
*/
14-
public function resourceExists(): bool
15-
{
16-
$user = $this->route()->parameter('user');
17-
18-
return $user instanceof User && $user->exists;
19-
}
20-
219
/**
2210
* Return the validation rules for this request.
2311
*
12+
* @param array|null $rules
2413
* @return array
2514
*/
26-
public function rules(): array
15+
public function rules(array $rules = null): array
2716
{
28-
$userId = $this->route()->parameter('user')->id;
17+
$userId = $this->getModel(User::class)->id;
2918

30-
return collect(User::getUpdateRulesForId($userId))->only([
31-
'external_id',
32-
'email',
33-
'username',
34-
'name_first',
35-
'name_last',
36-
'password',
37-
'language',
38-
'root_admin',
39-
])->toArray();
19+
return parent::rules(User::getUpdateRulesForId($userId));
4020
}
4121
}

app/Transformers/Api/Application/UserTransformer.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,27 @@ public function getResourceName(): string
2525
}
2626

2727
/**
28-
* Return a generic transformed subuser array.
28+
* Return a transformed User model that can be consumed by external services.
2929
*
3030
* @param \Pterodactyl\Models\User $user
3131
* @return array
3232
*/
3333
public function transform(User $user): array
3434
{
35-
return $user->toArray();
35+
return [
36+
'id' => $user->id,
37+
'external_id' => $user->external_id,
38+
'uuid' => $user->uuid,
39+
'username' => $user->username,
40+
'email' => $user->email,
41+
'first_name' => $user->name_first,
42+
'last_name' => $user->name_last,
43+
'language' => $user->language,
44+
'root_admin' => (bool) $user->root_admin,
45+
'2fa' => (bool) $user->use_totp,
46+
'created_at' => $this->formatTimestamp($user->created_at),
47+
'updated_at' => $this->formatTimestamp($user->updated_at),
48+
];
3649
}
3750

3851
/**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AllowTextInUserExternalId extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('users', function (Blueprint $table) {
17+
$table->string('external_id')->nullable()->change();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('users', function (Blueprint $table) {
29+
$table->unsignedInteger('external_id')->change();
30+
});
31+
}
32+
}

routes/api-application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
Route::group(['prefix' => '/users'], function () {
1212
Route::get('/', 'Users\UserController@index')->name('api.application.users');
13-
Route::get('/{user}', 'Users\UserController@view')->name('api.applications.users.view');
13+
Route::get('/{user}', 'Users\UserController@view')->name('api.application.users.view');
1414

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

0 commit comments

Comments
 (0)