- $c -> $_ in Authentication::Credential::Password
[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
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
28sub 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
46Catalyst::Plugin::Authentication::Store::Minimal::Backend - Minimal
47authentication 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
76You probably want L<Catalyst::Plugin::Authentication::Store::Minimal>, unless
77you are mixing several stores in a single app and one of them is Minimal.
78
79Otherwise, this lets you create a store manually.
80
81=head1 METHODS
82
83=over 4
84
85=item new $hash_ref
86
87Constructs a new store object, which uses the supplied hash ref as it's backing
88structure.
89
90=item get_user $id
91
92Keys the hash by $id and returns the value.
93
94If the return value is unblessed it will be blessed as
95L<Catalyst::Plugin::Authentication::User::Hash>.
96
97=item user_supports
98
99Chooses a random user from the hash and delegates to it.
100
101=back
102
103=cut
104
105