Swap tabs for space
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Authentication / User / Hash.pm
CommitLineData
5c5af345 1package Catalyst::Authentication::User::Hash;
06675d2e 2
3use strict;
4use warnings;
5
5c5af345 6use base qw/Catalyst::Authentication::User/;
5afc0dde 7
06675d2e 8sub new {
c8cdf03d 9 my $class = shift;
06675d2e 10
1ca38877 11 bless { ( @_ > 1 ) ? @_ : %{ $_[0] } }, $class;
06675d2e 12}
13
14sub 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
22sub can { 1 }
23
96777f3a 24sub 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
35sub _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 51my %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
62sub 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
90sub for_session {
91 my $self = shift;
45f78c39 92
0862b100 93 return $self; # we serialize the whole user
06675d2e 94}
95
12dae309 96sub 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
5c5af345 109Catalyst::Authentication::User::Hash - An easy authentication user
06675d2e 110object based on hashes.
111
112=head1 SYNOPSIS
113
675fe850 114 use Catalyst::Authentication::User::Hash;
115
116 Catalyst::Authentication::User::Hash->new(
117 password => "s3cr3t",
118 );
06675d2e 119
120=head1 DESCRIPTION
121
122This implementation of authentication user handles is supposed to go hand in
5c5af345 123hand with L<Catalyst::Authentication::Store::Minimal>.
06675d2e 124
125=head1 METHODS
126
5afc0dde 127=head2 new( @pairs )
06675d2e 128
129Create a new object with the key-value-pairs listed in the arg list.
130
5afc0dde 131=head2 supports( )
06675d2e 132
133Checks for existence of keys that correspond with features.
134
5afc0dde 135=head2 for_session( )
06675d2e 136
137Just returns $self, expecting it to be serializable.
138
5afc0dde 139=head2 from_session( )
06675d2e 140
141Just passes returns the unserialized object, hoping it's intact.
142
5afc0dde 143=head2 AUTOLOAD( )
06675d2e 144
145Accessor for the key whose name is the method.
146
5afc0dde 147=head2 store( )
4fbe2e14 148
149Accessors that override superclass's dying virtual methods.
150
078c2289 151=head2 id( )
152
153=head2 can( )
154
06675d2e 155=head1 SEE ALSO
156
157L<Hash::AsObject>
158
159=cut
160
161