| 1 | |||
| 2 | package Apache2::ASP::RequestFilter; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings 'all'; | ||
| 6 | use base 'Apache2::ASP::HTTPHandler'; | ||
| 7 | |||
| 8 | sub run; | ||
| 9 | |||
| 10 | 1;# return true: | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Apache2::ASP::RequestFilter - Filter incoming requests | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | package My::MemberFilter; | ||
| 19 | |||
| 20 | use strict; | ||
| 21 | use warnings 'all'; | ||
| 22 | use base 'Apache2::ASP::RequestFilter'; | ||
| 23 | use vars __PACKAGE__->VARS; | ||
| 24 | |||
| 25 | sub run { | ||
| 26 | my ($self, $context) = @_; | ||
| 27 | |||
| 28 | if( $Session->{is_logged_in} ) | ||
| 29 | { | ||
| 30 | # The user is logged in - we can ignore this request: | ||
| 31 | return $Response->Declined; | ||
| 32 | } | ||
| 33 | else | ||
| 34 | { | ||
| 35 | # The user must authenticate first: | ||
| 36 | $Session->{validation_errors} = { general => "You must log in first" }; | ||
| 37 | return $Response->Redirect("/login/"); | ||
| 38 | }# end if() | ||
| 39 | } | ||
| 40 | |||
| 41 | 1;# return true: | ||
| 42 | |||
| 43 | Then, in your C<apache2-asp-config.xml>: | ||
| 44 | |||
| 45 | <config> | ||
| 46 | ... | ||
| 47 | <web> | ||
| 48 | ... | ||
| 49 | <request_filters> | ||
| 50 | <filter> | ||
| 51 | <uri_match>/members/*</uri_match> | ||
| 52 | <class>My::MemberFilter</class> | ||
| 53 | </filter> | ||
| 54 | ... | ||
| 55 | </request_filters> | ||
| 56 | </web> | ||
| 57 | ... | ||
| 58 | </config> | ||
| 59 | |||
| 60 | =head1 DESCRIPTION | ||
| 61 | |||
| 62 | Subclass C<Apache2::ASP::RequestFilter> to instantly apply rules to incoming | ||
| 63 | requests. | ||
| 64 | |||
| 65 | These RequestFilters also work for testing via L<Apache2::ASP::Test::Base> and | ||
| 66 | L<Apache2::ASP::API>. | ||
| 67 | |||
| 68 | =head2 RequestFilters vs TransHandlers | ||
| 69 | |||
| 70 | The difference between RequestFilters and L<Apache2::ASP::TransHandler>s is that | ||
| 71 | within a RequestFilter, you have access to all of the normal ASP objects ($Request, $Response, $Session, etc). | ||
| 72 | |||
| 73 | In a TransHandler, you only have access to the L<Apache2::RequestRec> C<$r> and the | ||
| 74 | L<Apache2::ASP::Config> (and only then if you load it up yourself via L<Apache2::ASP::ConfigLoader>. | ||
| 75 | |||
| 76 | B<NOTE>: - TransHandlers are configured in the C<httpd.conf> and are only executed | ||
| 77 | in a real Apache2 httpd environment. They are not executed during testing or via | ||
| 78 | L<Apache2::ASP::API>. | ||
| 79 | |||
| 80 | =head1 ABSTRACT METHODS | ||
| 81 | |||
| 82 | =head2 run( $self, Apache2::ASP::HTTPContext $context ) | ||
| 83 | |||
| 84 | Return C<-1> (or $Response->Declined) to allow the current RequestFilter to be ignored. | ||
| 85 | |||
| 86 | Returning anything else... | ||
| 87 | |||
| 88 | return $Response->Redirect("/unauthorized/"); | ||
| 89 | |||
| 90 | ...results in the termination of the current request right away. | ||
| 91 | |||
| 92 | =head1 BUGS | ||
| 93 | |||
| 94 | It's possible that some bugs have found their way into this release. | ||
| 95 | |||
| 96 | Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Apache2-ASP> to submit bug reports. | ||
| 97 | |||
| 98 | =head1 HOMEPAGE | ||
| 99 | |||
| 100 | Please visit the Apache2::ASP homepage at L<http://www.devstack.com/> to see examples | ||
| 101 | of Apache2::ASP in action. | ||
| 102 | |||
| 103 | =head1 AUTHOR | ||
| 104 | |||
| 105 | John Drago <jdrago_999@yahoo.com> | ||
| 106 | |||
| 107 | =head1 COPYRIGHT AND LICENSE | ||
| 108 | |||
| 109 | Copyright 2007 John Drago, All rights reserved. | ||
| 110 | |||
| 111 | This software is free software. It may be used and distributed under the | ||
| 112 | same terms as Perl itself. | ||
| 113 | |||
| 114 | =cut | ||
| 115 |