File Coverage

blib/lib/Mail/MtPolicyd/LdapConnection.pm
Criterion Covered Total %
statement 15 30 50.0
branch 1 12 8.3
condition n/a
subroutine 4 6 66.6
pod 0 2 0.0
total 20 50 40.0


line stmt bran cond sub pod time code
1             package Mail::MtPolicyd::LdapConnection;
2              
3 3     3   31917 use strict;
  3         5  
  3         85  
4 3     3   781 use MooseX::Singleton;
  3         523150  
  3         24  
5              
6             # ABSTRACT: singleton class to hold the ldap server connection
7             our $VERSION = '1.23'; # VERSION
8              
9 3     3   58330 use Net::LDAP;
  3         467441  
  3         19  
10              
11             has 'host' => ( is => 'ro', isa => 'Str', required => 1 );
12             has 'port' => ( is => 'ro', isa => 'Int', default => 389 );
13              
14             has 'keepalive' => ( is => 'ro', isa => 'Bool', default => 1 );
15             has 'timeout' => ( is => 'ro', isa => 'Int', default => 120 );
16              
17             has 'binddn' => ( is => 'ro', isa => 'Maybe[Str]' );
18             has 'password' => ( is => 'ro', isa => 'Maybe[Str]' );
19              
20             has 'starttls' => ( is => 'ro', isa => 'Bool', default => 1 );
21              
22             has 'handle' => ( is => 'rw', isa => 'Net::LDAP', lazy => 1,
23             default => sub {
24             my $self = shift;
25             return $self->_connect_ldap;
26             },
27             handles => {
28             'disconnect' => 'unbind',
29             },
30             );
31              
32             has 'connection_class' => ( is => 'ro', isa => 'Maybe[Str]' );
33              
34             sub _connect_ldap {
35 1     1   2 my $self = shift;
36 1         2 my $ldap_class = 'Net::LDAP';
37              
38 1 50       43 if( defined $self->connection_class ) {
39 1         65 $ldap_class = $self->connection_class;
40 1         92 eval "require $ldap_class;"; ## no critic
41             }
42              
43 1 0       48 my $ldap = $ldap_class->new(
44             $self->host,
45             port => $self->port,
46             keepalive => $self->keepalive,
47             timeout => $self->timeout,
48             onerror => 'die',
49             ) or die ('cant connect ldap: '.$@);
50              
51 0 0         if( $self->starttls ) {
52 0           eval{ $ldap->start_tls( verify => 'require' ); };
  0            
53 0 0         if( $@ ) { die('starttls on ldap connection failed: '.$@); }
  0            
54             }
55              
56 0 0         if( defined $self->binddn ) {
57 0           $ldap->bind( $self->binddn, password => $self->password );
58             } else {
59 0           $ldap->bind; # anonymous bind
60             }
61              
62 0           return $ldap;
63             }
64              
65             sub reconnect {
66 0     0 0   my $self = shift;
67 0           $self->handle( $self->_connect_ldap );
68             }
69              
70             sub is_initialized {
71 0     0 0   my ( $class, @args ) = @_;
72              
73 0 0         if( $class->meta->existing_singleton ) {
74 0           return( 1 );
75             }
76 0           return( 0 );
77             }
78              
79             1;
80              
81             __END__
82              
83             =pod
84              
85             =encoding UTF-8
86              
87             =head1 NAME
88              
89             Mail::MtPolicyd::LdapConnection - singleton class to hold the ldap server connection
90              
91             =head1 VERSION
92              
93             version 1.23
94              
95             =head1 AUTHOR
96              
97             Markus Benning <ich@markusbenning.de>
98              
99             =head1 COPYRIGHT AND LICENSE
100              
101             This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
102              
103             This is free software, licensed under:
104              
105             The GNU General Public License, Version 2, June 1991
106              
107             =cut