Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to delete EPrint users
#!/usr/bin/perl -w
# NB This file is designed to be saved as [EPRINTS_ROOT]/bin/local/delete_user
# If you want to save it elsewhere, the 'FindBin' on line 52 may need altering.
=pod
=head1 NAME
B<delete_user> - Removes an EPrints user account
=head1 SYNOPSIS
B<delete_user> I<repository_id> [B<options>] I<userid> [I<userid> ...]
=head1 DESCRIPTION
This script prints some summary information about users including any current login tickets, eprints owned and whether they are a contact for any eprints.
It will ask for confirmation that you want to delete a user. NB User deletion cannot be undone.
=head1 ARGUMENTS
=over 8
=item B<repository_id>
The ID of the repository to use.
=item B<userid>
The integer userid of the user to be deleted. Multiple space-separated userids can be provided.
=back
=head1 OPTIONS
=over 8
=item B<--no-confirm>
Don't ask for confirmation to delete each user, just do it. Be careful!
=item B<--man>
Print the full manual page and then exit.
=back
=cut
use FindBin;
use lib "$FindBin::Bin/../../perl_lib";
use EPrints;
use strict;
use Getopt::Long;
use Pod::Usage;
# Set STDOUT to auto flush (without needing a \n)
$|=1;
my $man = 0;
my $no_confirm = 0;
Getopt::Long::Configure("permute");
GetOptions(
'man' => \$man,
'no-confirm' => \$no_confirm,
) || pod2usage( 2 );
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
my $noise = 1;
my $repoid = $ARGV[0];
if( !defined $repoid )
{
print STDERR "Please supply a repository ID and one or more userids\n";
exit 1;
}
my $session = new EPrints::Session( 1 , $repoid , $noise );
if( !defined $session )
{
print STDERR "Failed to load repository: $repoid\n";
exit 1;
}
my $REGEXP_YESNO = '^(yes|no)$';
if( !defined $ARGV[1] )
{
print STDERR "Silly. You need to provide a UserID. I'm not just going to delete everyone!.\n";
exit 1;
}
foreach my $argnum (1 .. $#ARGV)
{
my $user = EPrints::DataObj::User->new( $session, $ARGV[$argnum] );
if( !defined $user )
{
$session->get_repository->log( "User #$ARGV[$argnum] not found." );
}
else
{
my $repo = $session->get_repository;
print "Checking user #$ARGV[$argnum]\n";
# print some basic info
foreach( qw/username usertype email name joined/ )
{
#print "$_:\t", ( defined $user->value( $_ ) ? $user->value( $_ ) : "-" ), "\n";
print "$_:\t", EPrints::Utils::tree_to_utf8( $user->render_value( $_ ) ), "\n";
}
# are they currently logged-in?
my $user_login_tickets = $repo->dataset('loginticket')->search(
filters => [ {meta_fields => ['userid'], value => $user->id} ]
);
print "User has ", $user_login_tickets->count, " current login tickets\n";
my $owned_ep = $user->owned_eprints_list( dataset => $user->repository->dataset( "eprint" ) );
print "This user owns ", $owned_ep->count , " eprints.\n";
# could print out summary here if needed using $owned_ep list.
my $is_contact_for_list;
if( defined $user->value( "email" ) )
{
$is_contact_for_list = $session->get_repository->dataset( 'eprint' )->search( filters => [
{ meta_fields => [qw/ contact_email /], value => $user->value( "email" ) },
] );
print "User email address is contact for ", $is_contact_for_list->count, " eprints.\n";
}
my $do_it = "no";
if( !$no_confirm )
{
$do_it = EPrints::Utils::get_input( $REGEXP_YESNO, "Remove this user?", $do_it );
}
if( $do_it eq "yes" || $no_confirm )
{
print "Removing user:\t";
if( $user->remove )
{
print "Gone!\n";
}
else
{
print "...failed :(\n";
}
}
else
{
print "OK, not removing them.\n";
}
}
}
$session->terminate();
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment