| 1 | |||
| 2 | package ASP4::RequestFilter; | ||
| 3 | |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use base 'ASP4::HTTPHandler'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use vars __PACKAGE__->VARS; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | sub run; | ||
| 10 | |||
| 11 | 1 | 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 | =head1 BUGS | ||
| 87 | |||
| 88 | It's possible that some bugs have found their way into this release. | ||
| 89 | |||
| 90 | Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports. | ||
| 91 | |||
| 92 | =head1 HOMEPAGE | ||
| 93 | |||
| 94 | Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples | ||
| 95 | of ASP4 in action. | ||
| 96 | |||
| 97 | =pod | ||
| 98 | |||