--- loncom/auth/migrateuser.pm	2005/10/24 21:32:42	1.1
+++ loncom/auth/migrateuser.pm	2005/10/25 19:14:32	1.2
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Starts a user off based of an existing token.
 #
-# $Id: migrateuser.pm,v 1.1 2005/10/24 21:32:42 albertel Exp $
+# $Id: migrateuser.pm,v 1.2 2005/10/25 19:14:32 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -26,7 +26,7 @@
 # http://www.lon-capa.org/
 #
 
-package Apache::startuser;
+package Apache::migrateuser;
 
 use strict;
 use Apache::Constants qw(:common :http :methods);
@@ -34,13 +34,13 @@ use Apache::lonauth;
 use Apache::lonnet;
 
 sub goto_login {
-  my ($r) = @_;
-  &Apache::loncommon::content_type($r,'text/html');
-  $r->send_http_header;
-  $r->print(<<TOLOGIN);
+    my ($r) = @_;
+    &Apache::loncommon::content_type($r,'text/html');
+    $r->send_http_header;
+    $r->print(<<TOLOGIN);
 <html>
   <head>
-    <meta http-equiv="refresh" content="10;url=/adm/login" />
+    <meta http-equiv="refresh" content="0;url=/adm/login" />
     <title>Going to login</title>
   </head>
   <body>
@@ -52,31 +52,38 @@ sub goto_login {
   </body>
 </html>
 TOLOGIN
-    return '';
+    return OK;
 }
 
+
 sub handler {
     my ($r) = @_;
     
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
-    my $data = &Apache::lonnet::reply('tmpget:'.$env{'form.token'},
-				      $Apache::lonnet::perlvar{'lonHostID'});
-    my ($ip,$udom,$uname,$role) = split('&',$data);
-
-    if ($ip ne $ENV{'REMOTE_ADDR'} && $ip ne '127.0.0.1') {
-	#error or invalid token
-	&goto_login($r);
+    my %data = &Apache::lonnet::tmpget($env{'form.token'});
+
+    if ($data{'ip'} ne $ENV{'REMOTE_ADDR'} || !defined($data{'username'}) ||
+	!defined($data{'domain'}) ) {
+	return &goto_login($r);
+    }
+
+    &Apache::lonnet::logthis("Allowing access for $data{'username'}\@$data{'domain'} to $data{'role'}");
+    my $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
+    if ($home =~ /(con_lost|no_such_host)/) { return &goto_login($r); }
+
+    if (!defined($data{'role'})) {
+	&Apache::lonauth::success($r,$data{'username'},$data{'domain'},
+				  $home,'/adm/roles');
 	return OK;
     }
 
-    &Apache::lonnet::logthis("Allowing access for $uname\@$udom to $role");
-    my $home=&Apache::lonnet::homeserver($uname,$udom);
-    my $cookie=&Apache::lonauth::success($r,$uname,$udom,$home,'noredirect');
+    my $cookie=&Apache::lonauth::success($r,$data{'username'},$data{'domain'},
+					 $home,'noredirect');
     $r->header_out('Set-cookie',"lonID=$cookie; path=/");
     &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
 					     $cookie);
     $env{'form.selectrole'}='1';
-    $env{'form.'.$role}='1';
+    $env{'form.'.$data{'role'}}='1';
     return &Apache::lonroles::handler($r);
 }