$c->request->user --> $c->request->{user}
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication.pm
1 #!/usr/bin/perl
2
3 package Catalyst::Plugin::Authentication;
4
5 use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
6
7 BEGIN {
8     __PACKAGE__->mk_accessors(qw/user/);
9     __PACKAGE__->mk_classdata(qw/default_auth_store/);
10 }
11
12 use strict;
13 use warnings;
14
15 our $VERSION = "0.01";
16
17 sub set_authenticated {
18     my ( $c, $user ) = @_;
19
20     $c->user($user);
21
22     if (    $c->isa("Catalyst::Plugin::Session")
23         and $c->config->{authentication}{use_session} )
24     {
25         $c->session->{__user} = $user->for_session
26           if $user->supports("session");
27         $c->session->{__user_class} = ref $user;
28     }
29 }
30
31 sub logout {
32     my $c = shift;
33
34     $c->user(undef);
35
36     if (    $c->isa("Catalyst::Plugin::Session")
37         and $c->config->{authentication}{use_session} )
38     {
39         delete @{ $c->session }{qw/__user __user_class/};
40     }
41 }
42
43 sub get_user {
44     my ( $c, $uid ) = @_;
45
46     if ( my $store = $c->default_auth_store ) {
47         return $store->get_user($uid);
48     }
49     else {
50         Catalyst::Exception->throw(
51                 "The user id $uid was passed to an authentication "
52               . "plugin, but no default store was specified" );
53     }
54 }
55
56 sub prepare {
57     my $c = shift->NEXT::prepare(@_);
58
59     if (    $c->isa("Catalyst::Plugin::Session")
60         and $c->default_auth_store
61         and !$c->user )
62     {
63         if ( $c->sessionid and my $user = $c->session->{__user} ) {
64             $c->user( $c->session->{__user_class}->from_session( $c, $user ) );
65             $c->request->{user} = $c->user; # compatibility kludge
66         }
67     }
68
69     return $c;
70 }
71
72 sub setup {
73     my $c = shift;
74
75     my $cfg = $c->config->{authentication};
76
77     %$cfg = (
78         use_session => 1,
79         %$cfg,
80     );
81
82     $c->NEXT::setup(@_);
83 }
84
85 __PACKAGE__;
86
87 __END__
88
89 =pod
90
91 =head1 NAME
92
93 Catalyst::Plugin::Authentication - 
94
95 =head1 SYNOPSIS
96
97         use Catalyst qw/
98                 Authentication
99                 Authentication::Store::Foo
100                 Authentication::Credential::Password
101         /;
102
103 =head1 DESCRIPTION
104
105 The authentication plugin is used by the various authentication and
106 authorization plugins in catalyst.
107
108 It defines the notion of a logged in user, and provides integration with the 
109
110 =head1 METHODS
111
112 =over 4 
113
114 =item logout
115
116 Delete the currently logged in user from C<user> and the session.
117
118 =item user
119
120 Returns the currently logged user or undef if there is none.
121
122 =item get_user $uid
123
124 Delegate C<get_user> to the default store.
125
126 =item default_auth_store
127
128 Returns C<< $c->config->{authentication}{store} >>.
129
130 =back
131
132 =head1 INTERNAL METHODS
133
134 =over 4
135
136 =item set_authenticated $user
137
138 Marks a user as authenticated. Should be called from a
139 C<Catalyst::Plugin::Authentication::Credential> plugin after successful
140 authentication.
141
142 This involves setting C<user> and the internal data in C<session> if
143 L<Catalyst::Plugin::Session> is loaded.
144
145 =item prepare
146
147 Revives a user from the session object if there is one.
148
149 =item setup
150
151 Sets the default configuration parameters.
152
153 =item 
154
155 =back
156
157 =head1 CONFIGURATION
158
159 =over 4
160
161 =item use_session
162
163 Whether or not to store the user's logged in state in the session, if the
164 application is also using the L<Catalyst::Plugin::Authentication> plugin.
165
166 =back
167
168 =cut
169
170