Advertisement
Guest User

Untitled

a guest
May 9th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.21 KB | None | 0 0
  1. package Listy::Controller::User;
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use Captcha::reCAPTCHA;
  7. use base 'Catalyst::Controller::reCAPTCHA';
  8.  
  9. =head1 NAME
  10.  
  11. Listy::Controller::User - Catalyst Controller
  12.  
  13. =head1 DESCRIPTION
  14.  
  15. Catalyst Controller.
  16.  
  17. =head1 METHODS
  18.  
  19. =cut
  20.  
  21.  
  22. =head2 index
  23.  
  24. =cut
  25.  
  26. sub index : Private {
  27.     my ( $self, $c ) = @_;
  28.  
  29.     $c->response->body('Matched Listy::Controller::User in User.');
  30. }
  31.  
  32. sub signup :Local {
  33.     my ($self, $c) = @_;
  34.  
  35.     $c->forward('captcha_get');
  36.  
  37.     if ($c->request->method eq 'GET') {
  38.     $c->stash->{template} = 'user/signup.tt2';
  39.     return;
  40.     }
  41.  
  42.     my $error_msgs = {};
  43.     my $username = lc ($c->request->params->{username} || "");
  44.     my $password = $c->request->params->{password} || "";
  45.     my $confirm_password = $c->request->params->{confirm_password} || "";
  46.  
  47.     if (length $password < 8) {
  48.     $error_msgs -> {password} = "Must be at least 8 characters";
  49.     }
  50.  
  51.     if ($password ne $confirm_password) {
  52.     $error_msgs -> {confirm_password} = "Did not match password";
  53.     }
  54.  
  55.     if (not $username) {
  56.     $error_msgs -> {username} = "Must not be blank";
  57.     } elsif ($c->model('DB::Users')->find({username => $username})) {
  58.     $error_msgs -> {username} = "Username '$username' taken";
  59.     }
  60.  
  61.     $c->forward('captcha_check');
  62.  
  63.     if (not $c->stash->{recaptcha_ok}) {
  64.     $error_msgs -> {recaptcha} = $c->stash->{recaptcha_error}; #"CAPTCHA attempt failed";
  65.     }
  66.  
  67.     my $successful = scalar keys %$error_msgs;
  68.  
  69.     if ($successful) {
  70.     my $user = $c -> model('DB::Users') ->
  71.         create({username => $username, password => $password });
  72.  
  73.     if (not $user) {
  74.         $successful = 0;
  75.         $error_msgs -> {general} = "There was a system failure. Please try again later."
  76.     }  
  77.     }
  78.  
  79.     if ($successful) {
  80.     $c->forward('login')
  81.     } else {   
  82.     $c->stash->{error_msgs} = $error_msgs;
  83.     $c->stash->{username} = $username;
  84.     $c->stash->{template} = 'user/signup.tt2';
  85.     }
  86. }
  87.  
  88. sub login :Local {
  89.     my ($self, $c) = @_;
  90.  
  91.     # Get the username and password from form        
  92.     my $username = $c->request->params->{username} || "";
  93.     my $password = $c->request->params->{password} || "";
  94.     my $next_page = $c->request->params->{next_page} || "/lists/";
  95.  
  96.     # If the username and password values were found in form
  97.     if ($username && $password) {
  98.     # Attempt to log the user in
  99.     if ($c->authenticate({ username => $username,
  100.                                password => $password} )) {
  101.         # If successful, then let them use the application
  102.             $c->response->redirect($c->uri_for($next_page));
  103.             return;
  104.         } else {
  105.         # Set an error message
  106.             $c->stash->{error_msg} = "Bad username or password.";
  107.         }
  108.     }
  109.  
  110.     # If either of above don't work out, send to the login page
  111.     $c->stash->{template} = 'login.tt2';
  112. }
  113.  
  114. sub logout :Local {
  115.     my ($self, $c) = @_;
  116.  
  117.     # Clear the user's state                                  
  118.     $c->logout;
  119.  
  120.     #Send the user to the starting point
  121.     $c->response->redirect($c->uri_for('/'));
  122. }
  123.  
  124. =head1 AUTHOR
  125.  
  126. ,,,
  127.  
  128. =head1 LICENSE
  129.  
  130. This library is free software, you can redistribute it and/or modify
  131. it under the same terms as Perl itself.
  132.  
  133. =cut
  134.  
  135. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement