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