[Catalyst] how to get controller path
Wade.Stuart at fallon.com
Wade.Stuart at fallon.com
Wed Nov 21 17:55:18 GMT 2007
> Instead of trying to forward to the action that the URI would have
> given them, once they have logged in just redirect them back to the
> stored URL, and let the dispatcher handle it as normal. This also
> avoids problems where you forward to the action they were requesting,
> and they then bookmark that page, which actually bookmarks the login
> page because the URL didn't change with the ->forward().
>
This is what I consider best practice too. The only divergences happen
when you are building out the app to support multiple subsessions or
windows -- in which case you will need to handle the flash/session
placeholder a bit differently to avoid a race situation.
-Wade
> I usually handle it like this:
>
> package MyApp::Controller::Root;
> ...
> sub access_denied : Private {
> my ( $self, $c ) = @_;
>
> if ( $c->user_exists ) {
> $c->abort( 'Access Denied' );
> } else {
> $c->flash->{ 'login_dest' } = $c->request->path;
> $c->response->redirect( '/login' );
> }
> return 0;
> }
>
> sub login : Local {
> my ( $self, $c ) = @_;
>
> my $form = $c->model( 'FormFu' )->load_form( 'login.yml' );
>
> if ( $form->submitted_and_valid ) {
> my $params = $c->request->params;
> my $email = $params->{ 'email' };
> my $pass = $params->{ 'password' };
> if ( $c->login( $email, $pass ) ) {
> $c->response->redirect( $c->flash->{ 'login_dest' } || '/' );
> }
> $c->detach;
> } else {
> $form->form_error_message( 'Incorrect email or password' );
> }
> }
>
>
> package MyApp;
> ...
> sub assert_roles {
> my ( $self, @roles ) = @_;
>
> if ( ! $self->check_any_user_role( @roles ) ) {
> $self->detach( '/access_denied' );
> }
> return 1;
> }
>
> package MyApp::Controller::Admin;
> ...
> sub auto : Private {
> my ( $self, $c ) = @_;
>
> $c->assert_roles(qw( Administrator SuperAdmin ));
> return 1;
> }
>
> --
> Jason Kohles, RHCA RHCDS RHCE
> email at jasonkohles.com - http://www.jasonkohles.com/
> "A witty saying proves nothing." -- Voltaire
>
>
>
> _______________________________________________
> 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.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
More information about the Catalyst
mailing list