[Bps-public-commit] r12960 - RT-Extension-MergeUsers/lib/RT

ruz at bestpractical.com ruz at bestpractical.com
Fri Jun 6 08:56:00 EDT 2008


Author: ruz
Date: Fri Jun  6 08:55:59 2008
New Revision: 12960

Modified:
   RT-Extension-MergeUsers/lib/RT/User_Vendor.pm

Log:
* clean up code

Modified: RT-Extension-MergeUsers/lib/RT/User_Vendor.pm
==============================================================================
--- RT-Extension-MergeUsers/lib/RT/User_Vendor.pm	(original)
+++ RT-Extension-MergeUsers/lib/RT/User_Vendor.pm	Fri Jun  6 08:55:59 2008
@@ -58,37 +58,39 @@
 
     # get the user whose email address this is
     my $canonical_user = RT::User->new($RT::SystemUser);
-    $canonical_user->LoadByCol( "EmailAddress", $address );
+    $canonical_user->LoadByCols( EmailAddress => $address );
+    return $address unless $canonical_user->id;
 
     # if we got a user, check for a parent
-    if ($canonical_user->Id) {
-        my ($effective_id) = $canonical_user->Attributes->Named("EffectiveId");
-        if (defined $effective_id) {
-            $canonical_user->LoadById($effective_id->Content);
-            # is there another parent user above this one?
-            return $canonical_user->CanonicalizeEmailAddress($canonical_user->EmailAddress)
-              if ($canonical_user->Id);
-        }
-    }
-    # we've hit the primary user
-    return $address;
+    my ($effective_id) = $canonical_user->Attributes->Named("EffectiveId");
+    return $address unless $effective_id && $effective_id->id;
+
+    $canonical_user->LoadByCols( id => $effective_id->Content );
+    return $address unless $canonical_user->id;
+
+    # is there another parent user above this one?
+    return $canonical_user->CanonicalizeEmailAddress(
+        $canonical_user->EmailAddress
+    );
 }
 
 sub LoadByCols {
     my $self = shift;
     $self->SUPER::LoadByCols(@_);
-    if ($self->id) {
-        my $effective_id = RT::Attribute->new($RT::SystemUser);
-        $effective_id->LoadByCols(Name => 'EffectiveId', ObjectId => $self->id);
-        if ($effective_id->id && $effective_id->Content && $effective_id->Content != $self->id) {
-            $self->LoadByCols(id =>$effective_id->Content);
-        }
+    return $self->id unless $self->id;
+
+    my $effective_id = RT::Attribute->new( $RT::SystemUser );
+    $effective_id->LoadByCols(Name => 'EffectiveId', ObjectId => $self->id);
+    if ( $effective_id->id && $effective_id->Content && $effective_id->Content != $self->id ) {
+        $self->LoadByCols( id => $effective_id->Content );
     }
-    return ($self->id);
+    return $self->id;
 }
 
-
-
+sub LoadOriginal {
+    my $self = shift;
+    return $self->SUPER::LoadByCols( @_ );
+}
 
 sub MergeInto {
     my $self = shift;
@@ -97,82 +99,83 @@
     # Load the user objects we were called with
     my $merge;
     if (ref $user) {
+        return (0, "User is not loaded") unless $user->id;
+
         $merge = RT::User->new($RT::SystemUser);
-        $merge->Load($user->Id);
+        $merge->Load($user->id);
+        return (0, "Could not reload user #". $user->id)
+            unless $merge->id;
     } else {
         $merge = RT::User->new($RT::SystemUser);
         $merge->Load($user);
+        return (0, "Could not load user '$user'") unless $merge->id;
     }
 
-    return (0, "Could not load @{[$merge->Name]}") unless $merge->Id;
-
     # Get copies of the canonicalized users
     my $email;
     if (defined $merge->Attributes->Named('EffectiveId')) {
         $email = $merge->CanonicalizeEmailAddress($merge->EmailAddress);
         $merge->LoadByEmail($email);
     }
-    return (0, "Could not load user to be merged") unless $merge->Id;
+    return (0, "Could not load user to be merged") unless $merge->id;
 
     my $canonical_self = RT::User->new($RT::SystemUser);
-    $canonical_self->Load($self->Id);
+    $canonical_self->Load($self->id);
     if (defined $canonical_self->Attributes->Named('EffectiveId')) {
         $email = $canonical_self->CanonicalizeEmailAddress($canonical_self->EmailAddress);
         $canonical_self->LoadByEmail($email);
     }
-    return (0, "Could not load user to merge into") unless $canonical_self->Id;
+    return (0, "Could not load user to merge into") unless $canonical_self->id;
 
     # No merging into yourself!
     return (0, "Could not merge @{[$merge->Name]} into itself")
-           if $merge->Id == $canonical_self->Id;
+           if $merge->id == $canonical_self->id;
 
     # No merging if the user you're merging into was merged into you
     # (ie. you're the primary address for this user)
     my ($new) = $merge->Attributes->Named("EffectiveId");
     return (0, "User @{[$canonical_self->Name]} has already been merged")
-           if defined $new and $new->Content == $canonical_self->Id;
+           if defined $new and $new->Content == $canonical_self->id;
 
     # do the merge
-    $canonical_self->SetAttribute(Name => "EffectiveId",
-                        Description => "Primary ID of this email address",
-                        Content => $merge->Id,
-                       );
-
-    $canonical_self->SetComments(join "\n", grep {/\S/} (
-                                               $canonical_self->Comments,
-                                               "Merged into ".$merge->EmailAddress." (".$merge->id.")",
-                                              )
-                      );
-    $merge->SetComments(join "\n", grep {/\S/} (
-                                                $merge->Comments,
-                                                $canonical_self->EmailAddress." (".$canonical_self->Id.") merged into this user",
-                                               )
-                       );
+    $canonical_self->SetAttribute(
+        Name => "EffectiveId",
+        Description => "Primary ID of this email address",
+        Content => $merge->id,
+    );
+
+    $canonical_self->SetComments( join "\n", grep /\S/,
+        $canonical_self->Comments,
+        "Merged into ". $merge->EmailAddress ." (". $merge->id .")",
+    );
+    $merge->SetComments(join "\n", grep /\S/,
+        $merge->Comments,
+        $canonical_self->EmailAddress." (".$canonical_self->id.") merged into this user",
+    );
+    return (1, "Merged users successfuly");
 }
 
 sub UnMerge {
     my $self = shift;
-  
+
     my ($current) = $self->Attributes->Named("EffectiveId");
-    return (0, "No parent user") unless $current;
-    
+    return (0, "Not a merged user") unless $current;
+
     my $merge = RT::User->new($RT::SystemUser);
-    $merge->Load($current->Content);
+    $merge->Load( $current->Content );
 
     $current->Delete;
-    $self->SetComments(join "\n", grep {/\S/} (
-                                               $self->Comments,
-                                               "Unmerged from ".$merge->EmailAddress." (".$merge->Id.")",
-                                              )
-                      );
-    
-    $merge->SetComments(join "\n", grep {/\S/} (
-                                                $merge->Comments,
-                                                $self->EmailAddress." (".$self->Id.") unmerged from this user",
-                                              )
-                      );
+    $self->SetComments( join "\n", grep /\S/,
+        $self->Comments,
+        "Unmerged from ". $merge->EmailAddress ." (".$merge->id.")",
+    );
+
+    $merge->SetComments(join "\n", grep /\S/,
+        $merge->Comments,
+        $self->EmailAddress ." (". $self->id .") unmerged from this user",
+    );
 
-    return ($merge->Id, "Unmerged from @{[$merge->EmailAddress]}");
+    return ($merge->id, "Unmerged from @{[$merge->EmailAddress]}");
 }
 
 1;



More information about the Bps-public-commit mailing list