Add obj() as shorthand method for get_object()
[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/;
06675d2e 6
45c7644b 7## auth_realm is the realm this user came from.
8BEGIN {
815ee585 9 __PACKAGE__->mk_accessors(qw/auth_realm store/);
45c7644b 10}
11
12## THIS IS NOT A COMPLETE CLASS! it is intended to provide base functionality only.
13## translation - it won't work if you try to use it directly.
06675d2e 14
54c8dc06 15## chances are you want to override this.
16sub id { shift->get('id'); }
17
649de93b 18## this relies on 'supported_features' being implemented by the subclass..
19## but it is not an error if it is not. it just means you support nothing.
20## nihilist user objects are welcome here.
6c8b6e5e 21sub supports {
22 my ( $self, @spec ) = @_;
23
649de93b 24 my $cursor = undef;
25 if ($self->can('supported_features')) {
26 $cursor = $self->supported_features;
6c8b6e5e 27
649de93b 28 # traverse the feature list,
29 for (@spec) {
30 #die "bad feature spec: @spec" if ref($cursor) ne "HASH";
31 return if ref($cursor) ne "HASH";
6c8b6e5e 32
649de93b 33 $cursor = $cursor->{$_};
34 }
35 }
6c8b6e5e 36
37 return $cursor;
38}
06675d2e 39
54c8dc06 40## REQUIRED.
41## get should return the value of the field specified as it's single argument from the underlying
42## user object. This is here to provide a simple, standard way of accessing individual elements of a user
43## object - ensuring no overlap between C::P::A::User methods and actual fieldnames.
44## this is not the most effecient method, since it uses introspection. If you have an underlying object
45## you most likely want to write this yourself.
46sub get {
47 my ($self, $field) = @_;
48
49 my $object;
52eebd31 50 if ($object = $self->get_object and $object->can($field)) {
54c8dc06 51 return $object->$field();
52 } else {
53 return undef;
54 }
55}
56
57## REQUIRED.
58## get_object should return the underlying user object. This is for when more advanced uses of the
59## user is required. Modifications to the existing user, etc. Changes in the object returned
60## by this routine may not be reflected in the C::P::A::User object - if this is required, re-authenticating
61## the user is probably the best route to take.
62## note that it is perfectly acceptable to return $self in cases where there is no underlying object.
63sub get_object {
64 return shift;
65}
66
a2fb5d49 67## obj is shorthand for get_object. This is originally from the DBIx::Class store, but
68## as it has become common usage, this makes things more compatible. Plus, it's shorter.
69sub obj {
70 my $self = shift;
71 return $self->get_object(@_);
72}
73
54c8dc06 74## Backwards Compatibility
75## you probably want auth_realm, in fact. but this does work for backwards compatibility.
815ee585 76## store should be a read-write accessor - so it was moved to mk_accessors
77##sub store {
78## my ($self) = @_;
79## return $self->auth_realm->{store};
80##}
54c8dc06 81
06675d2e 82__PACKAGE__;
83
84__END__
85
86=pod
87
88=head1 NAME
89
5c5af345 90Catalyst::Authentication::User - Base class for user objects.
06675d2e 91
92=head1 SYNOPSIS
93
94 package MyStore::User;
5c5af345 95 use base qw/Catalyst::Authentication::User/;
06675d2e 96
97=head1 DESCRIPTION
98
99This is the base class for authenticated
100
101=head1 METHODS
102
5afc0dde 103=head2 id( )
06675d2e 104
105A unique ID by which a user can be retrieved from the store.
106
5afc0dde 107=head2 store( )
06675d2e 108
109Should return a class name that can be used to refetch the user using it's
110ID.
111
5afc0dde 112=head2 supports( )
06675d2e 113
114An introspection method used to determine what features a user object has, to support credential and authorization plugins.
115
a2fb5d49 116=head2 get( $field )
117
118Returns the value for the $field provided.
078c2289 119
120=head2 get_object( )
121
a2fb5d49 122Returns the underlying object storing the user data. The return value of this function will vary depending
123on the storage module used.
124
125=head2 obj( )
126
127Shorthand for get_object( )
128
06675d2e 129=cut
130