Commit | Line | Data |
5c5af345 |
1 | package Catalyst::Authentication::User; |
202af0af |
2 | use Moose; |
3 | use namespace::autoclean; |
06675d2e |
4 | |
202af0af |
5 | with 'MooseX::Emulate::Class::Accessor::Fast'; |
4b33bb96 |
6 | use Scalar::Util qw/refaddr/; |
06675d2e |
7 | |
202af0af |
8 | ## auth_realm is the realm this user came from. |
4b33bb96 |
9 | __PACKAGE__->mk_accessors(qw/auth_realm store/); |
45c7644b |
10 | |
202af0af |
11 | ## THIS IS NOT A COMPLETE CLASS! it is intended to provide base functionality only. |
45c7644b |
12 | ## translation - it won't work if you try to use it directly. |
06675d2e |
13 | |
54c8dc06 |
14 | ## chances are you want to override this. |
15 | sub id { shift->get('id'); } |
16 | |
202af0af |
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. |
649de93b |
19 | ## nihilist user objects are welcome here. |
6c8b6e5e |
20 | sub 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 | } |
202af0af |
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. |
45 | sub 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. |
202af0af |
57 | ## get_object should return the underlying user object. This is for when more advanced uses of the |
54c8dc06 |
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. |
62 | sub get_object { |
63 | return shift; |
64 | } |
65 | |
202af0af |
66 | ## obj is shorthand for get_object. This is originally from the DBIx::Class store, but |
a2fb5d49 |
67 | ## as it has become common usage, this makes things more compatible. Plus, it's shorter. |
68 | sub obj { |
69 | my $self = shift; |
70 | return $self->get_object(@_); |
71 | } |
72 | |
4b33bb96 |
73 | sub 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 |
93 | Catalyst::Authentication::User - Base class for user objects. |
06675d2e |
94 | |
95 | =head1 SYNOPSIS |
96 | |
675fe850 |
97 | package MyStore::User; |
98 | use base qw/Catalyst::Authentication::User/; |
06675d2e |
99 | |
100 | =head1 DESCRIPTION |
101 | |
4b33bb96 |
102 | This is the base class for authentication user objects. |
103 | |
104 | THIS IS NOT A COMPLETE CLASS! it is intended to provide base functionality only. |
105 | |
106 | It provides the base methods listed below, and any additional methods |
107 | are proxied onto the user object fetched from the underlieing store. |
108 | |
109 | =head1 NOTES TO STORE IMPLEMENTORS |
110 | |
111 | Please read the comments in the source code of this class to work out |
112 | which methods you should override. |
06675d2e |
113 | |
114 | =head1 METHODS |
115 | |
5afc0dde |
116 | =head2 id( ) |
06675d2e |
117 | |
118 | A unique ID by which a user can be retrieved from the store. |
119 | |
5afc0dde |
120 | =head2 store( ) |
06675d2e |
121 | |
122 | Should return a class name that can be used to refetch the user using it's |
123 | ID. |
124 | |
5afc0dde |
125 | =head2 supports( ) |
06675d2e |
126 | |
127 | An introspection method used to determine what features a user object has, to support credential and authorization plugins. |
128 | |
a2fb5d49 |
129 | =head2 get( $field ) |
130 | |
131 | Returns the value for the $field provided. |
078c2289 |
132 | |
133 | =head2 get_object( ) |
134 | |
4b33bb96 |
135 | Returns the underlying object storing the user data. The return value of this |
136 | method will vary depending |
a2fb5d49 |
137 | on the storage module used. |
138 | |
139 | =head2 obj( ) |
140 | |
141 | Shorthand for get_object( ) |
142 | |
4b33bb96 |
143 | =head2 AUTOLOAD |
144 | |
145 | Delegates any unknown methods onto the user object returned by ->obj |
146 | |
06675d2e |
147 | =cut |
148 | |