1. André Walker
  2. perl-pro

Commits

André Walker  committed b2244bc

Implement `change_password`

  • Participants
  • Parent commits 9bc8e7a
  • Branches master

Comments (0)

Files changed (3)

File lib/PerlPro/Schema/ResultSet/User.pm

View file
                 sub { HTML::Entities::encode_entities( $_[0], qr{<>&"'} ) }
             ],
         ),
+        change_password => Data::Verifier->new(
+            profile => {
+                login                => { required => 1, type => Str },
+                current_password     => {
+                    required   => 1,
+                    type       => Str,
+                    post_check => sub {
+                        my $s = shift;
+                        return $self->find($s->get_value('login'))->check_password($s->get_value('current_password'));
+                    }
+                },
+                new_password         => { required => 1, type => Str },
+                confirm_new_password => {
+                    required   => 1,
+                    type       => Str,
+                    post_check => sub {
+                        my $s = shift;
+
+                        my $p1 = $s->get_value('new_password');
+                        my $p2 = $s->get_value('confirm_new_password');
+
+                        if ( $p1 ne $p2 ) {
+                            die 'confirm-not-equal';
+                        }
+
+                        return 1;
+                    },
+                },
+            },
+        ),
     };
 }
 
 
             return $row;
         },
+        change_password => sub {
+            my %values = shift->valid_values;
+            my $row = $self->find($values{login});
+            $row->update({ password => $values{new_password} });
+            return $row;
+        },
     };
 }
 

File lib/PerlPro/Web/Controller/Company/Data.pm

View file
 sub home :Chained('base') Does('DisplayExecute') Args(0) {
     my ( $self, $ctx ) = @_;
 
+    if (my $dm_data = delete $ctx->session->{_dm_CompanyData_change_password}) {
+        $ctx->stash(%$dm_data);
+    }
+
     $ctx->stash(
         template     => 'company/home.tx',
         current_page => 'home',
     }
 }
 
+sub change_password :Chained('base') Does('DisplayExecute') Args(0) {
+    my ( $self, $ctx ) = @_;
+
+    my $dm      = $ctx->model('DataManager');
+    my $params  = $ctx->req->body_params;
+    $params->{login} = $ctx->user->get_object->login;
+    $dm->apply_one( 'user.change_password', $params );
+
+    $ctx->stash(
+        uri_to_redirect => '/account/home',
+        DO_NOT_APPLY_DM => 1,
+    );
+}
+
 __PACKAGE__->meta->make_immutable;
 
 1;

File root/src/company/home.tx

View file
 </div><!-- end #account-home.content -->
 
 <!-- Category Selection modal -->
-<div class="modal fade" id="modal-change-password" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
+<div class="modal fade" id="modal-change-password" tabindex="-1" role="dialog" aria-hidden="true">
     <div class="modal-dialog">
         <div class="modal-content">
-            <form>
+            <form action="<: $uri('/account/change_password') :>" method="post">
 
                 <div class="modal-header">
                     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
 
                         <div class="form-group">
                             <label for="password">Senha atual:</label>
-                            <input type="password" name="password" class="form-control" />
+                            <input type="password" name="current_password" class="form-control" />
                         </div>
 
                         <div class="form-group">
 
                         <div class="form-group">
                             <label for="confirm_password">Confirme a nova senha:</label>
-                            <input type="password" name="confirm_password" class="form-control" />
+                            <input type="password" name="confirm_new_password" class="form-control" />
                         </div>
                     </div>