Much cleaner code for updating user details front-end

This commit is contained in:
Dane Everitt 2017-01-22 16:16:43 -05:00
parent 9e54dabe5b
commit 5f1bfcf980
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
6 changed files with 152 additions and 105 deletions

View file

@ -25,11 +25,13 @@
namespace Pterodactyl\Http\Controllers\Base;
use Log;
use Alert;
use Illuminate\Http\Request;
use Pterodactyl\Models\User;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Repositories\UserRepository;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Exceptions\DisplayValidationException;
class AccountController extends Controller
{
@ -45,61 +47,54 @@ class AccountController extends Controller
}
/**
* Update an account email.
*
* Update details for a users account.
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
* @return void
*/
public function email(Request $request)
public function update(Request $request)
{
$this->validate($request, [
'new_email' => 'required|email',
'password' => 'required',
]);
$data = [];
$user = $request->user();
// Request to update account Password
if ($request->input('do_action') === 'password') {
$this->validate($request, [
'current_password' => 'required',
'new_password' => 'required|confirmed|' . User::PASSWORD_RULES,
'new_password_confirmation' => 'required',
]);
if (! password_verify($request->input('password'), $user->password)) {
Alert::danger('The password provided was not valid for this account.')->flash();
$data['password'] = $request->input('new_password');
return redirect()->route('account');
// Request to update account Email
} else if ($request->input('do_action') === 'email') {
$data['email'] = $request->input('new_email');
// Request to update account Identity
} else if ($request->input('do_action') === 'identity') {
$data = $request->only(['name_first', 'name_last', 'username']);
// Unknown, hit em with a 404
} else {
return abort(404);
}
$user->email = $request->input('new_email');
$user->save();
Alert::success('Your email address has successfully been updated.')->flash();
return redirect()->route('account');
}
/**
* Update an account password.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function password(Request $request)
{
$this->validate($request, [
'current_password' => 'required',
'new_password' => 'required|confirmed|different:current_password|' . User::PASSWORD_RULES,
'new_password_confirmation' => 'required',
]);
$user = $request->user();
if (! password_verify($request->input('current_password'), $user->password)) {
Alert::danger('The password provided was not valid for this account.')->flash();
if (
in_array($request->input('do_action'), ['email', 'password'])
&& ! password_verify($request->input('password'), $request->user()->password)
) {
Alert::danger(trans('base.account.invalid_pass'))->flash();
return redirect()->route('account');
}
try {
$user->setPassword($request->input('new_password'));
Alert::success('Your password has successfully been updated.')->flash();
} catch (DisplayException $e) {
Alert::danger($e->getMessage())->flash();
$repo = new UserRepository;
$repo->update($request->user()->id, $data);
Alert::success('Your account details were successfully updated.')->flash();
} catch (DisplayValidationException $ex) {
return redirect()->route('account')->withErrors(json_decode($ex->getMessage()));
} catch (\Exception $ex) {
Log::error($ex);
Alert::danger(trans('base.account.exception'))->flash();
}
return redirect()->route('account');

View file

@ -63,13 +63,8 @@ class BaseRoutes
'as' => 'account',
'uses' => 'Base\AccountController@index',
]);
$router->post('/password', [
'as' => 'account.password',
'uses' => 'Base\AccountController@password',
]);
$router->post('/email', [
'as' => 'account.email',
'uses' => 'Base\AccountController@email',
$router->post('/', [
'uses' => 'Base\AccountController@update',
]);
});

View file

@ -154,10 +154,6 @@ class UserRepository
unset($data['password']);
}
if (! empty($data['root_admin'])) {
$user->root_admin = $data['root_admin'];
}
$user->fill($data);
return $user->save();

View file

@ -121,3 +121,7 @@ td.has-progress {
.box-header > .form-group > div > p.small {
margin: 0;
}
.no-margin {
margin: 0 !important;
}

View file

@ -163,6 +163,12 @@ return [
'new_password' => 'New Password',
'new_password_again' => 'Repeat New Password',
'new_email' => 'New Email Address',
'first_name' => 'First Name',
'last_name' => 'Last Name',
'update_identitity' => 'Update Identity',
'username_help' => 'Your username must be unique to your account, and may only contain the following characters: :requirements.',
'invalid_pass' => 'The password provided was not valid for this account.',
'exception' => 'An error occurred while attempting to update your account.',
],
'security' => [
'header' => 'Account Security',

View file

@ -34,64 +34,115 @@
@section('content')
<div class="row">
<div class="col-sm-6">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">@lang('base.account.update_pass')</h3>
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">@lang('base.account.update_pass')</h3>
</div>
<form action="{{ route('account') }}" method="post">
<div class="box-body">
<div class="form-group">
<label for="current_password" class="control-label">@lang('base.account.current_password')</label>
<div>
<input type="password" class="form-control" name="current_password" />
</div>
</div>
<div class="form-group">
<label for="new_password" class="control-label">@lang('base.account.new_password')</label>
<div>
<input type="password" class="form-control" name="new_password" />
<p class="text-muted small no-margin">@lang('auth.password_requirements')</p>
</div>
</div>
<div class="form-group">
<label for="new_password_again" class="control-label">@lang('base.account.new_password_again')</label>
<div>
<input type="password" class="form-control" name="new_password_confirmation" />
</div>
</div>
</div>
<div class="box-footer">
{!! csrf_field() !!}
<input type="hidden" name="do_action" value="password" />
<input type="submit" class="btn btn-primary btn-sm" value="@lang('base.account.update_pass')" />
</div>
</form>
</div>
</div>
<form action="{{ route('account.password') }}" method="post">
<div class="box-body">
<div class="form-group">
<label for="current_password" class="control-label">@lang('base.account.current_password')</label>
<div>
<input type="password" class="form-control" name="current_password" />
</div>
</div>
<div class="form-group">
<label for="new_password" class="control-label">@lang('base.account.new_password')</label>
<div>
<input type="password" class="form-control" name="new_password" />
<p class="text-muted"><small>@lang('auth.password_requirements')</small></p>
</div>
</div>
<div class="form-group">
<label for="new_password_again" class="control-label">@lang('base.account.new_password_again')</label>
<div>
<input type="password" class="form-control" name="new_password_confirmation" />
</div>
</div>
</div>
<div class="box-footer">
{!! csrf_field() !!}
<input type="submit" class="btn btn-primary btn-sm" value="@lang('base.account.update_pass')" />
</div>
</form>
</div>
</div>
<div class="col-sm-6">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">@lang('base.account.update_email')</h3>
<div class="row">
<div class="col-xs-12">
<div class="box box-primary">
<form action="{{ route('account') }}" method="POST">
<div class="box-header with-border">
<h3 class="box-title">@lang('base.account.update_identitity')</h3>
</div>
<div class="box-body">
<div class="row">
<div class="form-group col-sm-6">
<label for="first_name" class="control-label">@lang('base.account.first_name')</label>
<div>
<input type="text" class="form-control" name="name_first" value="{{ Auth::user()->name_first }}" />
</div>
</div>
<div class="form-group col-sm-6">
<label for="last_name" class="control-label">@lang('base.account.last_name')</label>
<div>
<input type="text" class="form-control" name="name_last" value="{{ Auth::user()->name_last }}" />
</div>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12">
<label for="password" class="control-label">@lang('strings.username')</label>
<div>
<input type="text" class="form-control" name="username" value="{{ Auth::user()->username }}" />
<p class="text-muted small no-margin">@lang('base.account.username_help', [ 'requirements' => '<code>a-z A-Z 0-9 _ - .</code>'])</p>
</div>
</div>
</div>
</div>
<div class="box-footer with-border">
{!! csrf_field() !!}
<input type="hidden" name="do_action" value="identity" />
<button type="submit" class="btn btn-sm btn-primary">@lang('base.account.update_identitity')</button>
</div>
</form>
</div>
</div>
<form action="{{ route('account.email') }}" method="post">
<div class="box-body">
<div class="form-group">
<label for="new_email" class="control-label">@lang('base.account.new_email')</label>
<div>
<input type="text" class="form-control" name="new_email" />
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">@lang('base.account.update_email')</h3>
</div>
<div class="form-group">
<label for="password" class="control-label">@lang('base.account.current_password')</label>
<div>
<input type="password" class="form-control" name="password" />
<form action="{{ route('account') }}" method="post">
<div class="box-body">
<div class="form-group">
<label for="new_email" class="control-label">@lang('base.account.new_email')</label>
<div>
<input type="email" class="form-control" name="new_email" />
</div>
</div>
<div class="form-group">
<label for="password" class="control-label">@lang('base.account.current_password')</label>
<div>
<input type="password" class="form-control" name="password" />
</div>
</div>
</div>
</div>
<div class="box-footer">
{!! csrf_field() !!}
<input type="hidden" name="do_action" value="email" />
<input type="submit" class="btn btn-primary btn-sm" value="@lang('base.account.update_email')" />
</div>
</form>
</div>
<div class="box-footer">
{!! csrf_field() !!}
<input type="submit" class="btn btn-primary btn-sm" value="@lang('base.account.update_email')" />
</div>
</form>
</div>
</div>
</div>
</div>