Moving AuthRealmTestApp.pm to proper directory
[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;
87ca1824 93 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 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
109Catalyst::Plugin::Authentication::User::Hash - An easy authentication user
110object based on hashes.
111
112=head1 SYNOPSIS
113
114 use Catalyst::Plugin::Authentication::User::Hash;
115
116 Catalyst::Plugin::Authentication::User::Hash->new(
117 password => "s3cr3t",
118 );
119
120=head1 DESCRIPTION
121
122This implementation of authentication user handles is supposed to go hand in
123hand with L<Catalyst::Plugin::Authentication::Store::Minimal>.
124
125=head1 METHODS
126
127=over 4
128
129=item new @pairs
130
131Create a new object with the key-value-pairs listed in the arg list.
132
133=item supports
134
135Checks for existence of keys that correspond with features.
136
137=item for_session
138
139Just returns $self, expecting it to be serializable.
140
141=item from_session
142
143Just passes returns the unserialized object, hoping it's intact.
144
145=item AUTOLOAD
146
147Accessor for the key whose name is the method.
148
4fbe2e14 149=item id
150
151=item store
152
153Accessors that override superclass's dying virtual methods.
154
06675d2e 155=back
156
157=head1 SEE ALSO
158
159L<Hash::AsObject>
160
161=cut
162
163