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