| 1 | package MyApp::Base::Web::Controller; |
|---|
| 2 | use base 'Catalyst::Controller'; |
|---|
| 3 | |
|---|
| 4 | # Note that 'auto' runs after 'begin' but before your actions and that |
|---|
| 5 | # 'auto' "chain" (all from application path to most specific class are run) |
|---|
| 6 | # See the 'Actions' section of 'Catalyst::Manual::Intro' for more info. |
|---|
| 7 | sub auto : Private { |
|---|
| 8 | my ( $self, $c ) = @_; |
|---|
| 9 | |
|---|
| 10 | # Allow unauthenticated users to reach the login page. This |
|---|
| 11 | # allows anauthenticated users to reach any action in the Auth |
|---|
| 12 | # controller. To lock it down to a single action, we could use: |
|---|
| 13 | # if ($c->action eq $c->controller('Login')->action_for('index')) |
|---|
| 14 | # to only allow unauthenticated access to the C<index> action we |
|---|
| 15 | # added above. |
|---|
| 16 | if ( $c->controller eq $c->controller('Auth') ) { |
|---|
| 17 | return 1; |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | # If a user doesn't exist, force login |
|---|
| 21 | if ( !$c->user_exists ) { |
|---|
| 22 | |
|---|
| 23 | # Dump a log message to the development server debug output |
|---|
| 24 | $c->log->debug('***User not found, forwarding to /login'); |
|---|
| 25 | |
|---|
| 26 | # Redirect the user to the login page |
|---|
| 27 | $c->response->redirect( $c->uri_for('/auth/login') ); |
|---|
| 28 | |
|---|
| 29 | # Return 0 to cancel 'post-auto' processing and prevent use of application |
|---|
| 30 | return 0; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | $c->log->debug('***User found'); |
|---|
| 34 | # User found, so return 1 to continue with processing after this 'auto' |
|---|
| 35 | return 1; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | sub service: Private { |
|---|
| 39 | my ($self, $service_name) = @_; |
|---|
| 40 | return MyApp::ServiceContainer->get($service_name); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | 1; |
|---|