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