Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Catalyst controller comparison fails in Root controller auto()

by smallvaluesof2 (Novice)
on Jan 10, 2009 at 23:10 UTC ( [id://735445]=perlquestion: print w/replies, xml ) Need Help??

smallvaluesof2 has asked for the wisdom of the Perl Monks concerning the following question:

In lib/Controller/Root.pm I'm using a standard pattern for automatically redirecting to a login controller if a user is not logged in and the user is not running either the registration or login controllers. lib/Controller/Root.pm
sub auto : Private { my ($self, $c) = @_; if ($c->controller eq $c->controller('Login') || ## The following test eventually starts failing ## while running the register controller: $c->controller eq $c->controller('Register') ) { return 1; } if (!$c->user_exists) { $c->response->redirect($c->uri_for('/login')); } ...

I am tweaking CSS/JS error handling and run the Register page repeatedly with deliberate input errors (empty input field etc). After a few cycles I modify the register controller or the appropriate template/form definition.

After a while the above test fails even though I know (from Catalyst debug output) the Register controller is being invoked and control passes to the Login controller.

Once this happens, it continues to happens until I restart Catalyst. Sometimes the same thing also happens to the login controller so Catalyst loops indefinitely redirecting to the login controller.

I am running Catalyst with the -r option and have been modifying the the register controller. Is it possible that this is introducing some instability?

I can't imagine that Catalyst would be so flaky in production so I can only assume that either the -r option introduces some instability (and I can live with that) or that I am somehow corrupting Catalyst by doing something dumb.

Any thoughts appreciated.

Using Catalyst 5.7014/ CGI::FormBuilder 3.0501 / Ubuntu 8.10 on a dedicated development VM. Running catalyst with the -r option

Replies are listed 'Best First'.
Re: Catalyst controller comparison fails in Root controller auto()
by CountZero (Bishop) on Jan 11, 2009 at 08:43 UTC
    The reload "-r" option is probably the culprit. One should never use it in a production environment and dynamically [un|re]loading modules and controllers is a bit like cutting the branch you are sitting on.

    Does the same thing happen if you run your application without the "-r" option?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      I'm only using the -r option in development, but was initially alarmed at the possible prospect of similar behaviour in production, until I realized that Catalyst would never have become popular with that kind of instability so that the fault had to lie elsewhere (probably in my vicinity).

      Setting up a script to repeatedly submit an incorrectly completed registration page to Catalyst running without -r option for my reassurance.

      It seems like I was not following good practice, so will try an alternative way of achieving the same end.

      Thanks

Re: Catalyst controller comparison fails in Root controller auto()
by hobbs (Monk) on Jan 11, 2009 at 06:10 UTC
    I'm not really sure that comparing the stringified values of controller objects can be considered kosher. Perhaps you could compare the values of action_namespace called on the controllers, or simply change your logic to use $c->req->path instead of $c->controller?
      Hi, thanks for your response. I followed the example in the CPAN catalyst tutorial on authentication, assuming it to be good initial practice. I will try out your suggestions and report back.
        Hi. I hadn't seen that example in the tutorial. It should work, and there may be an underlying bug with -r, but I'm still not convinced that it really represents good practice.

        rafl recommended (in #catalyst IRC) doing the following: add a :NoAuth attribute to your login/register/etc. actions (note: actions, not controllers), and then check

        if ($c->action->attributes->{NoAuth})
        to decide whether to bypass auth.
Re: Catalyst controller comparison fails in Root controller auto()
by Anonymous Monk on Jan 13, 2009 at 15:15 UTC

    Thanks for the suggestions.

    I tried the following in place of directly comparing controllers.
    1. $self->action_namespace;
    2. $c->req->path;
    3. $c->action->attributes->{NoAuth} + :NoAuth

    They all worked but I found using :NoAuth the most applicable to my situation: I have a very small number of actions to permit as not requiring an authenticated session and may wish to reorganize controllers and so want to avoid path dependencies.

    Running catalyst with -r resulted in none of the funnies that prompted my original submission but then I did start and stop it a lot so even if there was a bug, it probably would not have gotten much chance to show itself.

    Thanks, again

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://735445]
Approved by Bloodnok
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-20 16:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found