blib/lib/Catalyst/Controller/RateLimit.pm

filter: Statementstmt 82.6% 19 / 23 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 74.2% 26 / 35 CC 3 SCAR 15.4
CC
3
Cov
70%
flood_control
12.0
BEGIN
1.0
BEGIN
1.0
CRAP 4.7
↑ Summary
1 package Catalyst::Controller::RateLimit;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use parent 'Catalyst::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Algorithm::FloodControl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use 5.008007;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # $Id: RateLimit.pm 23 2008-11-06 07:54:40Z gugu $
10 # $Source$
11 # $HeadURL: file:///var/svn/cps/trunk/lib/Catalyst/Controller/RateLimit.pm $
12  
13 =head1 NAME
14  
15 Catalyst::Controller::RateLimit - Protect your site from robots
16  
17 =head1 VERSION
18  
19 See $VERSION
20  
21 =cut
22  
23 2 our $VERSION = 0.28;
24  
25 =head1 SYNOPSIS
26  
27 Protects your site from flood, robots and spam.
28  
29 package MyApp::Controller::Post;
30 use parent qw/Catalyst::Controller::RateLimit Catalyst::Controller/;
31 # Catalyst::Controller is not required, but i think, it will look better if you include it
32 __PACKAGE__->config(
33 rate_limit_backend_name => 'Cache::Memcached::Fast',
34 # ^- Optional. Only if your module is not Cache::Memcached::Fast child, but has the same behavior.
35 rate_limit => {
36 default => [
37 {
38 attempts => 30,
39 period => 3600,
40 }, {
41 attempts => 5,
42 period => 60,
43 }
44 ]
45 }
46 );
47  
48 sub login_form : Local { #Only check
49 my ( $self, $c ) = @_;
50 my $is_overrated = $self->flood_control->is_user_overrated( $c->user->login || $c->request->address );
51 if ( $is_overrated ) {
52 $c->forward( 'show_captcha' );
53 }
54 #...
55 }
56  
57 sub login : Local { #Check and register attempt
58 my ( $self, $c ) = @_;
59 if ( $self->flood_control->register_attempt( $c->user->login || $c->request->address ) ) {
60 # checking for CAPTCHA
61 }
62 #...
63 }
64  
65 sub show_captcha : Local { # If user have reached his limits, it is called
66 ... code to add captcha to page ...
67 }
68  
69 =head1 DESCRIPTION
70  
71 Protects critical parts of your site from robots.
72  
73 =head1 NOTES
74  
75 =head1 METHODS
76  
77 =head2 new
78  
79 =head2 flood_control
80  
81 Returns Algorithm::FloodControl object.
82  
83 =cut
84  
85 0 ▶sub flood_control {
Subroutine flood_control: not called CC=3 SCAR=24.8
Pod documented
86 0 my $self = shift;
87 0 ▶ if ( ref $self->{rate_limit} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"rate_limit"} eq "HASH") 0 0 0
88 return new Algorithm::FloodControl(
89 $self->{rate_limit_backend_name} ?
90 ( backend_name => $self->{rate_limit_backend_name} ) :
91 (),
92 storage => $self->_application->cache,
93 limits => $self->{rate_limit}
94 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'rate_limit_backend_name'} ? : 0 0 0
95 }
96 0 return;
97 }
98  
99  
100 =head1 AUTHOR
101  
102 Andrey Kostenko, C<< <andrey at kostenko.name> >>
103  
104 =head1 BUGS
105  
106 Please report any bugs or feature requests to C<bug-catalyst-plugin-stoprobots at rt.cpan.org>, or through
107 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-RateLimit>. I will be notified, and then you'll
108 automatically be notified of progress on your bug as I make changes.
109  
110 =head1 SUPPORT
111  
112 You can find documentation for this module with the perldoc command.
113  
114 perldoc Catalyst::Controller::RateLimit
115  
116  
117 You can also look for information at:
118  
119 =over 4
120  
121 =item * RT: CPAN's request tracker
122  
123 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-RateLimit>
124  
125 =item * AnnoCPAN: Annotated CPAN documentation
126  
127 L<http://annocpan.org/dist/Catalyst-Controller-RateLimit>
128  
129 =item * CPAN Ratings
130  
131 L<http://cpanratings.perl.org/d/Catalyst-Controller-RateLimit>
132  
133 =item * Search CPAN
134  
135 L<http://search.cpan.org/dist/Catalyst-Controller-RateLimit>
136  
137 =back
138  
139  
140 =head1 ACKNOWLEDGEMENTS
141  
142  
143 =head1 COPYRIGHT & LICENSE
144  
145 Copyright 2008 Andrey Kostenko.
146  
147 This program is free software; you can redistribute it and/or modify it
148 under the same terms as Perl itself.
149  
150  
151 =cut
152  
153  
154 2 1; # End of Catalyst::Controller::RateLimit
↑ Summary