[Bps-public-commit] RT-Extension-LDAPImport branch, master, updated. 0.20_01-44-gd8e904b

Alex Vandiver alexmv at bestpractical.com
Fri Jul 30 18:20:58 EDT 2010


The branch, master has been updated
       via  d8e904b4677d475c7c0e51d74988d1829f0ff664 (commit)
       via  e139b1a16731f4be374041bd51847ecc43062160 (commit)
       via  9ef5a3f59237690f21d1186ecfb0087687ab5566 (commit)
      from  5a62f59c8f3337fd86cdd99475beb684e69885d1 (commit)

Summary of changes:
 lib/RT/Extension/LDAPImport.pm |  232 +++++++++++++++-------------------------
 1 files changed, 87 insertions(+), 145 deletions(-)

- Log -----------------------------------------------------------------
commit 9ef5a3f59237690f21d1186ecfb0087687ab5566
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Fri Jul 30 16:47:43 2010 -0400

    Simplify setting of the dnlist cache

diff --git a/lib/RT/Extension/LDAPImport.pm b/lib/RT/Extension/LDAPImport.pm
index ff9c597..b7b63d9 100644
--- a/lib/RT/Extension/LDAPImport.pm
+++ b/lib/RT/Extension/LDAPImport.pm
@@ -161,6 +161,8 @@ sub import_users {
     my $mapping = $RT::LDAPMapping;
     return unless $self->_check_ldap_mapping( mapping => $mapping );
 
+    $self->_dnlist({});
+
     while (my $entry = $results->shift_entry) {
         my $user = $self->_build_object( ldap_entry => $entry, skip => qr/(?i)^CF\./, mapping => $mapping );
         $user->{Name} ||= $user->{EmailAddress};
@@ -192,9 +194,7 @@ sub _import_user {
     $self->_debug("Processing user $user->{Name}");
     my $user_obj = $self->create_rt_user( user => $user );
     return unless $user_obj;
-    my $dnlist = $self->_dnlist;
-    $dnlist->{lc $ldap_entry->dn} = $user->{Name};
-    $self->_dnlist($dnlist);
+    $self->_dnlist->{lc $ldap_entry->dn} = $user->{Name};
     $self->add_user_to_group( user => $user_obj );
     $self->add_custom_field_value( user => $user_obj, ldap_entry => $ldap_entry );
     return;

commit e139b1a16731f4be374041bd51847ecc43062160
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Fri Jul 30 16:51:44 2010 -0400

    Merge show and actual import paths for user import

diff --git a/lib/RT/Extension/LDAPImport.pm b/lib/RT/Extension/LDAPImport.pm
index b7b63d9..25563b9 100644
--- a/lib/RT/Extension/LDAPImport.pm
+++ b/lib/RT/Extension/LDAPImport.pm
@@ -170,11 +170,7 @@ sub import_users {
             $self->_warn("No Name or Emailaddress for user, skipping ".Dumper $user);
             next;
         }
-        if ($args{import}) {
-            $self->_import_user( user => $user, ldap_entry => $entry );
-        } else {
-            $self->_show_user( user => $user );
-        }
+        $self->_import_user( user => $user, ldap_entry => $entry, import => $args{import} );
     }
     return 1;
 }
@@ -188,47 +184,17 @@ The user has run us with --import, so bring data in
 sub _import_user {
     my $self = shift;
     my %args = @_;
-    my $user = $args{user};
-    my $ldap_entry = $args{ldap_entry};
-
-    $self->_debug("Processing user $user->{Name}");
-    my $user_obj = $self->create_rt_user( user => $user );
-    return unless $user_obj;
-    $self->_dnlist->{lc $ldap_entry->dn} = $user->{Name};
-    $self->add_user_to_group( user => $user_obj );
-    $self->add_custom_field_value( user => $user_obj, ldap_entry => $ldap_entry );
-    return;
-}
-
-=head2 _show_user
-
-Show debugging information about the user record we're going to import
-when the users reruns us with --import
 
-=cut
+    $self->_debug("Processing user $args{user}{Name}");
+    $self->_dnlist->{lc $args{ldap_entry}->dn} = $args{user}{Name};
 
-sub _show_user {
-    my $self = shift;
-    my %args = @_;
-    my $user = $args{user};
+    $args{user} = $self->create_rt_user( %args );
+    return unless $args{user};
 
-    my $rt_user = $self->_load_rt_user(%args);
+    $self->add_user_to_group( %args );
+    $self->add_custom_field_value( %args );
 
-    if ( $rt_user->Id ) {
-        if ( $RT::LDAPUpdateUsers || $RT::LDAPUpdateOnly ) {
-            print "Found existing user $user->{Name} to update\n";
-            $self->_show_user_info( %args, rt_user => $rt_user );
-        } else {
-            print "Found existing user $user->{Name} skipping\n";
-        }
-    } else {
-        if ( $RT::LDAPUpdateOnly ) {
-            print "$user->{Name} doesn't exist in RT, skipping\n";
-        } else {
-            print "Found new user $user->{Name} to create in RT\n";
-            $self->_show_user_info( %args );
-        }
-    }
+    return 1;
 }
 
 sub _show_user_info {
@@ -373,25 +339,34 @@ sub create_rt_user {
         my $message = "User $user->{Name} already exists as ".$user_obj->Id;
         if ($RT::LDAPUpdateUsers || $RT::LDAPUpdateOnly) {
             $self->_debug("$message, updating their data");
-            my @results = $user_obj->Update( ARGSRef => $user, AttributesRef => [keys %$user] );
-            $self->_debug(join("\n", at results)||'no change');
+            if ($args{import}) {
+                my @results = $user_obj->Update( ARGSRef => $user, AttributesRef => [keys %$user] );
+                $self->_debug(join("\n", at results)||'no change');
+            } else {
+                $self->_debug("Found existing user $user->{Name} to update");
+                $self->_show_user_info( %args, rt_user => $user_obj );
+            }
         } else {
             $self->_debug("$message, skipping");
         }
-    }
-
-    if ( !$user_obj->Id ) {
+    } else {
         if ( $RT::LDAPUpdateOnly ) {
             $self->_debug("User $user->{Name} doesn't exist in RT, skipping");
             return;
         } else {
-            my ($val, $msg) = $user_obj->Create( %$user, Privileged => 0 );
-
-            unless ($val) {
-                $self->_error("couldn't create user_obj for $user->{Name}: $msg");
+            if ($args{import}) {
+                my ($val, $msg) = $user_obj->Create( %$user, Privileged => 0 );
+
+                unless ($val) {
+                    $self->_error("couldn't create user_obj for $user->{Name}: $msg");
+                    return;
+                }
+                $self->_debug("Created user for $user->{Name} with id ".$user_obj->Id);
+            } else {
+                print "Found new user $user->{Name} to create in RT\n";
+                $self->_show_user_info( %args );
                 return;
             }
-            $self->_debug("Created user for $user->{Name} with id ".$user_obj->Id);
         }
     }
 
@@ -441,14 +416,18 @@ sub add_user_to_group {
         return;
     }
 
-    my ($status, $msg) = $group->AddMember($principal->Id);
-    if ($status) {
-        $self->_debug("Added ".$user->Name." to ".$group->Name." [$msg]");
+    if ($args{import}) {
+        my ($status, $msg) = $group->AddMember($principal->Id);
+        if ($status) {
+            $self->_debug("Added ".$user->Name." to ".$group->Name." [$msg]");
+        } else {
+            $self->_error("Couldn't add ".$user->Name." to ".$group->Name." [$msg]");
+        }
+        return $status;
     } else {
-        $self->_error("Couldn't add ".$user->Name." to ".$group->Name." [$msg]");
+        $self->_debug("Would add to ".$group->Name);
+        return;
     }
-
-    return $status;
 }
 
 =head2 setup_group
@@ -524,11 +503,15 @@ sub add_custom_field_value {
             next;
         }
 
-        ($status, $msg) = $cf->AddValue( Name => $cfv_name );
-        if ($status) {
-            $self->_debug("Added '$cfv_name' to Custom Field '$cf_name' [$msg]");
+        if ($args{import}) {
+            ($status, $msg) = $cf->AddValue( Name => $cfv_name );
+            if ($status) {
+                $self->_debug("Added '$cfv_name' to Custom Field '$cf_name' [$msg]");
+            } else {
+                $self->_error("Couldn't add '$cfv_name' to '$cf_name' [$msg]");
+            }
         } else {
-            $self->_error("Couldn't add '$cfv_name' to '$cf_name' [$msg]");
+            $self->_debug("Would add '$cfv_name' to Custom Field '$cf_name'");
         }
     }
 

commit d8e904b4677d475c7c0e51d74988d1829f0ff664
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Fri Jul 30 18:22:49 2010 -0400

    Unify display and import paths for group import

diff --git a/lib/RT/Extension/LDAPImport.pm b/lib/RT/Extension/LDAPImport.pm
index 25563b9..ef86ac1 100644
--- a/lib/RT/Extension/LDAPImport.pm
+++ b/lib/RT/Extension/LDAPImport.pm
@@ -555,11 +555,7 @@ sub import_groups {
             $self->_warn("No Name for group, skipping ".Dumper $group);
             next;
         }
-        if ($args{import}) {
-            $self->_import_group( group => $group, ldap_entry => $entry );
-        } else {
-            $self->_show_group( group => $group, ldap_entry => $entry );
-        }
+        $self->_import_group( %args, group => $group, ldap_entry => $entry );
     }
     return 1;
 }
@@ -598,9 +594,9 @@ sub _import_group {
     my $ldap_entry = $args{ldap_entry};
 
     $self->_debug("Processing group $group->{Name}");
-    my $group_obj = $self->create_rt_group( group => $group );
-    return unless $group_obj;
-    $self->add_group_members( group => $group_obj, ldap_entry => $ldap_entry );
+    my $group_obj = $self->create_rt_group( %args, group => $group );
+    return if $args{import} and not $group_obj;
+    $self->add_group_members( %args, name => $group->{Name}, group => $group_obj, ldap_entry => $ldap_entry );
     return;
 }
 
@@ -627,26 +623,35 @@ sub create_rt_group {
     if ($group_obj->Id) {
         my $message = "Group $group->{Name} already exists as ".$group_obj->Id;
         if ($RT::LDAPUpdateOnly) {
-            $self->_debug("$message, updating their data");
-            my @results = $group_obj->Update( ARGSRef => $group, AttributesRef => [keys %$group] );
-            $self->_debug(join("\n", at results)||'no change');
+            if ($args{import}) {
+                $self->_debug("$message, updating their data");
+                my @results = $group_obj->Update( ARGSRef => $group, AttributesRef => [keys %$group] );
+                $self->_debug(join("\n", at results)||'no change');
+            } else {
+                print "Found existing group $group->{Name} to update\n";
+                $self->_show_group_info( %args, rt_group => $group_obj );
+            }
         } else {
             $self->_debug("$message, skipping");
         }
-    }
-
-    if ( !$group_obj->Id ) {
+    } else {
         if ( $RT::LDAPUpdateOnly ) {
             $self->_debug("Group $group->{Name} doesn't exist in RT, skipping");
             return;
         }
-        my ($val, $msg) = $group_obj->CreateUserDefinedGroup( %$group );
 
-        unless ($val) {
-            $self->_error("couldn't create group_obj for $group->{Name}: $msg");
+        if ($args{import}) {
+            my ($val, $msg) = $group_obj->CreateUserDefinedGroup( %$group );
+            unless ($val) {
+                $self->_error("couldn't create group_obj for $group->{Name}: $msg");
+                return;
+            }
+            $self->_debug("Created group for $group->{Name} with id ".$group_obj->Id);
+        } else {
+            print "Found new group $group->{Name} to create in RT\n";
+            $self->_show_group_info( %args );
             return;
         }
-        $self->_debug("Created group for $group->{Name} with id ".$group_obj->Id);
     }
 
     unless ($group_obj->Id) {
@@ -670,7 +675,7 @@ sub add_group_members {
     my $self = shift;
     my %args = @_;
     my $group = $args{group};
-    my $groupname = $group->Name;
+    my $groupname = $args{name};
     my $ldap_entry = $args{ldap_entry};
 
     $self->_debug("Processing group membership for $groupname");
@@ -682,22 +687,21 @@ sub add_group_members {
         return;
     }
 
-    my $rt_group_members;
-    my $user_members = $group->UserMembersObj;
-    while ( my $member = $user_members->Next ) {
-        $rt_group_members->{$member->Name}++;
+    my $rt_group_members = {};
+    if ($args{group}) {
+        my $user_members = $group->UserMembersObj;
+        while ( my $member = $user_members->Next ) {
+            $rt_group_members->{$member->Name}++;
+        }
+    } elsif (not $args{import}) {
+        $self->_debug("No group in RT, would create with members:");
     }
 
     my $dnlist = $self->_dnlist;
     foreach my $member (@$members) {
         my $username;
         if (exists $dnlist->{lc $member}) {
-            if ($username = $dnlist->{lc $member}) {
-                $self->_debug("Found $username in cache for $member");
-            } else {
-                $self->_debug("Negative cache in cache for $member");
-                next;
-            }
+            next unless $username = $dnlist->{lc $member};
         } else {
             my $ldap_users = $self->_run_search(
                 base   => $member,
@@ -712,9 +716,12 @@ sub add_group_members {
             $dnlist->{lc $member} = $username = $ldap_user->get_value($RT::LDAPMapping->{Name});
         }
         if ( delete $rt_group_members->{$username} ) {
-            $self->_debug("$username is already a member of $groupname skipping");
+            $self->_debug("\t$username\tin RT and LDAP");
             next;
         }
+        $self->_debug($group ? "\t$username\tin LDAP, adding to RT" : "\t$username");
+        next unless $args{import};
+
         my $rt_user = RT::User->new($RT::SystemUser);
         my ($res,$msg) = $rt_user->Load( $username );
         unless ($res) {
@@ -725,22 +732,22 @@ sub add_group_members {
         unless ($res) {
             $self->_warn("Failed to add $username to $groupname: $msg");
         }
-        $self->_debug("Added $username to $groupname");
     }
 
     for my $username (sort keys %$rt_group_members) {
+        $self->_debug("\t$username\tin RT, not in LDAP, removing");
+        next unless $args{import};
+
         my $rt_user = RT::User->new($RT::SystemUser);
         my ($res,$msg) = $rt_user->Load( $username );
         unless ($res) {
             $self->_warn("Unable to load $username: $msg");
             next;
         }
-        $self->_debug("Removing $username from $groupname because they are not a member in LDAP");
         ($res,$msg) = $group->DeleteMember($rt_user->PrincipalObj->Id);
         unless ($res) {
             $self->_warn("Failed to remove $username to $groupname: $msg");
         }
-
     }
 }
 
@@ -799,54 +806,6 @@ sub _show_group_info {
         $old_value ||= 'unset';
         print "\t$key\t$old_value => $group->{$key}\n";
     }
-
-    my $members = $self->_get_group_members_from_ldap(%args);
-
-    my $ldap_members;
-    my $dnlist = $self->_dnlist;
-    foreach my $member (@$members) {
-        my $username;
-        if (exists $dnlist->{lc $member}) {
-            if ($username = $dnlist->{lc $member}) {
-                $self->_debug("Found $username in cache for $member");
-            } else {
-                $self->_debug("Negative cache in cache for $member");
-                next;
-            }
-        } else {
-            my $ldap_users = $self->_run_search(
-                base   => $member,
-                filter => $RT::LDAPFilter,
-            );
-            unless ( $ldap_users && $ldap_users->count ) {
-                $dnlist->{lc $member} = undef;
-                $self->_error("No user found for $member who should be a member of $group->{Name}");
-                next;
-            }
-            my $ldap_user = $ldap_users->shift_entry;
-            $dnlist->{lc $member} = $username = $ldap_user->get_value($RT::LDAPMapping->{Name});
-        }
-        $ldap_members->{$username}++;
-    }
-    my $rt_members;
-    if ($rt_group) {
-        my $user_members = $rt_group->UserMembersObj;
-        while ( my $member = $user_members->Next ) {
-            $rt_members->{$member->Name}++;
-        }
-        print "Comparing members in LDAP and RT\n";
-        foreach my $username (sort keys %$ldap_members) {
-            if ( delete $rt_members->{$username} ) {
-                print "\t$username\t in RT and LDAP\n";
-            } else {
-                print "\t$username\t in LDAP, will add to RT\n";
-            }
-        }
-        map { print "\t$_\t In RT, not LDAP, will remove from RT\n" } sort keys %$rt_members;
-    } else {
-        print "No existing group, adding the following members\n";
-        map { print "$_\n" } sort keys %$ldap_members;
-    }
 }
 
 

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list