ASP4/RequestFilter.pm untested

filter: Statementstmt 0.0% 0 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 18 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1  
2 package ASP4::RequestFilter;
3  
4 use strict;
5 use warnings 'all';
6 use base 'ASP4::HTTPHandler';
7 use vars __PACKAGE__->VARS;
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<asp4-config.json>:
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<ASP4::RequestFilter> to instantly apply rules to incoming
70 requests.
71  
72 These RequestFilters also work for testing via L<ASP4::API>.
73  
74 =head1 ABSTRACT METHODS
75  
76 =head2 run( $self, $context )
77  
78 B<IMPORTANT:> 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