Data-Toolkit

 view release on metacpan or  search on metacpan

lib/Data/Toolkit/Connector/LDAP.pm  view on Meta::CPAN

as it is a reference to the connector's copy.

=cut

sub current {
	my $self = shift;
	my $newCurrent = shift;

	if ($newCurrent) {
		croak "Data::Toolkit::Connector::LDAP->current expects a Net::LDAP::Entry"
			unless $newCurrent->isa("Net::LDAP::Entry");
		carp "Data::Toolkit::Connector::LDAP->current converting Net::LDAP::Entry" if $debug;

		# Build an entry
		my $entry = Data::Toolkit::Entry->new();

		# Set the DN
		$entry->set( '_dn', [ $newCurrent->dn() ] );

		# Now step through the LDAP attributes and assign data to attributes in the entry
		my $attrib;
		my @attributes = $newCurrent->attributes();

		foreach $attrib (@attributes) {
			$entry->set( $attrib, $newCurrent->get_value( $attrib, asref => 1 ) );
		}

		$self->{current} = $entry;
		$self->{currentLDAP} = $newCurrent;
	}

	if ($debug) {
		my $dn;
		my $setting = '';
		$setting = "setting " if $newCurrent;
		$dn = $self->{current}->get('_dn') if $self->{current};
		carp "Data::Toolkit::Connector::LDAP->current $setting$self DN: $dn";
	}

	return $self->{current};
}


########################################

=head2 update

Update the current LDAP entry using data from a source entry and an optional map.
If no map is supplied, all attributes in the source entry are updated in the LDAP entry.

If a map I<is> supplied then any attribute listed in the map but not in the
source entry will be deleted from the current entry in LDAP.

Returns the Net::LDAP::Message result of the LDAP update operation.

   $msg = $ldapConn->update($sourceEntry);
   $msg = $ldapConn->update($sourceEntry, $updateMap);

=cut

sub update {
	my $self = shift;
	my $source = shift;
	my $map = shift;

	croak "Data::Toolkit::Connector::LDAP->update called without a source entry" if !$source;
	croak "Data::Toolkit::Connector::LDAP->update expects a Data::Toolkit::Entry parameter"
		if !$source->isa('Data::Toolkit::Entry');
	croak "Data::Toolkit::Connector::LDAP->update second parameter should be a Data::Toolkit::Map"
		if ($map and !$map->isa('Data::Toolkit::Map'));

	croak "Data::Toolkit::Connector::LDAP->update called without a valid current entry" if !$self->{current};

	my $dn = $self->{current}->get('_dn');
	$dn = $dn->[0] if $dn;
	carp "Data::Toolkit::Connector::LDAP->update $self DN: $dn" if $debug;

	# Save a copy of the current entry in case the update fails and we need to reset it
	my $currentSave = clone($self->{currentLDAP});

	# Apply the map if we have one
	$source = $source->map($map) if $map;

	# Work out which attributes we are going to deal with
	my @attrlist;
	if ($map) {
		# We have a map so take the list of attributes from that
		# This allows us to delete attributes that are not present in the source entry
		@attrlist = $map->outputs();
	}
	else {
		# No map supplied so we will only update attributes present in the source entry
		# i.e. we will not delete any attributes
		@attrlist = $source->attributes();
	}

	# Step through the list of attributes and compare source with current LDAP entry
	# Keep track of whether we do any actual changes, and avoid passing null change to LDAP
	# (need to synthesise an LDAP result message in that case)
	my $needUpdate = 0;
	foreach my $attr (@attrlist) {
		print "ATTR: $attr\n" if $debug;

		# We know that entry objects store attr lists in sorted order so we can use this
		# to compare them.
		my @sourcelist = $source->get($attr);
		my @currentlist = $self->{current}->get($attr);

		if ($useLDAPReplace) {
			# Delete or replace the whole set of values
			# Often inefficient, but works even if no equality match is defined in the schema

			# Delete attribute if no values are wanted
			if (!defined($sourcelist[0]) and defined($currentlist[0])) {
				print "DELETING $attr\n" if $debug;
				$self->{currentLDAP}->delete( $attr );
				$needUpdate = 1;
			}

			# Replace all values if we have any
			if (defined($sourcelist[0])) {



( run in 1.653 second using v1.01-cache-2.11-cpan-39bf76dae61 )