Skip to content

Commit

Permalink
added utility to change user passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
lstein committed Jan 15, 2011
1 parent 57e37a3 commit f64392b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
1 change: 1 addition & 0 deletions Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ my $build = GBrowseInstall->new(
'bin/gbrowse_clean.pl',
'bin/gbrowse_set_admin_passwd.pl',
'bin/gbrowse_create_account.pl',
'bin/gbrowse_change_passwd.pl',
'bin/gbrowse_slave',
'bin/gbrowse_syn_load_alignment_database.pl',
'bin/gbrowse_syn_load_alignments_msa.pl',
Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ bin/auto_install_databases.pl
bin/bed2gff3.pl
bin/bp_load_gff.pl
bin/gbrowse_clean.pl
bin/gbrowse_change_passwd.pl
bin/gbrowse_create_account.pl
bin/gbrowse_metadb_config.pl
bin/gbrowse_netinstall.pl
Expand Down
58 changes: 58 additions & 0 deletions bin/gbrowse_change_passwd.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/perl

use strict;

use FindBin '$Bin';
use lib "$Bin/../lib";
use GBrowse::ConfigData;
use Bio::Graphics::Browser2;
use Bio::Graphics::Browser2::UserDB;
use Getopt::Long;

my @ORIGINAL_ARGV = @ARGV;

use constant USAGE => <<END;
Usage: $0 user [pass]
Changes the password for the indicated user.
If no password is provided on the command line, then
a new random password will be chosen.
This script uses the "user_account_db" option in the currently
installed GBrowse.conf configuration file to find
the appropriate accounts database.
END

my $wwwuser = GBrowse::ConfigData->config('wwwuser');
my $uid = (getpwnam($wwwuser))[2];
unless ($uid == $<) {
print STDERR "Not running as $wwwuser. Trying to use sudo to remedy. You may be asked for your login password.\n";
my @args = ('sudo','-u',$wwwuser,$0, @ORIGINAL_ARGV);
exec @args;
exit 0;
}

my $globals = Bio::Graphics::Browser2->open_globals or die "Couldn't open GBrowse.conf";
my $userdb = Bio::Graphics::Browser2::UserDB->new($globals);

my $name = shift or die "Please provide a username. Run $0 --help for help\n";
my $pass = shift || get_random_password();

my $uid = $userdb->userid_from_username($name);
$uid || die "unknown user: $name\n";

$userdb->set_password($uid,$pass);
warn "Account \"$name\": password successfully set to $pass.\n";

exit 0;

sub get_random_password {
my $p = '';
my @a = ('a'..'z','A'..'Z',0..9);
for (1..10) {
$p .= $a[rand @a];
}
return $p;
}

__END__
28 changes: 23 additions & 5 deletions bin/gbrowse_create_account.pl
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
use Bio::Graphics::Browser2::UserDB;
use Getopt::Long;

my ($name,$pass);
my ($name,$pass,$fullname,$email);

my @ORIGINAL_ARGV = @ARGV;

use constant USAGE => <<END;
Usage: $0 [-pass password] user
Usage: $0 [-pass password -fullname name -email address] user
Creates a user account with the desired username and
password. If the account already exists, then the password
is reset.
-pass Login password for admin user.
-pass Login password for user.
-fullname User full name
-email User email address
This script uses the "user_account_db" option in the currently
installed GBrowse.conf configuration file to find
Expand All @@ -32,7 +34,10 @@
user, it will attempt to sudo itself for you.
END

GetOptions('password=s' => \$pass) or die USAGE;
GetOptions('password=s' => \$pass,
'fullname=s' => \$fullname,
'email-s' => \$email,
) or die USAGE;

my $wwwuser = GBrowse::ConfigData->config('wwwuser');
my $uid = (getpwnam($wwwuser))[2];
Expand Down Expand Up @@ -63,6 +68,11 @@ END
system "stty echo";
die "Passwords don't match!\n" unless $pass eq $newpass;
}
$fullname ||= prompt("Enter user's full name (optional)");
$email ||= prompt("Enter user's email address (optional)");

$fullname ||= $name;
$email ||= "$name\@nowhere.net";

my $uid = $userdb->userid_from_username($name);

Expand All @@ -74,7 +84,7 @@ END
$session->flush();

my ($status,undef,$message) =
$userdb->do_add_user($name,"$name\@nowhere.net",$name,$pass,$sessionid);
$userdb->do_add_user($name,$email,$fullname,$pass,$sessionid);
warn $message,"\n";
$userdb->set_confirmed_from_username($name);
warn "Account \"$name\": now registered with sessionid=$sessionid, uploadsid=$uploadsid.\n" if $message =~ /success/i;
Expand All @@ -85,4 +95,12 @@ END

exit 0;

sub prompt {
my $msg = shift;
print STDERR "$msg: ";
my $response = <STDIN>;
chomp $response;
return $response;
}

__END__

0 comments on commit f64392b

Please sign in to comment.