File Coverage

blib/lib/ASP4/RequestFilter.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1              
2             package ASP4::RequestFilter;
3              
4 6     6   3117 use strict;
  6         10  
  6         182  
5 6     6   40 use warnings 'all';
  6         10  
  6         208  
6 6     6   26 use base 'ASP4::HTTPHandler';
  6         8  
  6         520  
7 6     6   31 use vars __PACKAGE__->VARS;
  6         10  
  6         41  
8              
9             sub run;
10              
11             1;# return true:
12              
13             =pod
14              
15             =head1 NAME
16              
17             ASP4::RequestFilter - Filter incoming requests
18              
19             =head1 SYNOPSIS
20              
21             package My::MemberFilter;
22            
23             use strict;
24             use warnings 'all';
25             use base 'ASP4::RequestFilter';
26             use vars __PACKAGE__->VARS;
27            
28             sub run {
29             my ($self, $context) = @_;
30            
31             if( $Session->{is_logged_in} )
32             {
33             # The user is logged in - we can ignore this request:
34             return $Response->Declined;
35             }
36             else
37             {
38             # The user must authenticate first:
39             $Session->{validation_errors} = { general => "You must log in first" };
40             return $Response->Redirect("/login/");
41             }# end if()
42             }
43            
44             1;# return true:
45              
46             Then, in your C:
47              
48             {
49             ...
50             "web": {
51             ...
52             "request_filters": [
53             {
54             "uri_match": "/regexp/to/(path|place).*?",
55             "class" "My::MemberFilter"
56             },
57             {
58             "uri_match": "/regexp/to/(some|other|area).*?",
59             "class" "My::OtherFilter"
60             }
61             ]
62             ...
63             },
64             ...
65             }
66              
67             =head1 DESCRIPTION
68              
69             Subclass C to instantly apply rules to incoming
70             requests.
71              
72             These RequestFilters also work for testing via L.
73              
74             =head1 ABSTRACT METHODS
75              
76             =head2 run( $self, $context )
77              
78             B Return C<-1> (or $Response->Declined) to allow the current RequestFilter to be ignored.
79              
80             Returning anything else...
81              
82             return $Response->Redirect("/unauthorized/");
83              
84             ...results in the termination of the current request right away.
85              
86             =pod
87