Advertisement
Guest User

Untitled

a guest
Dec 28th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.40 KB | None | 0 0
  1. # file Errors.pm
  2.  
  3. package API::Model::Errors;
  4. use Mojo::Base 'MojoX::Model';
  5. use Moo;
  6.  
  7. # attribute errors
  8. has 'errors' => (
  9.     is => 'ro',
  10.     default => sub {
  11.         []
  12.     }
  13. );
  14.  
  15. # method set error
  16. sub setError {
  17.     my ($self, $error) = @_;
  18.    
  19.     push(@{$self->errors}, $error);
  20. }
  21.  
  22. # method get errors
  23. sub getErrors {
  24.     my $self = shift;
  25.    
  26.     my $result = {};
  27.     $result->{success} = 0;
  28.     $result->{errors} = $self->errors;
  29.    
  30.     return $result;
  31. }
  32.  
  33. # method get total errors
  34. sub totalErrors {
  35.     my $self = shift;
  36.    
  37.     return scalar(@{$self->errors});
  38. }
  39.  
  40. 1;
  41.  
  42. # file Settings.pm
  43.  
  44. package API::Model::Settings;
  45. use Mojo::Base 'MojoX::Model';
  46. use Moo;
  47. extends qw/API::Model::Errors/;
  48.  
  49. sub passwordChange {
  50.     my ($self, $password_current, $password_new) = @_;
  51.    
  52.     # get password in database and valid
  53.     $self->setError('Password current incorrect!') unless $self->app->model('users-read')->validIdAndPassword($self->app->usr_id, $password_current);
  54.    
  55.     # if have some problem, return error
  56.     return $self->getErrors if $self->totalErrors;
  57.    
  58.     # update password in database
  59.     $self->app->model('users-update')->passwordChange($self->app->usr_id, $password_new);
  60.    
  61.     # return successes
  62.     return $self->getSuccesses;
  63. }
  64.  
  65. 1;
  66.  
  67. # Action in Controller
  68.  
  69. sub password_change {
  70.     my $self = shift;
  71.    
  72.     # params
  73.     my $password_current = $self->param('password_current') || '';
  74.     my $password_new = $self->param('password_new') || '';
  75.     my $password_new2 = $self->param('password_new2') || '';
  76.    
  77.     # valid params
  78.     my $errors = $self->model('errors')->new;
  79.     $errors->setError('Password current invalid!') unless $password_current && $password_current =~ /^\w+$/;
  80.     $errors->setError('Password new invalid!') unless $password_new && $password_new =~ /^\w+$/;
  81.     $errors->setError('Password new 2 invalid!') unless $password_new2 && $password_new2 =~ /^\w+$/;
  82.     $errors->setError('Password new and Password new 2 are different!') unless $password_new =~ /^${password_new2}$/;
  83.    
  84.     # first test if have errors with ternary operator, if true run method, if false get errors
  85.     my $result = $errors->totalErrors == 0
  86.     ? $self->model('settings')->passwordChange($password_current, $password_new)
  87.     : $errors->getErrors;
  88.    
  89.     # print json
  90.     $self->render(
  91.         json => $result
  92.     );    
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement