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