| 1 | package CatalystX::SimpleLogin::Form::Login; | ||||||||||
| 2 | 10 | ▶ | use HTML::FormHandler::Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 10 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 10 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 10 | extends 'HTML::FormHandler'; | |||||||||
| 7 | 10 | ▶ | use MooseX::Types::Moose qw/ HashRef /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 10 | ▶ | use MooseX::Types::Common::String qw/ NonEmptySimpleStr /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 10 | has '+name' => ( default => 'login_form' ); | |||||||||
| 11 | |||||||||||
| 12 | 10 | has authenticate_args => ( | |||||||||
| 13 | is => 'ro', | ||||||||||
| 14 | isa => HashRef, | ||||||||||
| 15 | predicate => 'has_authenticate_args', | ||||||||||
| 16 | ); | ||||||||||
| 17 | |||||||||||
| 18 | 10 | has authenticate_realm => ( | |||||||||
| 19 | is => 'ro', | ||||||||||
| 20 | isa => NonEmptySimpleStr, | ||||||||||
| 21 | predicate => 'has_authenticate_realm', | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | 10 | has 'login_error_message' => ( | |||||||||
| 25 | is => 'ro', | ||||||||||
| 26 | isa => NonEmptySimpleStr, | ||||||||||
| 27 | required => 1, | ||||||||||
| 28 | default => 'Wrong username or password', | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | 10 | foreach my $type (qw/ username password /) { | |||||||||
| 32 | 20 | 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 | 10 | has_field 'username' => ( type => 'Text', tabindex => 1 ); | |||||||||
| 41 | 10 | has_field 'password' => ( type => 'Password', tabindex => 2 ); | |||||||||
| 42 | 10 | has_field 'remember' => ( type => 'Checkbox', tabindex => 3 ); | |||||||||
| 43 | 10 | has_field 'submit' => ( type => 'Submit', value => 'Login', tabindex => 4 ); | |||||||||
| 44 | |||||||||||
| 45 | 12 | ▶ | sub validate { | ||||||||
|
Subroutine
validate: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 46 | 12 | 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 | ▶ | unless ( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 12 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.5
| |||||||||||
| 53 | $self->ctx->authenticate( | ||||||||||
| 54 | { | ||||||||||
| 55 | (map { | ||||||||||
| 56 | 24 | my $param_name = sprintf("authenticate_%s_field_name", $_); | |||||||||
| 57 | 24 | ▶ | ($self->can($param_name) ? $self->$param_name() : $_) => $self->values->{$_}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | } | ||||||||||
| 59 | 36 | grep { ! /remember/ } | |||||||||
| 60 | 12 | ▶ | keys %{ $self->values }), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | ▶ | ($self->has_authenticate_args ? %{ $self->authenticate_args } : ()), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | }, | ||||||||||
| 63 | ($self->has_authenticate_realm ? $self->authenticate_realm : ()), | ||||||||||
| 64 | ); | ||||||||||
| 65 | } | ||||||||||
| 66 | 0 | ▶ | catch { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 67 | 0 | $self->ctx->log->error("$_"); | |||||||||
| 68 | 0 | return 0; | |||||||||
| 69 | } | ||||||||||
| 70 | ) { | ||||||||||
| 71 | 2 | $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 | return 0; | |||||||||
| 76 | } | ||||||||||
| 77 | 10 | return 1; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 2 | ▶ | sub add_auth_errors { | ||||||||
|
Subroutine
add_auth_errors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 81 | 2 | my $self = shift; | |||||||||
| 82 | 2 | $self->field( 'password' )->add_error( $self->login_error_message ); | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 10 | __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 | |||||||||||