Skip to content

Commit b8b9acd

Browse files
committed
Get the base email update working through the API.
Still going to need to determine the best course of action to update the token on the client side.
1 parent 14927c3 commit b8b9acd

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

app/Http/Controllers/Api/Client/AccountController.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,57 @@
33
namespace Pterodactyl\Http\Controllers\Api\Client;
44

55
use Illuminate\Http\Request;
6+
use Pterodactyl\Services\Users\UserUpdateService;
67
use Pterodactyl\Transformers\Api\Client\AccountTransformer;
8+
use Pterodactyl\Http\Requests\Api\Client\Account\UpdateEmailRequest;
79

810
class AccountController extends ClientApiController
911
{
12+
/**
13+
* @var \Pterodactyl\Services\Users\UserUpdateService
14+
*/
15+
private $updateService;
16+
17+
/**
18+
* AccountController constructor.
19+
*
20+
* @param \Pterodactyl\Services\Users\UserUpdateService $updateService
21+
*/
22+
public function __construct(UserUpdateService $updateService)
23+
{
24+
parent::__construct();
25+
26+
$this->updateService = $updateService;
27+
}
28+
29+
/**
30+
* @param Request $request
31+
* @return array
32+
*/
1033
public function index(Request $request): array
1134
{
1235
return $this->fractal->item($request->user())
1336
->transformWith($this->getTransformer(AccountTransformer::class))
1437
->toArray();
1538
}
39+
40+
/**
41+
* Update the authenticated user's email address if their password matches.
42+
*
43+
* @param UpdateEmailRequest $request
44+
* @return array
45+
*
46+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
47+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
48+
*/
49+
public function updateEmail(UpdateEmailRequest $request): array
50+
{
51+
$updated = $this->updateService->handle($request->user(), [
52+
'email' => $request->input('email'),
53+
]);
54+
55+
return $this->fractal->item($updated->get('model'))
56+
->transformWith($this->getTransformer(AccountTransformer::class))
57+
->toArray();
58+
}
1659
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Client\Account;
4+
5+
use Pterodactyl\Models\User;
6+
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
7+
use Pterodactyl\Exceptions\Http\Base\InvalidPasswordProvidedException;
8+
9+
class UpdateEmailRequest extends ClientApiRequest
10+
{
11+
/**
12+
* @return bool
13+
*
14+
* @throws \Pterodactyl\Exceptions\Http\Base\InvalidPasswordProvidedException
15+
*/
16+
public function authorize(): bool
17+
{
18+
if (! parent::authorize()) {
19+
return false;
20+
}
21+
22+
// Verify password matches when changing password or email.
23+
if (! password_verify($this->input('password'), $this->user()->password)) {
24+
throw new InvalidPasswordProvidedException(trans('base.account.invalid_password'));
25+
}
26+
27+
return true;
28+
}
29+
30+
/**
31+
* @return array
32+
*/
33+
public function rules(): array
34+
{
35+
$rules = User::getUpdateRulesForId($this->user()->id);
36+
37+
return [
38+
'email' => $rules['email'],
39+
'password' => array_merge($rules['password'], ['confirmed']),
40+
];
41+
}
42+
}

resources/assets/scripts/components/dashboard/account/UpdateEmail.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
})
6565
.catch(error => {
6666
if (!error.response) {
67-
return console.error(error);
67+
this.error(error.message);
6868
}
6969
7070
const response = error.response;

routes/api-client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
Route::group(['prefix' => '/account'], function () {
1616
Route::get('/', 'AccountController@index')->name('api.client.account');
17+
1718
Route::put('/email', 'AccountController@updateEmail')->name('api.client.account.update-email');
1819
});
1920

0 commit comments

Comments
 (0)