| 1 | package SPOPS::ClassFactory::LDAP; | ||
| 2 | |||
| 3 | # $Id: LDAP.pm,v 3.3 2004/06/02 00:48:22 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Log::Log4perl qw( get_logger ); | ||
| 7 | use SPOPS; | ||
| 8 | use SPOPS::ClassFactory qw( OK ERROR DONE ); | ||
| 9 | |||
| 10 | my $log = get_logger(); | ||
| 11 | |||
| 12 | $SPOPS::ClassFactory::LDAP::VERSION = sprintf("%d.%02d", q$Revision: 3.3 $ =~ /(\d+)\.(\d+)/); | ||
| 13 | |||
| 14 | |||
| 15 | ######################################## | ||
| 16 | # BEHAVIOR: read_code | ||
| 17 | ######################################## | ||
| 18 | |||
| 19 | my $generic_common_relate = <<'COMMONRELATE'; | ||
| 20 | |||
| 21 | sub %%CLASS%%::_ldap_get_linked_objects { | ||
| 22 | my ( $self, $item_list, $item_class, $p ) = @_; | ||
| 23 | my $log = Log::Log4perl::get_logger(); | ||
| 24 | $item_list = ( ref $item_list ) ? $item_list : [ $item_list ]; | ||
| 25 | my ( @object_list, @error_list ); | ||
| 26 | LINK_ITEM: | ||
| 27 | foreach my $item ( @{ $item_list } ) { | ||
| 28 | if ( ref $item ) { | ||
| 29 | push @object_list, $item; | ||
| 30 | $log->is_debug && | ||
| 31 | $log->debug( "Found linked object", "(", $item->id, ")" ); | ||
| 32 | next LINK_ITEM; | ||
| 33 | } | ||
| 34 | |||
| 35 | # First fetch the thing we're linking to, then put it in | ||
| 36 | # the list | ||
| 37 | |||
| 38 | my $item_obj = eval { $item_class->fetch( $item, $p ) }; | ||
| 39 | if ( $@ or ! $item_obj ) { | ||
| 40 | my $err = ( $@ ) ? $SPOPS::Error::system_msg : 'Object not found'; | ||
| 41 | my $msg = "Cannot fetch linked object with ID ($item)\nError: $err"; | ||
| 42 | $log->warn( $msg ); | ||
| 43 | push @error_list, $msg; | ||
| 44 | next LINK_ITEM; | ||
| 45 | } | ||
| 46 | $log->is_info && | ||
| 47 | $log->info( "Found linked object to ID (", $item_obj->id, ")" ); | ||
| 48 | push @object_list, $item_obj; | ||
| 49 | } | ||
| 50 | return ( \@object_list, \@error_list ); | ||
| 51 | } | ||
| 52 | |||
| 53 | COMMONRELATE | ||
| 54 | |||
| 55 | |||
| 56 | sub conf_read_code { | ||
| 57 | my ( $class ) = @_; | ||
| 58 | my $common_relate = $generic_common_relate; | ||
| 59 | $common_relate =~ s/%%CLASS%%/$class/g; | ||
| 60 | { | ||
| 61 | local $SIG{__WARN__} = sub { return undef }; | ||
| 62 | eval $common_relate; | ||
| 63 | } | ||
| 64 | if ( $@ ) { | ||
| 65 | return ( ERROR, "Cannot create common relationship routine ($class): $@" ); | ||
| 66 | } | ||
| 67 | $log->is_info && | ||
| 68 | $log->info( "Finished adding LDAP read_code sub for ($class)" ); | ||
| 69 | return ( OK, undef ); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | ######################################## | ||
| 74 | # BEHAVIOR: has_a | ||
| 75 | ######################################## | ||
| 76 | |||
| 77 | my $generic_hasa = <<'HASA'; | ||
| 78 | sub %%CLASS%%::%%HASA_ALIAS%% { | ||
| 79 | my ( $self, $p ) = @_; | ||
| 80 | unless ( $self->dn ) { SPOPS::Exception->throw( "Cannot call from unsaved object or class!" ) } | ||
| 81 | my $log = Log::Log4perl::get_logger(); | ||
| 82 | |||
| 83 | my @object_list = (); | ||
| 84 | my $conf_other = %%HASA_CLASS%%->CONFIG; | ||
| 85 | my $hasa_value = $self->{%%HASA_FIELD%%}; | ||
| 86 | $hasa_value = ( ref $hasa_value eq 'ARRAY' ) | ||
| 87 | ? $hasa_value : [ $hasa_value ]; | ||
| 88 | foreach my $other_dn ( @{ $hasa_value } ) { | ||
| 89 | $log->is_info && | ||
| 90 | $log->info( "Trying to retrieve linked %%HASA_ALIAS%%", | ||
| 91 | "with DN ($other_dn)" ); | ||
| 92 | my $object = eval { %%HASA_CLASS%%->fetch_by_dn( $other_dn, $p ) }; | ||
| 93 | if ( $@ ) { | ||
| 94 | $log->error( "Could not retrieve linked %%HASA_ALIAS%% with DN ($other_dn): $@" ); | ||
| 95 | next; | ||
| 96 | } | ||
| 97 | $log->is_info && | ||
| 98 | $log->info( "Fetched: ", ( ref $object ) | ||
| 99 | ? "object with ID (" . $object->id . ")" | ||
| 100 | : "nothing" ); | ||
| 101 | push @object_list, $object if ( $object ); | ||
| 102 | } | ||
| 103 | return \@object_list; | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | sub %%CLASS%%::%%HASA_ALIAS%%_add { | ||
| 108 | my ( $self, $link_item_list, $p ) = @_; | ||
| 109 | my $log = Log::Log4perl::get_logger(); | ||
| 110 | my ( $has_a_objects, $error_list ) = | ||
| 111 | $self->_ldap_get_linked_objects( $link_item_list, | ||
| 112 | '%%HASA_CLASS%%', $p ); | ||
| 113 | my $link_dn = $self->dn; | ||
| 114 | my $added = 0; | ||
| 115 | |||
| 116 | foreach my $has_a ( @{ $has_a_objects } ) { | ||
| 117 | $self->{%%HASA_FIELD%%} = $has_a->dn; | ||
| 118 | $added++; | ||
| 119 | $log->is_info && | ||
| 120 | $log->info( "Will add has_a object:", $has_a->dn ); | ||
| 121 | } | ||
| 122 | $self->save( $p ); | ||
| 123 | return $added; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | sub %%CLASS%%::%%HASA_ALIAS%%_remove { | ||
| 128 | my ( $self, $link_item_list, $p ) = @_; | ||
| 129 | my $log = Log::Log4perl::get_logger(); | ||
| 130 | my ( $has_a_objects, $error_list ) = | ||
| 131 | $self->_ldap_get_linked_objects( $link_item_list, | ||
| 132 | '%%HASA_CLASS%%', $p ); | ||
| 133 | my $link_dn = $self->dn; | ||
| 134 | my $removed = 0; | ||
| 135 | |||
| 136 | my $old_members = ( ref $self->{%%HASA_FIELD%%} ) | ||
| 137 | ? $self->{%%HASA_FIELD%%} : [ $self->{%%HASA_FIELD%%} ]; | ||
| 138 | foreach my $has_a ( @{ $has_a_objects } ) { | ||
| 139 | $self->{%%HASA_FIELD%%} = ( ref $self->{%%HASA_FIELD%%} ) | ||
| 140 | ? { remove => $has_a->dn } : undef; | ||
| 141 | $removed++; | ||
| 142 | $log->is_info && | ||
| 143 | $log->info( "Will remove has_a object:", $has_a->dn ); | ||
| 144 | |||
| 145 | } | ||
| 146 | $self->save( $p ); | ||
| 147 | return $removed; | ||
| 148 | } | ||
| 149 | |||
| 150 | HASA | ||
| 151 | |||
| 152 | |||
| 153 | # Process the 'has_a' aliases -- pretty straightforward (see pod) | ||
| 154 | |||
| 155 | sub conf_relate_has_a { | ||
| 156 | my ( $class ) = @_; | ||
| 157 | |||
| 158 | my $config = $class->CONFIG; | ||
| 159 | my $has_a = $config->{has_a}; | ||
| 160 | return ( OK, undef ) unless ( $has_a and ref $has_a eq 'HASH' ); | ||
| 161 | |||
| 162 | foreach my $hasa_class ( keys %{ $has_a } ) { | ||
| 163 | my $field_list = ( ref $has_a->{ $hasa_class } eq 'ARRAY' ) | ||
| 164 | ? $has_a->{ $hasa_class } | ||
| 165 | : [ $has_a->{ $hasa_class } ]; | ||
| 166 | my $hasa_config = $hasa_class->CONFIG; | ||
| 167 | my $hasa_alias = $hasa_config->{main_alias}; | ||
| 168 | foreach my $hasa_field ( @{ $field_list } ) { | ||
| 169 | my $hasa_sub = $generic_hasa; | ||
| 170 | $hasa_sub =~ s/%%CLASS%%/$class/g; | ||
| 171 | $hasa_sub =~ s/%%HASA_CLASS%%/$hasa_class/g; | ||
| 172 | $hasa_sub =~ s/%%HASA_ALIAS%%/$hasa_alias/g; | ||
| 173 | $hasa_sub =~ s/%%HASA_FIELD%%/$hasa_field/g; | ||
| 174 | $log->is_debug && | ||
| 175 | $log->debug( "Trying to create has_a routines with ($class) has_a", | ||
| 176 | "($hasa_class) using field ($hasa_field)" ); | ||
| 177 | $log->is_debug && | ||
| 178 | $log->debug( "Now going to eval the routine:\n$hasa_sub" ); | ||
| 179 | # warn "Trying\n$hasa_sub"; | ||
| 180 | { | ||
| 181 | local $SIG{__WARN__} = sub { return undef }; | ||
| 182 | eval $hasa_sub; | ||
| 183 | } | ||
| 184 | if ( $@ ) { | ||
| 185 | return ( ERROR, "Cannot create 'has_a' routine in ($class): $@" ); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | } | ||
| 189 | $log->is_info && | ||
| 190 | $log->info( "Finished adding LDAP has_a relationships for ($class)" ); | ||
| 191 | return ( DONE, undef ); | ||
| 192 | } | ||
| 193 | |||
| 194 | |||
| 195 | ######################################## | ||
| 196 | # BEHAVIOR: links_to | ||
| 197 | ######################################## | ||
| 198 | |||
| 199 | # EVAL'D SUBROUTINES | ||
| 200 | |||
| 201 | my $generic_linksto = <<'LINKSTO'; | ||
| 202 | |||
| 203 | sub %%CLASS%%::%%LINKSTO_ALIAS%% { | ||
| 204 | my ( $self, $p ) = @_; | ||
| 205 | $p ||= {}; | ||
| 206 | $p->{filter} = "(%%LINKSTO_FIELD%%=" . $self->dn . ")"; | ||
| 207 | return %%LINKSTO_CLASS%%->fetch_group( $p ); | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | sub %%CLASS%%::%%LINKSTO_ALIAS%%_add { | ||
| 212 | my ( $self, $link_item_list, $p ) = @_; | ||
| 213 | my ( $link_to_objects, $error_list ) = | ||
| 214 | $self->_ldap_get_linked_objects( $link_item_list, | ||
| 215 | '%%LINKSTO_CLASS%%', $p ); | ||
| 216 | my $link_dn = $self->dn; | ||
| 217 | my $added = 0; | ||
| 218 | |||
| 219 | foreach my $link_to ( @{ $link_to_objects } ) { | ||
| 220 | |||
| 221 | # Now add the DN for the linker -- this should work | ||
| 222 | # whether it's multivalue or not | ||
| 223 | |||
| 224 | $link_to->{%%LINKSTO_FIELD%%} = $link_dn; | ||
| 225 | $link_to->save( $p ); | ||
| 226 | $added++; | ||
| 227 | } | ||
| 228 | return $added; | ||
| 229 | } | ||
| 230 | |||
| 231 | |||
| 232 | sub %%CLASS%%::%%LINKSTO_ALIAS%%_remove { | ||
| 233 | my ( $self, $link_item_list, $p ) = @_; | ||
| 234 | my ( $link_to_objects, $error_list ) = | ||
| 235 | $self->_ldap_get_linked_objects( $link_item_list, | ||
| 236 | '%%LINKSTO_CLASS%%', $p ); | ||
| 237 | my $link_dn = $self->dn; | ||
| 238 | my $removed = 0; | ||
| 239 | |||
| 240 | foreach my $link_to ( @{ $link_to_objects } ) { | ||
| 241 | my $current_value = $link_to->{%%LINKSTO_FIELD%%}; | ||
| 242 | if ( ref $current_value ) { | ||
| 243 | $link_to->{%%LINKSTO_FIELD%%} = { remove => $link_dn }; | ||
| 244 | } | ||
| 245 | else { | ||
| 246 | $link_to->{%%LINKSTO_FIELD%%} = undef; | ||
| 247 | } | ||
| 248 | $link_to->save( $p ); | ||
| 249 | $removed++; | ||
| 250 | } | ||
| 251 | return $removed; | ||
| 252 | } | ||
| 253 | LINKSTO | ||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | sub conf_relate_links_to { | ||
| 258 | my ( $class ) = @_; | ||
| 259 | |||
| 260 | my $config = $class->CONFIG; | ||
| 261 | my $links_to = $config->{links_to}; | ||
| 262 | return ( OK, undef ) unless ( $links_to and ref $links_to eq 'HASH' ); | ||
| 263 | |||
| 264 | foreach my $linksto_class ( keys %{ $links_to } ) { | ||
| 265 | my $field_list = ( ref $links_to->{ $linksto_class } eq 'ARRAY' ) | ||
| 266 | ? $links_to->{ $linksto_class } | ||
| 267 | : [ $links_to->{ $linksto_class } ]; | ||
| 268 | my $linksto_config = $linksto_class->CONFIG; | ||
| 269 | my $linksto_alias = $linksto_config->{main_alias}; | ||
| 270 | foreach my $linksto_field ( @{ $field_list } ) { | ||
| 271 | my $linksto_sub = $generic_linksto; | ||
| 272 | $linksto_sub =~ s/%%CLASS%%/$class/g; | ||
| 273 | $linksto_sub =~ s/%%LINKSTO_CLASS%%/$linksto_class/g; | ||
| 274 | $linksto_sub =~ s/%%LINKSTO_ALIAS%%/$linksto_alias/g; | ||
| 275 | $linksto_sub =~ s/%%LINKSTO_FIELD%%/$linksto_field/g; | ||
| 276 | $log->is_debug && | ||
| 277 | $log->debug( "Trying to create links_to routines with ($class) links_to", | ||
| 278 | "($linksto_class) using field ($linksto_field)" ); | ||
| 279 | $log->is_debug && | ||
| 280 | $log->debug( "Now going to eval the routine:\n$linksto_sub" ); | ||
| 281 | { | ||
| 282 | local $SIG{__WARN__} = sub { return undef }; | ||
| 283 | eval $linksto_sub; | ||
| 284 | } | ||
| 285 | if ( $@ ) { | ||
| 286 | return ( ERROR, "Cannot create 'links_to' routine in ($class): $@" ); | ||
| 287 | } | ||
| 288 | |||
| 289 | } | ||
| 290 | } | ||
| 291 | $log->is_info && | ||
| 292 | $log->info( "Finished adding LDAP links_to relationships for ($class)" ); | ||
| 293 | return ( DONE, undef ); | ||
| 294 | } | ||
| 295 | |||
| 296 | # Empty method that halts the process -- don't use the 'fetch_by' from | ||
| 297 | # SPOPS | ||
| 298 | |||
| 299 | sub conf_fetch_by { | ||
| 300 | my ( $class ) = @_; | ||
| 301 | if ( ref $class->CONFIG->{fetch_by} and scalar @{ $class->CONFIG->{fetch_by} } ) { | ||
| 302 | warn "SPOPS::LDAP does not currently implement the 'fetch_by' ", | ||
| 303 | "mechanism of SPOPS, so methods for the fetch_by fields\n(", | ||
| 304 | join( ', ', @{ $class->CONFIG->{fetch_by} } ), | ||
| 305 | ") will not be created for class $class.\n"; | ||
| 306 | } | ||
| 307 | return ( DONE, undef ); | ||
| 308 | } | ||
| 309 | |||
| 310 | 1; | ||
| 311 | |||
| 312 | __END__ |