Annotation of loncom/auth/migrateuser.pm, revision 1.38
1.1 albertel 1: # The LearningOnline Network
2: # Starts a user off based of an existing token.
3: #
1.38 ! raeburn 4: # $Id: migrateuser.pm,v 1.37 2018/12/02 14:41:19 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'}) {
1.38 ! raeburn 342: if ($lonhost ne $data{'from_balancer'}) {
! 343: my ($is_balancer,$posshost,$setcookie,$offloadto,$dom_balancers) =
! 344: &Apache::lonnet::check_loadbalancing($data{'username'},$data{'domain'});
! 345: if ($is_balancer) {
! 346: if (ref($offloadto) eq 'HASH') {
! 347: $data{'offloadto'} = '';
! 348: foreach my $key (keys(%{$offloadto})) {
! 349: if (ref($offloadto->{$key}) eq 'ARRAY') {
! 350: $data{'offloadto'} .= $key.'='.join(',',@{$offloadto->{$key}}).'&';
1.37 raeburn 351: }
352: }
1.38 ! raeburn 353: $data{'offloadto'} =~ s/\&$//;
! 354: } elsif (ref($offloadto) eq 'ARRAY') {
! 355: $data{'offloadto'} = join(',',@{$offloadto});
1.37 raeburn 356: }
357: }
358: }
359: } else {
360: $conlost{$lonhost} = 1;
361: $data{'conlost'} = join(',',sort(keys(%conlost)));
362: }
1.36 raeburn 363: if ($data{'offloadto'} =~ /\&/) {
1.37 raeburn 364: foreach my $item (split(/\&/,$data{'offloadto'})) {
1.36 raeburn 365: my ($type,$targets) = split(/\=/,$item);
366: @{$posstargets{$type}} = split(/,/,$targets);
367: }
368: } elsif ($data{'offloadto'} =~ /=/) {
369: my ($type,$targets) = split(/\=/,$data{'offloadto'});
370: @{$posstargets{$type}} = split(/,/,$targets);
371: } else {
372: @{$posstargets{'default'}} = split(/,/,$data{'offloadto'});
373: }
374: if (ref($posstargets{'primary'}) eq 'ARRAY') {
375: foreach my $try_server (@{$posstargets{'primary'}}) {
376: next if ($conlost{$try_server});
1.37 raeburn 377: next if ((grep(/^\Q$try_server\E$/,@ids)) && !$data{'loncfail'});
1.36 raeburn 378: ($otherserver,$lowest_load) =
379: &Apache::lonnet::compare_server_load($try_server,
380: $otherserver,
381: $lowest_load);
382: }
383: }
384: my $found_server = ($otherserver ne '' && $lowest_load < 100);
385: if (!$found_server) {
386: if (ref($posstargets{'default'}) eq 'ARRAY') {
387: foreach my $try_server (@{$posstargets{'default'}}) {
1.37 raeburn 388: next if ($conlost{$try_server});
389: next if ((grep(/^\Q$try_server\E$/,@ids)) && !$data{'loncfail'});
1.36 raeburn 390: ($otherserver,$lowest_load) =
1.37 raeburn 391: &Apache::lonnet::compare_server_load($try_server,
392: $otherserver,
393: $lowest_load);
1.36 raeburn 394: }
395: }
396: }
397: if ($otherserver ne '') {
398: my $switchto = &Apache::lonnet::hostname($otherserver);
399: if ($switchto ne '') {
400: $data{'server'} = $lonhost;
1.37 raeburn 401: if ($data{'loncfail'}) {
402: delete($data{'loncfail'});
403: }
1.36 raeburn 404: if (grep(/^\Q$otherserver\E$/,split(/,/,$data{'dom_balancers'}))) {
1.37 raeburn 405: $data{'noloadbalance'} = $otherserver;
1.36 raeburn 406: }
1.37 raeburn 407: &do_server_switch($r,$otherserver,$switchto,$is_balancer,\%data);
1.36 raeburn 408: return $otherserver;
1.37 raeburn 409: } else {
410: #FIXME Contents of $data{offloadto} contains invalid hostID.
411: }
412: } else {
413: my (%poss_balancers,%tried_balancers);
414: map { $poss_balancers{$_} = 1; } split(/,/,$data{'dom_balancers'});
415: map { $tried_balancers{$_} = 1; } split(/,/,$data{'tried_balancers'});
416: if (keys(%poss_balancers)) {
417: foreach my $try_server (sort(keys(%poss_balancers))) {
418: next if (grep(/^\Q$try_server\E$/,@ids));
419: next if (($data{'loncfail'}) && ($tried_balancers{$try_server}));
420: my $alreadytried;
421: foreach my $key (keys(%posstargets)) {
422: if ((ref($posstargets{$key}) eq 'ARRAY') &&
423: (grep(/^\Q$try_server\E$/,@{$posstargets{$key}}))) {
424: $alreadytried = 1;
425: last;
426: }
427: }
428: unless ($alreadytried) {
429: if (&Apache::lonnet::reply('ping',$try_server) eq $try_server) {
430: $otherserver = $try_server;
431: last;
432: }
433: }
434: }
435: }
436: if ($otherserver ne '') {
437: my $switchto = &Apache::lonnet::hostname($otherserver);
438: if ($switchto ne '') {
439: $data{'server'} = $lonhost;
440: $data{'loncfail'} = $lonhost;
441: $tried_balancers{$otherserver} = 1;
442: $data{'tried_balancers'} = join(',',sort(keys(%tried_balancers)));
443: &do_server_switch($r,$otherserver,$switchto,$is_balancer,\%data);
444: return $otherserver;
445: } else {
446: #FIXME Contents of $data{'dom_balancers'} contains invalid hostID.
447: }
448: } else {
449: if ($data{'loncfail'}) {
450: #FIXME Nowhere to go.
451: } else {
452: # FIXME Send back to a balancer (no token); different balancer if this is a balancer.
453: # Send command to balancer to exclude this host using http instead on lonc/lond
454: }
1.36 raeburn 455: }
456: }
1.37 raeburn 457: return;
458: }
459:
460: sub do_server_switch {
461: my ($r,$otherserver,$switchto,$is_balancer,$dataref) = @_;
462: if (ref($dataref) eq 'HASH') {
463: my $domain = $dataref->{'domain'};
464: my $username = $dataref->{'username'};
465: # If this is a balancer set a balancer cookie unless browser already sent
466: # LON-CAPA load balancer cookie which points at the target server.
467: if ($is_balancer) {
468: my $newcookieid;
469: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
470: unless (($found_server eq $otherserver) &&
471: ($balancer_cookie =~ /^\Q$domain\E_\Q$username\E_/)) {
472: $newcookieid =
473: &Apache::switchserver::balancer_cookieid($r,$otherserver,$username,$domain);
474: if ($newcookieid) {
475: $dataref->{'balcookie'} = $newcookieid;
476: }
477: }
478: }
479: my $token = &Apache::lonnet::tmpput($dataref,$otherserver);
480: my $protocol = $Apache::lonnet::protocol{$otherserver};
481: $protocol = 'http' if ($protocol ne 'https');
482: my $url = $protocol.'://'.$switchto.'/adm/login?domain='.$domain.
483: '&username='.$username.'&token='.$token;
484: $r->send_http_header;
485: $r->print(
486: &Apache::loncommon::start_page('Switching Server ...',undef,
487: {'redirect' => [0.1,$url]}).
488: &Apache::loncommon::end_page());
489: }
490: return;
1.36 raeburn 491: }
492:
493: sub log_switch {
494: my ($r,$data,$lti_env) = @_;
495: my $lonhost = $r->dir_config('lonHostID');
496: return unless ((ref($data) eq 'HASH') && (ref($lti_env) eq 'HASH'));
497: my $now = time;
498: my %temp=('switchserver' => $now.':'.$lonhost,$data->{'role'});
499: &Apache::lonnet::put('email_status',\%temp);
500: my $logmsg = "Switch Server to $lonhost";
501: if ($data->{'role'}) {
502: $logmsg .= " with role: ".$data->{'role'};
503: } elsif (($lti_env->{'reqcrs'}) && ($lti_env->{'reqrole'} eq 'cc')) {
504: $logmsg .= " to create new LTI course";
505: } elsif ($lti_env->{'selfenrollrole'}) {
506: $logmsg .= " to selfenroll with role: ".$lti_env->{'selfenrollrole'};
507: } else {
508: $logmsg .= " (no role)";
509: }
510: $logmsg .= ' '.$ENV{'REMOTE_ADDR'};
511: &Apache::lonnet::log($data->{'domain'},$data->{'username'},$data->{'home'},$logmsg);
512: }
513:
1.28 raeburn 514: sub flush_course_logs {
515: &Apache::lonnet::flushcourselogs();
516: return OK;
517: }
518:
1.1 albertel 519: sub handler {
520: my ($r) = @_;
521:
522: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
1.4 albertel 523: my %data = &Apache::lonnet::tmpget($env{'form.token'});
1.13 raeburn 524: if (keys(%data) == 0) {
525: return &goto_login($r);
526: }
1.4 albertel 527: my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
528:
1.6 albertel 529: &Apache::lonlocal::get_language_handle($r);
530:
1.4 albertel 531: if ($delete ne 'ok') {
1.27 raeburn 532: return &goto_login($r,undef,\%data);
1.4 albertel 533: }
1.2 albertel 534:
1.18 raeburn 535: if (!defined($data{'username'}) || !defined($data{'domain'})) {
1.27 raeburn 536: return &goto_login($r,undef,\%data);
1.18 raeburn 537: }
1.37 raeburn 538: my ($home,@ids);
539: @ids=&Apache::lonnet::current_machine_ids();
1.18 raeburn 540: if ($data{'ip'} ne $ENV{'REMOTE_ADDR'}) {
1.24 raeburn 541: &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
542: $data{'ip'}.'; now: '.$ENV{'REMOTE_ADDR'}.' for '.$data{'username'}.':'.$data{'domain'});
1.37 raeburn 543: return &ip_changed($r,$data{'domain'},$data{'server'},\@ids,\%data);
1.2 albertel 544: }
1.37 raeburn 545: if ($data{'loncfail'}) {
546: &Apache::lonnet::logthis('Returned from '.$data{'server'}.' -- no connection to library node or other access node(s)');
547: my $otherserver = &conlost_userhome($r,\@ids,\%data);
548: if ($otherserver ne '') {
549: &Apache::lonnet::logthis("Switching to $otherserver");
550: }
551: return OK;
552: } else {
553: $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
554: if ($home eq 'no_host') {
555: if (($data{'home'} ne '') && (&Apache::lonnet::hostname($data{'home'}))) {
556: &Apache::lonnet::reconlonc($data{'home'});
557: $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
558: if ($home eq 'no_host') {
559: unless (grep(/^\Q$data{'home'}\E$/,@ids)) {
560: if (&Apache::lonnet::reply('ping',$data{'home'}) eq 'con_lost') {
561: my $otherserver = &conlost_userhome($r,\@ids,\%data);
562: if ($otherserver ne '') {
563: &Apache::lonnet::logthis("No connection to home server ($data{'home'}) for $data{'username'}:$data{'domain'}. Switching to $otherserver");
564: }
1.36 raeburn 565: return OK;
566: }
567: }
568: }
569: }
570: }
571: }
572:
1.24 raeburn 573: my $udom;
574: if (&Apache::lonnet::domain($data{'domain'})) {
575: $udom=$data{'domain'};
576: }
1.36 raeburn 577: if ($home eq 'no_host') { return &goto_login($r,$udom,\%data); }
1.38 ! raeburn 578: if (&Apache::lonnet::hostname($home) eq '') { return &goto_login($r,$udom,\%data); }
1.36 raeburn 579:
580: my $rolemsg;
581: if ($data{'role'}) {
1.38 ! raeburn 582: $rolemsg = "role: $data{'role'}";
1.36 raeburn 583: } else {
584: $rolemsg = '(no role)';
585: }
586:
587: &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} $rolemsg");
1.27 raeburn 588:
589: my $sso_env = &sso_check(\%data);
590: my $lti_env = <i_check(\%data);
1.2 albertel 591:
1.27 raeburn 592: my $extra_env;
593: if ((ref($sso_env) eq 'HASH') && (keys(%{$sso_env}))) {
594: $extra_env = $sso_env;
595: } elsif ((ref($lti_env) eq 'HASH') && (keys(%{$lti_env}))) {
596: $extra_env = $lti_env;
597: }
1.8 albertel 598:
1.37 raeburn 599: if (($data{'conlost'}) && ($data{'server'})) {
600: my @conlosts = split(/,/,$data{'conlost'});
601: my $switchfrom = $data{'server'};
602: if (@conlosts) {
603: if (grep(/^\Q$switchfrom\E$/,@conlosts)) {
604: &log_switch($r,\%data,$extra_env);
605: }
606: }
607: }
608:
1.16 raeburn 609: my %form;
610: if ($data{'symb'} ne '') {
611: $form{'symb'} = $data{'symb'};
612: }
1.21 raeburn 613: if ($data{'iptoken'} ne '') {
614: $form{'iptoken'} = $data{'iptoken'};
615: }
1.25 raeburn 616: if ($data{'noloadbalance'} ne '') {
617: $form{'noloadbalance'} = $data{'noloadbalance'};
618: }
1.16 raeburn 619:
1.3 albertel 620: if (!$data{'role'}) {
1.12 albertel 621: my $handle = &Apache::lonnet::check_for_valid_session($r);
622: if ($handle) {
1.11 albertel 623: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
624: $handle);
1.27 raeburn 625: if ($data{'lti.login'}) {
1.28 raeburn 626: my $needslogout;
627: if ($env{'request.lti.login'}) {
628: if (($env{'user.name'} ne $data{'username'}) ||
629: ($env{'user.domain'} ne $data{'domain'})) {
630: $needslogout = 1;
631: }
632: } else {
633: $needslogout = 1;
634: }
635: # If access is via LTI, and user already has a non-LTI session cookie
636: # (and session) or has an LTI session cookie for a different username,
637: # logout the existing session, and start a new one
638: if ($needslogout) {
639: &logout($r,$handle,\%data,$lti_env);
640: } elsif (($data{'lti.reqcrs'}) && ($data{'lti.reqrole'} eq 'cc')) {
1.27 raeburn 641: $form{'lti.reqcrs'} = $data{'lti.reqcrs'};
642: $form{'lti.reqrole'} = $data{'lti.reqrole'};
643: $form{'lti.sourcecrs'} = $data{'lti.sourcecrs'};
1.29 raeburn 644: $form{'lti.uri'} = $data{'lti.uri'};
1.28 raeburn 645: if ($data{'lti.passbackid'}) {
646: $form{'lti.passbackid'} = $data{'lti.passbackid'};
647: }
648: if ($data{'lti.passbackurl'}) {
649: $form{'lti.passbackurl'} = $data{'lti.passbackurl'};
650: }
651: if ($data{'lti.rosterid'}) {
652: $form{'lti.rosterid'} = $data{'lti.rosterid'};
653: }
654: if ($data{'lti.rosterurl'}) {
655: $form{'lti.rosterurl'} = $data{'lti.rosterurl'};
656: }
1.29 raeburn 657: if ($data{'lti.target'}) {
658: $form{'lti.target'} = $data{'lti.target'};
659: }
1.27 raeburn 660: &Apache::loncommon::content_type($r,'text/html');
661: $r->send_http_header;
662: &Apache::ltiauth::lti_reqcrs($r,$data{'domain'},\%form,$data{'username'},$data{'domain'});
1.28 raeburn 663: } else {
664: if (ref($lti_env) eq 'HASH') {
665: delete($lti_env->{'reqcrs'});
666: delete($lti_env->{'reqrole'});
667: delete($lti_env->{'selfenrollrole'});
668: }
669: if ($data{'lti.selfenrollrole'}) {
1.32 raeburn 670: if (&Apache::ltiauth::lti_enroll($data{'username'},$data{'domain'},
1.28 raeburn 671: $data{'lti.selfenrollrole'}) eq 'ok') {
672: my $url = '/adm/roles?selectrole=1&'.
673: &escape($data{'lti.selfenrollrole'}).'=1';
674: if ($data{'origurl'} =~ m{/default_\d+\.sequence$}) {
675: $url .= '&orgurl='.$data{'origurl'}.'&navmap=1';
676: } elsif ($data{'origurl'} ne '') {
677: $url .= '&orgurl='.$data{'origurl'};
678: }
679: if (ref($lti_env) eq 'HASH') {
680: &Apache::lonnet::appenv($lti_env);
681: }
682: $r->internal_redirect($url);
683: } else {
684: &Apache::ltiauth::invalid_request($r,23);
685: }
686: } elsif ($data{'origurl'} ne '') {
687: my $url = $data{'origurl'};
688: if ($url =~ m{/default_\d+\.sequence$}) {
689: $url .= (($url =~/\?/)?'&':'?').'navmap=1';
690: }
691: if (ref($lti_env) eq 'HASH') {
692: &Apache::lonnet::appenv($lti_env);
1.27 raeburn 693: }
694: $r->internal_redirect($url);
695: } else {
1.28 raeburn 696: if (ref($lti_env) eq 'HASH') {
697: &Apache::lonnet::appenv($lti_env);
698: }
1.27 raeburn 699: }
700: }
701: } elsif ($data{'origurl'} ne '') {
1.14 raeburn 702: $r->internal_redirect($data{'origurl'});
703: } elsif ($env{'request.course.id'}) {
704: $r->internal_redirect('/adm/navmaps');
1.11 albertel 705: } else {
706: $r->internal_redirect('/adm/roles');
707: }
708: } else {
1.14 raeburn 709: my $desturl = '/adm/roles';
710: if ($data{'origurl'} ne '') {
711: $desturl = $data{'origurl'};
1.27 raeburn 712: if ($data{'lti.login'}) {
713: $desturl = $data{'origurl'};
714: if ($desturl =~ m{/default_\d+\.sequence$}) {
715: $desturl .= (($desturl =~/\?/)?'&':'?').'navmap=1';
716: }
717: }
718: }
719: my $skipcritical;
720: if (($data{'lti.login'}) && ($data{'lti.reqcrs'}) &&
721: ($data{'lti.reqrole'} eq 'cc')) {
722: $skipcritical = 1;
1.14 raeburn 723: }
1.11 albertel 724: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
1.27 raeburn 725: $home,$desturl,$extra_env,\%form,$skipcritical);
1.11 albertel 726: }
1.1 albertel 727: return OK;
728: }
1.6 albertel 729:
1.33 raeburn 730: my $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
1.15 raeburn 731: if ($data{'origurl'} ne '') {
1.33 raeburn 732: $next_url .= '&orgurl='.&escape($data{'origurl'});
1.30 raeburn 733: if ($data{'lti.login'}) {
1.31 raeburn 734: if (($data{'origurl'} =~ m{/default_\d+\.sequence$}) ||
735: ($data{'origurl'} =~ m{^/res/.+\.sequence$})) {
1.33 raeburn 736: $next_url .= '&navmap=1';
1.30 raeburn 737: }
738: }
1.15 raeburn 739: }
1.6 albertel 740: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
1.16 raeburn 741: $next_url,$extra_env,\%form);
1.6 albertel 742: return OK;
1.1 albertel 743: }
744:
745: 1;
746: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>