Add Recaptcha support to the register action to help prevent spammers.
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Users.pm
CommitLineData
19262e3d 1package stemmaweb::Controller::Users;
2use Moose;
3use namespace::autoclean;
4
5BEGIN {extends 'CatalystX::Controller::Auth'; }
b74843e5 6with 'Catalyst::TraitFor::Controller::reCAPTCHA';
19262e3d 7
8=head1 NAME
9
10stemmaweb::Controller::Users - Catalyst Controller
11
12=head1 DESCRIPTION
13
b74843e5 14The Users controller is based on L<CatalystX::Controller::Auth>, see
15there for most of the functionality. Any localised parts are described
16below.
17
18This controller uses L<Catalyst::TraitFor::Controller::reCAPTCHA> to
19create and check a reCaptcha form shown on the C<register> form to
20help prevent spam signups.
19262e3d 21
22=head1 METHODS
23
24=cut
25
26sub base :Chained('/') :PathPart('') :CaptureArgs(0)
27{
28 my ( $self, $c ) = @_;
1628e97a 29
19262e3d 30 $self->next::method( $c );
31}
32
33=head2 index
34
b74843e5 35The index action is not currently used.
36
19262e3d 37=cut
38
39sub index :Path :Args(0) {
40 my ( $self, $c ) = @_;
41
42 $c->response->body('Matched stemmaweb::Controller::Users in Users.');
43}
44
b74843e5 45=head2 login with openid
46
47Logging in with openid/google requires two passes through the login
48action, on the 2nd pass the C<openid-check> value is passed in when
49the openid providing webserver links the user back to the stemmaweb
50site. This adaption to the C<login> action sets the realm we are
51authenticating against to be C<openid> in this case.
52
53=cut
54
b600c671 55before login => sub {
56 my($self, $c) = @_;
57 $c->req->param( realm => 'openid')
58 if $c->req->param('openid-check');
59};
19262e3d 60
b74843e5 61=head2 register with recaptcha
62
63This adapts the C<register> action to add the recaptcha HTML to the
64page, and verify the recaptcha info entered is correct when the form
65is submitted. If the recaptcha is not correct, we just redisplay the
66form with an error message.
67
68=cut
69
70before register => sub {
71 my ($self, $c) = @_;
72
73 ## Puts HTML into stash in "recaptcha" key.
74 $c->forward('captcha_get');
75
76 ## When submitting, check recaptcha passes, else re-draw form
77 if($c->req->method eq 'POST') {
78 if(!$c->forward('captcha_check')) {
79
80 ## Need these two lines to detach, so end can draw the correct template again:
81 my $form = $self->form_handler->new( active => [ $self->login_id_field, 'password', 'confirm_password' ] );
82 $c->stash( template => $self->register_template, form => $form );
83
84 $c->detach();
85 }
86 }
87};
88
19262e3d 89=head1 AUTHOR
90
91A clever guy
92
93=head1 LICENSE
94
95This library is free software. You can redistribute it and/or modify
96it under the same terms as Perl itself.
97
98=cut
99
100__PACKAGE__->meta->make_immutable;
101
1021;