dzilified this dist
[catagits/Catalyst-Authentication-Store-Htpasswd.git] / lib / Catalyst / Authentication / Store / Htpasswd / User.pm
1 #!/usr/bin/perl
2
3 package Catalyst::Authentication::Store::Htpasswd::User;
4 # ABSTRACT: A user object representing an entry in an htpasswd file.
5
6 use base qw/Catalyst::Authentication::User Class::Accessor::Fast/;
7
8 use strict;
9 use warnings;
10
11 our $VERSION = '1.005';
12
13 BEGIN { __PACKAGE__->mk_accessors(qw/_user _store/) }
14
15 use overload '""' => sub { shift->id }, fallback => 1;
16
17 sub new {
18         my ( $class, $store, $user ) = @_;
19
20         return unless $user;
21
22         bless { _store => $store, _user => $user }, $class;
23 }
24
25 sub id {
26     my $self = shift;
27     return $self->_user->username;
28 }
29
30 sub supported_features {
31         return {
32         password => {
33             self_check => 1,
34                 },
35         session => 1,
36         roles => 1,
37         };
38 }
39
40 sub check_password {
41         my ( $self, $password ) = @_;
42         return $self->_user->check_password( $password );
43 }
44
45 sub roles {
46         my $self = shift;
47         my $field = $self->_user->extra_info->[0];
48         return defined $field ? split /,/, $field : ();
49 }
50
51 *for_session = \&id;
52
53 *get_object = \&_user;
54
55 sub AUTOLOAD {
56         my $self = shift;
57         
58         ( my $method ) = ( our $AUTOLOAD =~ /([^:]+)$/ );
59
60         return if $method eq "DESTROY";
61         
62         $self->_user->$method;
63 }
64
65 1;
66
67 __END__
68
69 =pod
70
71 =head1 DESCRIPTION
72
73 This object wraps an L<Authen::Htpasswd::User> object. An instance of it will be returned
74 by C<< $c->user >> when using L<Catalyst::Authentication::Store::Htpasswd>. Methods 
75 not defined in this module are passed through to the L<Authen::Htpasswd::User> object. The
76 object stringifies to the username.
77
78 =head1 METHODS
79
80 =head2 new($store,$user)
81
82 Creates a new object from a store object, normally an instance of 
83 L<Catalyst::Authentication::Store::Htpasswd::Backend>, and a user object,
84 normally an instance of L<Authen::Htpasswd::User>.
85
86 =head2 id
87
88 Returns the username.
89
90 =head2 check_password($password)
91
92 Returns whether the password is valid.
93
94 =head2 roles
95
96 Returns an array of roles, which is extracted from a comma-separated list in the
97 third field of the htpasswd file.
98
99 =head2 for_session
100
101 Returns the username, which is then stored in the session.
102
103 =head2 supported_features
104
105 Returns data about which featurs this user module supports.
106
107 =head2 get_object
108
109 Returns the underlieing L<Authen::Htpasswd::User> object for this user
110
111 =cut