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