user_exists for C::P::Authen
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication / User / Hash.pm
1 #!/usr/bin/perl
2
3 package Catalyst::Plugin::Authentication::User::Hash;
4 use base qw/Catalyst::Plugin::Authentication::User/;
5
6 use strict;
7 use warnings;
8
9 sub new {
10     my $class = shift;
11
12     bless { ( @_ > 1 ) ? @_ : %{ $_[0] } }, $class;
13 }
14
15 sub AUTOLOAD {
16     my $self = shift;
17     ( my $key ) = ( our $AUTOLOAD =~ m/([^:]*)$/ );
18
19     $self->_accessor( $key, @_ );
20 }
21
22 sub id {
23     my $self = shift;
24     $self->_accessor( "id", @_ );
25 }
26
27 sub store {
28     my $self = shift;
29     ref $self || $self;
30 }
31
32 sub _accessor {
33     my $self = shift;
34     my $key  = shift;
35
36     if (@_) {
37         my $arr = $self->{__hash_obj_key_is_array}{$key} = @_ > 1;
38         $self->{$key} = $arr ? [@_] : shift;
39     }
40
41     my $data = $self->{$key};
42     ( $self->{__hash_obj_key_is_array}{$key} || $key =~ /roles/ )
43       ? @{ $data || [] }
44       : $data;
45 }
46
47 my %features = (
48     password => {
49         clear      => ["password"],
50         crypted    => ["crypted_password"],
51         hashed     => [qw/hashed_password hash_algorithm/],
52         self_check => undef,
53     },
54     roles   => ["roles"],
55     session => 1,
56 );
57
58 sub supports {
59     my ( $self, @spec ) = @_;
60
61     my $cursor = \%features;
62
63     return 1 if @spec == 1 and exists $self->{ $spec[0] };
64
65     # traverse the feature list,
66     for (@spec) {
67         die "bad feature spec: @spec"
68           if ref($cursor) ne "HASH";
69         $cursor = $cursor->{$_};
70     }
71
72     if ( ref $cursor ) {
73         die "bad feature spec: @spec" unless ref $cursor eq "ARRAY";
74
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         }
79
80         return 1;
81     }
82     else {
83         return $cursor;
84     }
85 }
86
87 sub for_session {
88     my $self = shift;
89     return $self;    # let's hope we're serialization happy
90 }
91
92 sub from_session {
93     my ( $self, $c, $user ) = @_;
94     $user;
95 }
96
97 __PACKAGE__;
98
99 __END__
100
101 =pod
102
103 =head1 NAME
104
105 Catalyst::Plugin::Authentication::User::Hash - An easy authentication user
106 object 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
118 This implementation of authentication user handles is supposed to go hand in
119 hand with L<Catalyst::Plugin::Authentication::Store::Minimal>.
120
121 =head1 METHODS
122
123 =over 4
124
125 =item new @pairs
126
127 Create a new object with the key-value-pairs listed in the arg list.
128
129 =item supports
130
131 Checks for existence of keys that correspond with features.
132
133 =item for_session
134
135 Just returns $self, expecting it to be serializable.
136
137 =item from_session
138
139 Just passes returns the unserialized object, hoping it's intact.
140
141 =item AUTOLOAD
142
143 Accessor for the key whose name is the method.
144
145 =item id
146
147 =item store
148
149 Accessors that override superclass's dying virtual methods.
150
151 =back
152
153 =head1 SEE ALSO
154
155 L<Hash::AsObject>
156
157 =cut
158
159