6 Catalyst::Authentication::Store::LDAP::User
7 - A User object representing an LDAP object.
11 You should be creating these objects through L<Catalyst::Authentication::Store::LDAP::Backend>'s "get_user" method, or just letting $c->authenticate do
15 my ( $self, $c ) = @_;
17 id => $c->req->param(username),
18 password => $c->req->param(password)
20 $c->log->debug($c->user->username . "is really neat!");
23 If you access just $c->user in a scalar context, it will return the current
28 This wraps up an LDAP object and presents a simplified interface to it's
29 contents. It uses some AUTOLOAD magic to pass method calls it doesn't
30 understand through as simple read only accessors for the LDAP entries
33 It gets grumpy if you ask for an attribute via the AUTOLOAD mechanism
34 that it doesn't know about. Avoid that with using "has_attribute",
35 discussed in more detail below.
37 You can skip all that and just go straight to the L<Net::LDAP::Entry>
38 object through the "ldap_entry" method:
40 my $entry = $c->user->ldap_entry;
42 It also has support for Roles.
46 package Catalyst::Authentication::Store::LDAP::User;
47 use base qw( Catalyst::Authentication::User Class::Accessor::Fast );
51 use Scalar::Util qw/refaddr/;
53 our $VERSION = '1.015';
55 BEGIN { __PACKAGE__->mk_accessors(qw/user store/) }
57 use overload '""' => sub { shift->stringify }, fallback => 1;
59 my %_ldap_connection_passwords; # Store inside-out so that they don't show up
64 =head2 new($store, $user, $c)
66 Takes a L<Catalyst::Authentication::Store::LDAP::Backend> object
67 as $store, and the data structure returned by that class's "get_user"
68 method as $user. The final argument is an instance of your application,
69 which is passed along for those wanting to subclass User and perhaps use
70 models for fetching data.
72 Returns a L<Catalyst::Authentication::Store::LDAP::User> object.
77 my ( $class, $store, $user, $c, $roles ) = @_;
81 bless { store => $store, user => $user, _roles => $roles }, $class;
86 Returns the results of the "stringify" method.
92 return $self->stringify;
97 Uses the "user_field" configuration option to determine what the "username"
98 of this object is, and returns it.
100 If you use the special value "dn" for user_field, it will return the DN
101 of the L<Net::LDAP::Entry> object.
107 my $userfield = $self->store->user_field;
108 $userfield = $$userfield[0] if ref $userfield eq 'ARRAY';
109 if ( $userfield eq "dn" ) {
110 my ($string) = $self->user->ldap_entry->dn;
114 my $val = $self->$userfield;
115 return ref($val) eq 'ARRAY' ? $val->[0] : $val;
119 =head2 supported_features
121 Returns hashref of features that this Authentication::User subclass supports.
125 sub supported_features {
127 password => { self_check => 1, },
129 roles => { self_check => 0, },
133 =head2 check_password($password)
135 Bind's to the directory as the DN of the internal L<Net::LDAP::Entry> object,
136 using the bind password supplied in $password. Returns 1 on a successful
142 my ( $self, $password ) = @_;
143 if ( $self->store->ldap_auth($self->ldap_entry->dn, $password) ) {
144 # Stash a closure which can be used to retrieve the connection in the users context later.
145 $_ldap_connection_passwords{refaddr($self)} = $password;
155 Returns the results of L<Catalyst::Authentication::Store::LDAP::Backend>'s "lookup_roles" method, an array of roles that are valid for this user.
161 $self->{_roles} ||= [$self->store->lookup_roles($self)];
162 return @{$self->{_roles}};
167 Returns the user for persistence in the session depending on the
168 persist_in_session config option.
175 if ( $self->store->persist_in_session eq 'all' ) {
176 # use the roles accessor to ensure the roles are fetched
177 return { user => $self->user, _roles => [ $self->roles ] };
180 return $self->stringify;
185 Returns the raw ldap_entry.
191 return $self->user->{'ldap_entry'};
194 =head2 attributes($type)
196 Returns an array of attributes present for this user. If $type is "ashash",
197 it will return a hash with the attribute names as keys. (And the values of
198 those attributes as, well, the values of the hash)
203 my ( $self, $type ) = @_;
204 if ( $type eq "ashash" ) {
205 return $self->user->{'attributes'};
208 return keys( %{ $self->user->{'attributes'} } );
214 Returns the values for an attribute, or undef if that attribute is not present.
215 The safest way to get at an attribute.
220 my ( $self, $attribute ) = @_;
221 if ( !defined($attribute) ) {
222 Catalyst::Exception->throw(
223 "You must provide an attribute to has_attribute!");
225 if ( $attribute eq "dn" ) {
226 return $self->ldap_entry->dn;
228 elsif ( $attribute eq "username" ) {
229 return $self->user->{'attributes'}->{$self->store->user_field};
231 elsif ( exists( $self->user->{'attributes'}->{$attribute} ) ) {
232 return $self->user->{'attributes'}->{$attribute};
241 A simple wrapper around has_attribute() to satisfy the Catalyst::Authentication::User API.
245 sub get { return shift->has_attribute(@_) }
249 Satisfies the Catalyst::Authentication::User API and returns the contents of the user()
254 sub get_object { return shift->user }
256 =head2 ldap_connection
258 Re-binds to the auth store with the credentials of the user you logged in
259 as, and returns a L<Net::LDAP> object which you can use to do further queries.
263 sub ldap_connection {
265 $self->store->ldap_bind( undef, $self->ldap_entry->dn,
266 $_ldap_connection_passwords{refaddr($self)} );
269 =head2 AUTOLOADed methods
271 We automatically map the attributes of the underlying L<Net::LDAP::Entry>
272 object to read-only accessor methods. So, if you have an entry that looks
275 dn: cn=adam,ou=users,dc=yourcompany,dc=com
278 homeDirectory: /home/adam
282 mail: adam@yourcompany.com
286 objectClass: inetOrgPerson
287 objectClass: organizationalPerson
290 objectClass: posixAccount
294 $c->user->homedirectory
296 And you'll get the value of the "homeDirectory" attribute. Note that
297 all the AUTOLOADed methods are automatically lower-cased.
299 =head2 Special Keywords
301 The highly useful and common method "username" will map to the configured
302 value of user_field (uid by default.)
304 $c->user->username == $c->user->uid
310 # Don't leak passwords..
311 delete $_ldap_connection_passwords{refaddr($self)};
315 my ($self, $method) = @_;
317 return $self->SUPER::can($method) || do {
318 return unless $self->has_attribute($method);
319 return sub { $_[0]->has_attribute($method) };
326 ( my $method ) = ( our $AUTOLOAD =~ /([^:]+)$/ );
328 if ( $method eq "DESTROY" ) {
332 if ( my $attribute = $self->has_attribute($method) ) {
336 Catalyst::Exception->throw(
337 "No attribute $method for User " . $self->stringify );
347 Adam Jacob <holoway@cpan.org>
349 Some parts stolen shamelessly and entirely from
350 L<Catalyst::Plugin::Authentication::Store::Htpasswd>.
352 Currently maintained by Peter Karman <karman@cpan.org>.
356 To nothingmuch, ghenry, castaway and the rest of #catalyst for the help. :)
360 L<Catalyst::Authentication::Store::LDAP>, L<Catalyst::Authentication::Store::LDAP::Backend>, L<Catalyst::Plugin::Authentication>, L<Net::LDAP>
362 =head1 COPYRIGHT & LICENSE
364 Copyright (c) 2005 the aforementioned authors. All rights
365 reserved. This program is free software; you can redistribute
366 it and/or modify it under the same terms as Perl itself.