[Catalyst] What's the best way to exclude static requests from needing user to log in?

kakimoto at tpg.com.au kakimoto at tpg.com.au
Sun Apr 19 00:06:51 GMT 2009


hi, all

   what's the best way to exclude static requests from needing the user
to log in?
Some parts of my site are open to general public. For example, the
'contact us', 'services portfolio' and so forth pages.

  At the moment, I have put in codes in my MyApp::Controller::Root->auto
and it seems to want every request to be logged on.

 Hence, identifying which path requests are for my static pages, I have
put in a filter in the  MyApp::Controller::Root->auto method to return a
1 and not go further. 


 Any better way around this? Another way is to have specific methods in
controllers themselves (any CRUD method)  checking if the user was
logged on each time they request a controller action that requires
authentication.


Anyway, here's the source code. Hope it makes sense and thanks, everyone!


------------------ extract - MyApp::Controller::Root->auto method
(start) --------------

sub auto : Private {
    my ($self, $c) = @_;

    # filter out the static requsts
    if ( $c->request->path() =~
m{^(sign_up|subscription_plans|services_portfolio|company_profile|contact_us)$}smx
)
    {   
        return 1;
    }
    elsif ($c->controller eq $c->controller('Login') or
           $c->controller eq $c->controller('Logout')) {
        return 1;
    }
    else{
        unless ($c->user_exists())
        {   
            $c->log->debug(" Root.pm -> auto  - USER's not logged in.
Forcing login and setting 'requested_page' = ". $c->req->path() );
            myApp::Controller::Shared->store_in_session ($c,
                { 'requested_page' => $c->req->path(), }
            );
            $c->response->redirect($c->uri_for('/login'));

            return 0;
        }


      return 1;
    }

------------------ extract - MyApp::Controller::Root->auto method (end)
--------------


 



More information about the Catalyst mailing list