[Rt-commit] rt branch 5.0/add-more-shredder-links created. rt-5.0.3-24-g61255dcdeb

BPS Git Server git at git.bestpractical.com
Thu Jul 21 22:59:20 UTC 2022


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 5.0/add-more-shredder-links has been created
        at  61255dcdeba9f1ad7bbfe318c5e4252175961ffb (commit)

- Log -----------------------------------------------------------------
commit 61255dcdeba9f1ad7bbfe318c5e4252175961ffb
Author: Brian Conry <bconry at bestpractical.com>
Date:   Thu Jul 21 13:50:36 2022 -0500

    Add shredder links to Asset and Transaction search
    
    This change provides users with the SuperUser right with a link under
    the Feeds menu to got to the Shredder page directly from the Asset and
    Transaction search results pages.

diff --git a/lib/RT/Interface/Web/MenuBuilder.pm b/lib/RT/Interface/Web/MenuBuilder.pm
index 7260a75c78..1e0c071691 100644
--- a/lib/RT/Interface/Web/MenuBuilder.pm
+++ b/lib/RT/Interface/Web/MenuBuilder.pm
@@ -749,21 +749,21 @@ sub BuildMainNav {
                     $current_user->UserObj->GenerateAuthString( $rss_data{Query} ),
                     $rss_data{Query};
                 $more->child( ical => title => loc('iCal'), path => '/NoAuth/iCal/' . $ical_path );
+            }
 
-                #XXX TODO better abstraction of SuperUser right check
-                if ( $current_user->HasRight( Right => 'SuperUser', Object => RT->System ) ) {
-                    my $shred_args = QueryString(
-                        Search          => 1,
-                        Plugin          => 'Tickets',
-                        'Tickets:query' => $rss_data{'Query'},
-                        'Tickets:limit' => $query_args->{'Rows'},
-                    );
+            if ( $current_user->HasRight( Right => 'SuperUser', Object => RT->System ) ) {
+                my ( $plugin ) = ( $class =~ /^RT::(.*)$/ );
+                my $shred_args = QueryString(
+                    Search          => 1,
+                    Plugin          => $plugin,
+                    "$plugin:query" => $query_args->{'Query'} || $fallback_query_args{'Query'} || '',
+                    "$plugin:limit" => $query_args->{'Rows'},
+                );
 
-                    $more->child(
-                        shredder => title => loc('Shredder'),
-                        path     => '/Admin/Tools/Shredder/?' . $shred_args
-                    );
-                }
+                $more->child(
+                    shredder => title => loc('Shredder'),
+                    path     => '/Admin/Tools/Shredder/?' . $shred_args
+                );
             }
         }
     }

commit a58025d78bbaaa13e02234d57667bb9aec79dcab
Author: Brian Conry <bconry at bestpractical.com>
Date:   Thu Jul 21 13:48:21 2022 -0500

    Add Shredder Plugin for Assets
    
    This change adds support for shredding assets, their associated role
    groups, and (optionally) other linked assets.

diff --git a/lib/RT/Asset.pm b/lib/RT/Asset.pm
index e313db5d63..e0a5b81e96 100644
--- a/lib/RT/Asset.pm
+++ b/lib/RT/Asset.pm
@@ -684,6 +684,34 @@ sub FindDependencies {
     $deps->Add( out => $self->CatalogObj );
 }
 
+sub __DependsOn {
+    my $self = shift;
+    my %args = (
+        Shredder => undef,
+        Dependencies => undef,
+        @_,
+    );
+    my $deps = $args{'Dependencies'};
+    my $list = [];
+
+    # Asset role groups( Owner, Held By, Contact )
+    my $objs = RT::Groups->new( $self->CurrentUser );
+    $objs->Limit( FIELD => 'Domain', VALUE => 'RT::Asset-Role', CASESENSITIVE => 0 );
+    $objs->Limit( FIELD => 'Instance', VALUE => $self->Id );
+    push( @$list, $objs );
+
+    #TODO: Users, Catalogs if we wish export tool
+    $deps->_PushDependencies(
+        BaseObject => $self,
+        Flags => RT::Shredder::Constants::DEPENDS_ON,
+        TargetObjects => $list,
+        Shredder => $args{'Shredder'}
+    );
+
+    return $self->SUPER::__DependsOn( %args );
+}
+
+
 sub CategoryObj {
     my $self = shift;
     return $self->CatalogObj;
diff --git a/lib/RT/Shredder.pm b/lib/RT/Shredder.pm
index 9a103ee3a3..71c643fa8b 100644
--- a/lib/RT/Shredder.pm
+++ b/lib/RT/Shredder.pm
@@ -235,6 +235,7 @@ BEGIN {
 
 our @SUPPORTED_OBJECTS = qw(
     ACE
+    Asset
     Attachment
     CachedGroupMember
     CustomField
diff --git a/lib/RT/Shredder/Plugin/Assets.pm b/lib/RT/Shredder/Plugin/Assets.pm
new file mode 100644
index 0000000000..05950e484d
--- /dev/null
+++ b/lib/RT/Shredder/Plugin/Assets.pm
@@ -0,0 +1,156 @@
+# BEGIN BPS TAGGED BLOCK {{{
+#
+# COPYRIGHT:
+#
+# This software is Copyright (c) 1996-2022 Best Practical Solutions, LLC
+#                                          <sales at bestpractical.com>
+#
+# (Except where explicitly superseded by other copyright notices)
+#
+#
+# LICENSE:
+#
+# This work is made available to you under the terms of Version 2 of
+# the GNU General Public License. A copy of that license should have
+# been provided with this software, but in any event can be snarfed
+# from www.gnu.org.
+#
+# This work is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 or visit their web page on the internet at
+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
+#
+#
+# CONTRIBUTION SUBMISSION POLICY:
+#
+# (The following paragraph is not intended to limit the rights granted
+# to you to modify and distribute this software under the terms of
+# the GNU General Public License and is only of importance to you if
+# you choose to contribute your changes and enhancements to the
+# community by submitting them to Best Practical Solutions, LLC.)
+#
+# By intentionally submitting any modifications, corrections or
+# derivatives to this work, or any other work intended for use with
+# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+# you are the copyright holder for those contributions and you grant
+# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+# royalty-free, perpetual, license to use, copy, create derivative
+# works based on those contributions, and sublicense and distribute
+# those contributions and any derivatives thereof.
+#
+# END BPS TAGGED BLOCK }}}
+
+package RT::Shredder::Plugin::Assets;
+
+use strict;
+use warnings FATAL => 'all';
+use base qw(RT::Shredder::Plugin::Base::Search);
+
+=head1 NAME
+
+RT::Shredder::Plugin::Assets - search plugin for wiping Assets.
+
+=head1 ARGUMENTS
+
+=head2 query - query string
+
+Search Assets with query string.
+Examples:
+  Catalog = 'my catalog' AND ( Status = 'deleted' OR Status = 'recyled' )
+  LastUpdated < '2013-12-31 23:59:59'
+
+B<Hint:> You can construct query with the query builder in RT's web
+interface and then open advanced page and copy query string.
+
+=head2 with_linked - boolean
+
+Deletes all Assets that are linked to Assets that match L<query>.
+
+=head2 apply_query_to_linked - boolean
+
+Delete linked Assets only if those too match L<query>.
+
+See also L<with_linked>.
+
+=cut
+
+sub SupportArgs { return $_[0]->SUPER::SupportArgs, qw(query with_linked apply_query_to_linked) }
+
+# used to genrate checkboxes instead of text fields in the web interface
+sub ArgIsBoolean {
+    my( $self, $arg ) = @_;
+    my %boolean_atts = map { $_ => 1 } qw( with_linked apply_query_to_linked );
+    return $boolean_atts{$arg};
+}
+
+sub TestArgs
+{
+    my $self = shift;
+    my %args = @_;
+    my $queue;
+    if( $args{'query'} ) {
+        my $objs = RT::Assets->new( RT->SystemUser );
+        $objs->{'allow_deleted_search'} = 1;
+        my ($status, $msg) = $objs->FromSQL( $args{'query'} );
+        return( 0, "Bad query argument, error: $msg" ) unless $status;
+        $self->{'opt'}{'objects'} = $objs;
+    }
+    $args{'with_linked'} = 1 if $args{'apply_query_to_linked'};
+
+    return $self->SUPER::TestArgs( %args );
+}
+
+sub Run
+{
+    my $self = shift;
+    my $objs = $self->{'opt'}{'objects'}
+        or return (1, undef);
+
+    $objs->OrderByCols( { FIELD => 'id', ORDER => 'ASC' } );
+    unless ( $self->{'opt'}{'with_linked'} ) {
+        if( $self->{'opt'}{'limit'} ) {
+            $objs->RowsPerPage( $self->{'opt'}{'limit'} );
+        }
+        return (1, $objs);
+    }
+
+    my (@top, @linked, %seen);
+    $self->FetchNext($objs, 1);
+    while ( my $obj = $self->FetchNext( $objs ) ) {
+        next if $seen{ $obj->id }++;
+        push @linked, $self->GetLinked( Object => $obj, Seen => \%seen );
+        push @top, $obj;
+        last if $self->{'opt'}{'limit'}
+                && @top >= $self->{'opt'}{'limit'};
+    }
+    return (1, @top, @linked);
+}
+
+sub GetLinked
+{
+    my $self = shift;
+    my %arg = @_;
+    my @res = ();
+    my $query = "Linked = 'asset:" . $arg{'Object'}->id . "'";
+    if ( $self->{'opt'}{'apply_query_to_linked'} ) {
+        $query .= " AND ( ". $self->{'opt'}{'query'} ." )";
+    }
+    my $objs = RT::Assets->new( RT->SystemUser );
+    $objs->{'allow_deleted_search'} = 1;
+    $objs->FromSQL( $query );
+    $self->FetchNext( $objs, 1 );
+    while ( my $linked_obj = $self->FetchNext( $objs ) ) {
+        next if $arg{'Seen'}->{ $linked_obj->id }++;
+        push @res, $self->GetLinked( %arg, Object => $linked_obj );
+        push @res, $linked_obj;
+    }
+    return @res;
+}
+
+1;
diff --git a/t/shredder/03plugin.t b/t/shredder/03plugin.t
index c8e278d3e4..32aa216856 100644
--- a/t/shredder/03plugin.t
+++ b/t/shredder/03plugin.t
@@ -3,10 +3,10 @@ use strict;
 use warnings;
 
 use Test::Deep;
-use RT::Test::Shredder nodb => 1, tests => 31;
+use RT::Test::Shredder nodb => 1, tests => 34;
 my $test = "RT::Test::Shredder";
 
-my @PLUGINS = sort qw(Attachments Base Objects SQLDump Summary Tickets Transactions Users);
+my @PLUGINS = sort qw(Assets Attachments Base Objects SQLDump Summary Tickets Transactions Users);
 
 use_ok('RT::Shredder::Plugin');
 {
diff --git a/t/shredder/03plugin_assets.t b/t/shredder/03plugin_assets.t
new file mode 100644
index 0000000000..ed7753b7b6
--- /dev/null
+++ b/t/shredder/03plugin_assets.t
@@ -0,0 +1,148 @@
+
+use strict;
+use warnings;
+
+use Test::Deep;
+use RT::Test::Shredder tests => 49;
+my $test = "RT::Test::Shredder";
+
+use_ok('RT::Shredder');
+
+use_ok('RT::Shredder::Plugin::Assets');
+{
+    my $plugin = RT::Shredder::Plugin::Assets->new;
+    isa_ok($plugin, 'RT::Shredder::Plugin::Assets');
+
+    is(lc $plugin->Type, 'search', 'correct type');
+}
+
+$test->create_savepoint('clean');
+use_ok('RT::Asset');
+use_ok('RT::Assets');
+
+{ # create parent and child and check functionality of 'with_linked' arg
+    my $parent = RT::Asset->new( RT->SystemUser );
+    my ($pid) = $parent->Create( Name => 'parent', Catalog => 1 );
+    ok( $pid, "created new asset" );
+    my $child = RT::Asset->new( RT->SystemUser );
+    my ($cid) = $child->Create( Name => 'child', Catalog => 1, MemberOf => "asset:$pid" );
+    ok( $cid, "created new asset" );
+
+    my $plugin = RT::Shredder::Plugin::Assets->new;
+    isa_ok($plugin, 'RT::Shredder::Plugin::Assets');
+
+    my ($status, $msg, @objs);
+    ($status, $msg) = $plugin->TestArgs( query => 'Name = "parent"' );
+    ok($status, "plugin arguments are ok") or diag "error: $msg";
+
+    ($status, @objs) = $plugin->Run;
+    ok($status, "executed plugin successfully") or diag "error: @objs";
+    @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
+    is(scalar @objs, 1, "only one object in result set");
+    is($objs[0]->id, $pid, "parent is in result set");
+
+    ($status, $msg) = $plugin->TestArgs( query => 'Name = "parent"', with_linked => 1 );
+    ok($status, "plugin arguments are ok") or diag "error: $msg";
+
+    ($status, @objs) = $plugin->Run;
+    ok($status, "executed plugin successfully") or diag "error: @objs";
+    @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
+    my %has = map { $_->id => 1 } @objs;
+    is(scalar @objs, 2, "two objects in the result set");
+    ok($has{$pid}, "parent is in the result set");
+    ok($has{$cid}, "child is in the result set");
+
+    my $shredder = $test->shredder_new();
+    $shredder->PutObjects( Objects => \@objs );
+    $shredder->WipeoutAll;
+    $test->db_is_valid;
+}
+cmp_deeply( $test->dump_current_and_savepoint('clean'), "current DB equal to savepoint");
+
+{ # create parent and child and link them reqursively to check that we don't hang
+    my $parent = RT::Asset->new( RT->SystemUser );
+    my ($pid) = $parent->Create( Name => 'parent', Catalog => 1 );
+    ok( $pid, "created new asset" );
+
+    my $child = RT::Asset->new( RT->SystemUser );
+    my ($cid) = $child->Create( Name => 'child', Catalog => 1, MemberOf => "asset:$pid" );
+    ok( $cid, "created new asset" );
+
+    my ($status, $msg) = $child->AddLink( Target => "asset:$pid", Type => 'DependsOn' );
+    ok($status, "added reqursive link") or diag "error: $msg";
+
+    my $plugin = RT::Shredder::Plugin::Assets->new;
+    isa_ok($plugin, 'RT::Shredder::Plugin::Assets');
+
+    my (@objs);
+    ($status, $msg) = $plugin->TestArgs( query => 'Name = "parent"' );
+    ok($status, "plugin arguments are ok") or diag "error: $msg";
+
+    ($status, @objs) = $plugin->Run;
+    ok($status, "executed plugin successfully") or diag "error: @objs";
+    @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
+    is(scalar @objs, 1, "only one object in result set");
+    is($objs[0]->id, $pid, "parent is in result set");
+
+    ($status, $msg) = $plugin->TestArgs( query => 'Name = "parent"', with_linked => 1 );
+    ok($status, "plugin arguments are ok") or diag "error: $msg";
+
+    ($status, @objs) = $plugin->Run;
+    ok($status, "executed plugin successfully") or diag "error: @objs";
+    @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
+    is(scalar @objs, 2, "two objects in the result set");
+    my %has = map { $_->id => 1 } @objs;
+    ok($has{$pid}, "parent is in the result set");
+    ok($has{$cid}, "child is in the result set");
+
+    my $shredder = $test->shredder_new();
+    $shredder->PutObjects( Objects => \@objs );
+    $shredder->WipeoutAll;
+    $test->db_is_valid;
+}
+cmp_deeply( $test->dump_current_and_savepoint('clean'), "current DB equal to savepoint");
+
+{ # create parent and child and check functionality of 'apply_query_to_linked' arg
+    my $parent = RT::Asset->new( RT->SystemUser );
+    my ($pid) = $parent->Create( Name => 'parent', Catalog => 1 );
+    ok( $pid, "created new asset" );
+    $parent->SetStatus('stolen');
+
+    my $child1 = RT::Asset->new( RT->SystemUser );
+    my ($cid1) = $child1->Create( Name => 'child1', Catalog => 1, MemberOf => "asset:$pid" );
+    ok( $cid1, "created new asset" );
+    my $child2 = RT::Asset->new( RT->SystemUser );
+    my ($cid2) = $child2->Create( Name => 'child2', Catalog => 1, MemberOf => "asset:$pid" );
+    ok( $cid2, "created new asset" );
+    $child2->SetStatus('stolen');
+
+    my $plugin = RT::Shredder::Plugin::Assets->new;
+    isa_ok($plugin, 'RT::Shredder::Plugin::Assets');
+
+    my ($status, $msg) = $plugin->TestArgs( query => 'Status = "stolen"', apply_query_to_linked => 1 );
+    ok($status, "plugin arguments are ok") or diag "error: $msg";
+
+    my @objs;
+    ($status, @objs) = $plugin->Run;
+    ok($status, "executed plugin successfully") or diag "error: @objs";
+    @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
+    is(scalar @objs, 2, "two objects in the result set");
+    my %has = map { $_->id => 1 } @objs;
+    ok($has{$pid}, "parent is in the result set");
+    ok(!$has{$cid1}, "first child is in the result set");
+    ok($has{$cid2}, "second child is in the result set");
+
+    my $shredder = $test->shredder_new();
+    $shredder->PutObjects( Objects => \@objs );
+    $shredder->WipeoutAll;
+    $test->db_is_valid;
+
+    my $asset = RT::Asset->new( RT->SystemUser );
+    $asset->Load( $cid1 );
+    is($asset->id, $cid1, 'loaded asset');
+
+    $shredder->PutObjects( Objects => $asset );
+    $shredder->WipeoutAll;
+    $test->db_is_valid;
+}
+cmp_deeply( $test->dump_current_and_savepoint('clean'), "current DB equal to savepoint");

commit 2b3175edee077d7e096702d7558751e87d0cc3c3
Author: Brian Conry <bconry at bestpractical.com>
Date:   Thu Jul 21 13:43:18 2022 -0500

    Add Shredder Plugin for Transactions
    
    This change allows transactions to be directly targeted for shredding
    while leaving the ticket.  This was possible before using the Object
    plugin, but this makes it clearer and may simplify some integrations.

diff --git a/lib/RT/Shredder/Plugin/Transactions.pm b/lib/RT/Shredder/Plugin/Transactions.pm
new file mode 100644
index 0000000000..9dfae371bd
--- /dev/null
+++ b/lib/RT/Shredder/Plugin/Transactions.pm
@@ -0,0 +1,118 @@
+# BEGIN BPS TAGGED BLOCK {{{
+#
+# COPYRIGHT:
+#
+# This software is Copyright (c) 1996-2022 Best Practical Solutions, LLC
+#                                          <sales at bestpractical.com>
+#
+# (Except where explicitly superseded by other copyright notices)
+#
+#
+# LICENSE:
+#
+# This work is made available to you under the terms of Version 2 of
+# the GNU General Public License. A copy of that license should have
+# been provided with this software, but in any event can be snarfed
+# from www.gnu.org.
+#
+# This work is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 or visit their web page on the internet at
+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
+#
+#
+# CONTRIBUTION SUBMISSION POLICY:
+#
+# (The following paragraph is not intended to limit the rights granted
+# to you to modify and distribute this software under the terms of
+# the GNU General Public License and is only of importance to you if
+# you choose to contribute your changes and enhancements to the
+# community by submitting them to Best Practical Solutions, LLC.)
+#
+# By intentionally submitting any modifications, corrections or
+# derivatives to this work, or any other work intended for use with
+# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+# you are the copyright holder for those contributions and you grant
+# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+# royalty-free, perpetual, license to use, copy, create derivative
+# works based on those contributions, and sublicense and distribute
+# those contributions and any derivatives thereof.
+#
+# END BPS TAGGED BLOCK }}}
+
+package RT::Shredder::Plugin::Transactions;
+
+use strict;
+use warnings FATAL => 'all';
+use base qw(RT::Shredder::Plugin::Base::Search);
+
+=head1 NAME
+
+RT::Shredder::Plugin::Transactions - search plugin for wiping Transactions.
+
+=head1 ARGUMENTS
+
+=head2 query - query string
+
+Search Transactions with query string.
+Examples:
+  Type = 'Status' AND NewValue = 'rejected' AND
+  TicketQueue = 'General' AND Created > '2022-07-27'
+
+B<Hint:> You can construct query with the query builder in RT's web
+interface and then open advanced page and copy query string.
+
+=cut
+
+sub SupportArgs { return $_[0]->SUPER::SupportArgs, qw(query) }
+
+# used to genrate checkboxes instead of text fields in the web interface
+sub ArgIsBoolean {
+    my( $self, $arg ) = @_;
+    my %boolean_atts = map { $_ => 1 } qw();
+    return $boolean_atts{$arg};
+}
+
+sub TestArgs
+{
+    my $self = shift;
+    my %args = @_;
+    my $queue;
+    if( $args{'query'} ) {
+        my $objs = RT::Transactions->new( RT->SystemUser );
+        $objs->{'allow_deleted_search'} = 1;
+        my ($status, $msg) = $objs->FromSQL( $args{'query'} );
+        return( 0, "Bad query argument, error: $msg" ) unless $status;
+        $self->{'opt'}{'objects'} = $objs;
+    }
+
+    return $self->SUPER::TestArgs( %args );
+}
+
+sub Run
+{
+    my $self = shift;
+    my $objs = $self->{'opt'}{'objects'}
+        or return (1, undef);
+
+    $objs->OrderByCols( { FIELD => 'id', ORDER => 'ASC' } );
+
+    my (@top, %seen);
+    $self->FetchNext($objs, 1);
+    while ( my $obj = $self->FetchNext( $objs ) ) {
+        next if $seen{ $obj->id }++;
+        push @top, $obj;
+        last if $self->{'opt'}{'limit'}
+                && @top >= $self->{'opt'}{'limit'};
+    }
+    return (1, @top);
+}
+
+1;
+
diff --git a/t/shredder/03plugin.t b/t/shredder/03plugin.t
index de5d44fa7d..c8e278d3e4 100644
--- a/t/shredder/03plugin.t
+++ b/t/shredder/03plugin.t
@@ -3,10 +3,10 @@ use strict;
 use warnings;
 
 use Test::Deep;
-use RT::Test::Shredder nodb => 1, tests => 28;
+use RT::Test::Shredder nodb => 1, tests => 31;
 my $test = "RT::Test::Shredder";
 
-my @PLUGINS = sort qw(Attachments Base Objects SQLDump Summary Tickets Users);
+my @PLUGINS = sort qw(Attachments Base Objects SQLDump Summary Tickets Transactions Users);
 
 use_ok('RT::Shredder::Plugin');
 {

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


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list