X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FAuthentication%2FStore%2FLDAP%2FUser.pm;h=1451c6423f5180a933c5cf2e7dcd76b3b66139da;hb=9638f14b4680792cf962de6c87125d4c88dd250b;hp=69d2c33e79263780ba084467b4141936ec7154cd;hpb=baf99620bce87348b86c56eb46edef9395eb7ebe;p=catagits%2FCatalyst-Authentication-Store-LDAP.git diff --git a/lib/Catalyst/Authentication/Store/LDAP/User.pm b/lib/Catalyst/Authentication/Store/LDAP/User.pm index 69d2c33..1451c64 100644 --- a/lib/Catalyst/Authentication/Store/LDAP/User.pm +++ b/lib/Catalyst/Authentication/Store/LDAP/User.pm @@ -4,7 +4,7 @@ =head1 NAME Catalyst::Authentication::Store::LDAP::User - - A User object representing an LDAP object. + - A User object representing an LDAP object. =head1 SYNOPSIS @@ -28,10 +28,10 @@ username. This wraps up an LDAP object and presents a simplified interface to it's contents. It uses some AUTOLOAD magic to pass method calls it doesn't understand through as simple read only accessors for the LDAP entries -various attributes. +various attributes. It gets grumpy if you ask for an attribute via the AUTOLOAD mechanism -that it doesn't know about. Avoid that with using "has_attribute", +that it doesn't know about. Avoid that with using "has_attribute", discussed in more detail below. You can skip all that and just go straight to the L @@ -48,13 +48,17 @@ use base qw( Catalyst::Authentication::User Class::Accessor::Fast ); use strict; use warnings; +use Scalar::Util qw/refaddr/; -our $VERSION = '0.1005'; +our $VERSION = '1.015'; -BEGIN { __PACKAGE__->mk_accessors(qw/user store _ldap_connection/) } +BEGIN { __PACKAGE__->mk_accessors(qw/user store/) } use overload '""' => sub { shift->stringify }, fallback => 1; +my %_ldap_connection_passwords; # Store inside-out so that they don't show up + # in dumps.. + =head1 METHODS =head2 new($store, $user, $c) @@ -107,8 +111,8 @@ sub stringify { return $string; } else { - my ($string) = $self->$userfield; - return $string; + my $val = $self->$userfield; + return ref($val) eq 'ARRAY' ? $val->[0] : $val; } } @@ -136,20 +140,9 @@ bind, 0 on failure. sub check_password { my ( $self, $password ) = @_; - my $ldap - = $self->store->ldap_bind( undef, $self->ldap_entry->dn, $password, - 'forauth' ); - if ( defined($ldap) ) { - if ($self->store->role_search_as_user) { - # FIXME - This can be removed and made to use the code below.. - # Have to do the role lookup _now_, as this is the only time - # that we have the user's password/ldap bind.. - $self->roles($ldap); - } + if ( $self->store->ldap_auth($self->ldap_entry->dn, $password) ) { # Stash a closure which can be used to retrieve the connection in the users context later. - $self->_ldap_connection( sub { - $self->store->ldap_bind( undef, $self->ldap_entry->dn, $password ) - }); + $_ldap_connection_passwords{refaddr($self)} = $password; return 1; } else { @@ -165,8 +158,7 @@ Returns the results of L's "look sub roles { my $self = shift; - my $ldap = shift; - $self->{_roles} ||= [$self->store->lookup_roles($self, $ldap)]; + $self->{_roles} ||= [$self->store->lookup_roles($self)]; return @{$self->{_roles}}; } @@ -183,7 +175,7 @@ sub for_session { =head2 ldap_entry -Returns the raw ldap_entry. +Returns the raw ldap_entry. =cut @@ -213,7 +205,7 @@ sub attributes { =head2 has_attribute Returns the values for an attribute, or undef if that attribute is not present. -The safest way to get at an attribute. +The safest way to get at an attribute. =cut @@ -226,6 +218,9 @@ sub has_attribute { if ( $attribute eq "dn" ) { return $self->ldap_entry->dn; } + elsif ( $attribute eq "username" ) { + return $self->user->{'attributes'}->{$self->store->user_field}; + } elsif ( exists( $self->user->{'attributes'}->{$attribute} ) ) { return $self->user->{'attributes'}->{$attribute}; } @@ -234,6 +229,36 @@ sub has_attribute { } } +=head2 get + +A simple wrapper around has_attribute() to satisfy the Catalyst::Authentication::User API. + +=cut + +sub get { return shift->has_attribute(@_) } + +=head2 get_object + +Satisfies the Catalyst::Authentication::User API and returns the contents of the user() +attribute. + +=cut + +sub get_object { return shift->user } + +=head2 ldap_connection + +Re-binds to the auth store with the credentials of the user you logged in +as, and returns a L object which you can use to do further queries. + +=cut + +sub ldap_connection { + my $self = shift; + $self->store->ldap_bind( undef, $self->ldap_entry->dn, + $_ldap_connection_passwords{refaddr($self)} ); +} + =head2 AUTOLOADed methods We automatically map the attributes of the underlying L @@ -262,17 +287,32 @@ You can call: $c->user->homedirectory And you'll get the value of the "homeDirectory" attribute. Note that -all the AUTOLOADed methods are automatically lower-cased. +all the AUTOLOADed methods are automatically lower-cased. =head2 Special Keywords The highly useful and common method "username" will map to the configured -value of user_field (uid by default.) +value of user_field (uid by default.) $c->user->username == $c->user->uid =cut +sub DESTROY { + my $self = shift; + # Don't leak passwords.. + delete $_ldap_connection_passwords{refaddr($self)}; +} + +sub can { + my ($self, $method) = @_; + + return $self->SUPER::can($method) || do { + return unless $self->has_attribute($method); + return sub { $_[0]->has_attribute($method) }; + }; +} + sub AUTOLOAD { my $self = shift; @@ -281,20 +321,9 @@ sub AUTOLOAD { if ( $method eq "DESTROY" ) { return; } - if ( exists( $self->user->{'attributes'}->{$method} ) ) { - return $self->user->{'attributes'}->{$method}; - } - elsif ( $method eq "username" ) { - my $userfield = $self->store->user_field; - my $username = $self->has_attribute($userfield); - if ($username) { - return $username; - } - else { - Catalyst::Exception->throw( "User is missing the " - . $userfield - . " attribute, which should not be possible!" ); - } + + if ( my $attribute = $self->has_attribute($method) ) { + return $attribute; } else { Catalyst::Exception->throw( @@ -311,7 +340,7 @@ __END__ Adam Jacob Some parts stolen shamelessly and entirely from -L. +L. Currently maintained by Peter Karman .