Dancer-Plugin-LDAP
view release on metacpan or search on metacpan
lib/Dancer/Plugin/LDAP.pm view on Meta::CPAN
if ($handle->{dbh}) {
if ($conn_details->{connection_check_threshold} &&
time - $handle->{last_connection_check}
< $conn_details->{connection_check_threshold})
{
return $handle->{dbh};
} else {
if (_check_connection($handle->{dbh})) {
$handle->{last_connection_check} = time;
return $handle->{dbh};
} else {
Dancer::Logger::debug(
"Database connection went away, reconnecting"
);
if ($handle->{dbh}) { $handle->{dbh}->disconnect; }
return $handle->{dbh}= _get_connection($conn_details);
}
}
} else {
# Get a new connection
if ($handle->{dbh} = _get_connection($conn_details)) {
$handle->{last_connection_check} = time;
$handles{$pid_tid}{$handle_key} = $handle;
# Dancer::Logger::debug("Handle: ", $handle);
return $handle->{dbh};
} else {
return;
}
}
};
register_plugin for_versions => [ 1, 2 ];
# Try to establish a LDAP connection based on the given settings
sub _get_connection {
my $settings = shift;
my ($ldap, $ldret);
unless ($ldap = Net::LDAP->new($settings->{uri})) {
Dancer::Logger::error("LDAP connection to $settings->{uri} failed: " . $@);
return;
}
$ldret = $ldap->bind($settings->{bind},
password => $settings->{password});
if ($ldret->code) {
Dancer::Logger::error('LDAP bind failed (' . $ldret->code . '): '
. $ldret->error);
return;
}
# pass reference to the settings
$ldap->{dancer_settings} = $settings;
return bless $ldap, 'Dancer::Plugin::LDAP::Handle';
}
# Check whether the connection is alive
sub _check_connection {
my $ldap = shift;
return unless $ldap;
return unless $ldap->socket;
return 1;
}
sub _get_settings {
my $name = shift;
my $return_settings;
# If no name given, just return the default settings
if (!defined $name) {
$return_settings = { %$settings };
} else {
# If there are no named connections in the config, bail now:
return unless exists $settings->{connections};
# OK, find a matching config for this name:
if (my $settings = $settings->{connections}{$name}) {
$return_settings = { %$settings };
} else {
# OK, didn't match anything
Dancer::Logger::error(
"Asked for a database handle named '$name' but no matching "
."connection details found in config"
);
}
}
# We should have soemthing to return now; make sure we have a
# connection_check_threshold, then return what we found. In previous
# versions the documentation contained a typo mentioning
# connectivity-check-threshold, so support that as an alias.
if (exists $return_settings->{'connectivity-check-threshold'}
&& !exists $return_settings->{connection_check_threshold})
{
$return_settings->{connection_check_threshold}
= delete $return_settings->{'connectivity-check-threshold'};
}
$return_settings->{connection_check_threshold} ||= 30;
return $return_settings;
}
sub _load_ldap_settings { $settings = plugin_setting; }
=head1 AUTHOR
Stefan Hornburg (Racke), C<< <racke at linuxia.de> >>
=head1 CONTRIBUTING
This module is developed on Github at:
L<https://github.com/racke/Dancer-Plugin-LDAP>
Feel free to fork the repo and submit pull requests! Also, it makes sense to
( run in 1.497 second using v1.01-cache-2.11-cpan-39bf76dae61 )