[cgiapp] Routes and get_current_runmode()

P Kishor punk.kish at gmail.com
Mon Aug 10 16:35:09 EDT 2009


I am experiencing a problem with C::A::P::Routes, and I think I know
what is going wrong, but I don’t know why or how to fix it.

Background: I have implemented my application with C::A::Dispatch and
C::A::P::Authentication, and it works well. For various reasons I like
C::A::P::Routes, and would like to use it instead of C::A::Dispatch.
However, C::A::P::Routes doesn’t seem to respect the protected modes
set up by C::A::P::Authentication, and shows the protected modes
without letting the login form kick in.

So, I investigated and discovered that C::A::P::Routes has a
disconnect between what it thinks is the runmode and the value of
$self->get_current_runmode. Here is my test --

sub setup {
    my $self = shift;

	$self->start_mode('welcome');
 	$self->routes([
 	
        # unprotected runmodes
        '/welcome' => 'welcome',
        '/login'   => 'login',

        # protected runmodes
        '/mainpage'=> 'mainpage',
 	]);
 	
    $self->param(protected_runmodes => ['mainpage']);
}

sub cgiapp_prerun {
    my $self = shift;

    $self->session->param('__RUN_MODE1', $self->get_current_runmode());
    my $protected_runmodes = $self->param('protected_runmodes');
    foreach my $rm (@$protected_runmodes) {
        if ($self->get_current_runmode() eq $rm) {
            $self->prerun_mode('login')
unless($self->session->param('__LOGGED_IN'));
            last;
        }
    }
	#$self->prerun_mode('login');
    $self->session->param('__RUN_MODE2', $self->get_current_runmode());
}

sub mainpage {
    my $self = shift;

    my $tmpl = $self->load_tmpl('mainpage.html');
    $tmpl->param(runmode1 => $self->session->param('__RUN_MODE1'));
    $tmpl->param(runmode2 => $self->session->param('__RUN_MODE2'));
    $tmpl->param(debug => $self->routes_dbg); #dumps all the
C::A::P::Routes info
    return $tmpl->output;
}

And, when I point my browser to ‘http://<myapp>/mainpage’ this is what I get

Main Page!

You are now logged in!

Runmode1: welcome

Runmode2: welcome

Debug: $VAR1 = { 'rule_matched: ' => '/mainpage', 'parsed_params: ' =>
{}, 'path_received: ' => '/mainpage', 'runmode: ' => 'mainpage' };

See what is going on -- C::A::P::Routes correctly thinks the runmode
is ‘mainpage’ as per the URL. However, $self->get_current_runmode()
returns ‘welcome’ in cgiapp_prerun(), and as a result, the mainpage is
not protected. Now, it could be that C::A::P::Routes is firing *after*
my cgiapp_prerun() protected_mode logic is run. To test that, I
uncommented the #$self->prerun_mode('login'); line in cgiapp_prerun(),
and lo and behold, the runmode is changed to login.

The remedy seems to be that once it has figured out the runmode
correctly, C::A::P::Routes should set the value of
$self->get_current_runmode() to it.

If I have diagnosed the problem correctly, please help me find a fix.
If not, please help me diagnose correctly.

Many thanks,


-- 
Puneet Kishor http://www.punkish.org
Carbon Model http://carbonmodel.org
Charter Member, Open Source Geospatial Foundation http://www.osgeo.org
Science Commons Fellow, http://sciencecommons.org/about/whoweare/kishor
Nelson Institute, UW-Madison http://www.nelson.wisc.edu
-----------------------------------------------------------------------
Assertions are politics; backing up assertions with evidence is science
=======================================================================
Sent from Madison, WI, United States


More information about the cgiapp mailing list