$c->request->user --> $c->request->{user}
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication.pm
CommitLineData
06675d2e 1#!/usr/bin/perl
2
3package Catalyst::Plugin::Authentication;
4
b003080b 5use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
06675d2e 6
b003080b 7BEGIN {
8 __PACKAGE__->mk_accessors(qw/user/);
9 __PACKAGE__->mk_classdata(qw/default_auth_store/);
10}
06675d2e 11
12use strict;
13use warnings;
14
c7c003d3 15our $VERSION = "0.01";
16
06675d2e 17sub 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 {
7d0922d8 25 $c->session->{__user} = $user->for_session
057c596f 26 if $user->supports("session");
06675d2e 27 $c->session->{__user_class} = ref $user;
28 }
29}
30
31sub logout {
32 my $c = shift;
33
34 $c->user(undef);
b003080b 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 }
06675d2e 41}
42
7d0922d8 43sub 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
06675d2e 56sub prepare {
57 my $c = shift->NEXT::prepare(@_);
58
59 if ( $c->isa("Catalyst::Plugin::Session")
22be989b 60 and $c->default_auth_store
06675d2e 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 ) );
6c983e53 65 $c->request->{user} = $c->user; # compatibility kludge
06675d2e 66 }
67 }
68
69 return $c;
70}
71
72sub setup {
73 my $c = shift;
74
75 my $cfg = $c->config->{authentication};
76
77 %$cfg = (
78 use_session => 1,
79 %$cfg,
80 );
b003080b 81
82 $c->NEXT::setup(@_);
06675d2e 83}
84
85__PACKAGE__;
86
87__END__
88
89=pod
90
91=head1 NAME
92
93Catalyst::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
105The authentication plugin is used by the various authentication and
106authorization plugins in catalyst.
107
108It 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
116Delete the currently logged in user from C<user> and the session.
117
118=item user
119
120Returns the currently logged user or undef if there is none.
121
7d0922d8 122=item get_user $uid
123
124Delegate C<get_user> to the default store.
125
126=item default_auth_store
127
128Returns C<< $c->config->{authentication}{store} >>.
129
06675d2e 130=back
131
132=head1 INTERNAL METHODS
133
134=over 4
135
136=item set_authenticated $user
137
138Marks a user as authenticated. Should be called from a
139C<Catalyst::Plugin::Authentication::Credential> plugin after successful
140authentication.
141
142This involves setting C<user> and the internal data in C<session> if
143L<Catalyst::Plugin::Session> is loaded.
144
145=item prepare
146
147Revives a user from the session object if there is one.
148
149=item setup
150
151Sets the default configuration parameters.
152
153=item
154
155=back
156
157=head1 CONFIGURATION
158
159=over 4
160
161=item use_session
162
163Whether or not to store the user's logged in state in the session, if the
164application is also using the L<Catalyst::Plugin::Authentication> plugin.
165
166=back
167
168=cut
169
170