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 );
52 our $VERSION = '0.1004';
54 BEGIN { __PACKAGE__->mk_accessors(qw/user store _ldap_connection_password/) }
56 use overload '""' => sub { shift->stringify }, fallback => 1;
60 =head2 new($store, $user)
62 Takes a L<Catalyst::Authentication::Store::LDAP::Backend> object
63 as $store, and the data structure returned by that class's "get_user"
66 Returns a L<Catalyst::Authentication::Store::LDAP::User> object.
71 my ( $class, $store, $user ) = @_;
75 bless { store => $store, user => $user, }, $class;
80 Returns the results of the "stringify" method.
86 return $self->stringify;
91 Uses the "user_field" configuration option to determine what the "username"
92 of this object is, and returns it.
94 If you use the special value "dn" for user_field, it will return the DN
95 of the L<Net::LDAP::Entry> object.
101 my $userfield = $self->store->user_field;
102 $userfield = $$userfield[0] if ref $userfield eq 'ARRAY';
103 if ( $userfield eq "dn" ) {
104 my ($string) = $self->user->ldap_entry->dn;
108 my ($string) = $self->$userfield;
113 =head2 supported_features
115 Returns hashref of features that this Authentication::User subclass supports.
119 sub supported_features {
121 password => { self_check => 1, },
123 roles => { self_check => 0, },
127 =head2 check_password($password)
129 Bind's to the directory as the DN of the internal L<Net::LDAP::Entry> object,
130 using the bind password supplied in $password. Returns 1 on a successful
136 my ( $self, $password ) = @_;
138 = $self->store->ldap_bind( undef, $self->ldap_entry->dn, $password,
140 if ( defined($ldap) ) {
141 if ($self->store->role_search_as_user) {
142 # FIXME - This can be removed and made to use the code below..
143 # Have to do the role lookup _now_, as this is the only time
144 # that we have the user's password/ldap bind..
147 # Stash a closure which can be used to retrieve the connection in the users context later.
148 $self->_ldap_connection_password( sub { $password } ); # Close over
149 # password to try to ensure it doesn't come out in debug dumps
150 # or get serialized into sessions etc..
160 Returns the results of L<Catalyst::Authentication::Store::LDAP::Backend>'s "lookup_roles" method, an array of roles that are valid for this user.
167 $self->{_roles} ||= [$self->store->lookup_roles($self, $ldap)];
168 return @{$self->{_roles}};
173 Returns the User object, stringified.
179 return $self->stringify;
184 Returns the raw ldap_entry.
190 return $self->user->{'ldap_entry'};
193 =head2 attributes($type)
195 Returns an array of attributes present for this user. If $type is "ashash",
196 it will return a hash with the attribute names as keys. (And the values of
197 those attributes as, well, the values of the hash)
202 my ( $self, $type ) = @_;
203 if ( $type eq "ashash" ) {
204 return $self->user->{'attributes'};
207 return keys( %{ $self->user->{'attributes'} } );
213 Returns the values for an attribute, or undef if that attribute is not present.
214 The safest way to get at an attribute.
219 my ( $self, $attribute ) = @_;
220 if ( !defined($attribute) ) {
221 Catalyst::Exception->throw(
222 "You must provide an attribute to has_attribute!");
224 if ( $attribute eq "dn" ) {
225 return $self->ldap_entry->dn;
227 elsif ( exists( $self->user->{'attributes'}->{$attribute} ) ) {
228 return $self->user->{'attributes'}->{$attribute};
235 =head2 ldap_connection
237 Re-binds to the auth store with the credentials of the user you logged in
238 as, and returns a L<Net::LDAP> object which you can use to do further queries.
242 sub ldap_connection {
244 $self->store->ldap_bind( undef, $self->ldap_entry->dn,
245 $self->_ldap_connection_password->() );
248 =head2 AUTOLOADed methods
250 We automatically map the attributes of the underlying L<Net::LDAP::Entry>
251 object to read-only accessor methods. So, if you have an entry that looks
254 dn: cn=adam,ou=users,dc=yourcompany,dc=com
257 homeDirectory: /home/adam
261 mail: adam@yourcompany.com
265 objectClass: inetOrgPerson
266 objectClass: organizationalPerson
269 objectClass: posixAccount
273 $c->user->homedirectory
275 And you'll get the value of the "homeDirectory" attribute. Note that
276 all the AUTOLOADed methods are automatically lower-cased.
278 =head2 Special Keywords
280 The highly useful and common method "username" will map to the configured
281 value of user_field (uid by default.)
283 $c->user->username == $c->user->uid
290 ( my $method ) = ( our $AUTOLOAD =~ /([^:]+)$/ );
292 if ( $method eq "DESTROY" ) {
295 if ( exists( $self->user->{'attributes'}->{$method} ) ) {
296 return $self->user->{'attributes'}->{$method};
298 elsif ( $method eq "username" ) {
299 my $userfield = $self->store->user_field;
300 my $username = $self->has_attribute($userfield);
305 Catalyst::Exception->throw( "User is missing the "
307 . " attribute, which should not be possible!" );
311 Catalyst::Exception->throw(
312 "No attribute $method for User " . $self->stringify );
322 Adam Jacob <holoway@cpan.org>
324 Some parts stolen shamelessly and entirely from
325 L<Catalyst::Plugin::Authentication::Store::Htpasswd>.
327 Currently maintained by Peter Karman <karman@cpan.org>.
331 To nothingmuch, ghenry, castaway and the rest of #catalyst for the help. :)
335 L<Catalyst::Authentication::Store::LDAP>, L<Catalyst::Authentication::Store::LDAP::Backend>, L<Catalyst::Plugin::Authentication>, L<Net::LDAP>
337 =head1 COPYRIGHT & LICENSE
339 Copyright (c) 2005 the aforementioned authors. All rights
340 reserved. This program is free software; you can redistribute
341 it and/or modify it under the same terms as Perl itself.