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