« blib/conf/MobyServices/text_plain_renderer.pm
↑ Summary
blib/lib/Bio/Graphics/Browser2/Session.pm »
| 1 | package Bio::Graphics::Browser2::UserDB; | ||
| 2 | |||
| 3 | # $Id: UserDB.pm 23607 2010-07-30 17:34:25Z cnvandev $ | ||
| 4 | use strict; | ||
| 5 | use Bio::Graphics::Browser2; | ||
| 6 | use Bio::Graphics::Browser2::SendMail; | ||
| 7 | use CGI qw(:standard); | ||
| 8 | use DBI; | ||
| 9 | use Digest::SHA qw(sha1_hex sha1); | ||
| 10 | use JSON; | ||
| 11 | use Text::ParseWords 'quotewords'; | ||
| 12 | use Digest::MD5 qw(md5_hex); | ||
| 13 | use Carp qw(confess cluck croak); | ||
| 14 | |||
| 15 | use constant HAVE_OPENID => eval "require Net::OpenID::Consumer; require LWP::UserAgent; 1" || 0; | ||
| 16 | use constant HAVE_SMTP => eval "require Net::SMTP;1" || 0; | ||
| 17 | |||
| 18 | # SOME CLARIFICATION ON TERMINOLOGY | ||
| 19 | # "userid" -- internal dbm ID for a user; a short integer | ||
| 20 | # "sessionid" -- GBrowse's session ID, a long hexadecimal | ||
| 21 | # "uploadsid" -- GBrowse's upload ID, a long hexadecimal | ||
| 22 | |||
| 23 | sub new { | ||
| 24 | my $class = shift; | ||
| 25 | my $globals = shift; | ||
| 26 | |||
| 27 | my $VERSION = '0.5'; | ||
| 28 | my $credentials = $globals->user_account_db | ||
| 29 | || "DBI:mysql:gbrowse_login;user=gbrowse;password=gbrowse"; | ||
| 30 | |||
| 31 | my $login = DBI->connect($credentials); | ||
| 32 | unless ($login) { | ||
| 33 | confess "Could not open login database $credentials"; | ||
| 34 | } | ||
| 35 | |||
| 36 | my $self = bless { | ||
| 37 | dbi => $login, | ||
| 38 | globals => $globals, | ||
| 39 | openid => HAVE_OPENID, | ||
| 40 | register => HAVE_SMTP, | ||
| 41 | }, ref $class || $class; | ||
| 42 | |||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub globals {shift->{globals} }; | ||
| 47 | sub dbi {shift->{dbi} }; | ||
| 48 | sub can_openid {shift->{openid} }; | ||
| 49 | sub can_register {shift->{register} }; | ||
| 50 | |||
| 51 | sub generate_salted_digest { | ||
| 52 | my $self = shift; | ||
| 53 | my $password = shift; | ||
| 54 | my $salt = $self->create_key(4); | ||
| 55 | return $salt . sha1_hex($salt,$password); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub salted_digest_match { | ||
| 59 | my $self = shift; | ||
| 60 | my ($offered,$correct) = @_; | ||
| 61 | my ($salt,$digest) = $correct =~ /^(.{4})(.+)/; | ||
| 62 | return sha1_hex($salt,$offered) eq $digest; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub is_salted { | ||
| 66 | my $self = shift; | ||
| 67 | my $correct = shift; | ||
| 68 | return $correct =~ /^[a-zA-Z0-9_]{4}[0-9a-f]{40}$/; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub passwd_match { | ||
| 72 | my $self = shift; | ||
| 73 | my ($offered,$correct) = @_; | ||
| 74 | return $self->is_salted($correct) ? $self->salted_digest_match($offered,$correct) | ||
| 75 | : sha1($offered) eq $correct; | ||
| 76 | } | ||
| 77 | |||
| 78 | # Get Header - Returns the message found at the top of all confirmation e-mails. | ||
| 79 | sub get_header { | ||
| 80 | my $self = shift; | ||
| 81 | my $globals = $self->{globals}; | ||
| 82 | my $message = "\nThank you for creating an account with " | ||
| 83 | . $globals->application_name | ||
| 84 | . ": " | ||
| 85 | . $globals->application_name_long . "\n\n"; | ||
| 86 | $message .= "The account information found below is for your reference only. "; | ||
| 87 | $message .= "Please keep all account names and passwords in a safe location "; | ||
| 88 | $message .= "and do not share your password with others."; | ||
| 89 | return $message; | ||
| 90 | } | ||
| 91 | |||
| 92 | # Get Footer - Returns the message found at the bottom of all e-mails. | ||
| 93 | sub get_footer { | ||
| 94 | my $self = shift; | ||
| 95 | my $globals = $self->{globals}; | ||
| 96 | my $message = "Courtesy of " . $globals->application_name . " Administration\n\n"; | ||
| 97 | $message .= "This message and any attachments may contain confidential and/or "; | ||
| 98 | $message .= "privileged information for the sole use of the intended recipient. "; | ||
| 99 | $message .= "Any review or distribution by anyone other than the person for whom "; | ||
| 100 | $message .= "it was originally intended is strictly prohibited. If you have "; | ||
| 101 | $message .= "received this message in error, please contact the sender and delete "; | ||
| 102 | $message .= "all copies. Opinions, conclusions or other information contained in "; | ||
| 103 | $message .= "this message may not be that of the organization."; | ||
| 104 | return $message; | ||
| 105 | } | ||
| 106 | |||
| 107 | # Create Key - Generates a random string of a given length. | ||
| 108 | sub create_key { | ||
| 109 | my $self = shift; | ||
| 110 | my $val = shift; | ||
| 111 | my $key; | ||
| 112 | my @char=('a'..'z','A'..'Z','0'..'9','_'); | ||
| 113 | foreach (1..$val) {$key.=$char[rand @char];} | ||
| 114 | return $key; | ||
| 115 | } | ||
| 116 | |||
| 117 | # Check E-mail - Returns true if an e-mail is in a valid format. | ||
| 118 | sub check_email { | ||
| 119 | my $self = shift; | ||
| 120 | if(shift =~ m/^(\w|\-|\_|\&|\+|\.)+\@((\w|\-|\_)+\.)+[a-zA-Z]{2,}$/) { | ||
| 121 | return 1; | ||
| 122 | } else { | ||
| 123 | return 0; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | # Check User - Returns true if a username is in a valid format. | ||
| 128 | sub check_user { | ||
| 129 | my $self = shift; | ||
| 130 | if(shift =~ m/^([!-\[]|[\]-~])+$/) { | ||
| 131 | return 1; | ||
| 132 | } else { | ||
| 133 | return 0; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | #Check Admin - Returns true if a user is an admin. | ||
| 138 | sub check_admin { | ||
| 139 | my $self = shift; | ||
| 140 | my $username = shift; | ||
| 141 | my $globals = $self->{globals}; | ||
| 142 | my $admin_name = $globals->admin_account; | ||
| 143 | return unless $admin_name; | ||
| 144 | return $username eq $admin_name; | ||
| 145 | } | ||
| 146 | |||
| 147 | # Check Old Confirmations - Deletes any unconfirmed accounts more than 3 days old. | ||
| 148 | sub check_old_confirmations { | ||
| 149 | my $self = shift; | ||
| 150 | my $nowfun = $self->nowfun(); | ||
| 151 | my $userdb = $self->{dbi}; | ||
| 152 | |||
| 153 | my $days = $self->globals->user_account_db =~ /sqlite/i ? "julianday('now')-julianday(last_login)" | ||
| 154 | : 'datediff(now(),last_login)'; | ||
| 155 | local $userdb->{AutoCommit} = 0; | ||
| 156 | local $userdb->{RaiseError} = 1; | ||
| 157 | eval { | ||
| 158 | my $ids = $userdb->selectcol_arrayref("SELECT userid FROM users WHERE confirmed=0 AND $days>3"); | ||
| 159 | for my $id (@$ids) { | ||
| 160 | $userdb->do('DELETE FROM users WHERE userid=?',undef,$id); | ||
| 161 | $userdb->do('DELETE FROM session WHERE userid=?',undef,$id); | ||
| 162 | } | ||
| 163 | $userdb->commit(); | ||
| 164 | }; | ||
| 165 | if ($@) { | ||
| 166 | warn "deletion of expired new accounts failed due to '$@'. Rolling back."; | ||
| 167 | eval {$userdb->rollback()}; | ||
| 168 | } | ||
| 169 | return; | ||
| 170 | } | ||
| 171 | |||
| 172 | # Now Function - return the database-dependent function for determining current date & time | ||
| 173 | sub nowfun { | ||
| 174 | my $self = shift; | ||
| 175 | my $globals = $self->{globals}; | ||
| 176 | return $globals->user_account_db =~ /sqlite/i ? "datetime('now','localtime')" : 'now()'; | ||
| 177 | } | ||
| 178 | |||
| 179 | #################### N O N - O P E N I D F U N C T I O N S ##################### | ||
| 180 | # Get User ID (User) - Returns a confirmed user's ID | ||
| 181 | sub get_user_id { | ||
| 182 | my $self = shift; | ||
| 183 | my $search = shift; | ||
| 184 | # inefficient use of three separate SQL statements | ||
| 185 | return $self->userid_from_username($search) || $self->userid_from_email($search) || $self->userid_from_fullname($search); | ||
| 186 | } | ||
| 187 | |||
| 188 | sub match_user { | ||
| 189 | my $self = shift; | ||
| 190 | my $search = shift; | ||
| 191 | |||
| 192 | my $userdb = $self->dbi; | ||
| 193 | my $select = $userdb->prepare(<<END) or die $userdb->errstr; | ||
| 194 | SELECT a.username,b.gecos,b.email | ||
| 195 | FROM session as a,users as b | ||
| 196 | WHERE a.userid=b.userid | ||
| 197 | AND (a.username LIKE ? OR | ||
| 198 | b.gecos LIKE ? OR | ||
| 199 | b.email LIKE ?) | ||
| 200 | END | ||
| 201 | ; | ||
| 202 | $search = quotemeta($search); | ||
| 203 | my $db_search = "%$search%"; | ||
| 204 | $select->execute($db_search,$db_search,$db_search) or die $select->errstr; | ||
| 205 | my @results; | ||
| 206 | while (my @a = $select->fetchrow_array) { | ||
| 207 | push @results, ($a[2] && $a[2] !~ /unused/i ? "$a[1] <$a[2]> ($a[0])" : "$a[1] ($a[0])"); | ||
| 208 | } | ||
| 209 | $select->finish; | ||
| 210 | return \@results; | ||
| 211 | } | ||
| 212 | |||
| 213 | # similar to match_user() except that it only finds users who are sharing files | ||
| 214 | sub match_sharing_user { | ||
| 215 | my $self = shift; | ||
| 216 | my ($source,$search) = @_; | ||
| 217 | my $userdb = $self->dbi; | ||
| 218 | my $select = $userdb->prepare(<<END) or die $userdb->errstr; | ||
| 219 | SELECT a.username,b.gecos,b.email | ||
| 220 | FROM session as a,users as b,uploads as c | ||
| 221 | WHERE a.userid=b.userid | ||
| 222 | AND a.userid=c.userid | ||
| 223 | AND c.sharing_policy='public' | ||
| 224 | AND c.data_source=? | ||
| 225 | AND (a.username LIKE ? OR | ||
| 226 | b.gecos LIKE ? OR | ||
| 227 | b.email LIKE ?) | ||
| 228 | END | ||
| 229 | ; | ||
| 230 | $search = quotemeta($search); | ||
| 231 | my $db_search = "%$search%"; | ||
| 232 | $select->execute($source,$db_search,$db_search,$db_search) or die $select->errstr; | ||
| 233 | my @results; | ||
| 234 | while (my @a = $select->fetchrow_array) { | ||
| 235 | push @results,(grep /$search/i,@a); | ||
| 236 | } | ||
| 237 | $select->finish; | ||
| 238 | return \@results; | ||
| 239 | } | ||
| 240 | |||
| 241 | sub userid_from_username { | ||
| 242 | my $self = shift; | ||
| 243 | my $username = shift; | ||
| 244 | |||
| 245 | my $userdb = $self->{dbi}; | ||
| 246 | my ($user_id) = | ||
| 247 | $userdb->selectrow_array(<<END ,undef,$username); | ||
| 248 | SELECT userid | ||
| 249 | FROM session as a | ||
| 250 | WHERE a.username=? | ||
| 251 | LIMIT 1 | ||
| 252 | END | ||
| 253 | return $user_id; | ||
| 254 | } | ||
| 255 | |||
| 256 | sub userid_from_email { | ||
| 257 | my $self = shift; | ||
| 258 | my $email = shift; | ||
| 259 | |||
| 260 | my $userdb = $self->{dbi}; | ||
| 261 | my ($user_id) = | ||
| 262 | $userdb->selectrow_array(<<END ,undef,$email); | ||
| 263 | SELECT userid | ||
| 264 | FROM users as a | ||
| 265 | WHERE a.email=? | ||
| 266 | LIMIT 1 | ||
| 267 | END | ||
| 268 | ; | ||
| 269 | return $user_id; | ||
| 270 | } | ||
| 271 | |||
| 272 | sub userid_from_fullname { | ||
| 273 | my $self = shift; | ||
| 274 | my $email = shift; | ||
| 275 | |||
| 276 | my $userdb = $self->{dbi}; | ||
| 277 | my ($user_id) = | ||
| 278 | $userdb->selectrow_array(<<END ,undef,$email); | ||
| 279 | SELECT userid | ||
| 280 | FROM users as a | ||
| 281 | WHERE a.gecos=? | ||
| 282 | LIMIT 1 | ||
| 283 | END | ||
| 284 | ; | ||
| 285 | return $user_id; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub userid_from_uploadsid { | ||
| 289 | my $self = shift; | ||
| 290 | my $uploadsid = shift; | ||
| 291 | |||
| 292 | my $userdb = $self->{dbi}; | ||
| 293 | my $user_id = | ||
| 294 | $userdb->selectrow_array(<<END ,undef,$uploadsid); | ||
| 295 | SELECT userid | ||
| 296 | FROM session as a | ||
| 297 | WHERE a.uploadsid=? | ||
| 298 | LIMIT 1 | ||
| 299 | END | ||
| 300 | } | ||
| 301 | |||
| 302 | sub set_fullname_from_username { | ||
| 303 | my $self = shift; | ||
| 304 | my ($username,$fullname,$email) = @_; | ||
| 305 | my $userdb = $self->dbi; | ||
| 306 | |||
| 307 | my $userid = $self->userid_from_username($username) or return; | ||
| 308 | |||
| 309 | local $userdb->{AutoCommit} = 0; | ||
| 310 | local $userdb->{RaiseError} = 1; | ||
| 311 | eval { | ||
| 312 | my ($rows) = $userdb->selectrow_array(<<END,undef,$userid); | ||
| 313 | SELECT count(*) FROM users WHERE userid=? | ||
| 314 | END | ||
| 315 | ; | ||
| 316 | if ($rows > 0) { | ||
| 317 | $userdb->do('UPDATE users SET gecos=?,email=? WHERE userid=?',undef,$fullname,$email||'',$userid); | ||
| 318 | } else { | ||
| 319 | my $nowfun = $self->nowfun(); | ||
| 320 | my $email = $email || ('unused_'.$self->create_key(32).'@nowhere.net'); | ||
| 321 | $userdb->do(<<END,undef,$userid,$fullname,$email); | ||
| 322 | INSERT INTO users(userid,gecos,email,pass,remember,openid_only,confirmed,cnfrm_code,last_login,created) | ||
| 323 | VALUES (?,?,?,'x',1,1,1,'x',$nowfun,$nowfun) | ||
| 324 | END | ||
| 325 | ; | ||
| 326 | } | ||
| 327 | $userdb->commit(); | ||
| 328 | }; | ||
| 329 | if ($@) { | ||
| 330 | warn "Setting fullname for $username failed due to $@. Rolling back."; | ||
| 331 | eval {$userdb->rollback()}; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | sub sessionid_from_username { | ||
| 336 | my $self = shift; | ||
| 337 | my $username = shift; | ||
| 338 | my $userdb = $self->{dbi}; | ||
| 339 | my $sessionid = $userdb->selectrow_array(<<END ,undef,$username); | ||
| 340 | SELECT sessionid | ||
| 341 | FROM session as a | ||
| 342 | WHERE a.username=? | ||
| 343 | LIMIT 1 | ||
| 344 | END | ||
| 345 | ; | ||
| 346 | return $sessionid; | ||
| 347 | } | ||
| 348 | |||
| 349 | # Username From UploadsID (Uploads ID) - Returns a user's name. | ||
| 350 | sub username_from_uploadsid { | ||
| 351 | my $self = shift; | ||
| 352 | my $uploadsid = shift; | ||
| 353 | |||
| 354 | my $userdb = $self->{dbi}; | ||
| 355 | my $user_id = | ||
| 356 | $userdb->selectrow_array(<<END ,undef,$uploadsid); | ||
| 357 | SELECT username | ||
| 358 | FROM session as a | ||
| 359 | WHERE a.uploadsid=? | ||
| 360 | LIMIT 1 | ||
| 361 | END | ||
| 362 | } | ||
| 363 | |||
| 364 | # Get Uploads ID (User ID) - Returns a user's Uploads ID. | ||
| 365 | sub get_uploads_id { | ||
| 366 | my $self = shift; | ||
| 367 | my $userid = shift; | ||
| 368 | my $userdb = $self->{dbi}; | ||
| 369 | return $userdb->selectrow_array("SELECT uploadsid FROM session WHERE userid=?", | ||
| 370 | undef,$userid); | ||
| 371 | } | ||
| 372 | |||
| 373 | sub get_sessionid { | ||
| 374 | my $self = shift; | ||
| 375 | my $userid = shift; | ||
| 376 | my $userdb = $self->{dbi}; | ||
| 377 | return $userdb->selectrow_array("SELECT sessionid FROM session WHERE userid=?", | ||
| 378 | undef,$userid); | ||
| 379 | } | ||
| 380 | |||
| 381 | sub accountinfo_from_username { | ||
| 382 | my $self = shift; | ||
| 383 | my $username = shift; | ||
| 384 | my $userdb = $self->dbi; | ||
| 385 | return $userdb->selectrow_array('SELECT a.gecos,a.email FROM users as a,session as b WHERE a.userid=b.userid AND b.username=?', | ||
| 386 | undef,$username); | ||
| 387 | } | ||
| 388 | |||
| 389 | # Get Username (User ID) - Returns a user's username, given their ID. | ||
| 390 | sub get_username { | ||
| 391 | croak "you probably want to call username_from_sessionid"; | ||
| 392 | } | ||
| 393 | |||
| 394 | sub username_from_userid { | ||
| 395 | my $self = shift; | ||
| 396 | my $userid = shift; | ||
| 397 | my $userdb = $self->{dbi}; | ||
| 398 | return $userdb->selectrow_array("SELECT username FROM session WHERE userid=?", undef, $userid) || 'an anonymous user'; | ||
| 399 | } | ||
| 400 | |||
| 401 | sub username_from_sessionid { | ||
| 402 | my $self = shift; | ||
| 403 | my $sessionid = shift; | ||
| 404 | my $userdb = $self->{dbi}; | ||
| 405 | |||
| 406 | return $userdb->selectrow_array(<<END ,undef,$sessionid)||'an anonymous user'; | ||
| 407 | SELECT username FROM session | ||
| 408 | WHERE sessionid=? | ||
| 409 | END | ||
| 410 | } | ||
| 411 | |||
| 412 | sub fullname_from_sessionid { | ||
| 413 | my $self = shift; | ||
| 414 | my $sessionid = shift; | ||
| 415 | my $userdb = $self->{dbi}; | ||
| 416 | |||
| 417 | my ($fullname,$username) = $userdb->selectrow_array(<<END ,undef,$sessionid); | ||
| 418 | SELECT b.gecos,a.username | ||
| 419 | FROM session as a,users as b | ||
| 420 | WHERE a.userid=b.userid | ||
| 421 | AND a.sessionid=? | ||
| 422 | END | ||
| 423 | ; | ||
| 424 | return $fullname || $username; # fallback to username if fullname not available | ||
| 425 | } | ||
| 426 | |||
| 427 | sub email_from_sessionid { | ||
| 428 | my $self = shift; | ||
| 429 | my $sessionid = shift; | ||
| 430 | my $userdb = $self->{dbi}; | ||
| 431 | |||
| 432 | my ($email) = $userdb->selectrow_array(<<END ,undef,$sessionid); | ||
| 433 | SELECT b.email | ||
| 434 | FROM session as a,users as b | ||
| 435 | WHERE a.userid=b.userid | ||
| 436 | AND a.sessionid=? | ||
| 437 | END | ||
| 438 | ; | ||
| 439 | return $email; | ||
| 440 | } | ||
| 441 | |||
| 442 | sub userid_from_sessionid { | ||
| 443 | my $self = shift; | ||
| 444 | my $sessionid = shift; | ||
| 445 | my $userdb = $self->{dbi}; | ||
| 446 | |||
| 447 | my ($userid) = $userdb->selectrow_array(<<END ,undef,$sessionid); | ||
| 448 | SELECT userid FROM session | ||
| 449 | WHERE sessionid=? | ||
| 450 | END | ||
| 451 | ; | ||
| 452 | return $userid; | ||
| 453 | } | ||
| 454 | |||
| 455 | sub set_confirmed_from_username { | ||
| 456 | my $self = shift; | ||
| 457 | my $username = shift; | ||
| 458 | my $userdb = $self->dbi; | ||
| 459 | my $userid = $self->userid_from_username($username) or return; | ||
| 460 | return $userdb->do('UPDATE users SET confirmed=1 WHERE userid=?',undef,$userid); | ||
| 461 | } | ||
| 462 | |||
| 463 | # Check Uploads ID (User ID, Uploads ID) - Makes sure a user's ID is in the database. | ||
| 464 | sub check_uploads_id { | ||
| 465 | my $self = shift; | ||
| 466 | croak "check_uploads_id() should no longer be necessary"; | ||
| 467 | my ($sessionid,$uploadsid) = @_; | ||
| 468 | my $userdb = $self->{dbi}; | ||
| 469 | |||
| 470 | my $rows = $userdb->selectrow_array(<<END ,undef,$sessionid,$uploadsid); | ||
| 471 | SELECT count(*) FROM session | ||
| 472 | WHERE sessionid=? and uploadsid=? | ||
| 473 | END | ||
| 474 | unless ($rows) { | ||
| 475 | $userdb->do(<<END ,undef,$sessionid,$uploadsid); | ||
| 476 | INSERT INTO session (sessionid,uploadsid) | ||
| 477 | VALUES (?,?) | ||
| 478 | END | ||
| 479 | ; | ||
| 480 | } | ||
| 481 | return $uploadsid; | ||
| 482 | } | ||
| 483 | |||
| 484 | sub check_or_add_named_session { | ||
| 485 | my $self = shift; | ||
| 486 | my ($sessionid,$username) = @_; | ||
| 487 | if (my $old_session = $self->sessionid_from_username($username)) { | ||
| 488 | return $old_session; | ||
| 489 | } else { | ||
| 490 | $self->add_named_session($sessionid,$username); | ||
| 491 | return $sessionid; | ||
| 492 | } | ||
| 493 | } | ||
| 494 | |||
| 495 | sub add_named_session { | ||
| 496 | my $self = shift; | ||
| 497 | my ($sessionid,$username) = @_; | ||
| 498 | |||
| 499 | my $userdb = $self->dbi; | ||
| 500 | |||
| 501 | my $session = $self->globals->session($sessionid); | ||
| 502 | $session->id eq $sessionid or die "Sessionid unavailable"; | ||
| 503 | my $uploadsid = $session->uploadsid; | ||
| 504 | |||
| 505 | my $insert_session = $userdb->prepare(<<END ); | ||
| 506 | REPLACE INTO session (username,sessionid,uploadsid) | ||
| 507 | VALUES (?,?,?) | ||
| 508 | END | ||
| 509 | ; | ||
| 510 | |||
| 511 | $insert_session->execute($username,$sessionid,$uploadsid) | ||
| 512 | or return; | ||
| 513 | return $userdb->last_insert_id('','','',''); | ||
| 514 | } | ||
| 515 | |||
| 516 | sub set_session_and_uploadsid { | ||
| 517 | my $self = shift; | ||
| 518 | my ($userid,$sessionid,$uploadsid) = @_; | ||
| 519 | |||
| 520 | my $userdb = $self->dbi; | ||
| 521 | $userdb->do('UPDATE session SET sessionid=?,uploadsid=? WHERE userid=?', | ||
| 522 | undef, | ||
| 523 | $sessionid,$uploadsid,$userid) or die $userdb->errstr; | ||
| 524 | } | ||
| 525 | |||
| 526 | sub delete_user_by_username { | ||
| 527 | my $self = shift; | ||
| 528 | my $username = shift; | ||
| 529 | my $userdb = $self->dbi; | ||
| 530 | my $userid = $self->userid_from_username($username) or return; | ||
| 531 | local $userdb->{AutoCommit} = 0; | ||
| 532 | local $userdb->{RaiseError} = 1; | ||
| 533 | eval { | ||
| 534 | $userdb->do('DELETE FROM users WHERE userid=?',undef,$userid); | ||
| 535 | $userdb->do('DELETE FROM session WHERE userid=?',undef,$userid); | ||
| 536 | $userdb->do('DELETE FROM openid_users WHERE userid=?',undef,$userid); | ||
| 537 | $userdb->do('DELETE FROM uploads WHERE userid=?',undef,$userid); | ||
| 538 | $userdb->do('DELETE FROM sharing WHERE userid=?',undef,$userid); | ||
| 539 | $userdb->commit(); | ||
| 540 | }; | ||
| 541 | if ($@) { | ||
| 542 | warn "Account deletion failed due to $@. Rolling back."; | ||
| 543 | eval {$userdb->rollback()}; | ||
| 544 | } | ||
| 545 | 1; | ||
| 546 | } | ||
| 547 | |||
| 548 | ##################################### | ||
| 549 | # BUG! | ||
| 550 | # Everything below here supports the | ||
| 551 | # login.js script, which expects return | ||
| 552 | # values as various combinations of | ||
| 553 | # strings and JSON structures. | ||
| 554 | # This means API is strongly tied | ||
| 555 | # to database queries. | ||
| 556 | ##################################### | ||
| 557 | |||
| 558 | # Validate - Ensures that a non-openid user's credentials are correct. | ||
| 559 | sub do_validate { | ||
| 560 | my $self = shift; | ||
| 561 | my ($user,$pass,$remember) = @_; | ||
| 562 | |||
| 563 | my $userdb = $self->{dbi}; | ||
| 564 | my $update; | ||
| 565 | |||
| 566 | # remove dangling unconfirmed accounts here | ||
| 567 | $self->check_old_confirmations(); | ||
| 568 | |||
| 569 | # return $self->string_result('Usernames cannot contain any backslashes, whitespace or non-ascii characters.') | ||
| 570 | return $self->code_result('INVALID_NAME'=>'Usernames cannot contain any backslashes, whitespace or non-ascii characters.') | ||
| 571 | unless $self->check_user($user); | ||
| 572 | |||
| 573 | my $userid = $self->userid_from_username($user); | ||
| 574 | my $nowfun = $self->nowfun(); | ||
| 575 | |||
| 576 | # WARNING: bad design here | ||
| 577 | # EXPLANATION: a remember value of "2" means to update last_login | ||
| 578 | # but not to retrieve session ID. This seems to be requested during account | ||
| 579 | # editing/updating. | ||
| 580 | if($remember == 2) { | ||
| 581 | $update = $userdb->prepare( | ||
| 582 | "UPDATE users SET last_login=$nowfun WHERE userid=? AND confirmed=1"); | ||
| 583 | } else { | ||
| 584 | $update = $userdb->prepare( | ||
| 585 | "UPDATE users SET last_login=$nowfun,remember=$remember WHERE userid=? AND confirmed=1"); | ||
| 586 | } | ||
| 587 | |||
| 588 | my $select = $userdb->prepare( | ||
| 589 | "SELECT sessionid,email,confirmed,pass FROM users as a,session as b WHERE a.userid=b.userid and a.userid=?"); | ||
| 590 | $select->execute($userid) | ||
| 591 | or return $self->dbi_err; | ||
| 592 | |||
| 593 | # BUG: this is truly nasty -- the session id is found by string searching in login.js!!!! | ||
| 594 | my ($session,$email,$confirmed,$correct_pass) = $select->fetchrow_array; | ||
| 595 | |||
| 596 | if ($session && $self->passwd_match($pass,$correct_pass)) { | ||
| 597 | if ($confirmed) { | ||
| 598 | my $result = $remember == 2 ? 'Success' : "session".$session; | ||
| 599 | return $self->string_result($result); | ||
| 600 | } else { | ||
| 601 | return $self->string_result("unconfirmed${email}"); | ||
| 602 | } | ||
| 603 | } else { | ||
| 604 | return $self->string_result('Invalid username or password provided, please try again.'); | ||
| 605 | } | ||
| 606 | |||
| 607 | # update time login now | ||
| 608 | $update->execute($userid) | ||
| 609 | or return $self->dbi_err; | ||
| 610 | } | ||
| 611 | |||
| 612 | # Add User Check - Checks to see if the user has already been added. | ||
| 613 | sub do_add_user_check { | ||
| 614 | my $self = shift; | ||
| 615 | my ($user,$email,$fullname,$pass,$userid) = @_; | ||
| 616 | |||
| 617 | my $userdb = $self->dbi; | ||
| 618 | |||
| 619 | return $self->string_result('Invalid e-mail address (',$email,') provided.') | ||
| 620 | unless $self->check_email($email); | ||
| 621 | |||
| 622 | return $self->string_result("Usernames cannot contain any backslashes, whitespace or non-ascii characters.") | ||
| 623 | unless $self->check_user($user); | ||
| 624 | |||
| 625 | my $select = $userdb->prepare( | ||
| 626 | "SELECT confirmed FROM users WHERE email=?"); | ||
| 627 | $select->execute($email) | ||
| 628 | or return $self->dbi_err; | ||
| 629 | |||
| 630 | my $confirmed = $select->fetchrow_array; | ||
| 631 | if($select->rows == 0) { | ||
| 632 | return $self->do_add_user($user,$email,$fullname,$pass,$userid); | ||
| 633 | } elsif($confirmed == 1) { | ||
| 634 | return $self->string_result('E-mail in use'); | ||
| 635 | } elsif($confirmed == 0) { | ||
| 636 | return $self->string_result('Message Already Sent'); | ||
| 637 | } | ||
| 638 | |||
| 639 | return $self->programmer_error; | ||
| 640 | } | ||
| 641 | |||
| 642 | # Add User - Adds a new non-openid user to the user database. | ||
| 643 | sub do_add_user { | ||
| 644 | my $self = shift; | ||
| 645 | my ($user,$email,$fullname,$pass,$sessionid,$allow_admin) = @_; | ||
| 646 | |||
| 647 | # for debugging front end, uncomment as needed | ||
| 648 | # return $self->string_result('Session Error'); | ||
| 649 | # return $self->string_result('Username already in use, please try another.'); | ||
| 650 | # return $self->string_result("Invalid e-mail address (",$email,") provided."); | ||
| 651 | # return $self->string_result('Success'); | ||
| 652 | |||
| 653 | my $userdb = $self->dbi; | ||
| 654 | |||
| 655 | return $self->string_result("Invalid e-mail address (",$email,") provided.") | ||
| 656 | unless $self->check_email($email); | ||
| 657 | |||
| 658 | return $self->string_result('Usernames cannot contain any backslashes, whitespace or non-ascii characters.') | ||
| 659 | unless $self->check_user($user); | ||
| 660 | |||
| 661 | return $self->string_result("Invalid username. Try a different one.") | ||
| 662 | if !$allow_admin && $self->check_admin($user); | ||
| 663 | |||
| 664 | # see if this username is already taken | ||
| 665 | return (200,'text/plain','Username already in use, please try another.') | ||
| 666 | if $self->userid_from_username($user); | ||
| 667 | |||
| 668 | my $confirm = $self->create_key('32'); | ||
| 669 | my $nowfun = $self->nowfun(); | ||
| 670 | |||
| 671 | local $userdb->{AutoCommit} = 0; | ||
| 672 | local $userdb->{RaiseError} = 1; | ||
| 673 | eval { | ||
| 674 | my $userid = $self->add_named_session($sessionid,$user) | ||
| 675 | or return $self->dbi_err; | ||
| 676 | |||
| 677 | my $insert_userinfo = $userdb->prepare (<<END ); | ||
| 678 | INSERT INTO users (userid, gecos, email, pass, remember, openid_only, | ||
| 679 | confirmed, cnfrm_code, last_login, created) | ||
| 680 | VALUES (?,?,?,?,0,0,0,?,$nowfun,$nowfun) | ||
| 681 | END | ||
| 682 | ; | ||
| 683 | my $sha_pass = $self->generate_salted_digest($pass); | ||
| 684 | $insert_userinfo->execute($userid,$fullname,$email,$sha_pass,$confirm) | ||
| 685 | or return $self->dbi_err; | ||
| 686 | $userdb->commit(); | ||
| 687 | }; | ||
| 688 | if ($@) { | ||
| 689 | warn "user account insertion failed due to $@. Rolling back."; | ||
| 690 | eval {$userdb->rollback()}; | ||
| 691 | if(DBI->errstr =~ m/for key 1$/ || DBI->errstr =~ m/username is not unique/) { | ||
| 692 | return $self->string_result("Username already in use, please try another."); | ||
| 693 | } elsif(DBI->errstr =~ m/for key 2$/ || DBI->errstr =~ m/email is not unique/) { | ||
| 694 | return $self->string_result("E-mail address already in use, please provide another."); | ||
| 695 | } elsif(DBI->errstr =~ m/for key 3$/ || DBI->errstr =~ m/userid is not unique/) { | ||
| 696 | return $self->string_result("Session Error"); | ||
| 697 | } else { | ||
| 698 | return $self->dbi_err; | ||
| 699 | } | ||
| 700 | } | ||
| 701 | |||
| 702 | else { | ||
| 703 | return $self->do_send_confirmation($email,$confirm,$user,$pass); | ||
| 704 | } | ||
| 705 | |||
| 706 | return $self->programmer_error; | ||
| 707 | } | ||
| 708 | |||
| 709 | # Send Confirmation - Sends an e-mail when a user creates a new non-openid account to ensure that the user is valid and the e-mail exists. | ||
| 710 | sub do_send_confirmation { | ||
| 711 | my $self = shift; | ||
| 712 | my ($email,$confirm,$user,$pass) = @_; | ||
| 713 | |||
| 714 | my $globals = $self->{globals}; | ||
| 715 | my $link = $globals->gbrowse_url()."/?confirm=1&code=$confirm"; | ||
| 716 | |||
| 717 | my $message = '<HTML><BODY>'.$self->get_header(); | ||
| 718 | $message .= <<END; | ||
| 719 | <p> | ||
| 720 | <table> | ||
| 721 | <tr><th align="right">Username</th><td>$user</td></tr> | ||
| 722 | <tr><th align="right">Password</th><td>$pass</td></tr> | ||
| 723 | <tr><th align="right">Email</th><td>$email</td></tr> | ||
| 724 | </table> | ||
| 725 | </p> | ||
| 726 | <p> | ||
| 727 | To activate your account and complete the sign up process, please click | ||
| 728 | on the following link: <a href="$link">$link</a> | ||
| 729 | </p> | ||
| 730 | END | ||
| 731 | $message .= '<p style="font-size:small">'.$self->get_footer().'</p></BODY></HTML>'; | ||
| 732 | |||
| 733 | my ($status,$err) = $self->Bio::Graphics::Browser2::SendMail::do_sendmail({ | ||
| 734 | from => $globals->email_address, | ||
| 735 | from_title => $globals->application_name, | ||
| 736 | to => $email, | ||
| 737 | subject => $globals->application_name . " Account Activation", | ||
| 738 | msg => $message, | ||
| 739 | HTML => 1, | ||
| 740 | },$globals); | ||
| 741 | unless ($status) { | ||
| 742 | warn $err; | ||
| 743 | return $self->string_result('Mail Error'); | ||
| 744 | } | ||
| 745 | return $self->string_result('Success'); | ||
| 746 | } | ||
| 747 | |||
| 748 | # Edit Confirmation - Deletes or resends unconfirmed information based on "option" | ||
| 749 | sub do_edit_confirmation { | ||
| 750 | my $self = shift; | ||
| 751 | my ($email,$option) = @_; | ||
| 752 | |||
| 753 | my $userdb = $self->{dbi}; | ||
| 754 | |||
| 755 | my $select = $userdb->prepare(<<END ); | ||
| 756 | SELECT b.username, a.userid, b.sessionid, a.gecos, a.cnfrm_code | ||
| 757 | FROM users as a,session as b | ||
| 758 | WHERE a.email=? AND a.userid=b.userid | ||
| 759 | END | ||
| 760 | $select->execute($email) | ||
| 761 | or return $self->dbi_err; | ||
| 762 | my ($username,$userid,$sessionid,$fullname, $confirm) = $select->fetchrow_array(); | ||
| 763 | |||
| 764 | if ($option == 0) { # delete account! | ||
| 765 | eval { | ||
| 766 | local $userdb->{AutoCommit} = 0; | ||
| 767 | local $userdb->{RaiseError} = 1; | ||
| 768 | $userdb->do("DELETE FROM users WHERE userid=?",undef,$userid); | ||
| 769 | $userdb->do("DELETE FROM openid_users WHERE userid=?",undef,$userid); | ||
| 770 | $userdb->do("DELETE FROM session WHERE userid=?",undef,$userid); | ||
| 771 | $userdb->commit(); | ||
| 772 | }; | ||
| 773 | if ($@) { | ||
| 774 | eval {$userdb->rollback()}; | ||
| 775 | return $self->string_result($@); | ||
| 776 | } else { | ||
| 777 | return $self->string_result("Your account has been successfully removed."); | ||
| 778 | } | ||
| 779 | } | ||
| 780 | |||
| 781 | elsif ($option == 1) { | ||
| 782 | my $pass = '******'; | ||
| 783 | return $self->do_send_confirmation($email,$confirm,$username,$pass); | ||
| 784 | # return $self->string_result("Success"); | ||
| 785 | } | ||
| 786 | |||
| 787 | return $self->programmer_error; | ||
| 788 | } | ||
| 789 | |||
| 790 | # Confirm Account - Activates a new account when the user follows the mailed link. | ||
| 791 | sub do_confirm_account { | ||
| 792 | my $self = shift; | ||
| 793 | my ($user,$confirm) = @_; | ||
| 794 | my $userdb = $self->{dbi}; | ||
| 795 | |||
| 796 | my $new_confirm = sha1_hex($confirm); | ||
| 797 | |||
| 798 | my ($rows) = $userdb->selectrow_array( | ||
| 799 | "SELECT count(*) FROM users WHERE cnfrm_code=? AND confirmed=0", | ||
| 800 | undef, | ||
| 801 | $confirm); | ||
| 802 | |||
| 803 | return $self->string_result('Already Active') unless $rows == 1; | ||
| 804 | |||
| 805 | my $userid = $self->userid_from_username($user); | ||
| 806 | |||
| 807 | my $update = $userdb->prepare( | ||
| 808 | "UPDATE users SET confirmed=1,cnfrm_code=? WHERE userid=? AND cnfrm_code=? AND confirmed=0"); | ||
| 809 | $update->execute($new_confirm,$userid,$confirm) | ||
| 810 | or return $self->dbi_err; | ||
| 811 | |||
| 812 | $rows = $update->rows; | ||
| 813 | if ($rows == 1) { | ||
| 814 | my $query = $userdb->prepare( | ||
| 815 | "SELECT b.sessionid FROM users as a,session as b WHERE b.username=? AND a.userid=b.userid AND cnfrm_code=? AND confirmed=1"); | ||
| 816 | |||
| 817 | $query->execute($user,$new_confirm) | ||
| 818 | or return $self->dbi_err; | ||
| 819 | |||
| 820 | return $self->string_result($query->fetchrow_array()); | ||
| 821 | } elsif($rows == 0) { | ||
| 822 | return $self->string_result("Error: Incorrect username provided, please check your spelling and try again."); | ||
| 823 | } else { | ||
| 824 | return $self->string_result("Error: $rows rows returned, please consult your service host."); | ||
| 825 | } | ||
| 826 | return; | ||
| 827 | } | ||
| 828 | |||
| 829 | # Edit Details - Updates the user's e-mail or password depending on the "column" | ||
| 830 | sub do_edit_details { | ||
| 831 | my $self = shift; | ||
| 832 | my ($user,$column,$old,$new,$session) = @_; | ||
| 833 | my $userdb = $self->dbi; | ||
| 834 | my $userid = $self->userid_from_username($user) | ||
| 835 | or return $self->string_result("Error: unkown user $user"); | ||
| 836 | |||
| 837 | $session->private && $session->username eq $user | ||
| 838 | or return $self->string_result('Error: Apparent attempt to change details for another user'); | ||
| 839 | |||
| 840 | if($column eq 'email') { | ||
| 841 | return $self->string_result("New e-mail address is invalid, please try another.") | ||
| 842 | unless $self->check_email($new); | ||
| 843 | } | ||
| 844 | |||
| 845 | if ($column eq 'pass') { | ||
| 846 | my ($pass) = $userdb->selectrow_array('SELECT pass FROM users WHERE userid=?',undef,$userid); | ||
| 847 | unless ($self->passwd_match($old,$pass)) { | ||
| 848 | return $self->string_result("Incorrect password provided, please check your spelling."); | ||
| 849 | } | ||
| 850 | $new = $self->generate_salted_digest($new); | ||
| 851 | $old = $pass; | ||
| 852 | } | ||
| 853 | |||
| 854 | my $querystring = "UPDATE users "; | ||
| 855 | $querystring .= " SET $column = ?"; | ||
| 856 | $querystring .= " WHERE userid = ?"; | ||
| 857 | |||
| 858 | my $update = $userdb->prepare($querystring); | ||
| 859 | unless($update->execute($new,$userid)) { | ||
| 860 | if ($column eq 'email') { | ||
| 861 | return $self->string_result("New e-mail already in use, please try another."); | ||
| 862 | } else { | ||
| 863 | return $self->dbi_err; | ||
| 864 | } | ||
| 865 | } | ||
| 866 | |||
| 867 | if (DBI->errstr =~ m/for key 3$/) { | ||
| 868 | return $self->string_result("New e-mail already in use, please try another."); | ||
| 869 | } | ||
| 870 | |||
| 871 | my $rows = $update->rows; | ||
| 872 | if($rows == 1) { | ||
| 873 | return $self->string_result("Success"); | ||
| 874 | } elsif ($rows == 0) { | ||
| 875 | my $explanation = $column eq 'pass' ? 'password' | ||
| 876 | :$column eq 'email' ? 'email address' | ||
| 877 | :'information'; | ||
| 878 | return $self->string_result("Incorrect $explanation provided, please check your spelling."); | ||
| 879 | } else { | ||
| 880 | if(($column eq 'email') and ($rows == -1)) { | ||
| 881 | return $self->string_result("New e-mail already in use, please try another."); | ||
| 882 | } else { | ||
| 883 | return $self->string_result("Error: $rows rows returned, please consult your service host."); | ||
| 884 | } | ||
| 885 | } | ||
| 886 | return $self->programmer_error; | ||
| 887 | } | ||
| 888 | |||
| 889 | # E-mail Info - Sends an e-mail when a user has forgotten their password. | ||
| 890 | sub do_email_info { | ||
| 891 | my $self = shift; | ||
| 892 | my $email = shift; | ||
| 893 | my $globals = $self->{globals}; | ||
| 894 | my $userdb = $self->{dbi}; | ||
| 895 | |||
| 896 | return $self->string_result("Invalid e-mail address provided.") | ||
| 897 | unless $self->check_email($email); | ||
| 898 | |||
| 899 | my ($user,$rows,$openid_ref) = $self->do_retrieve_user($email); | ||
| 900 | my @openids = @$openid_ref; | ||
| 901 | my $openid = ""; | ||
| 902 | |||
| 903 | return $self->string_result($user) unless $rows == 1; | ||
| 904 | |||
| 905 | if (@openids) {foreach(@openids) {$openid .= "$_\n ";}} | ||
| 906 | else {$openid = "None\n";} | ||
| 907 | |||
| 908 | my $pass = $self->create_key('8'); | ||
| 909 | my $message = "\nYour password has been reset to the one seen below. To fix this,"; | ||
| 910 | $message .= " select \"My Account\" from the log in menu and log in with the"; | ||
| 911 | $message .= " credentials found below.\n\n Username: $user\n "; | ||
| 912 | $message .= "Password: $pass\n\n OpenIDs: $openid\n\n"; | ||
| 913 | $message .= $self->get_footer(); | ||
| 914 | |||
| 915 | my ($status,$err) = $self->Bio::Graphics::Browser2::SendMail::do_sendmail({ | ||
| 916 | from => $globals->email_address, | ||
| 917 | from_title => $globals->application_name, | ||
| 918 | to => $email, | ||
| 919 | subject => $globals->application_name . " Account Information", | ||
| 920 | msg => $message | ||
| 921 | },$globals); | ||
| 922 | return $self->string_result($err) unless $status; | ||
| 923 | |||
| 924 | my $secret = $self->generate_salted_digest($pass); | ||
| 925 | my $update = $userdb->prepare( | ||
| 926 | "UPDATE users SET pass=? WHERE userid=? AND email=? AND confirmed=1"); | ||
| 927 | my $userid = $self->userid_from_username($user); | ||
| 928 | $update->execute($secret,$userid,$email) | ||
| 929 | or return $self->dbi_err; | ||
| 930 | |||
| 931 | return $self->string_result('Success'); | ||
| 932 | } | ||
| 933 | |||
| 934 | sub set_password { | ||
| 935 | my $self = shift; | ||
| 936 | my ($userid,$password) = @_; | ||
| 937 | my $userdb = $self->dbi; | ||
| 938 | my $secret = $self->generate_salted_digest($password); | ||
| 939 | my $update = $userdb->prepare( | ||
| 940 | "UPDATE users SET pass=? WHERE userid=?") or die $userdb->errstr; | ||
| 941 | my $status = $update->execute($secret,$userid) or die $userdb->errstr; | ||
| 942 | return $status; | ||
| 943 | } | ||
| 944 | |||
| 945 | # Retrieve User - Gets the username associated with a given e-mail. | ||
| 946 | sub do_retrieve_user { | ||
| 947 | my $self = shift; | ||
| 948 | my $email = shift; | ||
| 949 | |||
| 950 | my $userdb = $self->{dbi}; | ||
| 951 | |||
| 952 | my @openids; | ||
| 953 | |||
| 954 | my $users = $userdb->selectcol_arrayref( | ||
| 955 | "SELECT username FROM users as a,session as b WHERE a.userid=b.userid AND email=? AND confirmed=1", | ||
| 956 | undef, | ||
| 957 | $email) | ||
| 958 | or return $self->dbi_err; | ||
| 959 | |||
| 960 | my $rows = @$users; | ||
| 961 | if ($rows == 1) { | ||
| 962 | my $user = $users->[0]; | ||
| 963 | my $query = $userdb->prepare( | ||
| 964 | "SELECT openid_url FROM openid_users,session WHERE openid_users.userid=session.userid and session.username=?"); | ||
| 965 | $query->execute($user) | ||
| 966 | or return $self->dbi_err; | ||
| 967 | |||
| 968 | while (my $openid = $query->fetchrow_array) { | ||
| 969 | push (@openids,$openid); | ||
| 970 | } | ||
| 971 | |||
| 972 | return ($user,$rows,\@openids); | ||
| 973 | } elsif($rows == 0) { | ||
| 974 | return ("Sorry, an account does not exist for the e-mail provided.",$rows,\@openids); | ||
| 975 | } else { | ||
| 976 | return ("Error: $rows accounts match your e-mail, please consult your service host.",$rows,\@openids); | ||
| 977 | } | ||
| 978 | } | ||
| 979 | |||
| 980 | # Delete User - Removes a user from the database. | ||
| 981 | sub do_delete_user { | ||
| 982 | my $self = shift; | ||
| 983 | my ($user, $pass) = @_; | ||
| 984 | |||
| 985 | my $userdb = $self->dbi; | ||
| 986 | my $userid = $self->userid_from_username($user); | ||
| 987 | return $self->string_result("Error: unknown user $user") unless $userid; | ||
| 988 | |||
| 989 | my $sessionid = $self->get_sessionid($userid); | ||
| 990 | $userdb->do('DELETE FROM session where userid=?',undef,$userid); | ||
| 991 | my $session = $self->globals->session($sessionid); | ||
| 992 | $session->delete; | ||
| 993 | $session->flush; | ||
| 994 | |||
| 995 | $userdb->do('DELETE FROM users WHERE userid=?',undef,$userid); | ||
| 996 | |||
| 997 | my $query = $userdb->prepare( | ||
| 998 | "DELETE FROM openid_users WHERE userid=?"); | ||
| 999 | if ($query->execute($userid)) { | ||
| 1000 | return $self->string_result('Success'); | ||
| 1001 | } else { | ||
| 1002 | return $self->dbi_err; | ||
| 1003 | } | ||
| 1004 | return; | ||
| 1005 | } | ||
| 1006 | |||
| 1007 | ######################## O P E N I D F U N C T I O N S ######################### | ||
| 1008 | # Check OpenID - Sends a user to their openid host for confirmation. | ||
| 1009 | |||
| 1010 | # BUG: ALL THE OPENID FUNCTIONS NEED TO BE REVISED | ||
| 1011 | sub do_check_openid { | ||
| 1012 | my $self = shift; | ||
| 1013 | my $globals = $self->{globals}; | ||
| 1014 | my ($openid, $sessionid, $source, $option) = @_; | ||
| 1015 | |||
| 1016 | my $return_to = $globals->gbrowse_url($source)."/?openid_confirm=1;page=$option;s=$sessionid"; | ||
| 1017 | |||
| 1018 | my $csr = Net::OpenID::Consumer->new( | ||
| 1019 | ua => LWP::UserAgent->new, | ||
| 1020 | args => CGI->new, | ||
| 1021 | consumer_secret => Bio::Graphics::Browser2->openid_secret, | ||
| 1022 | required_root => "http://$ENV{'HTTP_HOST'}/" | ||
| 1023 | ); | ||
| 1024 | |||
| 1025 | my $claimed_identity = $csr->claimed_identity($openid) | ||
| 1026 | or return $self->string_result("The URL provided is not a valid OpenID, please check your spelling and try again."); | ||
| 1027 | |||
| 1028 | my $check_url = $claimed_identity->check_url( | ||
| 1029 | return_to => $return_to, | ||
| 1030 | trust_root => "http://$ENV{'HTTP_HOST'}/", | ||
| 1031 | delayed_return => 1 | ||
| 1032 | ); | ||
| 1033 | # request information about email address and full name | ||
| 1034 | $check_url .= "&openid.ns.ax=http://openid.net/srv/ax/1.0&openid.ax.mode=fetch_request&openid.ax.required=email,firstname,lastname&openid.ax.type.email=http://axschema.org/contact/email&openid.ax.type.firstname=http://axschema.org/namePerson/first&openid.ax.type.lastname=http://axschema.org/namePerson/last"; | ||
| 1035 | |||
| 1036 | # this shouldn't work, but oddly it does. | ||
| 1037 | # it has something to do with prototype ajax and the Location: string | ||
| 1038 | return (200,'text/html',"Location: $check_url"); # shouldn't work? | ||
| 1039 | |||
| 1040 | # this should work, but oddly it doesn't | ||
| 1041 | # return (302,undef,$check_url); | ||
| 1042 | |||
| 1043 | } | ||
| 1044 | |||
| 1045 | # Confirm OpenID - Checks that the returned credentials are valid. | ||
| 1046 | sub do_confirm_openid { | ||
| 1047 | my $self = shift; | ||
| 1048 | my ($callbacks, $sessionid, $option,$email,$fullname) = @_; | ||
| 1049 | |||
| 1050 | my $userdb = $self->{dbi}; | ||
| 1051 | |||
| 1052 | my ($error, @results, $select, $user, $only); | ||
| 1053 | |||
| 1054 | my $csr = Net::OpenID::Consumer->new( | ||
| 1055 | ua => LWP::UserAgent->new, | ||
| 1056 | args => $callbacks, | ||
| 1057 | consumer_secret => Bio::Graphics::Browser2->openid_secret, | ||
| 1058 | required_root => "http://$ENV{'HTTP_HOST'}/" | ||
| 1059 | ); | ||
| 1060 | |||
| 1061 | if ($option eq "openid-add") { | ||
| 1062 | ($user, $only) = $userdb->selectrow_array( | ||
| 1063 | "SELECT b.username,a.openid_only FROM users as a,session as b WHERE b.sessionid=? AND a.userid=b.userid", | ||
| 1064 | undef, | ||
| 1065 | $sessionid) | ||
| 1066 | or return(200,'application/json',[{error=>'Error: '.$userdb->errstr.'.'}]); | ||
| 1067 | unless (defined $user) { | ||
| 1068 | push @results,{error=>"Error: Wrong session ID provided, please try again."}; | ||
| 1069 | return (200,'application/json',\@results); | ||
| 1070 | } | ||
| 1071 | } | ||
| 1072 | |||
| 1073 | $csr->handle_server_response( | ||
| 1074 | not_openid => sub { | ||
| 1075 | push @results,{user=>$user,only=>$only,error=>"Invalid OpenID provided, please check your spelling."}; | ||
| 1076 | }, | ||
| 1077 | setup_required => sub { | ||
| 1078 | push @results,{user=>$user,only=>$only,error=>"Error: Your OpenID requires setup."}; | ||
| 1079 | }, | ||
| 1080 | cancelled => sub { | ||
| 1081 | push @results,{user=>$user,only=>$only,error=>"OpenID verification cancelled."}; | ||
| 1082 | }, | ||
| 1083 | verified => sub { | ||
| 1084 | my $vident = shift; | ||
| 1085 | if($option eq "openid-add") { | ||
| 1086 | push @results,$self->do_add_openid_to_account($sessionid, $user, $vident->url, $only) | ||
| 1087 | } else { | ||
| 1088 | push @results,$self->do_get_openid($vident->url,$email,$fullname); | ||
| 1089 | } | ||
| 1090 | }, | ||
| 1091 | error => sub { | ||
| 1092 | $error = $csr->err; | ||
| 1093 | push @results,{user=>$user,only=>$only,error=>"Error validating identity: $error."}; | ||
| 1094 | } | ||
| 1095 | ); | ||
| 1096 | return (200,'application/json',\@results); | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | sub do_get_gecos { | ||
| 1100 | my $self = shift; | ||
| 1101 | my $user = shift; | ||
| 1102 | my $sessionid = $self->sessionid_from_username($user) or return ''; | ||
| 1103 | my $fullname = $self->fullname_from_sessionid($sessionid); | ||
| 1104 | return $self->string_result($fullname); | ||
| 1105 | } | ||
| 1106 | |||
| 1107 | sub do_get_email { | ||
| 1108 | my $self = shift; | ||
| 1109 | my $user = shift; | ||
| 1110 | my $sessionid = $self->sessionid_from_username($user) or return ''; | ||
| 1111 | my $fullname = $self->email_from_sessionid($sessionid); | ||
| 1112 | return $self->string_result($fullname); | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | # Get OpenID - Check to see if the provided openid is unused | ||
| 1116 | sub do_get_openid { | ||
| 1117 | my $self = shift; | ||
| 1118 | my ($openid,$email,$fullname) = @_; | ||
| 1119 | |||
| 1120 | my $userdb = $self->{dbi}; | ||
| 1121 | |||
| 1122 | my $error; | ||
| 1123 | |||
| 1124 | my $from = <<END; | ||
| 1125 | FROM users as A, openid_users as B, session as C | ||
| 1126 | WHERE A.userid = B.userid | ||
| 1127 | AND A.userid = C.userid | ||
| 1128 | AND A.confirmed = 1 | ||
| 1129 | AND B.openid_url = ? | ||
| 1130 | END | ||
| 1131 | ; | ||
| 1132 | my ($rows) = $userdb->selectrow_array( | ||
| 1133 | "select count(*) $from", | ||
| 1134 | undef, | ||
| 1135 | $openid) | ||
| 1136 | or return {error=>'Error: '.$userdb->errstr.'.'}; | ||
| 1137 | |||
| 1138 | if($rows != 1) { | ||
| 1139 | if($rows == 0) { | ||
| 1140 | $error = "The OpenID provided has not been used before. "; | ||
| 1141 | $error .= "Please create an account first before trying to edit your information."; | ||
| 1142 | } else { | ||
| 1143 | $error = "Error: $rows rows returned, please consult your service host."; | ||
| 1144 | } | ||
| 1145 | return {error=>$error,openid=>$openid,email=>$email,fullname=>$fullname}; | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | my $select = $userdb->prepare("SELECT C.username, C.sessionid, A.remember, A.openid_only, A.userid $from"); | ||
| 1149 | $select->execute($openid) | ||
| 1150 | or return {error=>'Error: '.$select->errstr.'.'}; | ||
| 1151 | |||
| 1152 | my @info = $select->fetchrow_array; | ||
| 1153 | |||
| 1154 | my $nowfun = $self->nowfun(); | ||
| 1155 | my $update = $userdb->prepare( | ||
| 1156 | "UPDATE users SET last_login=$nowfun WHERE userid=? AND confirmed=1"); | ||
| 1157 | $update->execute($info[4]) | ||
| 1158 | or return {error=>'Error: '.$update->errstr.'.'}; | ||
| 1159 | |||
| 1160 | return {user=>$info[0],session=>$info[1],remember=>$info[2],only=>$info[3]}; | ||
| 1161 | } | ||
| 1162 | |||
| 1163 | # Change OpenID - Add or removes an openid from an account based on "option" | ||
| 1164 | sub do_change_openid { | ||
| 1165 | my $self = shift; | ||
| 1166 | my ($user, $pass, $openid, $option) = @_; | ||
| 1167 | |||
| 1168 | my $userdb = $self->dbi; | ||
| 1169 | my $users = $userdb->selectrow_arrayref('SELECT a.userid FROM users as a,session as b WHERE b.username=? AND a.userid=b.userid', | ||
| 1170 | undef,$user) | ||
| 1171 | or return $self->dbi_err; | ||
| 1172 | my $rows = @$users; | ||
| 1173 | |||
| 1174 | return $self->string_result("Error: unknown user $user.") unless $rows == 1; | ||
| 1175 | |||
| 1176 | if ($option eq "add") { | ||
| 1177 | return $self->do_check_openid($openid, $users->[0],"openid-add"); | ||
| 1178 | } | ||
| 1179 | |||
| 1180 | # if we get here, we are deleting | ||
| 1181 | my ($correct_pass) = $userdb->selectrow_array('SELECT a.pass FROM users as a WHERE a.userid=?',undef,$users->[0]) | ||
| 1182 | or return $self->dbi_err; | ||
| 1183 | |||
| 1184 | $self->passwd_match($pass,$correct_pass) | ||
| 1185 | or return $self->string_result('Invalid password. Please try again.'); | ||
| 1186 | |||
| 1187 | my $delete = $userdb->prepare(<<END ); | ||
| 1188 | DELETE FROM openid_users | ||
| 1189 | WHERE openid_url=? | ||
| 1190 | AND userid IN ( | ||
| 1191 | SELECT a.userid | ||
| 1192 | FROM openid_users as a,session as b | ||
| 1193 | WHERE a.userid=b.userid | ||
| 1194 | AND b.username=? | ||
| 1195 | ) | ||
| 1196 | END | ||
| 1197 | ; | ||
| 1198 | if ($delete->execute($openid,$user)) { | ||
| 1199 | return $self->string_result('Success'); | ||
| 1200 | } else { | ||
| 1201 | if(DBI->errstr =~ m/for key 1$/) { | ||
| 1202 | return $self->string_result("The OpenID provided is already in use, please try another."); | ||
| 1203 | } else { | ||
| 1204 | return $self->dbi_err; | ||
| 1205 | } | ||
| 1206 | } | ||
| 1207 | return $self->programmer_error; | ||
| 1208 | } | ||
| 1209 | |||
| 1210 | # Add OpenID to Account (UserID, Username, OpenID, Only(?)) - Adds a confirmed openid to an account. | ||
| 1211 | sub do_add_openid_to_account { | ||
| 1212 | my $self = shift; | ||
| 1213 | my ($sessionid, $user, $openid, $only) = @_; | ||
| 1214 | |||
| 1215 | my $userdb = $self->{dbi}; | ||
| 1216 | my $error; | ||
| 1217 | |||
| 1218 | my $userid = $self->userid_from_sessionid($sessionid) | ||
| 1219 | or return {user=>$user,only=>$only,error=>"Error: No userid associated with the current session"}; | ||
| 1220 | |||
| 1221 | my $insert = $userdb->prepare("INSERT INTO openid_users (userid,openid_url) VALUES (?,?)"); | ||
| 1222 | if($insert->execute($userid, $openid)) { | ||
| 1223 | $error = "Success"; | ||
| 1224 | } else { | ||
| 1225 | if(DBI->errstr =~ m/for key 1$/) { | ||
| 1226 | $error = "The OpenID provided is already in use, please try another."; | ||
| 1227 | } else { | ||
| 1228 | $error = "Error: ".DBI->errstr."."; | ||
| 1229 | } | ||
| 1230 | } | ||
| 1231 | return {user=>$user,only=>$only,error=>$error}; | ||
| 1232 | } | ||
| 1233 | |||
| 1234 | # Add OpenID User (Username, Email, Gecos(fullname), OpenID, UserID, Remember?) - Adds a new openid user to the user database. | ||
| 1235 | sub do_add_openid_user { | ||
| 1236 | my $self = shift; | ||
| 1237 | my ($user, $email, $gecos, $openid, $sessionid, $remember) = @_; | ||
| 1238 | |||
| 1239 | my $userdb = $self->{dbi}; | ||
| 1240 | |||
| 1241 | return $self->string_result("Usernames cannot contain any backslashes, whitespace or non-ascii characters.") | ||
| 1242 | unless $self->check_user($user); | ||
| 1243 | |||
| 1244 | my $confirm = sha1_hex($self->create_key('32')); | ||
| 1245 | my $pass = $self->generate_salted_digest($self->create_key('32')); | ||
| 1246 | |||
| 1247 | my $nowfun = $self->nowfun(); | ||
| 1248 | |||
| 1249 | local $userdb->{AutoCommit} = 0; | ||
| 1250 | local $userdb->{RaiseError} = 1; | ||
| 1251 | eval { | ||
| 1252 | my $userid = $self->add_named_session($sessionid,$user) | ||
| 1253 | or return $self->dbi_err; | ||
| 1254 | |||
| 1255 | my $query = $userdb->prepare(<<END ); | ||
| 1256 | INSERT INTO users (userid,email,gecos,pass,remember,openid_only,confirmed,cnfrm_code,last_login,created) | ||
| 1257 | VALUES (?,?,?,?,?,1,1,?, $nowfun, $nowfun) | ||
| 1258 | END | ||
| 1259 | ; | ||
| 1260 | |||
| 1261 | $query->execute($userid, $email, $gecos, $pass, $remember, $confirm) | ||
| 1262 | or return $self->dbi_err; | ||
| 1263 | |||
| 1264 | my $insert = $userdb->prepare("INSERT INTO openid_users (userid,openid_url) VALUES (?,?)"); | ||
| 1265 | $insert->execute($userid, $openid) or die "Couldn't insert url into openid_users table: ",$insert->errstr; | ||
| 1266 | |||
| 1267 | $userdb->commit(); | ||
| 1268 | }; | ||
| 1269 | |||
| 1270 | if ($@) { | ||
| 1271 | warn "openid user account insertion failed due to $@. Rolling back."; | ||
| 1272 | my $err = $@; | ||
| 1273 | eval {$userdb->rollback()}; | ||
| 1274 | |||
| 1275 | if($err =~ m/for key 1$/) { | ||
| 1276 | return $self->string_result("The OpenID provided is already in use, please try another."); | ||
| 1277 | } | ||
| 1278 | elsif ($err =~ m/for key 1$/ || DBI->errstr =~ m/for key 3$/) { | ||
| 1279 | #If the e-mail happens to match another, this will still be called. | ||
| 1280 | return $self->string_result("Username already in use, please try another."); | ||
| 1281 | } elsif($err =~ m/for key 2$/) { | ||
| 1282 | return $self->string_result("Session Error"); | ||
| 1283 | } else { | ||
| 1284 | return $self->dbi_err($err); | ||
| 1285 | } | ||
| 1286 | } | ||
| 1287 | else { | ||
| 1288 | return $self->string_result('Success'); | ||
| 1289 | } | ||
| 1290 | return $self->programmer_error; | ||
| 1291 | } | ||
| 1292 | |||
| 1293 | # List OpenID (User) - Generates a list of openids associated with a user's account. | ||
| 1294 | sub do_list_openid { | ||
| 1295 | my $self = shift; | ||
| 1296 | my $user = shift; | ||
| 1297 | my ($error,@openids); | ||
| 1298 | my $userdb = $self->{dbi}; | ||
| 1299 | |||
| 1300 | my $select = $userdb->prepare( | ||
| 1301 | "SELECT a.openid_url FROM openid_users as a,session as b WHERE b.username=? AND a.userid=b.userid"); | ||
| 1302 | $select->execute($user) or return (200,'application/json',[{error=>'Error: '.$userdb->errstr.'.'}]); | ||
| 1303 | |||
| 1304 | while (my $openid = $select->fetchrow_array) { | ||
| 1305 | push @openids,{name=>$openid}; | ||
| 1306 | } | ||
| 1307 | |||
| 1308 | unless (@openids) { | ||
| 1309 | push @openids,{error=>"There are no OpenIDs currently associated with this account."} | ||
| 1310 | } | ||
| 1311 | |||
| 1312 | my @results = sort {$a->{name} cmp $b->{name}} @openids; | ||
| 1313 | return(200,'application/json',\@results); | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | # convenience methods | ||
| 1317 | sub dbi_err { | ||
| 1318 | my $self = shift; | ||
| 1319 | my $err = shift; | ||
| 1320 | my $error = $err || DBI->errstr; | ||
| 1321 | $error =~ s/at.+line \d+//; | ||
| 1322 | return (200,'text/plain',"Error: $error"); | ||
| 1323 | } | ||
| 1324 | |||
| 1325 | sub code_result { | ||
| 1326 | my $self = shift; | ||
| 1327 | my ($code,@msg) = @_; | ||
| 1328 | return (200,'application/json',{code=>$code,message=>join('',@msg)}); | ||
| 1329 | } | ||
| 1330 | sub string_result { | ||
| 1331 | my $self = shift; | ||
| 1332 | my @msg = @_; | ||
| 1333 | return (200,'text/plain',join('',@msg)); | ||
| 1334 | } | ||
| 1335 | |||
| 1336 | sub programmer_error { | ||
| 1337 | my $self = shift; | ||
| 1338 | return (500,'text/plain','programmer error?'); | ||
| 1339 | } | ||
| 1340 | |||
| 1341 | # Remember (User) - Get's a user's remember flag. | ||
| 1342 | sub remember { | ||
| 1343 | my $self = shift; | ||
| 1344 | my $userid = shift; | ||
| 1345 | my $userdb = $self->{dbi}; | ||
| 1346 | return $userdb->selectrow_array("SELECT remember FROM users WHERE userid = ?", undef, $userid); | ||
| 1347 | } | ||
| 1348 | |||
| 1349 | # Remember (User) - Get's if a user is using OpenID login. | ||
| 1350 | sub using_openid { | ||
| 1351 | my $self = shift; | ||
| 1352 | my $userid = shift; | ||
| 1353 | my $userdb = $self->{dbi}; | ||
| 1354 | return $userdb->selectrow_array("SELECT userid FROM openid_users WHERE userid = ?", undef, $userid)? "true" : "false"; | ||
| 1355 | } | ||
| 1356 | |||
| 1357 | sub clone_database { | ||
| 1358 | my $self = shift; | ||
| 1359 | $self->{dbi}{InactiveDestroy} = 1; | ||
| 1360 | $self->{dbi} = $self->{dbi}->clone | ||
| 1361 | } | ||
| 1362 | |||
| 1363 | 1; | ||
| 1364 | |||
| 1365 | __END__ |