Commit | Line | Data |
06675d2e |
1 | package Catalyst::Plugin::Authentication::User::Hash; |
06675d2e |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
5afc0dde |
6 | use base qw/Catalyst::Plugin::Authentication::User/; |
7 | |
06675d2e |
8 | sub new { |
c8cdf03d |
9 | my $class = shift; |
06675d2e |
10 | |
1ca38877 |
11 | bless { ( @_ > 1 ) ? @_ : %{ $_[0] } }, $class; |
06675d2e |
12 | } |
13 | |
14 | sub AUTOLOAD { |
15 | my $self = shift; |
16 | ( my $key ) = ( our $AUTOLOAD =~ m/([^:]*)$/ ); |
17 | |
1ca38877 |
18 | $self->_accessor( $key, @_ ); |
96777f3a |
19 | } |
20 | |
52eebd31 |
21 | # this class effectively handles any method calls |
22 | sub can { 1 } |
23 | |
96777f3a |
24 | sub id { |
1ca38877 |
25 | my $self = shift; |
26 | $self->_accessor( "id", @_ ); |
96777f3a |
27 | } |
28 | |
54c8dc06 |
29 | ## deprecated. Let the base class handle this. |
30 | # sub store { |
31 | # my $self = shift; |
32 | # $self->_accessor( "store", @_ ) || ref $self; |
33 | # } |
96777f3a |
34 | |
35 | sub _accessor { |
1ca38877 |
36 | my $self = shift; |
37 | my $key = shift; |
96777f3a |
38 | |
c8cdf03d |
39 | if (@_) { |
40 | my $arr = $self->{__hash_obj_key_is_array}{$key} = @_ > 1; |
41 | $self->{$key} = $arr ? [@_] : shift; |
42 | } |
22be989b |
43 | |
c8cdf03d |
44 | my $data = $self->{$key}; |
0c4ddd06 |
45 | ( $self->{__hash_obj_key_is_array}{$key} || $key =~ /roles/ ) |
da422b3a |
46 | ? @{ $data || [] } |
0c4ddd06 |
47 | : $data; |
06675d2e |
48 | } |
49 | |
45c7644b |
50 | ## password portion of this is no longer necessary, but here for backwards compatibility. |
06675d2e |
51 | my %features = ( |
52 | password => { |
c8cdf03d |
53 | clear => ["password"], |
54 | crypted => ["crypted_password"], |
55 | hashed => [qw/hashed_password hash_algorithm/], |
56 | self_check => undef, |
06675d2e |
57 | }, |
c8cdf03d |
58 | roles => ["roles"], |
12dae309 |
59 | session => 1, |
06675d2e |
60 | ); |
61 | |
62 | sub supports { |
63 | my ( $self, @spec ) = @_; |
64 | |
65 | my $cursor = \%features; |
66 | |
1ca38877 |
67 | return 1 if @spec == 1 and exists $self->{ $spec[0] }; |
68 | |
06675d2e |
69 | # traverse the feature list, |
70 | for (@spec) { |
e5032c48 |
71 | return if ref($cursor) ne "HASH"; |
22be989b |
72 | $cursor = $cursor->{$_}; |
06675d2e |
73 | } |
74 | |
c8cdf03d |
75 | if ( ref $cursor ) { |
76 | die "bad feature spec: @spec" unless ref $cursor eq "ARRAY"; |
06675d2e |
77 | |
c8cdf03d |
78 | # check that all the keys required for a feature are in here |
79 | foreach my $key (@$cursor) { |
80 | return undef unless exists $self->{$key}; |
81 | } |
06675d2e |
82 | |
c8cdf03d |
83 | return 1; |
84 | } |
85 | else { |
86 | return $cursor; |
87 | } |
06675d2e |
88 | } |
89 | |
90 | sub for_session { |
91 | my $self = shift; |
45f78c39 |
92 | |
0862b100 |
93 | return $self; # we serialize the whole user |
06675d2e |
94 | } |
95 | |
12dae309 |
96 | sub from_session { |
1ca38877 |
97 | my ( $self, $c, $user ) = @_; |
98 | $user; |
12dae309 |
99 | } |
100 | |
06675d2e |
101 | __PACKAGE__; |
102 | |
103 | __END__ |
104 | |
105 | =pod |
106 | |
107 | =head1 NAME |
108 | |
109 | Catalyst::Plugin::Authentication::User::Hash - An easy authentication user |
110 | object based on hashes. |
111 | |
112 | =head1 SYNOPSIS |
113 | |
114 | use Catalyst::Plugin::Authentication::User::Hash; |
115 | |
116 | Catalyst::Plugin::Authentication::User::Hash->new( |
117 | password => "s3cr3t", |
118 | ); |
119 | |
120 | =head1 DESCRIPTION |
121 | |
122 | This implementation of authentication user handles is supposed to go hand in |
123 | hand with L<Catalyst::Plugin::Authentication::Store::Minimal>. |
124 | |
125 | =head1 METHODS |
126 | |
5afc0dde |
127 | =head2 new( @pairs ) |
06675d2e |
128 | |
129 | Create a new object with the key-value-pairs listed in the arg list. |
130 | |
5afc0dde |
131 | =head2 supports( ) |
06675d2e |
132 | |
133 | Checks for existence of keys that correspond with features. |
134 | |
5afc0dde |
135 | =head2 for_session( ) |
06675d2e |
136 | |
137 | Just returns $self, expecting it to be serializable. |
138 | |
5afc0dde |
139 | =head2 from_session( ) |
06675d2e |
140 | |
141 | Just passes returns the unserialized object, hoping it's intact. |
142 | |
5afc0dde |
143 | =head2 AUTOLOAD( ) |
06675d2e |
144 | |
145 | Accessor for the key whose name is the method. |
146 | |
5afc0dde |
147 | =head2 id( ) |
4fbe2e14 |
148 | |
5afc0dde |
149 | =head2 store( ) |
4fbe2e14 |
150 | |
151 | Accessors that override superclass's dying virtual methods. |
152 | |
06675d2e |
153 | =head1 SEE ALSO |
154 | |
155 | L<Hash::AsObject> |
156 | |
157 | =cut |
158 | |
159 | |