| 1 | package SPOPS::LDAP::MultiDatasource; | ||
| 2 | |||
| 3 | # $Id: MultiDatasource.pm,v 3.3 2004/06/02 00:48:23 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( SPOPS::LDAP ); | ||
| 7 | use Log::Log4perl qw( get_logger ); | ||
| 8 | |||
| 9 | use SPOPS; | ||
| 10 | use SPOPS::Exception::LDAP; | ||
| 11 | |||
| 12 | $SPOPS::LDAP::MultiDatasource::VERSION = sprintf("%d.%02d", q$Revision: 3.3 $ =~ /(\d+)\.(\d+)/); | ||
| 13 | |||
| 14 | use constant DEFAULT_CONNECT_KEY => 'main'; | ||
| 15 | |||
| 16 | my $log = get_logger(); | ||
| 17 | |||
| 18 | sub base_dn { | ||
| 19 | my ( $class, $connect_key ) = @_; | ||
| 20 | my $partial_dn = $class->get_partial_dn( $connect_key ); | ||
| 21 | unless ( $partial_dn ) { | ||
| 22 | SPOPS::Exception->throw( "No Base DN defined in configuration key 'ldap_base_dn'" ); | ||
| 23 | } | ||
| 24 | my $connect_info = $class->connection_info( $connect_key ); | ||
| 25 | return join( ',', $partial_dn, $connect_info->{base_dn} ); | ||
| 26 | } | ||
| 27 | |||
| 28 | # Retrieves the 'partial dn', or the section that's prepended to the | ||
| 29 | # server's 'base DN' to identify the branch on which these objects | ||
| 30 | # live | ||
| 31 | |||
| 32 | sub get_partial_dn { | ||
| 33 | my ( $class, $connect_key ) = @_; | ||
| 34 | my $base_dn_info = $class->CONFIG->{ldap_base_dn}; | ||
| 35 | return $base_dn_info unless ( ref $base_dn_info eq 'HASH' ); | ||
| 36 | $connect_key ||= $class->get_connect_key; | ||
| 37 | return $base_dn_info->{ $connect_key }; | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | sub get_connect_key { | ||
| 42 | my ( $class ) = @_; | ||
| 43 | return $class->CONFIG->{default_datasource} || DEFAULT_CONNECT_KEY; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | sub fetch { | ||
| 48 | my ( $class, $id, $p ) = @_; | ||
| 49 | |||
| 50 | # If passed in a handle, we will always use only that | ||
| 51 | |||
| 52 | if ( $p->{ldap} ) { | ||
| 53 | return $class->SUPER::fetch( $id, $p ); | ||
| 54 | } | ||
| 55 | |||
| 56 | my @ds_list = $class->_get_datasource_list( 'fetch', $id, $p ); | ||
| 57 | |||
| 58 | # Step through the datasource listing and try to retrieve each one | ||
| 59 | # in turn | ||
| 60 | |||
| 61 | foreach my $ds ( @ds_list ) { | ||
| 62 | $log->is_info && | ||
| 63 | $log->info( "(fetch) Trying to use datasource ($ds) for class ($class)" ); | ||
| 64 | $p->{connect_key} = $ds; | ||
| 65 | my $object = eval { $class->SUPER::fetch( $id, $p ) }; | ||
| 66 | if ( $object ) { | ||
| 67 | $object->{_datasource} = $ds; | ||
| 68 | return $object; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | return undef; | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | sub fetch_by_dn { | ||
| 76 | my ( $class, $dn, $p ) = @_; | ||
| 77 | if ( $p->{ldap} ) { | ||
| 78 | return $class->SUPER::fetch_by_dn( $dn, $p ); | ||
| 79 | } | ||
| 80 | my @ds_list = $class->_get_datasource_list( 'fetch_by_dn', $dn, $p ); | ||
| 81 | |||
| 82 | # Step through the datasource listing and try to retrieve each one | ||
| 83 | # in turn | ||
| 84 | |||
| 85 | foreach my $ds ( @ds_list ) { | ||
| 86 | $log->is_info && | ||
| 87 | $log->info( "(fetch_by_dn) Trying to use datasource ($ds) for class ($class)" ); | ||
| 88 | $p->{connect_key} = $ds; | ||
| 89 | my $object = eval { $class->SUPER::fetch_by_dn( $dn, $p ) }; | ||
| 90 | if ( $object ) { | ||
| 91 | $object->{_datasource} = $ds; | ||
| 92 | return $object | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | sub fetch_group_all { | ||
| 98 | my ( $class, $p ) = @_; | ||
| 99 | |||
| 100 | if ( $p->{ldap} ) { | ||
| 101 | return $class->SUPER::fetch_group( $p ); | ||
| 102 | } | ||
| 103 | my @ds_list = $class->_get_datasource_list( 'fetch_group', $p ); | ||
| 104 | my @all_objects = (); | ||
| 105 | foreach my $ds ( @ds_list ) { | ||
| 106 | $p->{connect_key} = $ds; | ||
| 107 | $log->is_info && | ||
| 108 | $log->info( "Trying to fetch from datasource ($ds)" ); | ||
| 109 | my $object_list = $class->SUPER::fetch_group( $p ); | ||
| 110 | if ( $object_list and ref $object_list eq 'ARRAY' ) { | ||
| 111 | foreach my $object ( @{ $object_list } ) { | ||
| 112 | $object->{_datasource} = $ds; | ||
| 113 | push @all_objects, $object; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | } | ||
| 117 | return \@all_objects; | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | # Just be sure we grab the right LDAP handle before saving or removing | ||
| 122 | # -- if people pass in a handle, we'll defer to their judgement that | ||
| 123 | # they know what they're doing | ||
| 124 | |||
| 125 | sub save { | ||
| 126 | my ( $self, $p ) = @_; | ||
| 127 | $p->{ldap} ||= $self->global_datasource_handle( $self->{_datasource} ); | ||
| 128 | return $self->SUPER::save( $p ); | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | sub remove { | ||
| 133 | my ( $self, $p ) = @_; | ||
| 134 | $p->{ldap} ||= $self->global_datasource_handle( $self->{_datasource} ); | ||
| 135 | return $self->SUPER::remove( $p ); | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | # Retrieve a datasource list from the class configuration. If it | ||
| 140 | # doesn't work (no list or the list is empty), run a specified method | ||
| 141 | # in the parent class. | ||
| 142 | |||
| 143 | sub _get_datasource_list { | ||
| 144 | my ( $class, $method, @args ) = @_; | ||
| 145 | my $ds_list = $class->CONFIG->{datasource}; | ||
| 146 | |||
| 147 | # If there are not multiple datasources specified, then bounce | ||
| 148 | # back to the SPOPS::LDAP-version of th method we were trying to | ||
| 149 | # call in the first place. | ||
| 150 | |||
| 151 | unless ( ref $ds_list eq 'ARRAY' and scalar @{ $ds_list } ) { | ||
| 152 | $log->is_info && | ||
| 153 | $log->info( "No datasources in configuration for ($class).", | ||
| 154 | "Using SPOPS::LDAP->$method()" ); | ||
| 155 | my $full_method = "SUPER::$method"; | ||
| 156 | return $class->$full_method( @args ); | ||
| 157 | } | ||
| 158 | return @{ $ds_list }; | ||
| 159 | } | ||
| 160 | |||
| 161 | 1; | ||
| 162 | |||
| 163 | __END__ |