[Catalyst] Global behaviour modification

Kiffin Gish kiffin.gish at planet.nl
Sat Mar 27 20:47:00 GMT 2010


You might try using an action role similar to the following. If a given
condition arises which requires user intervention before continuing, I
flag that in the user object, let's call it $user->pset.

In your controller:

sub base_check_something : Chained('base') PathPart('')
Does('CheckSomething') CaptureArgs(0) {
    my ( $self, $c ) = @_;
}

In MyApp::ActionRole::CheckSomething:

package MyApp::ActionRole::CheckSomething;
use Moose::Role;
use Moose::Autobox;

before 'execute' => sub {
    my ( $self, $controller, $c, @args ) = @_;

    my $user = $c->stash->{user};
    if ( $user->pset ) {
        if ( defined( $c->session->{redirect_to_after_pset} ) ) {
            my $url = $c->session->{redirect_to_after_pset};
	}
        else {
            # Save the url for redirecting back.
            my $url = $c->uri_for( $c->action, $c->req->captures,
                $c->req->args->flatten, $c->req->parameters );
            $c->session->{redirect_to_after_pset} = $url;
        }
        $c->detach(
            $c->controller('controller_name') ->  
                action_for('action_name'), [ $user->id ] );
    }
};

after 'execute' => sub {
    my ( $self, $controller, $c, @args ) = @_;

    # Do we need to redirect user back?
    if ( defined( $c->session->{redirect_to_after_pset} ) ) {
        my $url = delete $c->session->{redirect_to_after_pset};
        $c->res->redirect($url);
        $c->detach;
    }
};

1;


On Sat, 2010-03-27 at 02:22 -0700, Ovid wrote:
> Hi all,
> 
> What would be the best approach to intercept *everything* when a certain condition occurs and redirect to something like /user/waiting/?  Should I use "before execute" in the lib/MyApp.pm?
> 
> I'm working on an app where users must authenticate (via CatalystX::SimpleLogin and a custom ActionRole).  At times, users may engage in activity which temporarily makes it impossible to take any other action. Hypothetical example: users kick off a job which takes 5 minutes to run, so we restrict their ability to do *anything* else for that 5 minutes. Thus, it would be nice if I could globally redirect any authenticated user to something like /user/waiting/ or something like that.
> 
> Cheers,
> Ovid
> --
> Buy the book         - http://www.oreilly.com/catalog/perlhks/
> Tech blog            - http://blogs.perl.org/users/ovid/
> Twitter              - http://twitter.com/OvidPerl
> Official Perl 6 Wiki - http://www.perlfoundation.org/perl6
> 
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/


-- 
Kiffin Gish <Kiffin.Gish at planet.nl>
Gouda, The Netherlands





More information about the Catalyst mailing list