File Coverage

blib/lib/Catalyst/ActionRole/NeedsLogin.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package Catalyst::ActionRole::NeedsLogin;
2 8     8   1188554 use Moose::Role;
  8         28  
  8         88  
3 8     8   64220 use namespace::autoclean;
  8         26  
  8         100  
4              
5             around execute => sub {
6             my $orig = shift;
7             my $self = shift;
8             my ($controller, $c, @args) = @_;
9              
10             if (!$c->user) {
11             my $message = ($self->attributes->{LoginRedirectMessage}[0])
12             ? $self->attributes->{LoginRedirectMessage}[0]
13             :'You need to login to view this page!';
14             $c->controller('Login')->login_redirect($c, $message, @args);
15             $c->detach;
16             }
17             else {
18             return $self->$orig(@_);
19             }
20             };
21              
22             1;
23              
24             __END__
25              
26             =head1 NAME
27              
28             Catalyst::ActionRole::NeedsLogin - checks if a user is logged in and if not redirects him to login page
29              
30             =head1 SYNOPSIS
31              
32             package MyApp::Controller::NeedsAuth;
33              
34             use Moose;
35             use namespace::autoclean;
36              
37             # One needs to inherit from Catalyst::Controller in order
38             # to get the Does('NeedsLogin') functionality.
39             BEGIN { extends 'Catalyst::Controller'; }
40              
41             sub inbox : Path Does('NeedsLogin') {
42             # Redirects to /login if not logged in
43             my ($self, $c) = @_;
44              
45             $c->stash->{template} = "inbox.tt2";
46              
47             return;
48             }
49              
50             sub inbox : Path Does('NeedsLogin') :LoginRedirectMessage('Your custom Message') {
51             # Redirects to /login if not logged in-
52             }
53              
54             # Turn on in config
55             MyApp->config('Contoller::Login' => { traits => ['WithRedirect'] });
56              
57             =head1 DESCRIPTION
58              
59             Provides a ActionRole for forcing the user to login.
60              
61             =head1 WRAPPED METHODS
62              
63             =head2 execute
64              
65             If there is no logged-in user, call the login_redirect() method in the
66             C<'Login'> controller with the Catalyst context object, $c, and the
67             message specified by the C<:LoginRedirectMessage('Message here')> method
68             attribute (see the synopsis).
69              
70             If there is a user logged-in (i.e: C<< $c->user >> is true), execute the body
71             of the action as it is.
72              
73             =head1 SEE ALSO
74              
75             =over
76              
77             =item L<CatalystX::SimpleLogin::TraitFor::Controller::Login::WithRedirect>
78              
79             =item L<CatalystX::SimpleLogin::Controller::Login>
80              
81             =item L<CatalystX::SimpleLogin::Form::Login>
82              
83             =back
84              
85             =head1 AUTHORS
86              
87             See L<CatalystX::SimpleLogin> for authors.
88              
89             =head1 LICENSE
90              
91             See L<CatalystX::SimpleLogin> for license.
92              
93             =cut
94