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.
This commit is contained in:
Dane Everitt 2018-06-11 22:56:57 -07:00
parent 14927c3e7e
commit b8b9acd0e6
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 87 additions and 1 deletions

View file

@ -3,14 +3,57 @@
namespace Pterodactyl\Http\Controllers\Api\Client; namespace Pterodactyl\Http\Controllers\Api\Client;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Services\Users\UserUpdateService;
use Pterodactyl\Transformers\Api\Client\AccountTransformer; use Pterodactyl\Transformers\Api\Client\AccountTransformer;
use Pterodactyl\Http\Requests\Api\Client\Account\UpdateEmailRequest;
class AccountController extends ClientApiController 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 public function index(Request $request): array
{ {
return $this->fractal->item($request->user()) return $this->fractal->item($request->user())
->transformWith($this->getTransformer(AccountTransformer::class)) ->transformWith($this->getTransformer(AccountTransformer::class))
->toArray(); ->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();
}
} }

View file

@ -0,0 +1,42 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Client\Account;
use Pterodactyl\Models\User;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
use Pterodactyl\Exceptions\Http\Base\InvalidPasswordProvidedException;
class UpdateEmailRequest extends ClientApiRequest
{
/**
* @return bool
*
* @throws \Pterodactyl\Exceptions\Http\Base\InvalidPasswordProvidedException
*/
public function authorize(): bool
{
if (! parent::authorize()) {
return false;
}
// Verify password matches when changing password or email.
if (! password_verify($this->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']),
];
}
}

View file

@ -64,7 +64,7 @@
}) })
.catch(error => { .catch(error => {
if (!error.response) { if (!error.response) {
return console.error(error); this.error(error.message);
} }
const response = error.response; const response = error.response;

View file

@ -14,6 +14,7 @@ Route::get('/', 'ClientController@index')->name('api.client.index');
Route::group(['prefix' => '/account'], function () { Route::group(['prefix' => '/account'], function () {
Route::get('/', 'AccountController@index')->name('api.client.account'); Route::get('/', 'AccountController@index')->name('api.client.account');
Route::put('/email', 'AccountController@updateEmail')->name('api.client.account.update-email'); Route::put('/email', 'AccountController@updateEmail')->name('api.client.account.update-email');
}); });