Refactor UserSettings.

master
Manager Bot 3 years ago
parent d167ee3da9
commit 087b35f1ff
  1. 55
      Web/lib/MJB/Web/Controller/UserSettings.pm
  2. 6
      Web/templates/user_settings/change_password.html.ep
  3. 6
      Web/templates/user_settings/profile.html.ep

@ -2,30 +2,31 @@ package MJB::Web::Controller::UserSettings;
use Mojo::Base 'Mojolicious::Controller', -signatures; use Mojo::Base 'Mojolicious::Controller', -signatures;
sub profile ( $c ) { sub profile ( $c ) {
$c->stash->{form_name} = $c->stash->{person}->name; # Set the form values from the DB if they don't exist from the POST handler.
$c->stash->{form_email} = $c->stash->{person}->email; $c->stash->{form}->{name} ||= $c->stash->{person}->name;
$c->stash->{form}->{email} ||= $c->stash->{person}->email;
} }
sub do_profile ( $c ) { sub do_profile ( $c ) {
$c->stash->{template} = 'user_settings/profile'; my $name = $c->stash->{form}->{name} = $c->param('name');
my $email = $c->stash->{form}->{email} = $c->param('email');
my $name = $c->stash->{form_name} = $c->param('name'); my $password = $c->stash->{form}->{password} = $c->param('password');
my $email = $c->stash->{form_email} = $c->param('email');
my $password = $c->stash->{form_password} = $c->param('password');
# Populate errors if we don't have values. # Populate errors if we don't have values.
push @{$c->{stash}->{errors}}, "You must enter your name" unless $name; push @{$c->{stash}->{errors}}, "You must enter your name" unless $name;
push @{$c->{stash}->{errors}}, "You must enter your email" unless $email; push @{$c->{stash}->{errors}}, "You must enter your email" unless $email;
push @{$c->{stash}->{errors}}, "You must enter your password" unless $password; push @{$c->{stash}->{errors}}, "You must enter your password" unless $password;
# Bail out if we have errors now. # Bail out if we have errors now.
return 0 if $c->stash->{errors}; return $c->redirect_error( 'show_profile' )
if $c->stash->{errors};
$c->stash->{person}->auth_password->check_password( $password ) $c->stash->{person}->auth_password->check_password( $password )
or push @{$c->stash->{errors}}, "You must enter your current login password correctly."; or push @{$c->stash->{errors}}, "You must enter your current login password correctly.";
# Bail out if we have errors now. # Bail out if we have errors now.
return 0 if $c->stash->{errors}; return $c->redirect_error( 'show_profile' )
if $c->stash->{errors};
$c->stash->{person}->name( $name ); $c->stash->{person}->name( $name );
$c->stash->{person}->email( $email ); $c->stash->{person}->email( $email );
@ -33,19 +34,17 @@ sub do_profile ( $c ) {
$c->stash->{person}->update; $c->stash->{person}->update;
# Let the user know the action was successful. # Let the user know the action was successful.
$c->stash->{success} = 1; $c->flash( confirmation => "Your records have been updated." );
$c->stash->{success_message} = "Your records were updated."; $c->redirect_to( $c->url_for( 'show_profile' ) );
} }
sub change_password ( $c ) { } sub change_password ( $c ) { }
sub do_change_password ( $c ) { sub do_change_password ( $c ) {
$c->stash->{template} = 'user_settings/change_password';
# Get the values the user gave for the password change. # Get the values the user gave for the password change.
my $password = $c->stash->{form_password} = $c->param('password'); my $password = $c->stash->{form}->{password} = $c->param('password');
my $new_pass = $c->stash->{form_new_password} = $c->param('new_password'); my $new_pass = $c->stash->{form}->{new_password} = $c->param('new_password');
my $confirm = $c->stash->{form_password_confirm} = $c->param('password_confirm'); my $confirm = $c->stash->{form}->{password_confirm} = $c->param('password_confirm');
# Populate errors if we don't have values. # Populate errors if we don't have values.
push @{$c->{stash}->{errors}}, "You must enter your current password" unless $password; push @{$c->{stash}->{errors}}, "You must enter your current password" unless $password;
@ -53,13 +52,15 @@ sub do_change_password ( $c ) {
push @{$c->{stash}->{errors}}, "You must enter your new password again to confirm" unless $confirm; push @{$c->{stash}->{errors}}, "You must enter your new password again to confirm" unless $confirm;
# Bail out if we have errors now. # Bail out if we have errors now.
return 0 if $c->stash->{errors}; return $c->redirect_error( 'show_change_password' )
if $c->stash->{errors};
$c->stash->{person}->auth_password->check_password( $password ) $c->stash->{person}->auth_password->check_password( $password )
or push @{$c->stash->{errors}}, "You must enter your current login password correctly."; or push @{$c->stash->{errors}}, "You must enter your current login password correctly.";
# Bail out if we have errors now. # Bail out if we have errors now.
return 0 if $c->stash->{errors}; return $c->redirect_error( 'show_change_password' )
if $c->stash->{errors};
push @{$c->stash->{errors}}, "Password and confirm password must match" push @{$c->stash->{errors}}, "Password and confirm password must match"
unless $new_pass eq $confirm; unless $new_pass eq $confirm;
@ -68,21 +69,21 @@ sub do_change_password ( $c ) {
unless length($new_pass) >= 8; unless length($new_pass) >= 8;
# Bail out if we have errors now. # Bail out if we have errors now.
return if $c->stash->{errors}; return $c->redirect_error( 'show_change_password' )
if $c->stash->{errors};
# We can update the password now. # We can update the password now.
$c->stash->{person}->auth_password->update_password($new_pass); $c->stash->{person}->auth_password->update_password($new_pass);
# Let the user know the action was successful. # Let the user know the action was successful.
$c->stash->{success} = 1; $c->flash( confirmation => "Your password was updated." );
$c->stash->{success_message} = "Your password was updated."; $c->redirect_to( $c->url_for( 'show_change_password' ) );
# Clear the form values on success.
$c->stash->{form_password} = "";
$c->stash->{form_new_password} = "";
$c->stash->{form_password_confirm} = "";
} }
1;
__END__
sub subscription ($c) { sub subscription ($c) {
my $status = $c->param('status'); my $status = $c->param('status');

@ -9,19 +9,19 @@
<%= include '_base/form/input', type => 'password', name => 'password', <%= include '_base/form/input', type => 'password', name => 'password',
title => 'Your current password', title => 'Your current password',
help => '', help => '',
value => $c->stash->{form_password} value => $c->stash->{form}->{password}
%> %>
<%= include '_base/form/input', type => 'password', name => 'new_password', <%= include '_base/form/input', type => 'password', name => 'new_password',
title => 'Your new password', title => 'Your new password',
help => '', help => '',
value => $c->stash->{form_new_password} value => $c->stash->{form}->{new_password}
%> %>
<%= include '_base/form/input', type => 'password', name => 'password_confirm', <%= include '_base/form/input', type => 'password', name => 'password_confirm',
title => 'Confirm your new password', title => 'Confirm your new password',
help => '', help => '',
value => $c->stash->{form_password_confirm} value => $c->stash->{form}->{password_confirm}
%> %>

@ -9,19 +9,19 @@
<%= include '_base/form/input', type => 'text', name => 'name', <%= include '_base/form/input', type => 'text', name => 'name',
title => 'Your name', title => 'Your name',
help => '', help => '',
value => $c->stash->{form_name}, value => $c->stash->{form}->{name},
%> %>
<%= include '_base/form/input', type => 'email', name => 'email', <%= include '_base/form/input', type => 'email', name => 'email',
title => 'Email Address', title => 'Email Address',
help => '', help => '',
value => $c->stash->{form_email}, value => $c->stash->{form}->{email},
%> %>
<%= include '_base/form/input', type => 'password', name => 'password', <%= include '_base/form/input', type => 'password', name => 'password',
title => 'Your password (required for these changes)', title => 'Your password (required for these changes)',
help => '', help => '',
value => $c->stash->{form_password} value => $c->stash->{form}->{password}
%> %>
<button type="submit" class="btn btn-primary float-end">Update Profile</button> <button type="submit" class="btn btn-primary float-end">Update Profile</button>

Loading…
Cancel
Save