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