File:  [LON-CAPA] / loncom / auth / migrateuser.pm
Revision 1.20: download - view: text, annotated - select for diffs
Mon Dec 30 20:55:42 2013 UTC (10 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0, HEAD
- Punctuation.

    1: # The LearningOnline Network
    2: # Starts a user off based of an existing token.
    3: #
    4: # $Id: migrateuser.pm,v 1.20 2013/12/30 20:55:42 raeburn Exp $
    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::migrateuser;
   30: 
   31: use strict;
   32: use LONCAPA qw(:DEFAULT :match);
   33: use Apache::Constants qw(:common :http :methods);
   34: use Apache::lonauth;
   35: use Apache::lonnet;
   36: use Apache::lonlocal;
   37: use Apache::lonlogin();
   38: 
   39: sub goto_login {
   40:     my ($r) = @_;
   41:     &Apache::loncommon::content_type($r,'text/html');
   42:     $r->send_http_header;
   43:     $r->print(&Apache::loncommon::start_page('Going to login',undef,
   44: 					     {'redirect' =>
   45: 						  [0,'/adm/login'],}).
   46: 	      '<h1>'.&mt('One moment please...').'</h1>'.
   47: 	      '<p>'.&mt('Transferring to login page.').'</p>'.
   48: 	      &Apache::loncommon::end_page());
   49:     return OK;
   50: }
   51: 
   52: 
   53: sub sso_check {
   54:     my ($data) = @_;
   55:     my %extra_env;
   56:     if (ref($data) eq 'HASH') {
   57:         if ($data->{'sso.login'}) {
   58:             $extra_env{'request.sso.login'} = $data->{'sso.login'};
   59:         }
   60:         if ($data->{'sso.reloginserver'}) {
   61:             $extra_env{'request.sso.reloginserver'} = 
   62:                 $data->{'sso.reloginserver'};
   63:         }
   64:     }
   65:     return \%extra_env;
   66: }
   67: 
   68: sub ip_changed {
   69:     my ($r,$udom,$camefrom,$dataref) = @_;
   70:     &Apache::loncommon::content_type($r,'text/html');
   71:     $r->send_http_header;
   72:     if (ref($dataref) eq 'HASH') {
   73:         my $title = 'LON-CAPA Session redirected';
   74:         my $message = &mt('Your internet address has changed since you logged in.');  
   75:         my $camefrom = &Apache::lonnet::hostname($dataref->{'server'});
   76:         my $frombalancer = $dataref->{'balancer'};
   77:         my $rule_in_effect;
   78:         if ($frombalancer) {
   79:             my $balancerdom = &Apache::lonnet::host_domain($dataref->{'server'});
   80:             my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$balancerdom);
   81:             unless (defined($cached)) {
   82:                 my $cachetime = 60*60*24; 
   83:                 my %domconfig =
   84:                     &Apache::lonnet::get_dom('configuration',['loadbalancing'],$balancerdom);
   85:                 if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
   86:                     $result = &Apache::lonnet::do_cache_new('loadbalancing',$balancerdom,
   87:                                                             $domconfig{'loadbalancing'},$cachetime);
   88:                 }
   89:             }
   90:             if (ref($result) eq 'HASH') {
   91:                 (undef,my $currtargets,my $currrules) =
   92:                     &Apache::lonnet::check_balancer_result($result,$dataref->{'server'});
   93:                 if (ref($currrules) eq 'HASH') {
   94:                     if ($dataref->{'sso.login'}) {
   95:                         if ($currrules->{'_LC_ipchangesso'} ne '') {
   96:                             $rule_in_effect = $currrules->{'_LC_ipchangesso'};
   97:                         }
   98:                     } else {
   99:                         if ($currrules->{'_LC_ipchange'} ne '') {
  100:                             $rule_in_effect = $currrules->{'_LC_ipchange'};
  101:                         }
  102:                     }
  103:                 }
  104:             }
  105:         }
  106:         my $url;
  107:         my $lonhost= $r->dir_config('lonHostID');
  108:         my $switchto = $lonhost;
  109:         if ($rule_in_effect eq 'balancer') {
  110:             my $hosthere;
  111:             if ($dataref->{'role'}) {
  112:                 my ($adom,$aname);
  113:                 if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) {
  114:                     $adom = $1;
  115:                     $aname = $dataref->{'username'};
  116:                 } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
  117:                     $adom = $1;
  118:                     $aname = $2;
  119:                 }
  120:                 if ($adom ne '' && $aname ne '') {
  121:                     my $ahome = &Apache::lonnet::homeserver($aname,$adom);
  122:                     unless ($ahome eq 'no_host') {
  123:                         my @ids=&Apache::lonnet::current_machine_ids();
  124:                         if ($ahome && grep(/^\Q$ahome\E$/,@ids)) {
  125:                             $hosthere = 1;
  126:                         }
  127:                     }
  128:                 }
  129:             }
  130:             unless ($hosthere) { 
  131:                 my $hostname = &Apache::lonnet::hostname($dataref->{'server'});
  132:                 if ($hostname) {
  133:                     $switchto = $dataref->{'server'};
  134:                     my $protocol = $Apache::lonnet::protocol{$switchto};
  135:                     $protocol = 'http' if ($protocol ne 'https');
  136:                     $url = $protocol.'://'.$hostname;
  137:                     $message .= '<br />'.
  138:                                 &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.');
  139:                 }
  140:             }
  141:         }
  142:         if ($dataref->{'sso.login'}) {
  143:             $url .= '/adm/roles?';
  144:         } else {
  145:             $url .= '/adm/login?';
  146:             $message .= '<br />'.&mt('You will need to provide your password one more time.');
  147:         }
  148:         my %info= (
  149:                     'domain'          => $dataref->{'domain'},
  150:                     'username'        => $dataref->{'username'},
  151:                     'role'            => $dataref->{'role'},
  152:                     'sessionserver'   => $lonhost,
  153:                   );
  154:         if ($dataref->{'origurl'}) {
  155:             $info{'origurl'} = $dataref->{'origurl'};
  156:         }
  157:         if ($dataref->{'symb'}) {
  158:             $info{'symb'} = $dataref->{'symb'};
  159:         }
  160:         my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto);
  161:         unless ($iptoken eq 'conlost') {
  162:             $url .= 'iptoken='.$iptoken;
  163:         }
  164:         $r->print(&Apache::loncommon::start_page($title,undef,
  165:                                                  {'redirect' =>
  166:                                                   [2,$url],}).
  167:                   '<h1>'.&mt('One moment please...').'</h1>'.
  168:                   '<p class="LC_warning">'.$message.'</p>'.
  169:                   &Apache::loncommon::end_page());
  170:     } else {
  171:         return &goto_login($r);
  172:     }
  173:     return OK;
  174: }
  175: 
  176: sub handler {
  177:     my ($r) = @_;
  178:     
  179:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
  180:     my %data =   &Apache::lonnet::tmpget($env{'form.token'});
  181:     if (keys(%data) == 0) {
  182:         return &goto_login($r);
  183:     }
  184:     my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
  185: 
  186:     &Apache::lonlocal::get_language_handle($r);
  187: 
  188:     if ($delete ne 'ok') {
  189: 	return &goto_login($r);
  190:     }
  191: 
  192:     if (!defined($data{'username'}) || !defined($data{'domain'})) {
  193:         return &goto_login($r);
  194:     }
  195:     if ($data{'ip'} ne $ENV{'REMOTE_ADDR'}) {
  196: 	return &ip_changed($r,$data{'domain'},$data{'server'},\%data);
  197:     }
  198: 
  199:     &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} to $data{'role'}");
  200:     my $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
  201:     if ($home =~ /(con_lost|no_such_host)/) { return &goto_login($r); }
  202: 
  203:     my $extra_env = &sso_check(\%data);
  204: 
  205:     my %form;
  206:     if ($data{'symb'} ne '') {
  207:         $form{'symb'} = $data{'symb'};
  208:     }
  209: 
  210:     if (!$data{'role'}) {
  211: 	my $handle = &Apache::lonnet::check_for_valid_session($r);
  212: 	if ($handle) {
  213: 	    &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
  214: 						     $handle);
  215:             if ($data{'origurl'} ne '') {
  216:                 $r->internal_redirect($data{'origurl'});
  217:             } elsif ($env{'request.course.id'}) {
  218:                 $r->internal_redirect('/adm/navmaps');
  219: 	    } else {
  220: 		$r->internal_redirect('/adm/roles');
  221: 	    }
  222: 	} else {
  223:             my $desturl = '/adm/roles';
  224:             if ($data{'origurl'} ne '') {
  225:                 $desturl = $data{'origurl'};
  226:             }
  227: 	    &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
  228: 				      $home,$desturl,$extra_env,\%form);
  229: 
  230: 	}
  231: 	return OK;
  232: 
  233:     }
  234: 
  235:     my $next_url='/adm/roles?selectrole=1&amp;'.&escape($data{'role'}).'=1';
  236:     if ($data{'origurl'} ne '') {
  237:         $next_url .= '&amp;orgurl='.&escape($data{'origurl'});
  238:     }
  239:     &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
  240: 			      $next_url,$extra_env,\%form);
  241:     return OK;
  242: }
  243: 
  244: 1;
  245: __END__

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