| 1 | package SPOPS::LDAP; | ||
| 2 | |||
| 3 | # $Id: LDAP.pm,v 3.4 2004/06/02 00:48:21 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( SPOPS ); | ||
| 7 | use Log::Log4perl qw( get_logger ); | ||
| 8 | use Data::Dumper qw( Dumper ); | ||
| 9 | use Net::LDAP qw(); | ||
| 10 | use Net::LDAP::Entry qw(); | ||
| 11 | use Net::LDAP::Util qw(); | ||
| 12 | use SPOPS; | ||
| 13 | use SPOPS::Exception::LDAP; | ||
| 14 | use SPOPS::Secure qw( :level ); | ||
| 15 | |||
| 16 | my $log = get_logger(); | ||
| 17 | |||
| 18 | $SPOPS::LDAP::VERSION = sprintf("%d.%02d", q$Revision: 3.4 $ =~ /(\d+)\.(\d+)/); | ||
| 19 | |||
| 20 | |||
| 21 | ######################################## | ||
| 22 | # CONFIG | ||
| 23 | ######################################## | ||
| 24 | |||
| 25 | # LDAP config items available from class/object | ||
| 26 | |||
| 27 | sub no_insert { return $_[0]->CONFIG->{no_insert} || {} } | ||
| 28 | sub no_update { return $_[0]->CONFIG->{no_update} || {} } | ||
| 29 | sub skip_undef { return $_[0]->CONFIG->{skip_undef} || {} } | ||
| 30 | sub base_dn { | ||
| 31 | unless ( $_[0]->CONFIG->{ldap_base_dn} ) { | ||
| 32 | SPOPS::Exception->throw( "No Base DN defined" ); | ||
| 33 | } | ||
| 34 | return $_[0]->CONFIG->{ldap_base_dn}; | ||
| 35 | } | ||
| 36 | sub id_value_field { return $_[0]->CONFIG->{id_value_field} } | ||
| 37 | sub ldap_object_class { return $_[0]->CONFIG->{ldap_object_class} } | ||
| 38 | sub ldap_fetch_object_class { return $_[0]->CONFIG->{ldap_fetch_object_class} } | ||
| 39 | sub ldap_update_only_changed { return $_[0]->CONFIG->{ldap_update_only_changed} } | ||
| 40 | |||
| 41 | sub get_superuser_id { return $_[0]->CONFIG->{ldap_root_dn} } | ||
| 42 | sub get_supergroup_id { return $_[0]->CONFIG->{ldap_root_group_dn} } | ||
| 43 | |||
| 44 | sub is_superuser { | ||
| 45 | my ( $class, $id ) = @_; | ||
| 46 | return ( $id eq $class->get_superuser_id ); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub is_supergroup { | ||
| 50 | my ( $class, @id ) = @_; | ||
| 51 | my $super_gid = $class->get_supergroup_id; | ||
| 52 | return grep { $_ eq $super_gid } @id; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | ######################################## | ||
| 57 | # CONNECTION RETRIEVAL | ||
| 58 | ######################################## | ||
| 59 | |||
| 60 | # Subclass must override -- see POD for info | ||
| 61 | |||
| 62 | sub global_datasource_handle { return undef } | ||
| 63 | sub connection_info { return undef } | ||
| 64 | |||
| 65 | |||
| 66 | ######################################## | ||
| 67 | # CLASS CONFIGURATION | ||
| 68 | ######################################## | ||
| 69 | |||
| 70 | sub behavior_factory { | ||
| 71 | my ( $class ) = @_; | ||
| 72 | require SPOPS::ClassFactory::LDAP; | ||
| 73 | $log->is_debug && | ||
| 74 | $log->debug( "Installing SPOPS::LDAP behaviors for ($class)" ); | ||
| 75 | return { read_code => \&SPOPS::ClassFactory::LDAP::conf_read_code, | ||
| 76 | has_a => \&SPOPS::ClassFactory::LDAP::conf_relate_has_a, | ||
| 77 | links_to => \&SPOPS::ClassFactory::LDAP::conf_relate_links_to, | ||
| 78 | fetch_by => \&SPOPS::ClassFactory::LDAP::conf_fetch_by, }; | ||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | ######################################## | ||
| 84 | # CLASS INITIALIZATION | ||
| 85 | ######################################## | ||
| 86 | |||
| 87 | sub class_initialize { | ||
| 88 | my ( $class ) = @_; | ||
| 89 | $class->_class_initialize; | ||
| 90 | return 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _class_initialize {} | ||
| 94 | |||
| 95 | |||
| 96 | ######################################## | ||
| 97 | # OBJECT INFO | ||
| 98 | ######################################## | ||
| 99 | |||
| 100 | sub dn { | ||
| 101 | my ( $self, $dn ) = @_; | ||
| 102 | unless ( ref $self ) { | ||
| 103 | SPOPS::Exception->throw( "Cannot call dn() as class method" ); | ||
| 104 | } | ||
| 105 | $self->{tmp_dn} = $dn if ( $dn ); | ||
| 106 | return $self->{tmp_dn}; | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | ######################################## | ||
| 111 | # FETCH | ||
| 112 | ######################################## | ||
| 113 | |||
| 114 | sub create_id_filter { | ||
| 115 | my ( $item, $id ) = @_; | ||
| 116 | return join( '=', $item->id_field, $id ) if ( $id ); | ||
| 117 | unless ( ref $item ) { | ||
| 118 | SPOPS::Exception->throw( | ||
| 119 | "Cannot create ID filter with a class method call and no ID" ); | ||
| 120 | } | ||
| 121 | return join( '=', $item->id_field, $item->id ); | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | # TODO: If the object is requested with a 'filter' argument rather | ||
| 126 | # than the ID, we might need to fetch the object twice, or perhaps | ||
| 127 | # fetch the object and create it, and then call 'fetch' again with an | ||
| 128 | # 'object' argument which we can clone rather than executing the | ||
| 129 | # actual fetch again. Fetching via filter plays a little havoc with | ||
| 130 | # the security check and pre_fetch_action -- right now we've | ||
| 131 | # duct-taped it but it should be fixed shortly... | ||
| 132 | |||
| 133 | sub fetch { | ||
| 134 | my ( $class, $id, $p ) = @_; | ||
| 135 | $p ||= {}; | ||
| 136 | $log->is_debug && | ||
| 137 | $log->debug( "Trying to fetch an item of $class with ID $id and params ", | ||
| 138 | join " // ", | ||
| 139 | map { $_ . ' -> ' . ( defined( $p->{$_} ) ? $p->{$_} : '' ) } | ||
| 140 | keys %{ $p } ); | ||
| 141 | return undef unless ( $id or $p->{filter} ); | ||
| 142 | |||
| 143 | my $info = $class->_perform_prefetch( $p ); | ||
| 144 | |||
| 145 | # Run the search | ||
| 146 | |||
| 147 | my $filter = ( $p->{no_filter} ) | ||
| 148 | ? '' : $p->{filter} || $class->create_id_filter( $id ); | ||
| 149 | my $entry = $class->_fetch_single_entry({ connect_key => $p->{connect_key}, | ||
| 150 | ldap => $p->{ldap}, | ||
| 151 | base => $p->{base}, | ||
| 152 | scope => $p->{scope}, | ||
| 153 | filter => $filter }); | ||
| 154 | unless ( $entry ) { | ||
| 155 | $log->is_info && | ||
| 156 | $log->info( "No entry found matching object ID ($id)" ); | ||
| 157 | return undef; | ||
| 158 | } | ||
| 159 | my $obj = $class->_perform_postfetch( $p, $info, $entry ); | ||
| 160 | return $obj; | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | sub _perform_prefetch { | ||
| 165 | my ( $class, $p, $info ) = @_; | ||
| 166 | $info ||= {}; | ||
| 167 | |||
| 168 | # If an ID was not passed in but a filter was, we need to delay | ||
| 169 | # security checks until after the object has already been fetched | ||
| 170 | # so we can grab the ID from the object | ||
| 171 | |||
| 172 | $info->{delay_security_check} = ( ! $p->{id} and $p->{filter} ) ? 1 : 0; | ||
| 173 | |||
| 174 | # Let security errors bubble up | ||
| 175 | |||
| 176 | $info->{level} = $p->{security_level}; | ||
| 177 | unless ( $info->{delay_security_check} or $p->{skip_security} ) { | ||
| 178 | $info->{level} ||= $class->check_action_security({ id => $p->{id}, | ||
| 179 | required => SEC_LEVEL_READ }); | ||
| 180 | } | ||
| 181 | |||
| 182 | # Do any actions the class wants before fetching -- note that if | ||
| 183 | # any of the actions returns undef (false), we bail. | ||
| 184 | |||
| 185 | return undef unless ( $class->pre_fetch_action({ %{ $p }, id => $p->{id} }) ); | ||
| 186 | $log->is_info && | ||
| 187 | $log->info( "Pre fetch actions executed ok" ); | ||
| 188 | return $info; | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | sub _perform_postfetch { | ||
| 193 | my ( $class, $p, $info, $entry ) = @_; | ||
| 194 | $log->is_info && | ||
| 195 | $log->info( "Single entry found ok; setting values into object", | ||
| 196 | "(Delay security: $info->{delay_security_check})" ); | ||
| 197 | my $obj = $class->new({ skip_default_values => 1 }); | ||
| 198 | $obj->_fetch_assign_row( undef, $entry ); | ||
| 199 | if ( $info->{delay_security_check} && ! $p->{skip_security} ) { | ||
| 200 | $info->{level} ||= $class->check_action_security({ id => $obj->id, | ||
| 201 | required => SEC_LEVEL_READ }) | ||
| 202 | } | ||
| 203 | $obj->_fetch_post_process( $p, $info->{level} ); | ||
| 204 | return $obj; | ||
| 205 | } | ||
| 206 | |||
| 207 | |||
| 208 | sub _fetch_single_entry { | ||
| 209 | my ( $class, $p ) = @_; | ||
| 210 | my $ldap = $p->{ldap} || $class->global_datasource_handle( $p->{connect_key} ); | ||
| 211 | $log->is_info && | ||
| 212 | $log->info( "Base DN (", $class->base_dn( $p->{connect_key} ), ")", | ||
| 213 | "and filter <<$p->{filter}>> being used to fetch single object" ); | ||
| 214 | my %args = ( base => $p->{base} || $class->base_dn( $p->{connect_key} ), | ||
| 215 | scope => $p->{scope} || 'sub' ); | ||
| 216 | $args{filter} = $p->{filter} if ( $p->{filter} ); | ||
| 217 | my $ldap_msg = $ldap->search( %args ); | ||
| 218 | $class->_check_error( $ldap_msg, 'fetch' ); | ||
| 219 | |||
| 220 | # Go ahead and use $count here since we've hopefully only | ||
| 221 | # retrieved a single record and don't have to worry about blocking | ||
| 222 | # (etc.) for a long time | ||
| 223 | |||
| 224 | my $count = $ldap_msg->count; | ||
| 225 | if ( $count > 1 ) { | ||
| 226 | SPOPS::Exception::LDAP->throw( "Trying to retrieve unique record, retrieved [$count]", | ||
| 227 | { filter => $p->{filter} } ); | ||
| 228 | } | ||
| 229 | if ( $count == 0 ) { | ||
| 230 | $log->is_info && | ||
| 231 | $log->info( "No entry found matching filter ($p->{filter})" ); | ||
| 232 | return undef; | ||
| 233 | } | ||
| 234 | return $ldap_msg->entry( 0 ); | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | # Given a DN, return an object | ||
| 239 | |||
| 240 | sub fetch_by_dn { | ||
| 241 | my ( $class, $dn, $p ) = @_; | ||
| 242 | $p->{base} = $dn; | ||
| 243 | $p->{scope} = 'base'; | ||
| 244 | $p->{filter} = '(objectclass=*)'; | ||
| 245 | return $class->fetch( undef, $p ); | ||
| 246 | } | ||
| 247 | |||
| 248 | |||
| 249 | # Return implementation of SPOPS::Iterator with results | ||
| 250 | |||
| 251 | sub fetch_iterator { | ||
| 252 | my ( $class, $p ) = @_; | ||
| 253 | require SPOPS::Iterator::LDAP; | ||
| 254 | $log->is_info && | ||
| 255 | $log->info( "Trying to create an Iterator with: ", Dumper( $p ) ); | ||
| 256 | $p->{class} = $class; | ||
| 257 | ( $p->{offset}, $p->{max} ) = SPOPS::Utility->determine_limit( $p->{limit} ); | ||
| 258 | unless ( ref $p->{id_list} ) { | ||
| 259 | $p->{ldap_msg} = $class->_execute_multiple_record_query( $p ); | ||
| 260 | $class->_check_error( $p->{ldap_msg}, 'fetch_iterator' ); | ||
| 261 | } | ||
| 262 | return SPOPS::Iterator::LDAP->new( { %{ $p }, skip_default_values => 1 }); | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | # Given a filter, return an arrayref of objects | ||
| 267 | |||
| 268 | sub fetch_group { | ||
| 269 | my ( $class, $p ) = @_; | ||
| 270 | my ( $offset, $max ) = SPOPS::Utility->determine_limit( $p->{limit} ); | ||
| 271 | my $ldap_msg = $class->_execute_multiple_record_query( $p ); | ||
| 272 | $class->_check_error( $ldap_msg, 'fetch_group' ); | ||
| 273 | |||
| 274 | my $entry_count = 0; | ||
| 275 | my @group = (); | ||
| 276 | ENTRY: | ||
| 277 | while ( my $entry = $ldap_msg->shift_entry ) { | ||
| 278 | my $obj = $class->new({ skip_default_values => 1 }); | ||
| 279 | $obj->_fetch_assign_row( undef, $entry ); | ||
| 280 | my $level = ( $p->{skip_security} ) | ||
| 281 | ? SEC_LEVEL_WRITE | ||
| 282 | : eval { $obj->check_action_security({ required => SEC_LEVEL_READ }) }; | ||
| 283 | if ( $@ ) { | ||
| 284 | $log->is_info && | ||
| 285 | $log->info( "Security check for object (", $obj->dn, ")", | ||
| 286 | "in fetch_group() failed, skipping." ); | ||
| 287 | next ENTRY; | ||
| 288 | } | ||
| 289 | |||
| 290 | if ( $offset and ( $entry_count < $offset ) ) { | ||
| 291 | $entry_count++; | ||
| 292 | next ENTRY | ||
| 293 | } | ||
| 294 | last ENTRY if ( $max and ( $entry_count >= $max ) ); | ||
| 295 | $entry_count++; | ||
| 296 | |||
| 297 | $obj->_fetch_post_process( $p, $level ); | ||
| 298 | push @group, $obj; | ||
| 299 | } | ||
| 300 | return \@group; | ||
| 301 | } | ||
| 302 | |||
| 303 | |||
| 304 | sub _execute_multiple_record_query { | ||
| 305 | my ( $class, $p ) = @_; | ||
| 306 | my $filter = $p->{where} || $p->{filter} || ''; | ||
| 307 | |||
| 308 | # If there is a filter, be sure it's in () | ||
| 309 | if ( $filter and $filter !~ /^\(.*\)$/ ) { | ||
| 310 | $filter = "($filter)"; | ||
| 311 | } | ||
| 312 | |||
| 313 | # Specify an object class in the filter if the filter doesn't | ||
| 314 | # already specify an object class and our config says we should | ||
| 315 | |||
| 316 | if ( ( my $fetch_oc = $class->ldap_fetch_object_class ) and $filter !~ /objectclass/ ) { | ||
| 317 | my $oc_filter = "(objectclass=$fetch_oc)"; | ||
| 318 | $log->is_debug && | ||
| 319 | $log->debug( "Adding filter for object class ($fetch_oc)" ); | ||
| 320 | $filter = ( $filter ) ? "(&$oc_filter$filter)" : $oc_filter; | ||
| 321 | } | ||
| 322 | my $ldap = $p->{ldap} || $class->global_datasource_handle( $p->{connect_key} ); | ||
| 323 | $log->is_info && | ||
| 324 | $log->info( "Base DN (", $class->base_dn( $p->{connect_key} ), ")\nFilter <<$filter>>\n", | ||
| 325 | "being used to fetch one or more objects" ); | ||
| 326 | return $ldap->search( base => $class->base_dn( $p->{connect_key} ), | ||
| 327 | scope => 'sub', | ||
| 328 | filter => $filter ); | ||
| 329 | } | ||
| 330 | |||
| 331 | |||
| 332 | sub _fetch_assign_row { | ||
| 333 | my ( $self, $field_list, $entry ) = @_; | ||
| 334 | $log->is_info && | ||
| 335 | $log->info( "Setting data from row into", ref $self, "using DN of entry ", $entry->dn ); | ||
| 336 | $self->clear_all_loaded(); | ||
| 337 | my $CONF = $self->CONFIG; | ||
| 338 | $field_list ||= $self->field_list; | ||
| 339 | foreach my $field ( @{ $field_list } ) { | ||
| 340 | my @values = $entry->get_value( $field ); | ||
| 341 | if ( $CONF->{multivalue}{ $field } ) { | ||
| 342 | $self->{ $field } = \@values; | ||
| 343 | $log->is_info && | ||
| 344 | $log->info( sprintf( " ( multi) %-20s --> %s", $field, join( '||', @values ) ) ); | ||
| 345 | } | ||
| 346 | else { | ||
| 347 | $self->{ $field } = $values[0]; | ||
| 348 | $log->is_info && | ||
| 349 | $log->info( sprintf( " (single) %-20s --> %s", $field, $values[0] ) ); | ||
| 350 | } | ||
| 351 | $self->set_loaded( $field ); | ||
| 352 | } | ||
| 353 | $self->dn( $entry->dn ); | ||
| 354 | return $self; | ||
| 355 | } | ||
| 356 | |||
| 357 | |||
| 358 | sub _fetch_post_process { | ||
| 359 | my ( $self, $p, $security_level ) = @_; | ||
| 360 | |||
| 361 | # Create an entry for this object in the cache unless either the | ||
| 362 | # class or this call to fetch() doesn't want us to. | ||
| 363 | |||
| 364 | $self->set_cached_object( $p ); | ||
| 365 | |||
| 366 | # Execute any actions the class (or any parent) wants after | ||
| 367 | # creating the object (see SPOPS.pm) | ||
| 368 | |||
| 369 | return undef unless ( $self->post_fetch_action( $p ) ); | ||
| 370 | |||
| 371 | # Set object flags | ||
| 372 | |||
| 373 | $self->clear_change; | ||
| 374 | $self->has_save; | ||
| 375 | |||
| 376 | # Set the security fetched from above into this object | ||
| 377 | # as a temporary property (see SPOPS::Tie for more info | ||
| 378 | # on temporary properties); note that this is set whether | ||
| 379 | # we retrieve a cached copy or not | ||
| 380 | |||
| 381 | $self->{tmp_security_level} = $security_level; | ||
| 382 | $log->is_info && | ||
| 383 | $log->info( ref $self, "(", $self->id, ") : cache set (if available),", | ||
| 384 | "post_fetch_action() done, change flag cleared and save ", | ||
| 385 | "flag set. Security: $security_level" ); | ||
| 386 | return $self; | ||
| 387 | } | ||
| 388 | |||
| 389 | |||
| 390 | ######################################## | ||
| 391 | # SAVE | ||
| 392 | ######################################## | ||
| 393 | |||
| 394 | sub save { | ||
| 395 | my ( $self, $p ) = @_; | ||
| 396 | my $id = $self->id; | ||
| 397 | $log->is_info && | ||
| 398 | $log->info( "Trying to save a (", ref $self, ") with ID ($id)" ); | ||
| 399 | |||
| 400 | # We can force save() to be an INSERT by passing in a true value | ||
| 401 | # for the is_add parameter; otherwise, we rely on the flag within | ||
| 402 | # SPOPS::Tie to reflect whether an object has been saved or not. | ||
| 403 | |||
| 404 | my $is_add = ( $p->{is_add} or ! $self->saved ); | ||
| 405 | |||
| 406 | # If this is an update and it hasn't changed, we don't need to do | ||
| 407 | # anything. | ||
| 408 | |||
| 409 | unless ( $is_add or $self->changed ) { | ||
| 410 | $log->is_info && | ||
| 411 | $log->info( "This object exists and has not changed. Exiting." ); | ||
| 412 | return $self; | ||
| 413 | } | ||
| 414 | |||
| 415 | # Check security for create/update | ||
| 416 | |||
| 417 | my ( $level ); | ||
| 418 | unless ( $p->{skip_security} ) { | ||
| 419 | $level = $self->check_action_security({ required => SEC_LEVEL_WRITE, | ||
| 420 | is_add => $is_add }); | ||
| 421 | } | ||
| 422 | $log->is_info && | ||
| 423 | $log->info( "Security check passed ok. Continuing." ); | ||
| 424 | |||
| 425 | # Callback for objects to do something before they're saved | ||
| 426 | |||
| 427 | return undef unless ( $self->pre_save_action({ %{ $p }, | ||
| 428 | is_add => $is_add }) ); | ||
| 429 | |||
| 430 | # Do the insert/update based on whether the object is new; don't | ||
| 431 | # catch the die() that might be thrown -- let that percolate | ||
| 432 | |||
| 433 | if ( $is_add ) { $self->_save_insert( $p ) } | ||
| 434 | else { $self->_save_update( $p ) } | ||
| 435 | |||
| 436 | # Do any actions that need to happen after you save the object | ||
| 437 | |||
| 438 | return undef unless ( $self->post_save_action({ %{ $p }, | ||
| 439 | is_add => $is_add }) ); | ||
| 440 | $log->is_info && | ||
| 441 | $log->info( "Post save action executed ok." ); | ||
| 442 | |||
| 443 | # Save the newly-created/updated object to the cache | ||
| 444 | |||
| 445 | $self->set_cached_object( $p ); | ||
| 446 | |||
| 447 | # Note the action that we've just taken (opportunity for subclasses) | ||
| 448 | |||
| 449 | my $action = ( $is_add ) ? 'create' : 'update'; | ||
| 450 | unless ( $p->{skip_log} ) { | ||
| 451 | $self->log_action( $action, $self->id ); | ||
| 452 | } | ||
| 453 | |||
| 454 | # Set object flags and we're done | ||
| 455 | |||
| 456 | $self->has_save; | ||
| 457 | $self->clear_change; | ||
| 458 | return $self; | ||
| 459 | } | ||
| 460 | |||
| 461 | |||
| 462 | sub _save_insert { | ||
| 463 | my ( $self, $p ) = @_; | ||
| 464 | $p ||= {}; | ||
| 465 | $log->is_info && | ||
| 466 | $log->info( 'Treating save as INSERT' ); | ||
| 467 | my $ldap = $p->{ldap} || $self->global_datasource_handle( $p->{connect_key} ); | ||
| 468 | $self->dn( $self->build_dn ); | ||
| 469 | my $num_objectclass = ( ref $self->{objectclass} ) | ||
| 470 | ? @{ $self->{objectclass} } : 0; | ||
| 471 | if ( $num_objectclass == 0 ) { | ||
| 472 | $self->{objectclass} = $self->ldap_object_class; | ||
| 473 | $log->is_info && | ||
| 474 | $log->info( "Using object class from config in new object (", | ||
| 475 | join( ', ', @{ $self->{objectclass} } ), ")" ); | ||
| 476 | } | ||
| 477 | $log->is_info && | ||
| 478 | $log->info( "Trying to create record with DN: (", $self->dn, ")" ); | ||
| 479 | my %insert_data = (); | ||
| 480 | |||
| 481 | $p->{no_insert} ||= []; | ||
| 482 | my $no_insert = $self->no_insert; | ||
| 483 | map { $no_insert->{ $_ } = 1 } @{ $p->{no_insert} }; | ||
| 484 | $p->{skip_undef} ||= []; | ||
| 485 | my $skip_undef = $self->skip_undef; | ||
| 486 | map { $skip_undef->{ $_ } = 1 } @{ $p->{skip_undef} }; | ||
| 487 | |||
| 488 | foreach my $attr ( @{ $self->field_list } ) { | ||
| 489 | next if ( $no_insert->{ $attr } ); | ||
| 490 | next if ( $skip_undef->{ $attr } and ! defined $self->{ $attr } ); | ||
| 491 | $insert_data{ $attr } = $self->{ $attr }; | ||
| 492 | |||
| 493 | # Trick LDAP to creating object with multivalue property that | ||
| 494 | # has no values | ||
| 495 | |||
| 496 | if ( ref $insert_data{ $attr } eq 'ARRAY' | ||
| 497 | and scalar @{ $insert_data{ $attr } } == 0 ) { | ||
| 498 | $insert_data{ $attr } = undef; | ||
| 499 | } | ||
| 500 | } | ||
| 501 | $log->is_info && | ||
| 502 | $log->info( "Trying to create a record with:\n", Dumper( \%insert_data ) ); | ||
| 503 | my $ldap_msg = $ldap->add( dn => $self->dn, | ||
| 504 | attr => [ %insert_data ]); | ||
| 505 | $self->_check_error( $ldap_msg, 'save' ); | ||
| 506 | $log->is_info && | ||
| 507 | $log->info( "Record created ok." ); | ||
| 508 | } | ||
| 509 | |||
| 510 | |||
| 511 | sub _save_update { | ||
| 512 | my ( $self, $p ) = @_; | ||
| 513 | $p ||= {}; | ||
| 514 | $log->is_info && | ||
| 515 | $log->info( "Treating save as UPDATE with DN: (", $self->dn, ")" ); | ||
| 516 | my $ldap = $p->{ldap} || $self->global_datasource_handle( $p->{connect_key} ); | ||
| 517 | my $entry = $self->_fetch_single_entry({ filter => $self->create_id_filter, | ||
| 518 | ldap => $ldap }); | ||
| 519 | $log->is_info && | ||
| 520 | $log->info( "Loaded entry for update:\n", Dumper( $entry ) ); | ||
| 521 | $p->{no_update} ||= []; | ||
| 522 | my $no_update = $self->no_update; | ||
| 523 | map { $no_update->{ $_ } = 1 } @{ $p->{no_update} }; | ||
| 524 | $p->{skip_undef} ||= []; | ||
| 525 | my $skip_undef = $self->skip_undef; | ||
| 526 | map { $skip_undef->{ $_ } = 1 } @{ $p->{skip_undef} }; | ||
| 527 | |||
| 528 | my $only_changed = $self->ldap_update_only_changed; | ||
| 529 | |||
| 530 | ATTRIB: | ||
| 531 | foreach my $attr ( @{ $self->field_list } ) { | ||
| 532 | next ATTRIB if ( $no_update->{ $attr } ); | ||
| 533 | my $object_value = $self->{ $attr }; | ||
| 534 | next ATTRIB if ( $skip_undef->{ $attr } and ! defined $object_value ); | ||
| 535 | if ( $only_changed ) { | ||
| 536 | my @existing_values = $entry->get_value( $attr ); | ||
| 537 | $log->is_info && | ||
| 538 | $log->info( "Toggle for updating only changed values set.", | ||
| 539 | "Checking if ($attr) different: ", Dumper( $object_value ), | ||
| 540 | "vs.", Dumper( \@existing_values ) ); | ||
| 541 | next ATTRIB if ( $self->_values_are_same( $object_value, \@existing_values ) ); | ||
| 542 | $log->is_info && | ||
| 543 | $log->info( "Values for ($attr) are different. Updating..." ); | ||
| 544 | } | ||
| 545 | |||
| 546 | # Trick LDAP to updating object with multivalue property that | ||
| 547 | # has no values | ||
| 548 | |||
| 549 | if ( ref $object_value eq 'ARRAY' and scalar @{ $object_value } == 0 ) { | ||
| 550 | $object_value = undef; | ||
| 551 | } | ||
| 552 | $entry->replace( $attr, $object_value ); | ||
| 553 | } | ||
| 554 | $log->is_info && | ||
| 555 | $log->info( "Entry before Update:\n", Dumper( $entry ) ); | ||
| 556 | my $ldap_msg = $entry->update( $ldap ); | ||
| 557 | $self->_check_error( $ldap_msg, 'save' ); | ||
| 558 | $log->is_info && | ||
| 559 | $log->info( "Record updated ok." ); | ||
| 560 | } | ||
| 561 | |||
| 562 | |||
| 563 | # Return true if the two values are the same, false if not. | ||
| 564 | |||
| 565 | sub _values_are_same { | ||
| 566 | my ( $self, $val1, $val2 ) = @_; | ||
| 567 | $val1 = ( ref $val1 ) ? $val1 : [ $val1 ]; | ||
| 568 | $val2 = ( ref $val2 ) ? $val2 : [ $val2 ]; | ||
| 569 | my %v1 = map { $_ => 1 } @{ $val1 }; | ||
| 570 | my %v2 = map { $_ => 1 } @{ $val2 }; | ||
| 571 | foreach my $field ( keys %v1 ) { | ||
| 572 | return undef unless ( $v2{ $field } ); | ||
| 573 | } | ||
| 574 | foreach my $field ( keys %v2 ) { | ||
| 575 | return undef unless ( $v1{ $field } ); | ||
| 576 | } | ||
| 577 | return 1; | ||
| 578 | } | ||
| 579 | |||
| 580 | |||
| 581 | ######################################## | ||
| 582 | # REMOVE | ||
| 583 | ######################################## | ||
| 584 | |||
| 585 | sub remove { | ||
| 586 | my ( $self, $p ) = @_; | ||
| 587 | |||
| 588 | # Don't remove it unless it's been saved already | ||
| 589 | |||
| 590 | return undef unless ( $self->is_saved ); | ||
| 591 | |||
| 592 | my $level = SEC_LEVEL_WRITE; | ||
| 593 | unless ( $p->{skip_security} ) { | ||
| 594 | $level = $self->check_action_security({ required => SEC_LEVEL_WRITE }); | ||
| 595 | } | ||
| 596 | |||
| 597 | $log->is_info && | ||
| 598 | $log->info( "Security check passed ok. Continuing." ); | ||
| 599 | |||
| 600 | # Allow members to perform an action before getting removed | ||
| 601 | |||
| 602 | return undef unless ( $self->pre_remove_action( $p ) ); | ||
| 603 | |||
| 604 | # Do the removal, building the where clause if necessary | ||
| 605 | |||
| 606 | my $id = $self->id; | ||
| 607 | my $dn = $self->dn; | ||
| 608 | my $ldap = $p->{ldap} || $self->global_datasource_handle( $p->{connect_key} );; | ||
| 609 | my $ldap_msg = $ldap->delete( $dn ); | ||
| 610 | $self->_check_error( $ldap_msg, 'remove' ); | ||
| 611 | |||
| 612 | # Otherwise... | ||
| 613 | # ... remove this item from the cache | ||
| 614 | |||
| 615 | if ( $self->use_cache( $p ) ) { | ||
| 616 | $self->global_cache->clear({ data => $self }); | ||
| 617 | } | ||
| 618 | |||
| 619 | # ... execute any actions after a successful removal | ||
| 620 | |||
| 621 | return undef unless ( $self->post_remove_action( $p ) ); | ||
| 622 | |||
| 623 | # ... and log the deletion | ||
| 624 | |||
| 625 | $self->log_action( 'delete', $id ) unless ( $p->{skip_log} ); | ||
| 626 | |||
| 627 | # Clear flags | ||
| 628 | |||
| 629 | $self->clear_change; | ||
| 630 | $self->clear_save; | ||
| 631 | return 1; | ||
| 632 | } | ||
| 633 | |||
| 634 | |||
| 635 | ######################################## | ||
| 636 | # INTERNAL METHODS | ||
| 637 | ######################################## | ||
| 638 | |||
| 639 | # Error consolidation routine | ||
| 640 | |||
| 641 | sub _check_error { | ||
| 642 | my ( $class, $ldap_msg, $action ) = @_; | ||
| 643 | my $code = $ldap_msg->code; | ||
| 644 | return undef unless ( $code ); | ||
| 645 | SPOPS::Exception::LDAP->throw( | ||
| 646 | Net::LDAP::Util::ldap_error_desc( $code ), | ||
| 647 | { code => $code, | ||
| 648 | action => $action, | ||
| 649 | error_name => Net::LDAP::Util::ldap_error_name( $code ), | ||
| 650 | error_text => Net::LDAP::Util::ldap_error_text( $code ) } ); | ||
| 651 | } | ||
| 652 | |||
| 653 | |||
| 654 | # Build the full DN | ||
| 655 | |||
| 656 | sub build_dn { | ||
| 657 | my ( $item, $p ) = @_; | ||
| 658 | my $base_dn = $p->{base_dn} || $item->base_dn( $p->{connect_key} ); | ||
| 659 | my $id_field = $p->{id_field} || $item->id_field; | ||
| 660 | my $id_value_field = $p->{id_value_field} || $item->id_value_field; | ||
| 661 | my $id_value = $p->{id}; | ||
| 662 | unless ( $id_value ) { | ||
| 663 | unless ( ref $item ) { | ||
| 664 | SPOPS::Exception->throw( | ||
| 665 | "Cannot create DN for object without an ID value as " . | ||
| 666 | "parameter when called as class method" ); | ||
| 667 | } | ||
| 668 | $id_value = $item->{ $id_value_field } || $item->id; | ||
| 669 | unless ( $id_value ) { | ||
| 670 | SPOPS::Exception->throw( | ||
| 671 | "Cannot create DN for object without an ID value" ); | ||
| 672 | } | ||
| 673 | } | ||
| 674 | unless ( $id_field and $id_value and $base_dn ) { | ||
| 675 | SPOPS::Exception->throw( | ||
| 676 | "Cannot create Base DN without all parts: ", | ||
| 677 | "field: [$id_field]; ID: [$id_value]; BaseDN: [$base_dn]" ); | ||
| 678 | } | ||
| 679 | return join( ',', join( '=', $id_field, $id_value ), $base_dn ); | ||
| 680 | } | ||
| 681 | |||
| 682 | 1; | ||
| 683 | |||
| 684 | __END__ |