Footprintless-Plugin-Ldap

 view release on metacpan or  search on metacpan

lib/Footprintless/Plugin/Ldap/Ldap.pm  view on Meta::CPAN

        $search_args,
        sub {
            my ($entry) = @_;
            my ( $key, $value );
            if ( defined($entry_mapper) ) {
                ( $key, $value ) = &{$entry_mapper}($entry);
            }
            else {
                ( $key, $value ) = [ $entry->dn(), $entry ];
            }
            $entries{$key} = $value;
        }
    );

    return \%entries;
}

sub search_for_scalar {
    my ( $self, $search_args, $entry_mapper ) = @_;

    # enforce single result
    $search_args->{sizelimit} = 1;

    my $result;
    $self->search(
        $search_args,
        sub {
            my ($entry) = @_;

            $result =
                $entry_mapper
                ? &{$entry_mapper}($entry)
                : $entry;
        }
    );

    return $result;
}

sub to_string {
    my ($self) = @_;

    my @string = ( ( $self->{secure} ? 'ldaps' : 'ldap' ), '://' );
    push( @string, $self->{hostname} );
    push( @string, ":$self->{port}" ) if ( $self->{port} );

    return join( '', @string );
}

sub unbind {
    my ($self) = @_;
    croak('not connected') unless ( $self->{connection} );

    $logger->trace('unbinding');
    my $message = $self->{connection}->unbind();
    $message->code() && croak( $message->error() );

    return $self;
}

sub update {
    my ( $self, $entry ) = @_;
    croak('not connected') unless ( $self->{connection} );

    $logger->tracef( 'updating %s', $entry->dn() );
    my $message = $entry->update( $self->{connection} );
    $message->code() && croak( $message->error() );

    return $self;
}

sub with_connection {
    my ( $self, $sub ) = @_;
    eval {
        $self->connect()->bind();
        &$sub();
    };
    my $error = $@;
    eval { $self->disconnect() };
    die($error) if ($error);
}

sub _write_ldif_entry {
    my ( $ldif, $entry, $options ) = @_;
    if ( $options->{around_write_ldif_entry} ) {
        $options->{around_write_ldif_entry}( $ldif, $entry, $options );
    }
    else {
        if ( $options->{set_password} && $entry->exists('userPassword') ) {
            $entry->replace( userPassword => $options->{set_password} );
        }
        $ldif->write_entry($entry);
    }
}

1;

__END__

=pod

=head1 NAME

Footprintless::Plugin::Ldap::Ldap; - The ldap client implementation

=head1 VERSION

version 1.00

=head1 SYNOPSIS

Standard way of getting an ldap client:

    use Footprintless;
    my $ldap = Footprintless->new()->ldap('proj.env');

    # Export:
    $ldap->with_connection(sub { $ldap->export_ldif('/tmp/export.ldif') });

    # Import:
    $ldap->with_connection(sub { $ldap->import_ldif('/tmp/export.ldif') });



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