File Coverage

blib/lib/CatalystX/SimpleLogin/Form/Login.pm
Criterion Covered Total %
statement 27 30 90.0
branch 6 8 75.0
condition n/a
subroutine 8 9 88.8
pod 2 2 100.0
total 43 49 87.7


line stmt bran cond sub pod time code
1             package CatalystX::SimpleLogin::Form::Login;
2 10     10   5000 use HTML::FormHandler::Moose;
  10         125481  
  10         35  
3 10     10   1369511 use Try::Tiny;
  10         15  
  10         664  
4 10     10   49 use namespace::autoclean;
  10         12  
  10         80  
5              
6             extends 'HTML::FormHandler';
7 10     10   1360 use MooseX::Types::Moose qw/ HashRef /;
  10         33512  
  10         106  
8 10     10   35920 use MooseX::Types::Common::String qw/ NonEmptySimpleStr /;
  10         65203  
  10         76  
9              
10             has '+name' => ( default => 'login_form' );
11              
12             has authenticate_args => (
13             is => 'ro',
14             isa => HashRef,
15             predicate => 'has_authenticate_args',
16             );
17              
18             has authenticate_realm => (
19             is => 'ro',
20             isa => NonEmptySimpleStr,
21             predicate => 'has_authenticate_realm',
22             );
23              
24             has 'login_error_message' => (
25             is => 'ro',
26             isa => NonEmptySimpleStr,
27             required => 1,
28             default => 'Wrong username or password',
29             );
30              
31             foreach my $type (qw/ username password /) {
32             has sprintf("authenticate_%s_field_name", $type) => (
33             is => 'ro',
34             isa => NonEmptySimpleStr,
35             default => $type
36             );
37             # FIXME - be able to change field names in rendered form also!
38             }
39              
40             has_field 'username' => ( type => 'Text', tabindex => 1 );
41             has_field 'password' => ( type => 'Password', tabindex => 2 );
42             has_field 'remember' => ( type => 'Checkbox', tabindex => 3 );
43             has_field 'submit' => ( type => 'Submit', value => 'Login', tabindex => 4 );
44              
45             sub validate {
46 12     12 1 165129 my $self = shift;
47              
48             # as HTML::Formhandler doesn't handle exceptions thrown by user provided
49             # validate methods and fails to clear the 'posted' attribute we need to
50             # catch them
51 12 100       142 unless (
52             try {
53             $self->ctx->authenticate(
54             {
55             (map {
56 24         913 my $param_name = sprintf("authenticate_%s_field_name", $_);
57 24 100       958 ($self->can($param_name) ? $self->$param_name() : $_) => $self->values->{$_};
58             }
59 36         1047 grep { ! /remember/ }
60 12         120 keys %{ $self->values }),
61 12 50   12   775 ($self->has_authenticate_args ? %{ $self->authenticate_args } : ()),
  0 50       0  
62             },
63             ($self->has_authenticate_realm ? $self->authenticate_realm : ()),
64             );
65             }
66             catch {
67 0     0   0 $self->ctx->log->error("$_");
68 0         0 return 0;
69             }
70             ) {
71 2         2575 $self->add_auth_errors;
72             # the return value of this method is ignored by HTML::FormHandler
73             # 0.40064, only errors added to the form itself or its fields control
74             # the forms' 'validated' attribute
75 2         1461 return 0;
76             }
77 10         75648 return 1;
78             }
79              
80             sub add_auth_errors {
81 2     2 1 4 my $self = shift;
82 2         21 $self->field( 'password' )->add_error( $self->login_error_message );
83             }
84              
85             __PACKAGE__->meta->make_immutable;
86              
87             =head1 NAME
88              
89             CatalystX::SimpleLogin::Form::Login - validation for the login form
90              
91             =head1 DESCRIPTION
92              
93             A L<HTML::FormHandler> form for the login form.
94              
95             =head1 FIELDS
96              
97              
98             =over
99              
100             =item username
101              
102             =item password
103              
104             =item remember
105              
106             =item submit
107              
108             =back
109              
110             =head1 METHODS
111              
112             =over
113              
114             =item validate
115              
116             =item add_auth_errors
117              
118             =back
119              
120             =head1 SEE ALSO
121              
122             =over
123              
124             =item L<CatalystX::SimpleLogin::Controller::Login>
125              
126             =back
127              
128             =head1 CUSTOMIZATION
129              
130             By default, the params passed to authenticate() are 'username' and
131             'password'. If you need to use different names, then you'll need to
132             set the correct value(s) via login_form_args in the configuration.
133             The keys are 'authenticate_username_field_name' and/or
134             'authenticate_password_field_name'.
135              
136             __PACKAGE__->config(
137             'Controller::Login' => {
138             login_form_args => {
139             authenticate_username_field_name => 'name',
140             authenticate_password_field_name => 'password2',
141             },
142             },
143             );
144              
145             You can also change the way that the form is displayed by setting
146             attributes. In MyApp.pm:
147              
148             __PACKAGE__->config(
149             'Controller::Login' => {
150             login_form_args => {
151             login_error_message => 'Login failed',
152             field_list => [
153             '+submit' => { value => 'Login' },
154             ]
155             }
156             },
157             );
158              
159             Additional fields can be added:
160              
161             field_list => [
162             'foo' => ( type => 'MyField' ),
163             'bar' => { type => 'Text' },
164             ]
165              
166             Additional arguments to the authenticate call can be added:
167             If your user table has a column C<status> and you want only those with C<status = 'active'>to be able to log .in
168              
169             __PACKAGE__->config(
170             'Controller::Login' => {
171             login_form_args => {
172             authenticate_args => { status => 1 },
173             },
174             },
175             };
176              
177             =head1 AUTHORS
178              
179             See L<CatalystX::SimpleLogin> for authors.
180              
181             =head1 LICENSE
182              
183             See L<CatalystX::SimpleLogin> for license.
184              
185             =cut
186