diff --git a/app/Http/Controllers/Api/Client/AccountController.php b/app/Http/Controllers/Api/Client/AccountController.php index e683d3a0b..fc0f22ecd 100644 --- a/app/Http/Controllers/Api/Client/AccountController.php +++ b/app/Http/Controllers/Api/Client/AccountController.php @@ -3,14 +3,57 @@ namespace Pterodactyl\Http\Controllers\Api\Client; use Illuminate\Http\Request; +use Pterodactyl\Services\Users\UserUpdateService; use Pterodactyl\Transformers\Api\Client\AccountTransformer; +use Pterodactyl\Http\Requests\Api\Client\Account\UpdateEmailRequest; class AccountController extends ClientApiController { + /** + * @var \Pterodactyl\Services\Users\UserUpdateService + */ + private $updateService; + + /** + * AccountController constructor. + * + * @param \Pterodactyl\Services\Users\UserUpdateService $updateService + */ + public function __construct(UserUpdateService $updateService) + { + parent::__construct(); + + $this->updateService = $updateService; + } + + /** + * @param Request $request + * @return array + */ public function index(Request $request): array { return $this->fractal->item($request->user()) ->transformWith($this->getTransformer(AccountTransformer::class)) ->toArray(); } + + /** + * Update the authenticated user's email address if their password matches. + * + * @param UpdateEmailRequest $request + * @return array + * + * @throws \Pterodactyl\Exceptions\Model\DataValidationException + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + */ + public function updateEmail(UpdateEmailRequest $request): array + { + $updated = $this->updateService->handle($request->user(), [ + 'email' => $request->input('email'), + ]); + + return $this->fractal->item($updated->get('model')) + ->transformWith($this->getTransformer(AccountTransformer::class)) + ->toArray(); + } } diff --git a/app/Http/Requests/Api/Client/Account/UpdateEmailRequest.php b/app/Http/Requests/Api/Client/Account/UpdateEmailRequest.php new file mode 100644 index 000000000..f354d120a --- /dev/null +++ b/app/Http/Requests/Api/Client/Account/UpdateEmailRequest.php @@ -0,0 +1,42 @@ +input('password'), $this->user()->password)) { + throw new InvalidPasswordProvidedException(trans('base.account.invalid_password')); + } + + return true; + } + + /** + * @return array + */ + public function rules(): array + { + $rules = User::getUpdateRulesForId($this->user()->id); + + return [ + 'email' => $rules['email'], + 'password' => array_merge($rules['password'], ['confirmed']), + ]; + } +} diff --git a/resources/assets/scripts/components/dashboard/account/UpdateEmail.vue b/resources/assets/scripts/components/dashboard/account/UpdateEmail.vue index 5f06ac77c..0e4d82b69 100644 --- a/resources/assets/scripts/components/dashboard/account/UpdateEmail.vue +++ b/resources/assets/scripts/components/dashboard/account/UpdateEmail.vue @@ -64,7 +64,7 @@ }) .catch(error => { if (!error.response) { - return console.error(error); + this.error(error.message); } const response = error.response; diff --git a/routes/api-client.php b/routes/api-client.php index afee2defd..77aeac295 100644 --- a/routes/api-client.php +++ b/routes/api-client.php @@ -14,6 +14,7 @@ Route::get('/', 'ClientController@index')->name('api.client.index'); Route::group(['prefix' => '/account'], function () { Route::get('/', 'AccountController@index')->name('api.client.account'); + Route::put('/email', 'AccountController@updateEmail')->name('api.client.account.update-email'); });