| 1 | package Plack::App::Login::Request; | ||
| 2 | |||
| 3 | 2 | ▶ | use base qw(Plack::Component::Tags::HTML); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Plack::Request; | ||
| 8 | use Plack::Response; | ||
| 9 | use Plack::Session; | ||
| 10 | use Plack::Util::Accessor qw(generator lang login_request_cb logo_image_url | ||
| 11 | message_cb redirect_login redirect_error text title); | ||
| 12 | use Tags::HTML::Container; | ||
| 13 | use Tags::HTML::Login::Request; | ||
| 14 | |||
| 15 | our $VERSION = 0.03; | ||
| 16 | |||
| 17 | sub _css { | ||
| 18 | my $self = shift; | ||
| 19 | |||
| 20 | $self->{'_tags_container'}->process_css; | ||
| 21 | $self->{'_tags_login_request'}->process_css({ | ||
| 22 | 'error' => 'red', | ||
| 23 | 'info' => 'blue', | ||
| 24 | }); | ||
| 25 | |||
| 26 | return; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub _login_check { | ||
| 30 | my ($self, $env, $body_parameters_hr) = @_; | ||
| 31 | |||
| 32 | if (! exists $body_parameters_hr->{'login_request'} | ||
| 33 | || $body_parameters_hr->{'login_request'} ne 'login_request') { | ||
| 34 | |||
| 35 | $self->_message($env, 'error', 'There is no login request POST.'); | ||
| 36 | return 0; | ||
| 37 | } | ||
| 38 | if (! defined $body_parameters_hr->{'email'} || ! $body_parameters_hr->{'email'}) { | ||
| 39 | $self->_message($env, 'error', "Missing email."); | ||
| 40 | return 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | return 1; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _message { | ||
| 47 | my ($self, $env, $message_type, $message) = @_; | ||
| 48 | |||
| 49 | if (defined $self->message_cb) { | ||
| 50 | $self->message_cb->($env, $message_type, $message); | ||
| 51 | } | ||
| 52 | |||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub _prepare_app { | ||
| 57 | my $self = shift; | ||
| 58 | |||
| 59 | # Defaults which rewrite defaults in module which I am inheriting. | ||
| 60 | if (! $self->generator) { | ||
| 61 | $self->generator(__PACKAGE__.'; Version: '.$VERSION); | ||
| 62 | } | ||
| 63 | |||
| 64 | if (! $self->title) { | ||
| 65 | $self->title('Login request page'); | ||
| 66 | } | ||
| 67 | |||
| 68 | # Inherite defaults. | ||
| 69 | $self->SUPER::_prepare_app; | ||
| 70 | |||
| 71 | # Defaults from this module. | ||
| 72 | my %p = ( | ||
| 73 | 'css' => $self->css, | ||
| 74 | 'tags' => $self->tags, | ||
| 75 | ); | ||
| 76 | |||
| 77 | # Tags helper for login button. | ||
| 78 | $self->{'_tags_login_request'} = Tags::HTML::Login::Request->new( | ||
| 79 | %p, | ||
| 80 | defined $self->lang ? ( | ||
| 81 | 'lang' => $self->lang, | ||
| 82 | ) : (), | ||
| 83 | 'logo_image_url' => $self->logo_image_url, | ||
| 84 | defined $self->text ? ( | ||
| 85 | 'text' => $self->text, | ||
| 86 | ) : (), | ||
| 87 | ); | ||
| 88 | $self->{'_tags_container'} = Tags::HTML::Container->new(%p); | ||
| 89 | |||
| 90 | return; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _process_actions { | ||
| 94 | my ($self, $env) = @_; | ||
| 95 | |||
| 96 | if (defined $self->login_request_cb && $env->{'REQUEST_METHOD'} eq 'POST') { | ||
| 97 | my $req = Plack::Request->new($env); | ||
| 98 | my $body_params_hr = $req->body_parameters; | ||
| 99 | my ($status, $messages_ar) = $self->_login_check($env, $body_params_hr); | ||
| 100 | my $res = Plack::Response->new; | ||
| 101 | if ($status) { | ||
| 102 | if ($self->login_request_cb->($env, $body_params_hr->{'email'})) { | ||
| 103 | $self->_message($env, 'info', | ||
| 104 | "Login information for email '$body_params_hr->{'email'}' was sent."); | ||
| 105 | $res->redirect($self->redirect_login); | ||
| 106 | } else { | ||
| 107 | $self->_message($env, 'error', 'Bad login email.'); | ||
| 108 | $res->redirect($self->redirect_error); | ||
| 109 | } | ||
| 110 | } else { | ||
| 111 | $res->redirect($self->redirect_error); | ||
| 112 | } | ||
| 113 | $self->psgi_app($res->finalize); | ||
| 114 | } | ||
| 115 | |||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub _tags_middle { | ||
| 120 | my ($self, $env) = @_; | ||
| 121 | |||
| 122 | my $messages_ar = []; | ||
| 123 | if (exists $env->{'psgix.session'}) { | ||
| 124 | my $session = Plack::Session->new($env); | ||
| 125 | $messages_ar = $session->get('messages'); | ||
| 126 | $session->set('messages', []); | ||
| 127 | } | ||
| 128 | $self->{'_tags_container'}->process( | ||
| 129 | sub { | ||
| 130 | $self->{'_tags_login_request'}->process($messages_ar); | ||
| 131 | }, | ||
| 132 | ); | ||
| 133 | |||
| 134 | return; | ||
| 135 | } | ||
| 136 | |||
| 137 | 1; | ||
| 138 | |||
| 139 | __END__ | ||