| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use FindBin '$Bin'; | ||
| 6 | use lib "$Bin/../lib"; | ||
| 7 | use GBrowse::ConfigData; | ||
| 8 | use Bio::Graphics::Browser2; | ||
| 9 | use Bio::Graphics::Browser2::UserDB; | ||
| 10 | use Getopt::Long; | ||
| 11 | |||
| 12 | my ($name,$pass,$fullname,$email); | ||
| 13 | |||
| 14 | my @ORIGINAL_ARGV = @ARGV; | ||
| 15 | |||
| 16 | use constant USAGE => <<END; | ||
| 17 | Usage: $0 [-pass password -fullname name -email address] user | ||
| 18 | |||
| 19 | Creates a user account with the desired username and | ||
| 20 | password. If the account already exists, then the password | ||
| 21 | is reset. | ||
| 22 | |||
| 23 | -pass Login password for user. | ||
| 24 | -fullname User full name | ||
| 25 | -email User email address | ||
| 26 | |||
| 27 | This script uses the "user_account_db" option in the currently | ||
| 28 | installed GBrowse.conf configuration file to find | ||
| 29 | the appropriate accounts database. If a password is not provided | ||
| 30 | on the command line, you will be prompted for it on standard input. | ||
| 31 | |||
| 32 | WARNING: This script should be run as the web server user using | ||
| 33 | "sudo -u www-data $0". If it detects that it is not being run as this | ||
| 34 | user, it will attempt to sudo itself for you. | ||
| 35 | END | ||
| 36 | |||
| 37 | GetOptions('password=s' => \$pass, | ||
| 38 | 'fullname=s' => \$fullname, | ||
| 39 | 'email-s' => \$email, | ||
| 40 | ) or die USAGE; | ||
| 41 | |||
| 42 | my $wwwuser = GBrowse::ConfigData->config('wwwuser'); | ||
| 43 | my $uid = (getpwnam($wwwuser))[2]; | ||
| 44 | unless ($uid == $<) { | ||
| 45 | print STDERR "Not running as $wwwuser. Trying to use sudo to remedy. You may be asked for your login password.\n"; | ||
| 46 | my @args = ('sudo','-u',$wwwuser,$0, @ORIGINAL_ARGV); | ||
| 47 | exec @args; | ||
| 48 | exit 0; | ||
| 49 | } | ||
| 50 | |||
| 51 | my $globals = Bio::Graphics::Browser2->open_globals or die "Couldn't open GBrowse.conf"; | ||
| 52 | my $userdb = Bio::Graphics::Browser2::UserDB->new($globals); | ||
| 53 | |||
| 54 | $name=shift or die "Please provide a username. Run $0 --help for help\n"; | ||
| 55 | |||
| 56 | unless ($pass) { | ||
| 57 | print STDERR "New password for $name: "; | ||
| 58 | system "stty -echo"; | ||
| 59 | $pass = <STDIN>; | ||
| 60 | chomp($pass); | ||
| 61 | print STDERR "\n"; | ||
| 62 | |||
| 63 | my $newpass; | ||
| 64 | print STDERR "New password for $name (confirm): "; | ||
| 65 | $newpass = <STDIN>; | ||
| 66 | chomp($newpass); | ||
| 67 | print STDERR "\n"; | ||
| 68 | system "stty echo"; | ||
| 69 | die "Passwords don't match!\n" unless $pass eq $newpass; | ||
| 70 | } | ||
| 71 | $fullname ||= prompt("Enter user's full name (optional)"); | ||
| 72 | $email ||= prompt("Enter user's email address (optional)"); | ||
| 73 | |||
| 74 | $fullname ||= $name; | ||
| 75 | $email ||= "$name\@nowhere.net"; | ||
| 76 | |||
| 77 | my $uid = $userdb->userid_from_username($name); | ||
| 78 | |||
| 79 | unless ($uid) { | ||
| 80 | # this creates a new session for the admin user | ||
| 81 | my $session = $globals->session; | ||
| 82 | |||
| 83 | my $sessionid = $session->id; | ||
| 84 | my $uploadsid = $session->uploadsid; | ||
| 85 | |||
| 86 | $session->flush(); | ||
| 87 | |||
| 88 | my ($status,undef,$message) = | ||
| 89 | $userdb->do_add_user($name,$email,$fullname,$pass,$sessionid); | ||
| 90 | warn $message,"\n"; | ||
| 91 | $userdb->set_confirmed_from_username($name); | ||
| 92 | warn "Account \"$name\": now registered with sessionid=$sessionid, uploadsid=$uploadsid.\n" if $message =~ /success/i; | ||
| 93 | } else { | ||
| 94 | $userdb->set_password($uid,$pass); | ||
| 95 | warn "Account \"$name\": password successfully set.\n"; | ||
| 96 | } | ||
| 97 | |||
| 98 | exit 0; | ||
| 99 | |||
| 100 | sub prompt { | ||
| 101 | my $msg = shift; | ||
| 102 | print STDERR "$msg: "; | ||
| 103 | my $response = <STDIN>; | ||
| 104 | chomp $response; | ||
| 105 | return $response; | ||
| 106 | } | ||
| 107 | |||
| 108 | __END__ |