| 1 | package SPOPS::Tool::LDAP::Datasource; | ||
| 2 | |||
| 3 | # $Id: Datasource.pm,v 3.3 2004/06/02 00:48:24 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Log::Log4perl qw( get_logger ); | ||
| 7 | use SPOPS; | ||
| 8 | use SPOPS::ClassFactory qw( ERROR OK NOTIFY ); | ||
| 9 | |||
| 10 | my $log = get_logger(); | ||
| 11 | |||
| 12 | $SPOPS::Tool::LDAP::Datasource::VERSION = sprintf("%d.%02d", q$Revision: 3.3 $ =~ /(\d+)\.(\d+)/); | ||
| 13 | |||
| 14 | sub behavior_factory { | ||
| 15 | my ( $class ) = @_; | ||
| 16 | $log->is_info && | ||
| 17 | $log->info( "Installing LDAP datasource configuration for ($class)" ); | ||
| 18 | return { manipulate_configuration => \&datasource_access }; | ||
| 19 | } | ||
| 20 | |||
| 21 | my $generic_ds_sub = <<'DS'; | ||
| 22 | |||
| 23 | sub %%CLASS%%::global_datasource_handle { | ||
| 24 | my ( $class ) = @_; | ||
| 25 | unless ( $%%CLASS%%::LDAP ) { | ||
| 26 | require Net::LDAP; | ||
| 27 | $%%CLASS%%::LDAP = Net::LDAP->new( '%%HOST%%', port => %%PORT%%, | ||
| 28 | version => %%VERSION%% ) | ||
| 29 | or SPOPS::Exception->throw( "Cannot connect to LDAP directory: $@" ); | ||
| 30 | $%%CLASS%%::LDAP->bind(%%BIND_INFO%%); | ||
| 31 | } | ||
| 32 | return $%%CLASS%%::LDAP; | ||
| 33 | } | ||
| 34 | DS | ||
| 35 | |||
| 36 | sub datasource_access { | ||
| 37 | my ( $class ) = @_; | ||
| 38 | my $ldap_config = $class->CONFIG->{ldap_config}; | ||
| 39 | |||
| 40 | my $host = $ldap_config->{host} || 'localhost'; | ||
| 41 | my $port = $ldap_config->{port} || 389; | ||
| 42 | my $version = $ldap_config->{version} || 2; | ||
| 43 | |||
| 44 | my $bind_info = ''; | ||
| 45 | if ( $ldap_config->{bind_dn} ) { | ||
| 46 | $bind_info = " dn => '$ldap_config->{bind_dn}', " . | ||
| 47 | " password => '$ldap_config->{bind_password}' "; | ||
| 48 | } | ||
| 49 | |||
| 50 | my $ds_code = $generic_ds_sub; | ||
| 51 | $ds_code =~ s/%%CLASS%%/$class/g; | ||
| 52 | $ds_code =~ s/%%HOST%%/$host/g; | ||
| 53 | $ds_code =~ s/%%PORT%%/$port/g; | ||
| 54 | $ds_code =~ s/%%VERSION%%/$version/g; | ||
| 55 | $ds_code =~ s/%%BIND_INFO%%/$bind_info/g; | ||
| 56 | { | ||
| 57 | local $SIG{__WARN__} = sub { return undef }; | ||
| 58 | eval $ds_code; | ||
| 59 | } | ||
| 60 | if ( $@ ) { | ||
| 61 | warn "Code: $ds_code\n"; | ||
| 62 | return ( ERROR, "Cannot create 'global_datasource_handle() for ($class): $@" ); | ||
| 63 | } | ||
| 64 | return ( OK, undef ); | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; | ||
| 68 | |||
| 69 | __END__ |