Fix all the tests after merge
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Authentication / User.pm
CommitLineData
e0499ed6 1package Catalyst::Authentication::User;
3583d7ee 2use Moose;
26595d39 3use Scalar::Util qw/refaddr/;
3583d7ee 4use namespace::autoclean;
07e49bc7 5
808fc188 6## auth_realm is the realm this user came from.
3583d7ee 7has [qw/auth_realm store/] => ( is => 'rw' );
808fc188 8
9## THIS IS NOT A COMPLETE CLASS! it is intended to provide base functionality only.
10## translation - it won't work if you try to use it directly.
07e49bc7 11
f869137d 12## chances are you want to override this.
13sub id { shift->get('id'); }
14
6229329e 15## this relies on 'supported_features' being implemented by the subclass..
16## but it is not an error if it is not. it just means you support nothing.
17## nihilist user objects are welcome here.
f1d5a035 18sub supports {
19 my ( $self, @spec ) = @_;
20
6229329e 21 my $cursor = undef;
22 if ($self->can('supported_features')) {
23 $cursor = $self->supported_features;
f1d5a035 24
6229329e 25 # traverse the feature list,
26 for (@spec) {
27 #die "bad feature spec: @spec" if ref($cursor) ne "HASH";
28 return if ref($cursor) ne "HASH";
f1d5a035 29
6229329e 30 $cursor = $cursor->{$_};
31 }
32 }
f1d5a035 33
34 return $cursor;
35}
07e49bc7 36
f869137d 37## REQUIRED.
38## get should return the value of the field specified as it's single argument from the underlying
39## user object. This is here to provide a simple, standard way of accessing individual elements of a user
40## object - ensuring no overlap between C::P::A::User methods and actual fieldnames.
41## this is not the most effecient method, since it uses introspection. If you have an underlying object
42## you most likely want to write this yourself.
43sub get {
44 my ($self, $field) = @_;
26595d39 45
f869137d 46 my $object;
502176fa 47 if ($object = $self->get_object and $object->can($field)) {
f869137d 48 return $object->$field();
49 } else {
50 return undef;
51 }
52}
53
54## REQUIRED.
55## get_object should return the underlying user object. This is for when more advanced uses of the
56## user is required. Modifications to the existing user, etc. Changes in the object returned
57## by this routine may not be reflected in the C::P::A::User object - if this is required, re-authenticating
58## the user is probably the best route to take.
59## note that it is perfectly acceptable to return $self in cases where there is no underlying object.
60sub get_object {
61 return shift;
62}
63
10c0d683 64## obj is shorthand for get_object. This is originally from the DBIx::Class store, but
65## as it has become common usage, this makes things more compatible. Plus, it's shorter.
66sub obj {
67 my $self = shift;
68 return $self->get_object(@_);
69}
70
26595d39 71sub AUTOLOAD {
72 my $self = shift;
73 (my $method) = (our $AUTOLOAD =~ /([^:]+)$/);
74 return if $method eq "DESTROY";
4b607a0e 75 $self->get_object->$method(@_);
76}
f869137d 77
3583d7ee 78__PACKAGE__->meta->make_immutable;
07e49bc7 79__PACKAGE__;
80
81__END__
82
83=pod
84
85=head1 NAME
86
e0499ed6 87Catalyst::Authentication::User - Base class for user objects.
07e49bc7 88
89=head1 SYNOPSIS
90
91 package MyStore::User;
e0499ed6 92 use base qw/Catalyst::Authentication::User/;
07e49bc7 93
94=head1 DESCRIPTION
95
26595d39 96This is the base class for authentication user objects.
97
98THIS IS NOT A COMPLETE CLASS! it is intended to provide base functionality only.
99
100It provides the base methods listed below, and any additional methods
101are proxied onto the user object fetched from the underlieing store.
102
103=head1 NOTES TO STORE IMPLEMENTORS
104
105Please read the comments in the source code of this class to work out
106which methods you should override.
07e49bc7 107
108=head1 METHODS
109
d2ca09b8 110=head2 id( )
07e49bc7 111
112A unique ID by which a user can be retrieved from the store.
113
d2ca09b8 114=head2 store( )
07e49bc7 115
116Should return a class name that can be used to refetch the user using it's
117ID.
118
d2ca09b8 119=head2 supports( )
07e49bc7 120
121An introspection method used to determine what features a user object has, to support credential and authorization plugins.
122
10c0d683 123=head2 get( $field )
124
125Returns the value for the $field provided.
8bcf4f49 126
127=head2 get_object( )
128
26595d39 129Returns the underlying object storing the user data. The return value of this
130method will vary depending
10c0d683 131on the storage module used.
132
133=head2 obj( )
134
135Shorthand for get_object( )
136
26595d39 137=head2 AUTOLOAD
138
139Delegates any unknown methods onto the user object returned by ->obj
140
07e49bc7 141=cut
142