[Bps-public-commit] RT-Extension-MergeUsers branch, shredder-compatibility, created. 0.12-5-gba4cedb
Jim Brandt
jbrandt at bestpractical.com
Tue Jun 10 16:05:32 EDT 2014
The branch, shredder-compatibility has been created
at ba4cedb2d57cb2b206e25801a7bf6dd6165d7222 (commit)
- Log -----------------------------------------------------------------
commit ba0799cc3b2f90beacdc529311388aae4b780e17
Author: Jim Brandt <jbrandt at bestpractical.com>
Date: Tue Jun 10 15:24:42 2014 -0400
Confirm loading effective id, return original on failure
When running shredder to clean up users, the effective id
for a merged ticket can be shredded. Add error checking
when loading an effective id to detect this case. On
failure, return the id of the current user id and warn
that the user it was merged into is missing.
diff --git a/lib/RT/Extension/MergeUsers.pm b/lib/RT/Extension/MergeUsers.pm
index 3172081..da07605 100644
--- a/lib/RT/Extension/MergeUsers.pm
+++ b/lib/RT/Extension/MergeUsers.pm
@@ -147,7 +147,8 @@ sub LoadByCols {
);
if ( $effective_id->id && $effective_id->Content && $effective_id->Content != $oid ) {
$self->LoadByCols( id => $effective_id->Content );
- $EFFECTIVE_ID_CACHE{ $oid } = $self->id;
+ $EFFECTIVE_ID_CACHE{ $oid } = $self->id
+ if $self->Id;
} else {
$EFFECTIVE_ID_CACHE{ $oid } = undef;
}
@@ -156,6 +157,13 @@ sub LoadByCols {
$self->LoadByCols( id => $EFFECTIVE_ID_CACHE{ $oid } );
}
+ if ( not $self->Id ){
+ # Unable to load the effective user, so return actual user
+ RT::Logger->warning("Unable to load user by effective id. "
+ . "You may need to run rt-clean-merged-users if some users have been "
+ . "deleted or shredded.");
+ $self->SUPER::LoadByCols( Id => $oid );
+ }
return $self->id;
}
@@ -327,10 +335,16 @@ sub Next {
my ($effective_id) = $user->Attributes->Named("EffectiveId");
+ my $original_id = $user->Id;
if ($effective_id && $effective_id->Content && $effective_id->Content != $user->id) {
$user->LoadByCols(id =>$effective_id->Content);
}
- return $self->Next() if ($self->{seen_users}->{$user->id}++);
+ return $self->Next() if ($user->Id and $self->{seen_users}->{$user->id}++);
+
+ # Failed to load the effective user record for some reason, so expose
+ # this user again.
+ $user->LoadByCols( Id => $original_id )
+ unless $user->Id;
return $user;
}
commit b66d5cb9ec236a83a43eeafbe07cb96b041f1ec7
Author: Jim Brandt <jbrandt at bestpractical.com>
Date: Tue Jun 10 15:54:54 2014 -0400
Add script to clean out dangling attributes on merged users
When a user with another user merged into it is shredded,
the attributes on that user are also shredded, but the
merged user will remain, along with attributes that may point
to the now missing user id. This script cleans up attributes
if the merged-into user record is now gone.
diff --git a/.gitignore b/.gitignore
index cec9ded..faa588c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,4 +7,5 @@ pm_to_blib
/RT-Extension-MergeUsers*
/bin/rt-merge-users
/bin/rt-update-merged-users
+/bin/rt-clean-merged-users
/xt/tmp
diff --git a/Makefile.PL b/Makefile.PL
index 9f23bd5..63dc506 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -24,6 +24,7 @@ substitute( {
{ sufix => '.in' },
'bin/rt-update-merged-users',
'bin/rt-merge-users',
+ 'bin/rt-clean-merged-users',
);
no_index( package => 'RT::User', 'RT::Users', 'RT::Principal');
diff --git a/bin/rt-clean-merged-users.in b/bin/rt-clean-merged-users.in
new file mode 100755
index 0000000..40367da
--- /dev/null
+++ b/bin/rt-clean-merged-users.in
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+### before: #!@PERL@
+
+use strict;
+use warnings;
+
+### after: use lib qw(@RT_LIB_PATH@);
+use lib qw(/opt/rt4/local/lib /opt/rt4/lib);
+use RT;
+RT::LoadConfig();
+RT::Init();
+
+my $Attributes = RT::Attributes->new($RT::SystemUser);
+$Attributes->Limit( FIELD => 'Name', VALUE => 'EffectiveId' );
+$Attributes->Limit( FIELD => 'ObjectType', VALUE => 'RT::User' );
+
+my $primary_user = RT::User->new($RT::SystemUser);
+
+while ( my $Attr = $Attributes->Next ) {
+ $primary_user->LoadById($Attr->Content);
+
+ # Was the primary user deleted/shredded?
+ unless ( $primary_user and $primary_user->Id ){
+ RT::Logger->debug("Primary user missing. Clearing merge relationship for user id "
+ . $Attr->Content);
+ my $deleted_id = $Attr->Content; # Save for removing from content later
+ my ($ret, $msg) = $Attr->Delete;
+ RT::Logger->debug("Unable to delete attribute for " . $Attr->Content)
+ unless $ret;
+
+ my $secondary_user = RT::User->new($RT::SystemUser);
+ ($ret, $msg) = $secondary_user->LoadById($Attr->ObjectId);
+ RT::Logger->debug("Unable to load merged user " . $Attr->ObjectId . " $msg")
+ unless $ret;
+
+ my $merged_users = $secondary_user->GetMergedUsers;
+ if ( not $merged_users->Content ){
+ # Content is empty, delete attribute
+ $merged_users->Delete;
+ }
+ else{
+ # Clear the deleted user id
+ my @content = grep { $_ != $deleted_id } @{$merged_users->Content};
+ if ( scalar(@content) ){
+ $merged_users->SetContent(\@content);
+ }
+ else{
+ # It's now empty, so delete
+ $merged_users->Delete;
+ }
+ }
+ }
+}
+
+print "DONE.\n";
+exit 0;
commit ba4cedb2d57cb2b206e25801a7bf6dd6165d7222
Author: Jim Brandt <jbrandt at bestpractical.com>
Date: Tue Jun 10 16:03:44 2014 -0400
Add docs for new cleaner script
diff --git a/lib/RT/Extension/MergeUsers.pm b/lib/RT/Extension/MergeUsers.pm
index da07605..a17cce8 100644
--- a/lib/RT/Extension/MergeUsers.pm
+++ b/lib/RT/Extension/MergeUsers.pm
@@ -95,6 +95,17 @@ at a user that other users have been merged into. If you don't run this script,
you'll have issues unmerging users. It can be safely run multiple times, it will
only create Attributes as needed.
+=head1 UTILITIES
+
+=head2 rt-clean-merged-users
+
+When a user with another user merged into it is shredded,
+the attributes on that user are also shredded, but the
+merged user will remain, along with attributes that may point
+to the now missing user id. This script cleans up attributes
+if the merged-into user record is now gone. These users will then be
+converted back to regular unmerged users.
+
=cut
package RT::User;
-----------------------------------------------------------------------
More information about the Bps-public-commit
mailing list