Annotation of loncom/auth/switchserver.pm, revision 1.17

1.1       albertel    1: # The LearningOnline Network
                      2: # Switch Servers Handler
                      3: #
1.17    ! albertel    4: # $Id: switchserver.pm,v 1.16 2007/09/08 02:43:33 raeburn Exp $
1.1       albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: 
                     29: package Apache::switchserver;
                     30: 
                     31: use strict;
                     32: use Apache::Constants qw(:common);
                     33: use Apache::lonnet;
                     34: use Apache::lonmenu;
                     35: use CGI::Cookie();
                     36: use Apache::lonlocal;
                     37: 
                     38: sub init_env {
                     39:     my ($r) = @_;
1.5       albertel   40:     
                     41:     if (-e $env{'user.environment'}) {
                     42: 	return  $env{'user.environment'};
                     43:     }
1.1       albertel   44:     my $requrl=$r->uri;
                     45:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                     46:     my $lonid=$cookies{'lonID'};
                     47:     my $cookie;
                     48:     if (!$lonid) { return undef; }
                     49: 
1.10      albertel   50:     my $handle=&LONCAPA::clean_handle($lonid->value);
1.1       albertel   51:     my $lonidsdir=$r->dir_config('lonIDsDir');
                     52:     if ((!-e "$lonidsdir/$handle.id") || ($handle eq '')) {
                     53: 	$r->log_reason("Cookie $handle not valid", $r->filename); 
                     54: 	return undef;
                     55:     }
                     56: 
                     57:     &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
                     58:     
1.5       albertel   59:     return $r->dir_config('lonIDsDir')."/$handle.id";
1.1       albertel   60: }
                     61: 
1.14      albertel   62: sub do_redirect {
                     63:     my ($r,$url,$only_body,$extra_text) = @_;
                     64:     $r->send_http_header;
                     65:     my $start_page = 
1.16      raeburn    66: 	&Apache::loncommon::start_page('Switching Server ...',undef,
                     67: 				       {'redirect'       => [0.5,$url],
                     68:                                         'no_inline_link' => 1,
1.14      albertel   69: 					'only_body'      => $only_body,});
                     70:     my $end_page   = &Apache::loncommon::end_page();
                     71:     $r->print($start_page.$extra_text.$end_page);
                     72:     return OK;
                     73: 
                     74: }
                     75: 
1.1       albertel   76: sub handler {
1.5       albertel   77:     my ($r) = @_;
1.1       albertel   78:     
                     79:     my $handle=&init_env($r);
                     80:     if (!defined($handle)) { return FORBIDDEN; }
                     81: 
                     82:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                     83: 					    ['otherserver','role']);
1.5       albertel   84: 
1.13      albertel   85:     my $switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
1.17    ! albertel   86:     if (! $env{'form.otherserver'}) {
        !            87: 	$env{'form.otherserver'} =	
        !            88: 	    &Apache::lonnet::find_existing_session($env{'user.domain'},
        !            89: 						   $env{'user.name'});
        !            90: 	if (! $env{'form.otherserver'}) {
        !            91: 	    $env{'form.otherserver'} = 
        !            92: 		&Apache::lonnet::spareserver(30000,undef,1);
        !            93: 	}
        !            94: 
1.13      albertel   95: 	$switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
1.4       albertel   96:     }
1.5       albertel   97: 
1.1       albertel   98:     if (!defined($switch_to)) { return FORBIDDEN; }
1.4       albertel   99: 
1.14      albertel  100:     if ($env{'user.name'} eq 'public'
                    101: 	&& $env{'user.domain'} eq 'public') {
                    102: 	my $url = 'http://'.$switch_to.'/'.$r->uri;
                    103: 	return &do_redirect($r,$url,1)
                    104:     }
                    105: 
1.4       albertel  106:     if ($env{'form.role'} && 
                    107: 	!exists($env{'user.role.'.$env{'form.role'}})) { return FORBIDDEN; }
1.1       albertel  108: 
                    109:     #remove session env, and log event
1.5       albertel  110:     unlink($handle);
1.1       albertel  111:     my %temp=('switchserver' => time.':'.$env{'form.otherserver'},
                    112: 	      $env{'form.role'});
                    113:     &Apache::lonnet::put('email_status',\%temp);
                    114:     &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
                    115: 			 $env{'user.home'},
                    116: 			 "Switch Server to $env{'form.otherserver'} with role $env{'form.role'} $ENV{'REMOTE_ADDR'}");
                    117: 
                    118:     &Apache::loncommon::content_type($r,'text/html');
1.12      albertel  119: 
                    120:     #expire the cookie
                    121:     my $c = new CGI::Cookie(-name    => 'lonID',
                    122: 			    -value   => '',
                    123: 			    -expires => '-10y',);
                    124:     $r->header_out('Set-cookie' => $c);
1.1       albertel  125:     $r->send_http_header;
                    126:     return OK if $r->header_only;
                    127: # -------------------------------------------------------- Menu script and info
                    128: 	   
1.14      albertel  129:     my $windowinfo=
                    130: 	&Apache::lonmenu::close().
                    131: 	&Apache::lonnavmaps::close();
1.1       albertel  132: # ---------------------------------------------------------------- Get handover
                    133: 
1.3       albertel  134:     my %info=('ip'       => $ENV{'REMOTE_ADDR'},
                    135: 	      'domain'   => $env{'user.domain'},
                    136: 	      'username' => $env{'user.name'},
                    137: 	      'role'     => $env{'form.role'},
                    138: 	      'server'   => $r->dir_config('lonHostID'));
1.8       albertel  139:     if ($env{'request.sso.login'}) {
                    140: 	$info{'sso.login'} = $env{'request.sso.login'};
                    141:     }
1.11      raeburn   142:     if ($env{'request.sso.reloginserver'}) {
                    143:         $info{'sso.reloginserver'} = $env{'request.sso.reloginserver'};
                    144:     }
1.3       albertel  145:     my $token = &Apache::lonnet::tmpput(\%info,$env{'form.otherserver'});
1.14      albertel  146:     my $url ='http://'.$switch_to.'/adm/login?'.
                    147: 	'domain='.$env{'user.domain'}.
1.15      albertel  148: 	'&username='.$env{'user.name'}.
                    149: 	'&token='.$token;
1.1       albertel  150: # --------------------------------------------------------------- Screen Output
                    151:     &Apache::lonnet::flushcourselogs();
1.14      albertel  152:     return &do_redirect($r,$url,0,$windowinfo);
1.1       albertel  153: }
                    154: 
                    155: 1;
                    156: __END__
                    157: 
                    158: 
                    159: 
                    160: 
                    161: 
                    162: 
                    163: 
                    164: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>