[Rt-commit] rt branch, 4.4/add-clear-user-option-rt-clean-sessions, created. rt-4.4.4-236-gae52527163

Blaine Motsinger blaine at bestpractical.com
Tue Jan 26 19:24:25 EST 2021


The branch, 4.4/add-clear-user-option-rt-clean-sessions has been created
        at  ae52527163927f311b51748b31ed44135fc66ae5 (commit)

- Log -----------------------------------------------------------------
commit ae52527163927f311b51748b31ed44135fc66ae5
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Tue Jan 26 18:21:49 2021 -0600

    Add --clear-user option to rt-clean-sessions
    
    This option will delete all user sessions for a given user.

diff --git a/lib/RT/Interface/Web/Session.pm b/lib/RT/Interface/Web/Session.pm
index 3ee4cc6800..0571a9351b 100644
--- a/lib/RT/Interface/Web/Session.pm
+++ b/lib/RT/Interface/Web/Session.pm
@@ -316,6 +316,41 @@ sub ClearByUser {
     $self->ClearOrphanLockFiles if $deleted;
 }
 
+=head3 ClearAllUser
+
+Delete all sessions for a given user id.
+
+=cut
+
+sub ClearAllUser {
+    my $self  = shift || __PACKAGE__;
+    my $class = $self->Class;
+    my $attrs = $self->Attributes;
+    my $user_id = shift;
+
+    die "user id is required\n"
+        unless $user_id;
+
+    my $deleted;
+    foreach my $id ( @{ $self->Ids } ) {
+        my %session;
+        local $@;
+        eval { tie %session, $class, $id, $attrs };
+        if ( $@ ) {
+            $RT::Logger->debug("skipped session '$id', couldn't load: $@");
+            next;
+        }
+
+        next if $session{'CurrentUser'} && $session{'CurrentUser'}->id
+                && $session{'CurrentUser'}->id != $user_id;
+
+        tied(%session)->delete;
+        $RT::Logger->info("successfully deleted session '$id'");
+        $deleted++;
+    }
+    $self->ClearOrphanLockFiles if $deleted;
+}
+
 sub TIEHASH {
     my $self = shift;
     my $id = shift;
diff --git a/sbin/rt-clean-sessions.in b/sbin/rt-clean-sessions.in
index ac82eae03e..423ddaf7b4 100644
--- a/sbin/rt-clean-sessions.in
+++ b/sbin/rt-clean-sessions.in
@@ -67,16 +67,22 @@ BEGIN { # BEGIN RT CMD BOILERPLATE
 }
 
 use Getopt::Long;
+use Pod::Usage ();
 my %opt;
-GetOptions( \%opt, "older=s", "debug", "help|h", "skip-user" );
+GetOptions( \%opt, "older=s", "debug", "help|h", "skip-user", "clear-user=s" )
+    or Pod::Usage::pod2usage( -exitval => 1, -verbose => 0 );
 
 
 if ( $opt{help} ) {
-    require Pod::Usage;
     Pod::Usage::pod2usage({ verbose => 2 });
     exit;    
 }
 
+if ( $opt{'clear-user'} && ( $opt{'older'} || $opt{'skip-user'} ) ) {
+    print STDERR "the --clear-user option is not compatible with other options\n";
+    Pod::Usage::pod2usage({ verbose => 0 });
+    exit(1);
+}
 
 if( $opt{'older'} ) {
     unless( $opt{'older'} =~ /^\s*([0-9]+)\s*(H|D|M|Y)?$/i ) {
@@ -105,6 +111,17 @@ RT::InitLogging();
 
 require RT::Interface::Web::Session;
 
+if ( $opt{'clear-user'} ) {
+    require RT::User;
+    my $user = RT::User->new( RT->SystemUser );
+    my ( $ret, $msg ) = $user->Load( $opt{'clear-user'} );
+    die "Couldn't load user '" . $opt{'clear-user'} . "': $msg\n"
+        unless $ret;
+
+    RT::Interface::Web::Session->ClearAllUser( $user->id );
+    exit(0);
+}
+
 my $alogoff = int RT->Config->Get('AutoLogoff');
 if ( $opt{'older'} or $alogoff ) {
     my $min;
@@ -138,12 +155,16 @@ rt-clean-sessions - clean old and duplicate RT sessions
      rt-clean-sessions --debug --older 1M
      rt-clean-sessions --older 10D --skip-user
 
+     rt-clean-sessions --clear-user <username>
+
 =head1 DESCRIPTION
 
 Script cleans RT sessions from DB or dir with sessions data.
 Leaves in DB only one session per RT user and sessions that aren't older
 than specified(see options).
 
+Can optionally clear out all sessions for a single user(see options).
+
 Script is safe because data in the sessions is temporary and can be deleted.
 
 =head1 OPTIONS
@@ -164,6 +185,13 @@ By default only one session per user left in the DB, so users that have
 sessions on multiple computers or in different browsers will be logged out.
 Use this option to avoid this.
 
+=item clear-user
+
+Clear out all sessions for a single user, defined by username.
+
+The C<--clear-user> functionality is not compatible with other options
+and will error if run with C<--older> or C<--skip-user>.
+
 =item debug
 
 Turn on debug output.

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


More information about the rt-commit mailing list