Permalink
Browse files

Initial commit.

  • Loading branch information...
racke committed Jun 1, 2011
0 parents commit aefef007d123214469222cd25d7689cc1a05b698
Showing with 631 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +5 −0 Changes
  3. +9 −0 MANIFEST
  4. +22 −0 Makefile.PL
  5. +55 −0 README
  6. +12 −0 ignore.txt
  7. +241 −0 lib/Dancer/Plugin/LDAP.pm
  8. +174 −0 lib/Dancer/Plugin/LDAP/Handle.pm
  9. +9 −0 t/00-load.t
  10. +56 −0 t/boilerplate.t
  11. +13 −0 t/manifest.t
  12. +18 −0 t/pod-coverage.t
  13. +12 −0 t/pod.t
@@ -0,0 +1,5 @@
+*.tar.gz
+blib
+pm_to_blib
+Makefile
+Makefile.old
@@ -0,0 +1,5 @@
+Revision history for Dancer-Plugin-LDAP
+
+0.01 Date/time
+ First version, released on an unsuspecting world.
+
@@ -0,0 +1,9 @@
+Changes
+lib/Dancer/Plugin/LDAP.pm
+Makefile.PL
+MANIFEST This list of files
+README
+t/00-load.t
+t/manifest.t
+t/pod-coverage.t
+t/pod.t
@@ -0,0 +1,22 @@
+use 5.006;
+use strict;
+use warnings;
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+ NAME => 'Dancer::Plugin::LDAP',
+ AUTHOR => q{Stefan Hornburg (Racke) <racke@linuxia.de>},
+ VERSION_FROM => 'lib/Dancer/Plugin/LDAP.pm',
+ ABSTRACT_FROM => 'lib/Dancer/Plugin/LDAP.pm',
+ ($ExtUtils::MakeMaker::VERSION >= 6.3002
+ ? ('LICENSE'=> 'perl')
+ : ()),
+ PL_FILES => {},
+ PREREQ_PM => {
+ 'Test::More' => 0,
+ 'Dancer' => 0,
+ 'Net::LDAP' => 0,
+ },
+ dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
+ clean => { FILES => 'Dancer-Plugin-LDAP-*' },
+);
55 README
@@ -0,0 +1,55 @@
+Dancer-Plugin-LDAP
+
+The README is used to introduce the module and provide instructions on
+how to install the module, any machine dependencies it may have (for
+example C compilers and installed libraries) and any other information
+that should be provided before the module is installed.
+
+A README file is required for CPAN modules since CPAN extracts the README
+file from a module distribution so that people browsing the archive
+can use it to get an idea of the module's uses. It is usually a good idea
+to provide version information here so that people can decide whether
+fixes for the module are worth downloading.
+
+
+INSTALLATION
+
+To install this module, run the following commands:
+
+ perl Makefile.PL
+ make
+ make test
+ make install
+
+SUPPORT AND DOCUMENTATION
+
+After installing, you can find documentation for this module with the
+perldoc command.
+
+ perldoc Dancer::Plugin::LDAP
+
+You can also look for information at:
+
+ RT, CPAN's request tracker (report bugs here)
+ http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dancer-Plugin-LDAP
+
+ AnnoCPAN, Annotated CPAN documentation
+ http://annocpan.org/dist/Dancer-Plugin-LDAP
+
+ CPAN Ratings
+ http://cpanratings.perl.org/d/Dancer-Plugin-LDAP
+
+ Search CPAN
+ http://search.cpan.org/dist/Dancer-Plugin-LDAP/
+
+
+LICENSE AND COPYRIGHT
+
+Copyright (C) 2011 Stefan Hornburg (Racke)
+
+This program is free software; you can redistribute it and/or modify it
+under the terms of either: the GNU General Public License as published
+by the Free Software Foundation; or the Artistic License.
+
+See http://dev.perl.org/licenses/ for more information.
+
@@ -0,0 +1,12 @@
+blib*
+Makefile
+Makefile.old
+Build
+Build.bat
+_build*
+pm_to_blib*
+*.tar.gz
+.lwpcookies
+cover_db
+pod2htm*.tmp
+Dancer-Plugin-LDAP-*
@@ -0,0 +1,241 @@
+package Dancer::Plugin::LDAP;
+
+use 5.006;
+use strict;
+use warnings;
+
+use Dancer::Plugin;
+use Net::LDAP;
+use Dancer::Plugin::LDAP::Handle;
+
+=head1 NAME
+
+Dancer::Plugin::LDAP - LDAP plugin for Dancer micro framework
+
+=head1 VERSION
+
+Version 0.0001
+
+=cut
+
+our $VERSION = '0.0001';
+
+
+=head1 SYNOPSIS
+
+Quick summary of what the module does.
+
+Perhaps a little code snippet.
+
+ use Dancer::Plugin;
+ use Dancer::Plugin::LDAP;
+
+ my $foo = Dancer::Plugin::LDAP->new();
+ ...
+
+=head1 CONFIGURATION
+
+ plugins:
+ LDAP:
+ uri: 'ldap://127.0.0.1:389/'
+ base: 'dc=linuxia,dc=de'
+
+=head1 SUBROUTINES/METHODS
+
+=cut
+
+my $settings = undef;
+my %handles;
+my $def_handle = {};
+
+register ldap => sub {
+ my $arg = shift;
+
+ _load_ldap_settings() unless $settings;
+
+ # The key to use to store this handle in %handles. This will be either the
+ # name supplied to database(), the hashref supplied to database() (thus, as
+ # long as the same hashref of settings is passed, the same handle will be
+ # reused) or $def_handle if database() is called without args:
+ my $handle_key;
+ my $conn_details; # connection settings to use.
+ my $handle;
+
+ # Accept a hashref of settings to use, if desired. If so, we use this
+ # hashref to look for the handle, too, so as long as the same hashref is
+ # passed to the database() keyword, we'll reuse the same handle:
+ if (ref $arg eq 'HASH') {
+ $handle_key = $arg;
+ $conn_details = $arg;
+ } else {
+ $handle_key = defined $arg ? $arg : $def_handle;
+ $conn_details = _get_settings($arg);
+ if (!$conn_details) {
+ Dancer::Logger::error(
+ "No LDAP settings for " . ($arg || "default connection")
+ );
+ return;
+ }
+ }
+
+# Dancer::Logger::debug("Details: ", $conn_details);
+
+ # OK, see if we have a matching handle
+ $handle = $handles{$handle_key} || {};
+
+ 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{$handle_key} = $handle;
+# Dancer::Logger::debug("Handle: ", $handle);
+ return $handle->{dbh};
+ } else {
+ return;
+ }
+ }
+};
+
+register_plugin;
+
+# Try to establish a LDAP connection based on the given settings
+sub _get_connection {
+ my $settings = shift;
+ my $ldap;
+
+ unless ($ldap = Net::LDAP->new($settings->{uri})) {
+ Dancer::Logger::error('LDAP connection failed - ' . $@);
+ return;
+ }
+
+ # pass reference to the settings
+ $ldap->{dancer_settings} = $settings;
+
+ return bless $ldap, 'Dancer::Plugin::LDAP::Handle';
+}
+
+sub _check_connection {
+ 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 BUGS
+
+Please report any bugs or feature requests to C<bug-dancer-plugin-ldap at rt.cpan.org>, or through
+the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dancer-Plugin-LDAP>. I will be notified, and then you'll
+automatically be notified of progress on your bug as I make changes.
+
+
+
+
+=head1 SUPPORT
+
+You can find documentation for this module with the perldoc command.
+
+ perldoc Dancer::Plugin::LDAP
+
+
+You can also look for information at:
+
+=over 4
+
+=item * RT: CPAN's request tracker (report bugs here)
+
+L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dancer-Plugin-LDAP>
+
+=item * AnnoCPAN: Annotated CPAN documentation
+
+L<http://annocpan.org/dist/Dancer-Plugin-LDAP>
+
+=item * CPAN Ratings
+
+L<http://cpanratings.perl.org/d/Dancer-Plugin-LDAP>
+
+=item * Search CPAN
+
+L<http://search.cpan.org/dist/Dancer-Plugin-LDAP/>
+
+=back
+
+
+=head1 ACKNOWLEDGEMENTS
+
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyright 2011 Stefan Hornburg (Racke).
+
+This program is free software; you can redistribute it and/or modify it
+under the terms of either: the GNU General Public License as published
+by the Free Software Foundation; or the Artistic License.
+
+See http://dev.perl.org/licenses/ for more information.
+
+
+=cut
+
+1; # End of Dancer::Plugin::LDAP
Oops, something went wrong.

0 comments on commit aefef00

Please sign in to comment.