Annotation of loncom/auth/migrateuser.pm, revision 1.37
1.1 albertel 1: # The LearningOnline Network
2: # Starts a user off based of an existing token.
3: #
1.37 ! raeburn 4: # $Id: migrateuser.pm,v 1.36 2018/11/28 05:05:30 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:
1.2 albertel 29: package Apache::migrateuser;
1.1 albertel 30:
31: use strict;
1.19 raeburn 32: use LONCAPA qw(:DEFAULT :match);
1.1 albertel 33: use Apache::Constants qw(:common :http :methods);
34: use Apache::lonauth;
35: use Apache::lonnet;
1.37 ! raeburn 36: use Apache::loncommon;
1.6 albertel 37: use Apache::lonlocal;
1.18 raeburn 38: use Apache::lonlogin();
1.27 raeburn 39: use Apache::ltiauth;
1.37 ! raeburn 40: use Apache::switchserver;
1.28 raeburn 41: use CGI::Cookie;
1.1 albertel 42:
43: sub goto_login {
1.27 raeburn 44: my ($r,$domain,$data) = @_;
45: if ((ref($data) eq 'HASH') && ($data->{'lti.login'})) {
46: &Apache::ltiauth::invalid_request($r,'22');
47: } else {
48: &Apache::loncommon::content_type($r,'text/html');
49: $r->send_http_header;
50: my $url = '/adm/login';
51: if ($domain) {
52: $url .= '?domain='.$domain;
53: }
54: $r->print(&Apache::loncommon::start_page('Going to login',undef,
55: {'redirect' => [0,$url],}).
56: '<h1>'.&mt('One moment please...').'</h1>'.
57: '<p>'.&mt('Transferring to login page.').'</p>'.
58: &Apache::loncommon::end_page());
59: }
1.2 albertel 60: return OK;
1.1 albertel 61: }
62:
1.27 raeburn 63: sub sso_check {
1.7 albertel 64: my ($data) = @_;
1.8 albertel 65: my %extra_env;
1.18 raeburn 66: if (ref($data) eq 'HASH') {
67: if ($data->{'sso.login'}) {
68: $extra_env{'request.sso.login'} = $data->{'sso.login'};
69: }
70: if ($data->{'sso.reloginserver'}) {
71: $extra_env{'request.sso.reloginserver'} =
72: $data->{'sso.reloginserver'};
73: }
1.35 raeburn 74: if (($data->{'balancer'}) && ($data->{'server'}) && ($data->{'balcookie'})) {
75: $extra_env{'request.balancercookie'} = $data->{'server'}.':'.$data->{'balcookie'};
76: }
1.27 raeburn 77: }
78: return \%extra_env;
79: }
80:
81: sub lti_check {
82: my ($data) = @_;
83: my %lti_env;
84: if (ref($data) eq 'HASH') {
1.26 raeburn 85: if ($data->{'lti.login'}) {
1.27 raeburn 86: $lti_env{'request.lti.login'} = $data->{'lti.login'};
87: if ($data->{'lti.reqcrs'}) {
88: $lti_env{'request.lti.reqcrs'} = $data->{'lti.reqcrs'};
89: }
90: if ($data->{'lti.reqrole'}) {
91: $lti_env{'request.lti.reqrole'} = $data->{'lti.reqrole'};
92: }
93: if ($data->{'lti.selfenrollrole'}) {
94: $lti_env{'request.lti.selfenrollrole'} = $data->{'lti.selfenrollrole'};
95: }
1.29 raeburn 96: if ($data->{'lti.uri'}) {
97: $lti_env{'request.lti.uri'} = $data->{'lti.uri'};
98: }
99: if ($data->{'lti.target'}) {
100: $lti_env{'request.lti.target'} = $data->{'lti.target'};
101: }
1.32 raeburn 102: if ($data->{'lti.sourcecrs'}) {
103: $lti_env{'request.lti.sourcecrs'} = $data->{'lti.sourcecrs'};
104: }
1.26 raeburn 105: }
106: if ($data->{'lti.passbackid'}) {
1.27 raeburn 107: $lti_env{'request.lti.passbackid'} = $data->{'lti.passbackid'};
1.26 raeburn 108: }
109: if ($data->{'lti.passbackurl'}) {
1.27 raeburn 110: $lti_env{'request.lti.passbackurl'} = $data->{'lti.passbackurl'};
1.26 raeburn 111: }
112: if ($data->{'lti.rosterid'}) {
1.27 raeburn 113: $lti_env{'request.lti.rosterid'} = $data->{'lti.rosterid'};
1.26 raeburn 114: }
115: if ($data->{'lti.rosterurl'}) {
1.27 raeburn 116: $lti_env{'request.lti.rosterurl'} = $data->{'lti.rosterurl'};
1.26 raeburn 117: }
1.7 albertel 118: }
1.27 raeburn 119: return \%lti_env;
1.18 raeburn 120: }
121:
122: sub ip_changed {
1.37 ! raeburn 123: my ($r,$udom,$camefrom,$idsref,$dataref) = @_;
1.18 raeburn 124: &Apache::loncommon::content_type($r,'text/html');
125: $r->send_http_header;
126: if (ref($dataref) eq 'HASH') {
127: my $title = 'LON-CAPA Session redirected';
128: my $message = &mt('Your internet address has changed since you logged in.');
129: my $rule_in_effect;
1.22 raeburn 130: if ($dataref->{'balancer'}) {
131: my $baldom = &Apache::lonnet::host_domain($camefrom);
132: my $balprimaryid = &Apache::lonnet::domain($baldom,'primary');
133: my $balintdom = &Apache::lonnet::internet_dom($balprimaryid);
134: my $uprimaryid = &Apache::lonnet::domain($udom,'primary');
135: my $uintdom = &Apache::lonnet::internet_dom($uprimaryid);
136: my $dom_in_use;
137: if (($uintdom ne '') && ($uintdom eq $balintdom)) {
138: $dom_in_use = $udom;
139: } else {
140: $dom_in_use = $baldom;
1.21 raeburn 141: }
1.22 raeburn 142: my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
1.18 raeburn 143: unless (defined($cached)) {
144: my $cachetime = 60*60*24;
145: my %domconfig =
1.22 raeburn 146: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1.18 raeburn 147: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.22 raeburn 148: $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
1.18 raeburn 149: $domconfig{'loadbalancing'},$cachetime);
150: }
151: }
152: if (ref($result) eq 'HASH') {
153: (undef,my $currtargets,my $currrules) =
154: &Apache::lonnet::check_balancer_result($result,$dataref->{'server'});
155: if (ref($currrules) eq 'HASH') {
156: if ($dataref->{'sso.login'}) {
157: if ($currrules->{'_LC_ipchangesso'} ne '') {
158: $rule_in_effect = $currrules->{'_LC_ipchangesso'};
159: }
160: } else {
161: if ($currrules->{'_LC_ipchange'} ne '') {
162: $rule_in_effect = $currrules->{'_LC_ipchange'};
163: }
164: }
165: }
166: }
167: }
168: my $url;
169: my $lonhost= $r->dir_config('lonHostID');
170: my $switchto = $lonhost;
1.23 raeburn 171: if ($rule_in_effect ne 'offloadedto') {
1.37 ! raeburn 172: my ($hosthere,@ids);
! 173: if (ref($idsref) eq 'ARRAY') {
! 174: @ids=@{$idsref};
! 175: }
1.23 raeburn 176: unless ($rule_in_effect eq 'balancer') {
177: if (grep(/^\Q$rule_in_effect\E$/,@ids)) {
178: $hosthere = 1;
179: }
180: }
181: unless ($hosthere) {
182: if ($dataref->{'role'}) {
183: my ($adom,$aname);
184: if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) {
185: $adom = $1;
186: $aname = $dataref->{'username'};
187: } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
188: $adom = $1;
189: $aname = $2;
190: }
191: if ($adom ne '' && $aname ne '') {
192: my $ahome = &Apache::lonnet::homeserver($aname,$adom);
193: unless ($ahome eq 'no_host') {
194: if ($ahome && grep(/^\Q$ahome\E$/,@ids)) {
195: $hosthere = 1;
196: }
1.18 raeburn 197: }
198: }
199: }
200: }
1.23 raeburn 201: unless ($hosthere) {
202: my $hostname;
203: if ($rule_in_effect eq 'balancer') {
204: $hostname = &Apache::lonnet::hostname($dataref->{'server'});
205: if ($hostname) {
206: $switchto = $dataref->{'server'};
207: }
208: } else {
209: $hostname = &Apache::lonnet::hostname($rule_in_effect);
210: if ($hostname) {
211: $switchto = $rule_in_effect;
212: }
213: }
1.18 raeburn 214: if ($hostname) {
215: my $protocol = $Apache::lonnet::protocol{$switchto};
216: $protocol = 'http' if ($protocol ne 'https');
217: $url = $protocol.'://'.$hostname;
1.23 raeburn 218: if ($rule_in_effect eq 'balancer') {
219: $message .= '<br />'.
220: &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.');
221: } else {
222: $message .= '<br />'.
223: &mt('As a result, your LON-CAPA session is being redirected.');
224: }
1.18 raeburn 225: }
226: }
1.35 raeburn 227: unless ($hosthere) {
228: if (($dataref->{'balancer'}) && ($dataref->{'balcookie'})) {
229: &Apache::lonnet::delbalcookie($dataref->{'balcookie'},$dataref->{'balancer'});
230: }
231: }
1.18 raeburn 232: }
233: if ($dataref->{'sso.login'}) {
1.21 raeburn 234: $url .= '/adm/roles';
1.18 raeburn 235: } else {
1.21 raeburn 236: $url .= '/adm/login';
1.24 raeburn 237: if ($udom) {
238: $url .= '?domain='.$udom;
239: }
1.20 raeburn 240: $message .= '<br />'.&mt('You will need to provide your password one more time.');
1.18 raeburn 241: }
242: my %info= (
1.24 raeburn 243: 'domain' => $udom,
1.18 raeburn 244: 'username' => $dataref->{'username'},
245: 'role' => $dataref->{'role'},
246: 'sessionserver' => $lonhost,
247: );
248: if ($dataref->{'origurl'}) {
249: $info{'origurl'} = $dataref->{'origurl'};
250: }
251: if ($dataref->{'symb'}) {
252: $info{'symb'} = $dataref->{'symb'};
253: }
254: my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto);
255: unless ($iptoken eq 'conlost') {
1.24 raeburn 256: $url .= ($url =~ /\?/) ? '&' : '?';
257: $url .= 'iptoken='.$iptoken;
1.18 raeburn 258: }
259: $r->print(&Apache::loncommon::start_page($title,undef,
260: {'redirect' =>
261: [2,$url],}).
262: '<h1>'.&mt('One moment please...').'</h1>'.
263: '<p class="LC_warning">'.$message.'</p>'.
264: &Apache::loncommon::end_page());
265: } else {
266: return &goto_login($r);
1.10 raeburn 267: }
1.18 raeburn 268: return OK;
1.7 albertel 269: }
270:
1.28 raeburn 271: sub logout {
272: my ($r,$handle,$data,$lti_env) = @_;
1.34 raeburn 273: my $lonidsdir=$r->dir_config('lonIDsDir');
274: if (unlink("$lonidsdir/$handle.id")) {
275: if (($env{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
276: (-l "$lonidsdir/$env{'user.linkedenv'}.id") &&
277: (readlink("$lonidsdir/$env{'user.linkedenv'}.id") eq "$lonidsdir/$handle.id")) {
278: unlink("$lonidsdir/$env{'user.linkedenv'}.id");
1.28 raeburn 279: }
280: }
281: my %temp=('logout' => time);
282: &Apache::lonnet::put('email_status',\%temp);
283: &Apache::lonnet::log($env{'user.domain'},
284: $env{'user.name'},
285: $env{'user.home'},
286: "Logout $ENV{'REMOTE_ADDR'}");
287:
288: &Apache::loncommon::content_type($r,'text/html');
289:
1.34 raeburn 290: #expire the cookies
291: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
292: foreach my $name (keys(%cookies)) {
293: next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
294: my $c = new CGI::Cookie(-name => $name,
295: -value => '',
296: -expires => '-10y',);
297: $r->headers_out->add('Set-cookie' => $c);
1.28 raeburn 298: }
299: my (%info,%user_info,%lti_info);
300: if (ref($lti_env) eq 'HASH') {
301: %lti_info = %{$lti_env};
302: }
303: my $lonhost = $r->dir_config('lonHostID');
304: if (ref($data) eq 'HASH') {
305: %user_info=('ip' => $ENV{'REMOTE_ADDR'},
306: 'domain' => $data->{'domain'},
307: 'username' => $data->{'username'},
1.36 raeburn 308: 'home' => $data->{'home'},
1.28 raeburn 309: 'role' => $data->{'role'},
310: 'origurl' => $data->{'origurl'},
311: 'symb' => $data->{'symb'},
312: 'server' => $lonhost);
313: }
314: %info = (%user_info,%lti_info);
315: my $token = &Apache::lonnet::tmpput(\%info,$lonhost);
316: my $url = '/adm/migrateuser?token='.$token;
317: $r->send_http_header;
318: $r->print(
319: &Apache::loncommon::start_page('Updating Session ...',undef,
320: {'redirect' => [0.1,$url],
321: 'only_body' => 1,}).
322: &Apache::loncommon::end_page());
323: $r->register_cleanup(\&flush_course_logs);
324: return;
325: }
326:
1.36 raeburn 327: sub conlost_userhome {
328: my ($r,$idsref,$dataref) = @_;
329: return unless ((ref($idsref) eq 'ARRAY') && (ref($dataref) eq 'HASH'));
330: my @ids = @{$idsref};
331: my %data = %{$dataref};
1.37 ! raeburn 332: my ($lonhost,$lowest_load,$otherserver,$is_balancer,%conlost,%posstargets);
! 333: $lonhost = $r->dir_config('lonHostID');
! 334: if (grep(/^\Q$lonhost\E$/,split(/,/,$data{'dom_balancers'}))) {
! 335: $is_balancer = 1;
! 336: }
! 337: $lowest_load = 30000;
1.36 raeburn 338: if ($data{'conlost'} ne '') {
339: map { $conlost{$_} = 1; } split(/,/,$data{'conlost'});
340: }
1.37 ! raeburn 341: if ($data{'loncfail'}) {
! 342: if ($is_balancer) {
! 343: if ($lonhost ne $data{'from_balancer'}) {
! 344: my ($is_balancer,$posshost,$setcookie,$offloadto,$dom_balancers) =
! 345: &Apache::lonnet::check_loadbalancing($data{'username'},$data{'domain'});
! 346: if ($is_balancer) {
! 347: if (ref($offloadto) eq 'HASH') {
! 348: $data{'offloadto'} = '';
! 349: foreach my $key (keys(%{$offloadto})) {
! 350: if (ref($offloadto->{$key}) eq 'ARRAY') {
! 351: $data{'offloadto'} .= $key.'='.join(',',@{$offloadto->{$key}}).'&';
! 352: }
! 353: }
! 354: $data{'offloadto'} =~ s/\&$//;
! 355: } elsif (ref($offloadto) eq 'ARRAY') {
! 356: $data{'offloadto'} = join(',',@{$offloadto});
! 357: }
! 358: }
! 359: }
! 360: }
! 361: } else {
! 362: $conlost{$lonhost} = 1;
! 363: $data{'conlost'} = join(',',sort(keys(%conlost)));
! 364: }
1.36 raeburn 365: if ($data{'offloadto'} =~ /\&/) {
1.37 ! raeburn 366: foreach my $item (split(/\&/,$data{'offloadto'})) {
1.36 raeburn 367: my ($type,$targets) = split(/\=/,$item);
368: @{$posstargets{$type}} = split(/,/,$targets);
369: }
370: } elsif ($data{'offloadto'} =~ /=/) {
371: my ($type,$targets) = split(/\=/,$data{'offloadto'});
372: @{$posstargets{$type}} = split(/,/,$targets);
373: } else {
374: @{$posstargets{'default'}} = split(/,/,$data{'offloadto'});
375: }
376: if (ref($posstargets{'primary'}) eq 'ARRAY') {
377: foreach my $try_server (@{$posstargets{'primary'}}) {
378: next if ($conlost{$try_server});
1.37 ! raeburn 379: next if ((grep(/^\Q$try_server\E$/,@ids)) && !$data{'loncfail'});
1.36 raeburn 380: ($otherserver,$lowest_load) =
381: &Apache::lonnet::compare_server_load($try_server,
382: $otherserver,
383: $lowest_load);
384: }
385: }
386: my $found_server = ($otherserver ne '' && $lowest_load < 100);
387: if (!$found_server) {
388: if (ref($posstargets{'default'}) eq 'ARRAY') {
389: foreach my $try_server (@{$posstargets{'default'}}) {
1.37 ! raeburn 390: next if ($conlost{$try_server});
! 391: next if ((grep(/^\Q$try_server\E$/,@ids)) && !$data{'loncfail'});
1.36 raeburn 392: ($otherserver,$lowest_load) =
1.37 ! raeburn 393: &Apache::lonnet::compare_server_load($try_server,
! 394: $otherserver,
! 395: $lowest_load);
1.36 raeburn 396: }
397: }
398: }
399: if ($otherserver ne '') {
400: my $switchto = &Apache::lonnet::hostname($otherserver);
401: if ($switchto ne '') {
402: $data{'server'} = $lonhost;
1.37 ! raeburn 403: if ($data{'loncfail'}) {
! 404: delete($data{'loncfail'});
! 405: }
1.36 raeburn 406: if (grep(/^\Q$otherserver\E$/,split(/,/,$data{'dom_balancers'}))) {
1.37 ! raeburn 407: $data{'noloadbalance'} = $otherserver;
1.36 raeburn 408: }
1.37 ! raeburn 409: &do_server_switch($r,$otherserver,$switchto,$is_balancer,\%data);
1.36 raeburn 410: return $otherserver;
1.37 ! raeburn 411: } else {
! 412: #FIXME Contents of $data{offloadto} contains invalid hostID.
! 413: }
! 414: } else {
! 415: my (%poss_balancers,%tried_balancers);
! 416: map { $poss_balancers{$_} = 1; } split(/,/,$data{'dom_balancers'});
! 417: map { $tried_balancers{$_} = 1; } split(/,/,$data{'tried_balancers'});
! 418: if (keys(%poss_balancers)) {
! 419: foreach my $try_server (sort(keys(%poss_balancers))) {
! 420: next if (grep(/^\Q$try_server\E$/,@ids));
! 421: next if (($data{'loncfail'}) && ($tried_balancers{$try_server}));
! 422: my $alreadytried;
! 423: foreach my $key (keys(%posstargets)) {
! 424: if ((ref($posstargets{$key}) eq 'ARRAY') &&
! 425: (grep(/^\Q$try_server\E$/,@{$posstargets{$key}}))) {
! 426: $alreadytried = 1;
! 427: last;
! 428: }
! 429: }
! 430: unless ($alreadytried) {
! 431: if (&Apache::lonnet::reply('ping',$try_server) eq $try_server) {
! 432: $otherserver = $try_server;
! 433: last;
! 434: }
! 435: }
! 436: }
! 437: }
! 438: if ($otherserver ne '') {
! 439: my $switchto = &Apache::lonnet::hostname($otherserver);
! 440: if ($switchto ne '') {
! 441: $data{'server'} = $lonhost;
! 442: $data{'loncfail'} = $lonhost;
! 443: $tried_balancers{$otherserver} = 1;
! 444: $data{'tried_balancers'} = join(',',sort(keys(%tried_balancers)));
! 445: &do_server_switch($r,$otherserver,$switchto,$is_balancer,\%data);
! 446: return $otherserver;
! 447: } else {
! 448: #FIXME Contents of $data{'dom_balancers'} contains invalid hostID.
! 449: }
! 450: } else {
! 451: if ($data{'loncfail'}) {
! 452: #FIXME Nowhere to go.
! 453: } else {
! 454: # FIXME Send back to a balancer (no token); different balancer if this is a balancer.
! 455: # Send command to balancer to exclude this host using http instead on lonc/lond
! 456: }
1.36 raeburn 457: }
458: }
1.37 ! raeburn 459: return;
! 460: }
! 461:
! 462: sub do_server_switch {
! 463: my ($r,$otherserver,$switchto,$is_balancer,$dataref) = @_;
! 464: if (ref($dataref) eq 'HASH') {
! 465: my $domain = $dataref->{'domain'};
! 466: my $username = $dataref->{'username'};
! 467: # If this is a balancer set a balancer cookie unless browser already sent
! 468: # LON-CAPA load balancer cookie which points at the target server.
! 469: if ($is_balancer) {
! 470: my $newcookieid;
! 471: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
! 472: unless (($found_server eq $otherserver) &&
! 473: ($balancer_cookie =~ /^\Q$domain\E_\Q$username\E_/)) {
! 474: $newcookieid =
! 475: &Apache::switchserver::balancer_cookieid($r,$otherserver,$username,$domain);
! 476: if ($newcookieid) {
! 477: $dataref->{'balcookie'} = $newcookieid;
! 478: }
! 479: }
! 480: }
! 481: my $token = &Apache::lonnet::tmpput($dataref,$otherserver);
! 482: my $protocol = $Apache::lonnet::protocol{$otherserver};
! 483: $protocol = 'http' if ($protocol ne 'https');
! 484: my $url = $protocol.'://'.$switchto.'/adm/login?domain='.$domain.
! 485: '&username='.$username.'&token='.$token;
! 486: $r->send_http_header;
! 487: $r->print(
! 488: &Apache::loncommon::start_page('Switching Server ...',undef,
! 489: {'redirect' => [0.1,$url]}).
! 490: &Apache::loncommon::end_page());
! 491: }
! 492: return;
1.36 raeburn 493: }
494:
495: sub log_switch {
496: my ($r,$data,$lti_env) = @_;
497: my $lonhost = $r->dir_config('lonHostID');
498: return unless ((ref($data) eq 'HASH') && (ref($lti_env) eq 'HASH'));
499: my $now = time;
500: my %temp=('switchserver' => $now.':'.$lonhost,$data->{'role'});
501: &Apache::lonnet::put('email_status',\%temp);
502: my $logmsg = "Switch Server to $lonhost";
503: if ($data->{'role'}) {
504: $logmsg .= " with role: ".$data->{'role'};
505: } elsif (($lti_env->{'reqcrs'}) && ($lti_env->{'reqrole'} eq 'cc')) {
506: $logmsg .= " to create new LTI course";
507: } elsif ($lti_env->{'selfenrollrole'}) {
508: $logmsg .= " to selfenroll with role: ".$lti_env->{'selfenrollrole'};
509: } else {
510: $logmsg .= " (no role)";
511: }
512: $logmsg .= ' '.$ENV{'REMOTE_ADDR'};
513: &Apache::lonnet::log($data->{'domain'},$data->{'username'},$data->{'home'},$logmsg);
514: }
515:
1.28 raeburn 516: sub flush_course_logs {
517: &Apache::lonnet::flushcourselogs();
518: return OK;
519: }
520:
1.1 albertel 521: sub handler {
522: my ($r) = @_;
523:
524: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
1.4 albertel 525: my %data = &Apache::lonnet::tmpget($env{'form.token'});
1.13 raeburn 526: if (keys(%data) == 0) {
527: return &goto_login($r);
528: }
1.4 albertel 529: my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
530:
1.6 albertel 531: &Apache::lonlocal::get_language_handle($r);
532:
1.4 albertel 533: if ($delete ne 'ok') {
1.27 raeburn 534: return &goto_login($r,undef,\%data);
1.4 albertel 535: }
1.2 albertel 536:
1.18 raeburn 537: if (!defined($data{'username'}) || !defined($data{'domain'})) {
1.27 raeburn 538: return &goto_login($r,undef,\%data);
1.18 raeburn 539: }
1.37 ! raeburn 540: my ($home,@ids);
! 541: @ids=&Apache::lonnet::current_machine_ids();
1.18 raeburn 542: if ($data{'ip'} ne $ENV{'REMOTE_ADDR'}) {
1.24 raeburn 543: &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
544: $data{'ip'}.'; now: '.$ENV{'REMOTE_ADDR'}.' for '.$data{'username'}.':'.$data{'domain'});
1.37 ! raeburn 545: return &ip_changed($r,$data{'domain'},$data{'server'},\@ids,\%data);
1.2 albertel 546: }
1.37 ! raeburn 547: if ($data{'loncfail'}) {
! 548: &Apache::lonnet::logthis('Returned from '.$data{'server'}.' -- no connection to library node or other access node(s)');
! 549: my $otherserver = &conlost_userhome($r,\@ids,\%data);
! 550: if ($otherserver ne '') {
! 551: &Apache::lonnet::logthis("Switching to $otherserver");
! 552: }
! 553: return OK;
! 554: } else {
! 555: $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
! 556: if ($home eq 'no_host') {
! 557: if (($data{'home'} ne '') && (&Apache::lonnet::hostname($data{'home'}))) {
! 558: &Apache::lonnet::reconlonc($data{'home'});
! 559: $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
! 560: if ($home eq 'no_host') {
! 561: unless (grep(/^\Q$data{'home'}\E$/,@ids)) {
! 562: if (&Apache::lonnet::reply('ping',$data{'home'}) eq 'con_lost') {
! 563: my $otherserver = &conlost_userhome($r,\@ids,\%data);
! 564: if ($otherserver ne '') {
! 565: &Apache::lonnet::logthis("No connection to home server ($data{'home'}) for $data{'username'}:$data{'domain'}. Switching to $otherserver");
! 566: }
1.36 raeburn 567: return OK;
568: }
569: }
570: }
571: }
572: }
573: }
574:
1.24 raeburn 575: my $udom;
576: if (&Apache::lonnet::domain($data{'domain'})) {
577: $udom=$data{'domain'};
578: }
1.36 raeburn 579: if ($home eq 'no_host') { return &goto_login($r,$udom,\%data); }
580: if (&Apache::lonnet::hostname($home) eq '') { return &goto_login($r,$udom,\%data); }
581:
582: my $rolemsg;
583: if ($data{'role'}) {
584: $rolemsg = "role: $data{'role'}";
585: } else {
586: $rolemsg = '(no role)';
587: }
588:
589: &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} $rolemsg");
1.27 raeburn 590:
591: my $sso_env = &sso_check(\%data);
592: my $lti_env = <i_check(\%data);
1.2 albertel 593:
1.27 raeburn 594: my $extra_env;
595: if ((ref($sso_env) eq 'HASH') && (keys(%{$sso_env}))) {
596: $extra_env = $sso_env;
597: } elsif ((ref($lti_env) eq 'HASH') && (keys(%{$lti_env}))) {
598: $extra_env = $lti_env;
599: }
1.8 albertel 600:
1.37 ! raeburn 601: if (($data{'conlost'}) && ($data{'server'})) {
! 602: my @conlosts = split(/,/,$data{'conlost'});
! 603: my $switchfrom = $data{'server'};
! 604: if (@conlosts) {
! 605: if (grep(/^\Q$switchfrom\E$/,@conlosts)) {
! 606: &log_switch($r,\%data,$extra_env);
! 607: }
! 608: }
! 609: }
! 610:
1.16 raeburn 611: my %form;
612: if ($data{'symb'} ne '') {
613: $form{'symb'} = $data{'symb'};
614: }
1.21 raeburn 615: if ($data{'iptoken'} ne '') {
616: $form{'iptoken'} = $data{'iptoken'};
617: }
1.25 raeburn 618: if ($data{'noloadbalance'} ne '') {
619: $form{'noloadbalance'} = $data{'noloadbalance'};
620: }
1.16 raeburn 621:
1.3 albertel 622: if (!$data{'role'}) {
1.12 albertel 623: my $handle = &Apache::lonnet::check_for_valid_session($r);
624: if ($handle) {
1.11 albertel 625: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
626: $handle);
1.27 raeburn 627: if ($data{'lti.login'}) {
1.28 raeburn 628: my $needslogout;
629: if ($env{'request.lti.login'}) {
630: if (($env{'user.name'} ne $data{'username'}) ||
631: ($env{'user.domain'} ne $data{'domain'})) {
632: $needslogout = 1;
633: }
634: } else {
635: $needslogout = 1;
636: }
637: # If access is via LTI, and user already has a non-LTI session cookie
638: # (and session) or has an LTI session cookie for a different username,
639: # logout the existing session, and start a new one
640: if ($needslogout) {
641: &logout($r,$handle,\%data,$lti_env);
642: } elsif (($data{'lti.reqcrs'}) && ($data{'lti.reqrole'} eq 'cc')) {
1.27 raeburn 643: $form{'lti.reqcrs'} = $data{'lti.reqcrs'};
644: $form{'lti.reqrole'} = $data{'lti.reqrole'};
645: $form{'lti.sourcecrs'} = $data{'lti.sourcecrs'};
1.29 raeburn 646: $form{'lti.uri'} = $data{'lti.uri'};
1.28 raeburn 647: if ($data{'lti.passbackid'}) {
648: $form{'lti.passbackid'} = $data{'lti.passbackid'};
649: }
650: if ($data{'lti.passbackurl'}) {
651: $form{'lti.passbackurl'} = $data{'lti.passbackurl'};
652: }
653: if ($data{'lti.rosterid'}) {
654: $form{'lti.rosterid'} = $data{'lti.rosterid'};
655: }
656: if ($data{'lti.rosterurl'}) {
657: $form{'lti.rosterurl'} = $data{'lti.rosterurl'};
658: }
1.29 raeburn 659: if ($data{'lti.target'}) {
660: $form{'lti.target'} = $data{'lti.target'};
661: }
1.27 raeburn 662: &Apache::loncommon::content_type($r,'text/html');
663: $r->send_http_header;
664: &Apache::ltiauth::lti_reqcrs($r,$data{'domain'},\%form,$data{'username'},$data{'domain'});
1.28 raeburn 665: } else {
666: if (ref($lti_env) eq 'HASH') {
667: delete($lti_env->{'reqcrs'});
668: delete($lti_env->{'reqrole'});
669: delete($lti_env->{'selfenrollrole'});
670: }
671: if ($data{'lti.selfenrollrole'}) {
1.32 raeburn 672: if (&Apache::ltiauth::lti_enroll($data{'username'},$data{'domain'},
1.28 raeburn 673: $data{'lti.selfenrollrole'}) eq 'ok') {
674: my $url = '/adm/roles?selectrole=1&'.
675: &escape($data{'lti.selfenrollrole'}).'=1';
676: if ($data{'origurl'} =~ m{/default_\d+\.sequence$}) {
677: $url .= '&orgurl='.$data{'origurl'}.'&navmap=1';
678: } elsif ($data{'origurl'} ne '') {
679: $url .= '&orgurl='.$data{'origurl'};
680: }
681: if (ref($lti_env) eq 'HASH') {
682: &Apache::lonnet::appenv($lti_env);
683: }
684: $r->internal_redirect($url);
685: } else {
686: &Apache::ltiauth::invalid_request($r,23);
687: }
688: } elsif ($data{'origurl'} ne '') {
689: my $url = $data{'origurl'};
690: if ($url =~ m{/default_\d+\.sequence$}) {
691: $url .= (($url =~/\?/)?'&':'?').'navmap=1';
692: }
693: if (ref($lti_env) eq 'HASH') {
694: &Apache::lonnet::appenv($lti_env);
1.27 raeburn 695: }
696: $r->internal_redirect($url);
697: } else {
1.28 raeburn 698: if (ref($lti_env) eq 'HASH') {
699: &Apache::lonnet::appenv($lti_env);
700: }
1.27 raeburn 701: }
702: }
703: } elsif ($data{'origurl'} ne '') {
1.14 raeburn 704: $r->internal_redirect($data{'origurl'});
705: } elsif ($env{'request.course.id'}) {
706: $r->internal_redirect('/adm/navmaps');
1.11 albertel 707: } else {
708: $r->internal_redirect('/adm/roles');
709: }
710: } else {
1.14 raeburn 711: my $desturl = '/adm/roles';
712: if ($data{'origurl'} ne '') {
713: $desturl = $data{'origurl'};
1.27 raeburn 714: if ($data{'lti.login'}) {
715: $desturl = $data{'origurl'};
716: if ($desturl =~ m{/default_\d+\.sequence$}) {
717: $desturl .= (($desturl =~/\?/)?'&':'?').'navmap=1';
718: }
719: }
720: }
721: my $skipcritical;
722: if (($data{'lti.login'}) && ($data{'lti.reqcrs'}) &&
723: ($data{'lti.reqrole'} eq 'cc')) {
724: $skipcritical = 1;
1.14 raeburn 725: }
1.11 albertel 726: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
1.27 raeburn 727: $home,$desturl,$extra_env,\%form,$skipcritical);
1.11 albertel 728: }
1.1 albertel 729: return OK;
730: }
1.6 albertel 731:
1.33 raeburn 732: my $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
1.15 raeburn 733: if ($data{'origurl'} ne '') {
1.33 raeburn 734: $next_url .= '&orgurl='.&escape($data{'origurl'});
1.30 raeburn 735: if ($data{'lti.login'}) {
1.31 raeburn 736: if (($data{'origurl'} =~ m{/default_\d+\.sequence$}) ||
737: ($data{'origurl'} =~ m{^/res/.+\.sequence$})) {
1.33 raeburn 738: $next_url .= '&navmap=1';
1.30 raeburn 739: }
740: }
1.15 raeburn 741: }
1.6 albertel 742: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
1.16 raeburn 743: $next_url,$extra_env,\%form);
1.6 albertel 744: return OK;
1.1 albertel 745: }
746:
747: 1;
748: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>