User::Hash prefers id/store based serialization if it's possible
[catagits/Catalyst-Plugin-Authentication.git] / lib / Catalyst / Plugin / Authentication / Store / Minimal / Backend.pm
CommitLineData
06675d2e 1#!/usr/bin/perl
2
3package Catalyst::Plugin::Authentication::Store::Minimal::Backend;
4
5use strict;
6use warnings;
7
8use Catalyst::Plugin::Authentication::User::Hash;
9use Scalar::Util ();
10
11sub new {
12 my ( $class, $hash ) = @_;
13
14 bless { hash => $hash }, $class;
15}
16
96777f3a 17sub from_session {
18 my ( $self, $c, $id ) = @_;
19
20 return $id if ref $id;
21
22 $self->get_user( $id );
23}
24
06675d2e 25sub get_user {
26 my ( $self, $id ) = @_;
27
0c4ddd06 28 return unless exists $self->{hash}{$id};
29
30 my $user = $self->{hash}{$id};
31
32 if ( ref $user ) {
33 if ( Scalar::Util::blessed($user) ) {
96777f3a 34 $user->store( $self );
35 $user->id( $id );
0c4ddd06 36 return $user;
37 }
38 elsif ( ref $user eq "HASH" ) {
87ca1824 39 $user->{id} ||= $id;
40 $user->{store} ||= $self;
0c4ddd06 41 return bless $user, "Catalyst::Plugin::Authentication::User::Hash";
42 }
43 else {
44 Catalyst::Exception->throw( "The user '$id' is a reference of type "
45 . ref($user)
46 . " but should be a HASH" );
47 }
48 }
49 else {
50 Catalyst::Exception->throw(
51 "The user '$id' is has to be a hash reference or an object");
52 }
06675d2e 53
54 return $user;
55}
56
57sub user_supports {
58 my $self = shift;
59
60 # choose a random user
61 scalar keys %{ $self->{hash} };
62 ( undef, my $user ) = each %{ $self->{hash} };
63
64 $user->supports(@_);
65}
66
67__PACKAGE__;
68
69__END__
70
71=pod
72
73=head1 NAME
74
75Catalyst::Plugin::Authentication::Store::Minimal::Backend - Minimal
76authentication storage backend.
77
78=head1 SYNOPSIS
79
80 # you probably just want Store::Minimal under most cases,
81 # but if you insist you can instantiate your own store:
82
83 use Catalyst::Plugin::Authentication::Store::Minimal::Backend;
84
85 use Catalyst qw/
86 Authentication
87 Authentication::Credential::Password
88 /;
89
90 my %users = (
91 user => { password => "s3cr3t" },
92 );
93
94 our $users = Catalyst::Plugin::Authentication::Store::Minimal::Backend->new(\%users);
95
96 sub action : Local {
97 my ( $self, $c ) = @_;
98
99 $c->login( $users->get_user( $c->req->param("login") ),
100 $c->req->param("password") );
101 }
102
103=head1 DESCRIPTION
104
105You probably want L<Catalyst::Plugin::Authentication::Store::Minimal>, unless
106you are mixing several stores in a single app and one of them is Minimal.
107
108Otherwise, this lets you create a store manually.
109
110=head1 METHODS
111
112=over 4
113
114=item new $hash_ref
115
116Constructs a new store object, which uses the supplied hash ref as it's backing
117structure.
118
119=item get_user $id
120
121Keys the hash by $id and returns the value.
122
123If the return value is unblessed it will be blessed as
124L<Catalyst::Plugin::Authentication::User::Hash>.
125
4fbe2e14 126=item from_session $id
127
128Delegates to C<get_user>.
129
06675d2e 130=item user_supports
131
132Chooses a random user from the hash and delegates to it.
133
134=back
135
136=cut
137
138