5eecd3d5f36c7d06aa1332961d02a7a8659d1e57
[catagits/Catalyst-Authentication-Store-Htpasswd.git] / lib / Catalyst / Plugin / Authentication / Store / Htpasswd / Backend.pm
1 #!/usr/bin/perl
2
3 package Catalyst::Plugin::Authentication::Store::Minimal::Backend;
4
5 use strict;
6 use warnings;
7
8 use Catalyst::Plugin::Authentication::User::Hash;
9 use Scalar::Util ();
10
11 sub new {
12     my ( $class, $hash ) = @_;
13
14     bless { hash => $hash }, $class;
15 }
16
17 sub get_user {
18     my ( $self, $id ) = @_;
19
20         my $user = $self->{hash}{$id};
21
22         bless $user, "Catalyst::Plugin::Authentication::User::Hash"
23           unless Scalar::Util::blessed($user);
24
25     return $user;
26 }
27
28 sub user_supports {
29     my $self = shift;
30
31     # choose a random user
32     scalar keys %{ $self->{hash} };
33     ( undef, my $user ) = each %{ $self->{hash} };
34
35     $user->supports(@_);
36 }
37
38 __PACKAGE__;
39
40 __END__
41
42 =pod
43
44 =head1 NAME
45
46 Catalyst::Plugin::Authentication::Store::Minimal::Backend - Minimal
47 authentication storage backend.
48
49 =head1 SYNOPSIS
50
51     # you probably just want Store::Minimal under most cases,
52     # but if you insist you can instantiate your own store:
53
54     use Catalyst::Plugin::Authentication::Store::Minimal::Backend;
55
56     use Catalyst qw/
57         Authentication
58         Authentication::Credential::Password
59     /;
60
61     my %users = (
62         user => { password => "s3cr3t" },
63     );
64     
65     our $users = Catalyst::Plugin::Authentication::Store::Minimal::Backend->new(\%users);
66
67     sub action : Local {
68         my ( $self, $c ) = @_;
69
70         $c->login( $users->get_user( $c->req->param("login") ),
71             $c->req->param("password") );
72     }
73
74 =head1 DESCRIPTION
75
76 You probably want L<Catalyst::Plugin::Authentication::Store::Minimal>, unless
77 you are mixing several stores in a single app and one of them is Minimal.
78
79 Otherwise, this lets you create a store manually.
80
81 =head1 METHODS
82
83 =over 4
84
85 =item new $hash_ref
86
87 Constructs a new store object, which uses the supplied hash ref as it's backing
88 structure.
89
90 =item get_user $id
91
92 Keys the hash by $id and returns the value.
93
94 If the return value is unblessed it will be blessed as
95 L<Catalyst::Plugin::Authentication::User::Hash>.
96
97 =item user_supports
98
99 Chooses a random user from the hash and delegates to it.
100
101 =back
102
103 =cut
104
105