Annotation of loncom/auth/migrateuser.pm, revision 1.32
1.1 albertel 1: # The LearningOnline Network
2: # Starts a user off based of an existing token.
3: #
1.32 ! raeburn 4: # $Id: migrateuser.pm,v 1.31 2018/04/17 19:48:18 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.6 albertel 36: use Apache::lonlocal;
1.18 raeburn 37: use Apache::lonlogin();
1.27 raeburn 38: use Apache::ltiauth;
1.28 raeburn 39: use CGI::Cookie;
1.1 albertel 40:
41: sub goto_login {
1.27 raeburn 42: my ($r,$domain,$data) = @_;
43: if ((ref($data) eq 'HASH') && ($data->{'lti.login'})) {
44: &Apache::ltiauth::invalid_request($r,'22');
45: } else {
46: &Apache::loncommon::content_type($r,'text/html');
47: $r->send_http_header;
48: my $url = '/adm/login';
49: if ($domain) {
50: $url .= '?domain='.$domain;
51: }
52: $r->print(&Apache::loncommon::start_page('Going to login',undef,
53: {'redirect' => [0,$url],}).
54: '<h1>'.&mt('One moment please...').'</h1>'.
55: '<p>'.&mt('Transferring to login page.').'</p>'.
56: &Apache::loncommon::end_page());
57: }
1.2 albertel 58: return OK;
1.1 albertel 59: }
60:
1.27 raeburn 61: sub sso_check {
1.7 albertel 62: my ($data) = @_;
1.8 albertel 63: my %extra_env;
1.18 raeburn 64: if (ref($data) eq 'HASH') {
65: if ($data->{'sso.login'}) {
66: $extra_env{'request.sso.login'} = $data->{'sso.login'};
67: }
68: if ($data->{'sso.reloginserver'}) {
69: $extra_env{'request.sso.reloginserver'} =
70: $data->{'sso.reloginserver'};
71: }
1.27 raeburn 72: }
73: return \%extra_env;
74: }
75:
76: sub lti_check {
77: my ($data) = @_;
78: my %lti_env;
79: if (ref($data) eq 'HASH') {
1.26 raeburn 80: if ($data->{'lti.login'}) {
1.27 raeburn 81: $lti_env{'request.lti.login'} = $data->{'lti.login'};
82: if ($data->{'lti.reqcrs'}) {
83: $lti_env{'request.lti.reqcrs'} = $data->{'lti.reqcrs'};
84: }
85: if ($data->{'lti.reqrole'}) {
86: $lti_env{'request.lti.reqrole'} = $data->{'lti.reqrole'};
87: }
88: if ($data->{'lti.selfenrollrole'}) {
89: $lti_env{'request.lti.selfenrollrole'} = $data->{'lti.selfenrollrole'};
90: }
1.29 raeburn 91: if ($data->{'lti.uri'}) {
92: $lti_env{'request.lti.uri'} = $data->{'lti.uri'};
93: }
94: if ($data->{'lti.target'}) {
95: $lti_env{'request.lti.target'} = $data->{'lti.target'};
96: }
1.32 ! raeburn 97: if ($data->{'lti.sourcecrs'}) {
! 98: $lti_env{'request.lti.sourcecrs'} = $data->{'lti.sourcecrs'};
! 99: }
1.26 raeburn 100: }
101: if ($data->{'lti.passbackid'}) {
1.27 raeburn 102: $lti_env{'request.lti.passbackid'} = $data->{'lti.passbackid'};
1.26 raeburn 103: }
104: if ($data->{'lti.passbackurl'}) {
1.27 raeburn 105: $lti_env{'request.lti.passbackurl'} = $data->{'lti.passbackurl'};
1.26 raeburn 106: }
107: if ($data->{'lti.rosterid'}) {
1.27 raeburn 108: $lti_env{'request.lti.rosterid'} = $data->{'lti.rosterid'};
1.26 raeburn 109: }
110: if ($data->{'lti.rosterurl'}) {
1.27 raeburn 111: $lti_env{'request.lti.rosterurl'} = $data->{'lti.rosterurl'};
1.26 raeburn 112: }
1.7 albertel 113: }
1.27 raeburn 114: return \%lti_env;
1.18 raeburn 115: }
116:
117: sub ip_changed {
118: my ($r,$udom,$camefrom,$dataref) = @_;
119: &Apache::loncommon::content_type($r,'text/html');
120: $r->send_http_header;
121: if (ref($dataref) eq 'HASH') {
122: my $title = 'LON-CAPA Session redirected';
123: my $message = &mt('Your internet address has changed since you logged in.');
124: my $rule_in_effect;
1.22 raeburn 125: if ($dataref->{'balancer'}) {
126: my $baldom = &Apache::lonnet::host_domain($camefrom);
127: my $balprimaryid = &Apache::lonnet::domain($baldom,'primary');
128: my $balintdom = &Apache::lonnet::internet_dom($balprimaryid);
129: my $uprimaryid = &Apache::lonnet::domain($udom,'primary');
130: my $uintdom = &Apache::lonnet::internet_dom($uprimaryid);
131: my $dom_in_use;
132: if (($uintdom ne '') && ($uintdom eq $balintdom)) {
133: $dom_in_use = $udom;
134: } else {
135: $dom_in_use = $baldom;
1.21 raeburn 136: }
1.22 raeburn 137: my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
1.18 raeburn 138: unless (defined($cached)) {
139: my $cachetime = 60*60*24;
140: my %domconfig =
1.22 raeburn 141: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1.18 raeburn 142: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.22 raeburn 143: $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
1.18 raeburn 144: $domconfig{'loadbalancing'},$cachetime);
145: }
146: }
147: if (ref($result) eq 'HASH') {
148: (undef,my $currtargets,my $currrules) =
149: &Apache::lonnet::check_balancer_result($result,$dataref->{'server'});
150: if (ref($currrules) eq 'HASH') {
151: if ($dataref->{'sso.login'}) {
152: if ($currrules->{'_LC_ipchangesso'} ne '') {
153: $rule_in_effect = $currrules->{'_LC_ipchangesso'};
154: }
155: } else {
156: if ($currrules->{'_LC_ipchange'} ne '') {
157: $rule_in_effect = $currrules->{'_LC_ipchange'};
158: }
159: }
160: }
161: }
162: }
163: my $url;
164: my $lonhost= $r->dir_config('lonHostID');
165: my $switchto = $lonhost;
1.23 raeburn 166: if ($rule_in_effect ne 'offloadedto') {
1.18 raeburn 167: my $hosthere;
1.23 raeburn 168: my @ids=&Apache::lonnet::current_machine_ids();
169: unless ($rule_in_effect eq 'balancer') {
170: if (grep(/^\Q$rule_in_effect\E$/,@ids)) {
171: $hosthere = 1;
172: }
173: }
174: unless ($hosthere) {
175: if ($dataref->{'role'}) {
176: my ($adom,$aname);
177: if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) {
178: $adom = $1;
179: $aname = $dataref->{'username'};
180: } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
181: $adom = $1;
182: $aname = $2;
183: }
184: if ($adom ne '' && $aname ne '') {
185: my $ahome = &Apache::lonnet::homeserver($aname,$adom);
186: unless ($ahome eq 'no_host') {
187: if ($ahome && grep(/^\Q$ahome\E$/,@ids)) {
188: $hosthere = 1;
189: }
1.18 raeburn 190: }
191: }
192: }
193: }
1.23 raeburn 194: unless ($hosthere) {
195: my $hostname;
196: if ($rule_in_effect eq 'balancer') {
197: $hostname = &Apache::lonnet::hostname($dataref->{'server'});
198: if ($hostname) {
199: $switchto = $dataref->{'server'};
200: }
201: } else {
202: $hostname = &Apache::lonnet::hostname($rule_in_effect);
203: if ($hostname) {
204: $switchto = $rule_in_effect;
205: }
206: }
1.18 raeburn 207: if ($hostname) {
208: my $protocol = $Apache::lonnet::protocol{$switchto};
209: $protocol = 'http' if ($protocol ne 'https');
210: $url = $protocol.'://'.$hostname;
1.23 raeburn 211: if ($rule_in_effect eq 'balancer') {
212: $message .= '<br />'.
213: &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.');
214: } else {
215: $message .= '<br />'.
216: &mt('As a result, your LON-CAPA session is being redirected.');
217: }
1.18 raeburn 218: }
219: }
220: }
221: if ($dataref->{'sso.login'}) {
1.21 raeburn 222: $url .= '/adm/roles';
1.18 raeburn 223: } else {
1.21 raeburn 224: $url .= '/adm/login';
1.24 raeburn 225: if ($udom) {
226: $url .= '?domain='.$udom;
227: }
1.20 raeburn 228: $message .= '<br />'.&mt('You will need to provide your password one more time.');
1.18 raeburn 229: }
230: my %info= (
1.24 raeburn 231: 'domain' => $udom,
1.18 raeburn 232: 'username' => $dataref->{'username'},
233: 'role' => $dataref->{'role'},
234: 'sessionserver' => $lonhost,
235: );
236: if ($dataref->{'origurl'}) {
237: $info{'origurl'} = $dataref->{'origurl'};
238: }
239: if ($dataref->{'symb'}) {
240: $info{'symb'} = $dataref->{'symb'};
241: }
242: my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto);
243: unless ($iptoken eq 'conlost') {
1.24 raeburn 244: $url .= ($url =~ /\?/) ? '&' : '?';
245: $url .= 'iptoken='.$iptoken;
1.18 raeburn 246: }
247: $r->print(&Apache::loncommon::start_page($title,undef,
248: {'redirect' =>
249: [2,$url],}).
250: '<h1>'.&mt('One moment please...').'</h1>'.
251: '<p class="LC_warning">'.$message.'</p>'.
252: &Apache::loncommon::end_page());
253: } else {
254: return &goto_login($r);
1.10 raeburn 255: }
1.18 raeburn 256: return OK;
1.7 albertel 257: }
258:
1.28 raeburn 259: sub logout {
260: my ($r,$handle,$data,$lti_env) = @_;
261: unlink($handle);
262: if ($env{'user.linkedenv'} ne '') {
263: my $lonidsdir=$r->dir_config('lonIDsDir');
264: if ((-l $env{'user.linkedenv'}) &&
265: (readlink($env{'user.linkedenv'}) eq "$lonidsdir/$handle.id")) {
266: unlink($env{'user.linkedenv'});
267: }
268: }
269: my %temp=('logout' => time);
270: &Apache::lonnet::put('email_status',\%temp);
271: &Apache::lonnet::log($env{'user.domain'},
272: $env{'user.name'},
273: $env{'user.home'},
274: "Logout $ENV{'REMOTE_ADDR'}");
275:
276: &Apache::loncommon::content_type($r,'text/html');
277:
278: #expire the cookie
279: my $c = new CGI::Cookie(-name => 'lonID',
280: -value => '',
281: -expires => '-10y',);
282: $r->headers_out->add('Set-cookie' => $c);
283: if ($env{'user.linkedenv'}) {
284: my $linked = new CGI::Cookie(-name => 'lonLinkID',
285: -value => '',
286: -expires => '-10y',);
287: $r->headers_out->add('Set-cookie' => $linked);
288: }
289: my $lonhost = $r->dir_config('lonHostID');
290: my (%info,%user_info,%lti_info);
291: if (ref($lti_env) eq 'HASH') {
292: %lti_info = %{$lti_env};
293: }
294: my $lonhost = $r->dir_config('lonHostID');
295: if (ref($data) eq 'HASH') {
296: %user_info=('ip' => $ENV{'REMOTE_ADDR'},
297: 'domain' => $data->{'domain'},
298: 'username' => $data->{'username'},
299: 'role' => $data->{'role'},
300: 'origurl' => $data->{'origurl'},
301: 'symb' => $data->{'symb'},
302: 'server' => $lonhost);
303: }
304: %info = (%user_info,%lti_info);
305: my $token = &Apache::lonnet::tmpput(\%info,$lonhost);
306: my $url = '/adm/migrateuser?token='.$token;
307: $r->send_http_header;
308: $r->print(
309: &Apache::loncommon::start_page('Updating Session ...',undef,
310: {'redirect' => [0.1,$url],
311: 'only_body' => 1,}).
312: &Apache::loncommon::end_page());
313: $r->register_cleanup(\&flush_course_logs);
314: return;
315: }
316:
317: sub flush_course_logs {
318: &Apache::lonnet::flushcourselogs();
319: return OK;
320: }
321:
1.1 albertel 322: sub handler {
323: my ($r) = @_;
324:
325: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
1.4 albertel 326: my %data = &Apache::lonnet::tmpget($env{'form.token'});
1.13 raeburn 327: if (keys(%data) == 0) {
328: return &goto_login($r);
329: }
1.4 albertel 330: my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
331:
1.6 albertel 332: &Apache::lonlocal::get_language_handle($r);
333:
1.4 albertel 334: if ($delete ne 'ok') {
1.27 raeburn 335: return &goto_login($r,undef,\%data);
1.4 albertel 336: }
1.2 albertel 337:
1.18 raeburn 338: if (!defined($data{'username'}) || !defined($data{'domain'})) {
1.27 raeburn 339: return &goto_login($r,undef,\%data);
1.18 raeburn 340: }
341: if ($data{'ip'} ne $ENV{'REMOTE_ADDR'}) {
1.24 raeburn 342: &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
343: $data{'ip'}.'; now: '.$ENV{'REMOTE_ADDR'}.' for '.$data{'username'}.':'.$data{'domain'});
1.18 raeburn 344: return &ip_changed($r,$data{'domain'},$data{'server'},\%data);
1.2 albertel 345: }
346:
1.17 raeburn 347: &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} to $data{'role'}");
1.2 albertel 348: my $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
1.24 raeburn 349: my $udom;
350: if (&Apache::lonnet::domain($data{'domain'})) {
351: $udom=$data{'domain'};
352: }
1.27 raeburn 353: if ($home =~ /(con_lost|no_such_host)/) { return &goto_login($r,$udom,\%data); }
354:
355: my $sso_env = &sso_check(\%data);
356: my $lti_env = <i_check(\%data);
1.2 albertel 357:
1.27 raeburn 358: my $extra_env;
359: if ((ref($sso_env) eq 'HASH') && (keys(%{$sso_env}))) {
360: $extra_env = $sso_env;
361: } elsif ((ref($lti_env) eq 'HASH') && (keys(%{$lti_env}))) {
362: $extra_env = $lti_env;
363: }
1.8 albertel 364:
1.16 raeburn 365: my %form;
366: if ($data{'symb'} ne '') {
367: $form{'symb'} = $data{'symb'};
368: }
1.21 raeburn 369: if ($data{'iptoken'} ne '') {
370: $form{'iptoken'} = $data{'iptoken'};
371: }
1.25 raeburn 372: if ($data{'noloadbalance'} ne '') {
373: $form{'noloadbalance'} = $data{'noloadbalance'};
374: }
1.16 raeburn 375:
1.3 albertel 376: if (!$data{'role'}) {
1.12 albertel 377: my $handle = &Apache::lonnet::check_for_valid_session($r);
378: if ($handle) {
1.11 albertel 379: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
380: $handle);
1.27 raeburn 381: if ($data{'lti.login'}) {
1.28 raeburn 382: my $needslogout;
383: if ($env{'request.lti.login'}) {
384: if (($env{'user.name'} ne $data{'username'}) ||
385: ($env{'user.domain'} ne $data{'domain'})) {
386: $needslogout = 1;
387: }
388: } else {
389: $needslogout = 1;
390: }
391: # If access is via LTI, and user already has a non-LTI session cookie
392: # (and session) or has an LTI session cookie for a different username,
393: # logout the existing session, and start a new one
394: if ($needslogout) {
395: &logout($r,$handle,\%data,$lti_env);
396: } elsif (($data{'lti.reqcrs'}) && ($data{'lti.reqrole'} eq 'cc')) {
1.27 raeburn 397: $form{'lti.reqcrs'} = $data{'lti.reqcrs'};
398: $form{'lti.reqrole'} = $data{'lti.reqrole'};
399: $form{'lti.sourcecrs'} = $data{'lti.sourcecrs'};
1.29 raeburn 400: $form{'lti.uri'} = $data{'lti.uri'};
1.28 raeburn 401: if ($data{'lti.passbackid'}) {
402: $form{'lti.passbackid'} = $data{'lti.passbackid'};
403: }
404: if ($data{'lti.passbackurl'}) {
405: $form{'lti.passbackurl'} = $data{'lti.passbackurl'};
406: }
407: if ($data{'lti.rosterid'}) {
408: $form{'lti.rosterid'} = $data{'lti.rosterid'};
409: }
410: if ($data{'lti.rosterurl'}) {
411: $form{'lti.rosterurl'} = $data{'lti.rosterurl'};
412: }
1.29 raeburn 413: if ($data{'lti.target'}) {
414: $form{'lti.target'} = $data{'lti.target'};
415: }
1.27 raeburn 416: &Apache::loncommon::content_type($r,'text/html');
417: $r->send_http_header;
418: &Apache::ltiauth::lti_reqcrs($r,$data{'domain'},\%form,$data{'username'},$data{'domain'});
1.28 raeburn 419: } else {
420: if (ref($lti_env) eq 'HASH') {
421: delete($lti_env->{'reqcrs'});
422: delete($lti_env->{'reqrole'});
423: delete($lti_env->{'selfenrollrole'});
424: }
425: if ($data{'lti.selfenrollrole'}) {
1.32 ! raeburn 426: if (&Apache::ltiauth::lti_enroll($data{'username'},$data{'domain'},
1.28 raeburn 427: $data{'lti.selfenrollrole'}) eq 'ok') {
428: my $url = '/adm/roles?selectrole=1&'.
429: &escape($data{'lti.selfenrollrole'}).'=1';
430: if ($data{'origurl'} =~ m{/default_\d+\.sequence$}) {
431: $url .= '&orgurl='.$data{'origurl'}.'&navmap=1';
432: } elsif ($data{'origurl'} ne '') {
433: $url .= '&orgurl='.$data{'origurl'};
434: }
435: if (ref($lti_env) eq 'HASH') {
436: &Apache::lonnet::appenv($lti_env);
437: }
438: $r->internal_redirect($url);
439: } else {
440: &Apache::ltiauth::invalid_request($r,23);
441: }
442: } elsif ($data{'origurl'} ne '') {
443: my $url = $data{'origurl'};
444: if ($url =~ m{/default_\d+\.sequence$}) {
445: $url .= (($url =~/\?/)?'&':'?').'navmap=1';
446: }
447: if (ref($lti_env) eq 'HASH') {
448: &Apache::lonnet::appenv($lti_env);
1.27 raeburn 449: }
450: $r->internal_redirect($url);
451: } else {
1.28 raeburn 452: if (ref($lti_env) eq 'HASH') {
453: &Apache::lonnet::appenv($lti_env);
454: }
1.27 raeburn 455: }
456: }
457: } elsif ($data{'origurl'} ne '') {
1.14 raeburn 458: $r->internal_redirect($data{'origurl'});
459: } elsif ($env{'request.course.id'}) {
460: $r->internal_redirect('/adm/navmaps');
1.11 albertel 461: } else {
462: $r->internal_redirect('/adm/roles');
463: }
464: } else {
1.14 raeburn 465: my $desturl = '/adm/roles';
466: if ($data{'origurl'} ne '') {
467: $desturl = $data{'origurl'};
1.27 raeburn 468: if ($data{'lti.login'}) {
469: $desturl = $data{'origurl'};
470: if ($desturl =~ m{/default_\d+\.sequence$}) {
471: $desturl .= (($desturl =~/\?/)?'&':'?').'navmap=1';
472: }
473: }
474: }
475: my $skipcritical;
476: if (($data{'lti.login'}) && ($data{'lti.reqcrs'}) &&
477: ($data{'lti.reqrole'} eq 'cc')) {
478: $skipcritical = 1;
1.14 raeburn 479: }
1.11 albertel 480: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
1.27 raeburn 481: $home,$desturl,$extra_env,\%form,$skipcritical);
1.11 albertel 482: }
1.1 albertel 483: return OK;
484: }
1.6 albertel 485:
486: my $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
1.15 raeburn 487: if ($data{'origurl'} ne '') {
488: $next_url .= '&orgurl='.&escape($data{'origurl'});
1.30 raeburn 489: if ($data{'lti.login'}) {
1.31 raeburn 490: if (($data{'origurl'} =~ m{/default_\d+\.sequence$}) ||
491: ($data{'origurl'} =~ m{^/res/.+\.sequence$})) {
1.30 raeburn 492: $next_url .= '&navmap=1';
493: }
494: }
1.15 raeburn 495: }
1.6 albertel 496: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
1.16 raeburn 497: $next_url,$extra_env,\%form);
1.6 albertel 498: return OK;
1.1 albertel 499: }
500:
501: 1;
502: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>