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