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