Perltidy User/Hash.pm
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication / User / Hash.pm
CommitLineData
07e49bc7 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 {
56bb3e6b 10 my $class = shift;
07e49bc7 11
56bb3e6b 12 bless {@_}, $class;
07e49bc7 13}
14
15sub AUTOLOAD {
16 my $self = shift;
17 ( my $key ) = ( our $AUTOLOAD =~ m/([^:]*)$/ );
18
56bb3e6b 19 if (@_) {
20 my $arr = $self->{__hash_obj_key_is_array}{$key} = @_ > 1;
21 $self->{$key} = $arr ? [@_] : shift;
22 }
1bd3c97f 23
56bb3e6b 24 my $data = $self->{$key};
1bd3c97f 25 $self->{__hash_obj_key_is_array}{$key} ? @$data : $data;
07e49bc7 26}
27
28my %features = (
29 password => {
56bb3e6b 30 clear => ["password"],
31 crypted => ["crypted_password"],
32 hashed => [qw/hashed_password hash_algorithm/],
33 self_check => undef,
07e49bc7 34 },
56bb3e6b 35 roles => ["roles"],
07e49bc7 36 session => 1,
37);
38
39sub supports {
40 my ( $self, @spec ) = @_;
41
42 my $cursor = \%features;
43
44 # traverse the feature list,
45 for (@spec) {
46 die "bad feature spec: @spec"
1bd3c97f 47 if ref($cursor) ne "HASH";
48 $cursor = $cursor->{$_};
07e49bc7 49 }
50
56bb3e6b 51 if ( ref $cursor ) {
52 die "bad feature spec: @spec" unless ref $cursor eq "ARRAY";
07e49bc7 53
56bb3e6b 54 # check that all the keys required for a feature are in here
55 foreach my $key (@$cursor) {
56 return undef unless exists $self->{$key};
57 }
07e49bc7 58
56bb3e6b 59 return 1;
60 }
61 else {
62 return $cursor;
63 }
07e49bc7 64}
65
66sub for_session {
67 my $self = shift;
68
69 return $self; # let's hope we're serialization happy
70}
71
72sub from_session {
73 my ( $self, $c, $user ) = @_;
74
75 return $user; # if we're serialization happy this should work
76}
77
78__PACKAGE__;
79
80__END__
81
82=pod
83
84=head1 NAME
85
86Catalyst::Plugin::Authentication::User::Hash - An easy authentication user
87object based on hashes.
88
89=head1 SYNOPSIS
90
91 use Catalyst::Plugin::Authentication::User::Hash;
92
93 Catalyst::Plugin::Authentication::User::Hash->new(
94 password => "s3cr3t",
95 );
96
97=head1 DESCRIPTION
98
99This implementation of authentication user handles is supposed to go hand in
100hand with L<Catalyst::Plugin::Authentication::Store::Minimal>.
101
102=head1 METHODS
103
104=over 4
105
106=item new @pairs
107
108Create a new object with the key-value-pairs listed in the arg list.
109
110=item supports
111
112Checks for existence of keys that correspond with features.
113
114=item for_session
115
116Just returns $self, expecting it to be serializable.
117
118=item from_session
119
120Just passes returns the unserialized object, hoping it's intact.
121
122=item AUTOLOAD
123
124Accessor for the key whose name is the method.
125
126=back
127
128=head1 SEE ALSO
129
130L<Hash::AsObject>
131
132=cut
133
134