File:
[LON-CAPA] /
loncom /
auth /
migrateuser.pm
Revision
1.25.2.5.2.9:
download - view:
text,
annotated -
select for diffs
Wed Jul 5 20:42:05 2023 UTC (13 months, 2 weeks ago) by
raeburn
Branches:
version_2_11_4_msu
Diff to branchpoint 1.25.2.5:
preferred,
unified
- For 2.11.4 (modified)
Include changes in 1.26, 1.27, 1.28 (part), 1.29, 1.30, 1.31, 1.32, 1.33,
1.44, 1.47, 1.67
1: # The LearningOnline Network
2: # Starts a user off based of an existing token.
3: #
4: # $Id: migrateuser.pm,v 1.25.2.5.2.9 2023/07/05 20:42:05 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 :remotehost);
34: use Apache::lonauth;
35: use Apache::lonnet;
36: use Apache::loncommon;
37: use Apache::lonlocal;
38: use Apache::lonlogin();
39: use Apache::ltiauth;
40: use Apache::switchserver;
41: use CGI::Cookie;
42:
43: sub goto_login {
44: my ($r,$domain,$data) = @_;
45: my $write_to_opener;
46: if (ref($data) eq 'HASH') {
47: if ($data->{'lti.login'}) {
48: &Apache::ltiauth::invalid_request($r,'22');
49: } else {
50: $write_to_opener = $data->{'lcssowin'};
51: }
52: }
53: &Apache::loncommon::content_type($r,'text/html');
54: $r->send_http_header;
55: my $url = '/adm/login';
56: if ($domain) {
57: $url .= '?domain='.$domain;
58: }
59: $r->print(&Apache::loncommon::start_page('Going to login',undef,
60: {'redirect' => [0,$url,'',$write_to_opener],}).
61: '<h1>'.&mt('One moment please...').'</h1>'.
62: '<p>'.&mt('Transferring to login page.').'</p>'.
63: &Apache::loncommon::end_page());
64: return OK;
65: }
66:
67: sub sso_check {
68: my ($data) = @_;
69: my %extra_env;
70: if (ref($data) eq 'HASH') {
71: if ($data->{'sso.login'}) {
72: $extra_env{'request.sso.login'} = $data->{'sso.login'};
73: }
74: if ($data->{'sso.reloginserver'}) {
75: $extra_env{'request.sso.reloginserver'} =
76: $data->{'sso.reloginserver'};
77: }
78: }
79: return \%extra_env;
80: }
81:
82: sub lti_check {
83: my ($data) = @_;
84: my %lti_env;
85: if (ref($data) eq 'HASH') {
86: if ($data->{'lti.login'}) {
87: $lti_env{'request.lti.login'} = $data->{'lti.login'};
88: if ($data->{'lti.reqcrs'}) {
89: $lti_env{'request.lti.reqcrs'} = $data->{'lti.reqcrs'};
90: }
91: if ($data->{'lti.reqrole'}) {
92: $lti_env{'request.lti.reqrole'} = $data->{'lti.reqrole'};
93: }
94: if ($data->{'lti.selfenrollrole'}) {
95: $lti_env{'request.lti.selfenrollrole'} = $data->{'lti.selfenrollrole'};
96: }
97: if ($data->{'lti.uri'}) {
98: $lti_env{'request.lti.uri'} = $data->{'lti.uri'};
99: }
100: if ($data->{'lti.target'}) {
101: $lti_env{'request.lti.target'} = $data->{'lti.target'};
102: }
103: if ($data->{'lti.sourcecrs'}) {
104: $lti_env{'request.lti.sourcecrs'} = $data->{'lti.sourcecrs'};
105: }
106: }
107: if ($data->{'lti.passbackid'}) {
108: $lti_env{'request.lti.passbackid'} = $data->{'lti.passbackid'};
109: }
110: if ($data->{'lti.passbackurl'}) {
111: $lti_env{'request.lti.passbackurl'} = $data->{'lti.passbackurl'};
112: }
113: if ($data->{'lti.rosterid'}) {
114: $lti_env{'request.lti.rosterid'} = $data->{'lti.rosterid'};
115: }
116: if ($data->{'lti.rosterurl'}) {
117: $lti_env{'request.lti.rosterurl'} = $data->{'lti.rosterurl'};
118: }
119: }
120: return \%lti_env;
121: }
122:
123: sub ip_changed {
124: my ($r,$udom,$camefrom,$dataref) = @_;
125: &Apache::loncommon::content_type($r,'text/html');
126: $r->send_http_header;
127: if (ref($dataref) eq 'HASH') {
128: my $title = 'LON-CAPA Session redirected';
129: my $message = &mt('Your internet address has changed since you logged in.');
130: my $rule_in_effect;
131: if ($dataref->{'balancer'}) {
132: my $baldom = &Apache::lonnet::host_domain($camefrom);
133: my $balprimaryid = &Apache::lonnet::domain($baldom,'primary');
134: my $balintdom = &Apache::lonnet::internet_dom($balprimaryid);
135: my $uprimaryid = &Apache::lonnet::domain($udom,'primary');
136: my $uintdom = &Apache::lonnet::internet_dom($uprimaryid);
137: my $dom_in_use;
138: if (($uintdom ne '') && ($uintdom eq $balintdom)) {
139: $dom_in_use = $udom;
140: } else {
141: $dom_in_use = $baldom;
142: }
143: my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
144: unless (defined($cached)) {
145: my $cachetime = 60*60*24;
146: my %domconfig =
147: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
148: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
149: $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
150: $domconfig{'loadbalancing'},$cachetime);
151: }
152: }
153: if (ref($result) eq 'HASH') {
154: (undef,my $currtargets,my $currrules) =
155: &Apache::lonnet::check_balancer_result($result,$dataref->{'server'});
156: if (ref($currrules) eq 'HASH') {
157: if ($dataref->{'sso.login'}) {
158: if ($currrules->{'_LC_ipchangesso'} ne '') {
159: $rule_in_effect = $currrules->{'_LC_ipchangesso'};
160: }
161: } else {
162: if ($currrules->{'_LC_ipchange'} ne '') {
163: $rule_in_effect = $currrules->{'_LC_ipchange'};
164: }
165: }
166: }
167: }
168: }
169: my $url;
170: my $lonhost= $r->dir_config('lonHostID');
171: my $switchto = $lonhost;
172: if ($rule_in_effect ne 'offloadedto') {
173: my $hosthere;
174: my @ids=&Apache::lonnet::current_machine_ids();
175: unless ($rule_in_effect eq 'balancer') {
176: if (grep(/^\Q$rule_in_effect\E$/,@ids)) {
177: $hosthere = 1;
178: }
179: }
180: unless ($hosthere) {
181: if ($dataref->{'role'}) {
182: my ($adom,$aname);
183: if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) {
184: $adom = $1;
185: $aname = $dataref->{'username'};
186: } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
187: $adom = $1;
188: $aname = $2;
189: }
190: if ($adom ne '' && $aname ne '') {
191: my $ahome = &Apache::lonnet::homeserver($aname,$adom);
192: unless ($ahome eq 'no_host') {
193: if ($ahome && grep(/^\Q$ahome\E$/,@ids)) {
194: $hosthere = 1;
195: }
196: }
197: }
198: }
199: }
200: unless ($hosthere) {
201: my $hostname;
202: if ($rule_in_effect eq 'balancer') {
203: $hostname = &Apache::lonnet::hostname($dataref->{'server'});
204: if ($hostname) {
205: $switchto = $dataref->{'server'};
206: }
207: } else {
208: $hostname = &Apache::lonnet::hostname($rule_in_effect);
209: if ($hostname) {
210: $switchto = $rule_in_effect;
211: }
212: }
213: if ($hostname) {
214: my $protocol = $Apache::lonnet::protocol{$switchto};
215: $protocol = 'http' if ($protocol ne 'https');
216: my $alias = &Apache::lonnet::use_proxy_alias($r,$switchto);
217: $hostname = $alias if ($alias ne '');
218: $url = $protocol.'://'.$hostname;
219: if ($rule_in_effect eq 'balancer') {
220: $message .= '<br />'.
221: &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.');
222: } else {
223: $message .= '<br />'.
224: &mt('As a result, your LON-CAPA session is being redirected.');
225: }
226: }
227: }
228: unless ($hosthere) {
229: if (($dataref->{'balancer'}) && ($dataref->{'balcookie'})) {
230: &Apache::lonnet::delbalcookie($dataref->{'balcookie'},$dataref->{'balancer'});
231: }
232: }
233: }
234: if ($dataref->{'sso.login'}) {
235: $url .= '/adm/roles';
236: } else {
237: $url .= '/adm/login';
238: if ($udom) {
239: $url .= '?domain='.$udom;
240: }
241: $message .= '<br />'.&mt('You will need to provide your password one more time.');
242: }
243: my %info= (
244: 'domain' => $udom,
245: 'username' => $dataref->{'username'},
246: 'sessionserver' => $lonhost,
247: );
248: my @names = ('origurl','symb','role','linkprotuser','linkprotexit','linkprot','linkkey','linkprotpbid','linkprotpburl');
249: foreach my $name (@names) {
250: if ($dataref->{$name} ne '') {
251: $info{$name} = $dataref->{$name};
252: }
253: }
254: if ($info{'origurl'} eq '/adm/email') {
255: if (($dataref->{'display'}) && ($dataref->{'mailrecip'})) {
256: if (&unescape($dataref->{'mailrecip'}) eq $dataref->{'username'}.':'.$dataref->{'domain'}) {
257: $info{'display'} = $dataref->{'display'};
258: $info{'mailrecip'} = $dataref->{'mailrecip'};
259: }
260: }
261: }
262: my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto,'link');
263: unless ($iptoken eq 'conlost') {
264: $url .= (($url =~ /\?/) ? '&' : '?') . 'iptoken='.$iptoken;
265: }
266: $r->print(&Apache::loncommon::start_page($title,undef,
267: {'redirect' =>
268: [2,$url,'',$dataref->{'lcssowin'}]}).
269: '<h1>'.&mt('One moment please...').'</h1>'.
270: '<p class="LC_warning">'.$message.'</p>'.
271: &Apache::loncommon::end_page());
272: } else {
273: return &goto_login($r);
274: }
275: return OK;
276: }
277:
278: sub logout {
279: my ($r,$ip,$handle,$data,$lti_env,$linkprot_info) = @_;
280: my $lonidsdir=$r->dir_config('lonIDsDir');
281: if (unlink("$lonidsdir/$handle.id")) {
282: if (($env{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
283: (-l "$lonidsdir/$env{'user.linkedenv'}.id") &&
284: (readlink("$lonidsdir/$env{'user.linkedenv'}.id") eq "$lonidsdir/$handle.id")) {
285: unlink("$lonidsdir/$env{'user.linkedenv'}.id");
286: }
287: }
288: my %temp=('logout' => time);
289: &Apache::lonnet::put('email_status',\%temp);
290: &Apache::lonnet::log($env{'user.domain'},
291: $env{'user.name'},
292: $env{'user.home'},
293: "Logout $ip");
294:
295: &Apache::loncommon::content_type($r,'text/html');
296:
297: #expire the cookies
298: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
299: foreach my $name (keys(%cookies)) {
300: next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
301: my $c = new CGI::Cookie(-name => $name,
302: -value => '',
303: -expires => '-10y',);
304: $r->headers_out->add('Set-cookie' => $c);
305: }
306: my $lonhost = $r->dir_config('lonHostID');
307: my ($url,$token,$delay,$write_to_opener,%info,%user_info);
308: if (ref($data) eq 'HASH') {
309: %user_info=('ip' => $ip,
310: 'domain' => $data->{'domain'},
311: 'username' => $data->{'username'},
312: 'home' => $data->{'home'},
313: 'role' => $data->{'role'},
314: 'origurl' => $data->{'origurl'},
315: 'symb' => $data->{'symb'},
316: 'server' => $lonhost);
317: $write_to_opener = $data->{'lcssowin'};
318: }
319: $delay = 0.1;
320: if (ref($linkprot_info) eq 'HASH') {
321: if ($linkprot_info->{'linkprotuser'} ne '') {
322: $token = &Apache::lonnet::tmpput($linkprot_info,$lonhost);
323: $url = '/adm/login?ltoken='.$token;
324: } else {
325: foreach my $key (keys(%user_info)) {
326: if ($user_info{$key} eq '') {
327: delete($user_info{$key});
328: }
329: }
330: %info = (%user_info,%{$linkprot_info});
331: $token = &Apache::lonnet::tmpput(\%info,$lonhost);
332: $url = '/adm/migrateuser?token='.$token;
333: }
334: $delay = 0;
335: } else {
336: my %lti_info;
337: if (ref($lti_env) eq 'HASH') {
338: foreach my $key (sort(keys(%{$lti_env}))) {
339: if ($key =~ /^request\.(.+)$/) {
340: $lti_info{$1} = $lti_env->{$key};
341: }
342: }
343: }
344: %info = (%user_info,%lti_info);
345: $token = &Apache::lonnet::tmpput(\%info,$lonhost);
346: $url = '/adm/migrateuser?token='.$token;
347: }
348: $r->send_http_header;
349: $r->print(
350: &Apache::loncommon::start_page('Updating Session ...',undef,
351: {'redirect' => [$delay,$url,'',$write_to_opener],
352: 'only_body' => 1,}).
353: &Apache::loncommon::end_page());
354: if ($env{'request.balancercookie'}) {
355: my ($balancer,$cookie) = split(/:/,$env{'request.balancercookie'});
356: if ((&Apache::lonnet::hostname($balancer)) && ($cookie =~ /^[a-f0-9]{32}$/)) {
357: $cookie = $env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cookie;
358: &Apache::lonnet::delbalcookie($cookie,$balancer);
359: }
360: }
361: $r->register_cleanup(\&flush_course_logs);
362: return;
363: }
364:
365: sub flush_course_logs {
366: &Apache::lonnet::flushcourselogs();
367: return OK;
368: }
369:
370: sub handler {
371: my ($r) = @_;
372:
373: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
374: my %data = &Apache::lonnet::tmpget($env{'form.token'});
375: if (keys(%data) == 0) {
376: return &goto_login($r);
377: }
378: my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
379:
380: &Apache::lonlocal::get_language_handle($r);
381:
382: if ($delete ne 'ok') {
383: return &goto_login($r,'',\%data);
384: }
385:
386: if (!defined($data{'username'}) || !defined($data{'domain'})) {
387: return &goto_login($r,'',\%data);
388: }
389: my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
390: if ($data{'ip'} ne $ip) {
391: &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
392: $data{'ip'}.'; now: '.$ip.' for '.$data{'username'}.':'.$data{'domain'});
393: return &ip_changed($r,$data{'domain'},$data{'server'},\%data);
394: }
395:
396: my $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
397: my $udom;
398: if (&Apache::lonnet::domain($data{'domain'})) {
399: $udom=$data{'domain'};
400: }
401: if ($home =~ /(con_lost|no_such_host)/) { return &goto_login($r,$udom,\%data); }
402:
403: my $rolemsg;
404: if ($data{'role'}) {
405: $rolemsg = "role: $data{'role'}";
406: } else {
407: $rolemsg = '(no role)';
408: }
409:
410: &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} $rolemsg");
411:
412: my $sso_env = &sso_check(\%data);
413: my $lti_env = <i_check(\%data);
414:
415: my $extra_env;
416: if ((ref($sso_env) eq 'HASH') && (keys(%{$sso_env}))) {
417: $extra_env = $sso_env;
418: } elsif ((ref($lti_env) eq 'HASH') && (keys(%{$lti_env}))) {
419: $extra_env = $lti_env;
420: }
421:
422: if (($data{'balancer'}) && ($data{'server'}) && ($data{'balcookie'})) {
423: if (ref($extra_env) eq 'HASH') {
424: $extra_env->{'request.balancercookie'} = $data{'server'}.':'.$data{'balcookie'};
425: } else {
426: $extra_env = { 'request.balancercookie' => $data{'server'}.':'.$data{'balcookie'} };
427: }
428: } elsif (($data{'server'}) && ($data{'otherbalcookie'})) {
429: my ($balancer,$balcookie) = split(/:/,$data{'otherbalcookie'});
430: if (defined(&Apache::lonnet::hostname($balancer)) && $balcookie =~ /^[a-f0-9]{32}$/) {
431: my $baldom = &Apache::lonnet::host_domain($balancer);
432: if (&Apache::lonnet::shared_institution($baldom)) {
433: my $cookieid = join('_',$udom,$data{'username'},$balcookie);
434: &Apache::lonnet::updatebalcookie($cookieid,$balancer,$data{'server'});
435: if (ref($extra_env) eq 'HASH') {
436: $extra_env->{'request.balancercookie'} = $data{'otherbalcookie'};
437: } else {
438: $extra_env = { 'request.balancercookie' => $data{'otherbalcookie'} };
439: }
440: }
441: }
442: }
443:
444: my (%form,$cid);
445: if ($data{'symb'} ne '') {
446: $form{'symb'} = $data{'symb'};
447: }
448: if ($data{'iptoken'} ne '') {
449: $form{'iptoken'} = $data{'iptoken'};
450: }
451: if ($data{'noloadbalance'} ne '') {
452: $form{'noloadbalance'} = $data{'noloadbalance'};
453: }
454: if ($data{'role'}) {
455: if ($data{'role'} =~ m{\./($match_domain)/($match_courseid)(?:/\w+|$)}) {
456: unless (&Apache::lonnet::homeserver($2,$1) eq 'no_host') {
457: $cid = $1.'_'.$2;
458: }
459: }
460: } else {
461: my ($handle,$expirepub);
462: $handle = &Apache::lonnet::check_for_valid_session($r);
463:
464: # For "public user" - remove any existing "public" cookie so actual user is logged in.
465: if ($handle) {
466: if ($handle=~/^publicuser\_/) {
467: my $lonidsdir=$r->dir_config('lonIDsDir');
468: if ($lonidsdir ne '') {
469: unlink("$lonidsdir/$handle.id");
470: }
471: undef($handle);
472: $expirepub = 1;
473: }
474: }
475: if ($handle) {
476: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
477: $handle);
478: my $checklaunch;
479: if ($data{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) {
480: unless ($env{'request.linkprot'} eq $data{'linkprot'}) {
481: $checklaunch = 1;
482: }
483: unless ($env{'request.linkprotexit'} eq $data{'linkprotexit'}) {
484: $checklaunch = 1;
485: }
486: unless ($env{'request.linkprotpbid'} eq $data{'linkprotpbid'}) {
487: $checklaunch = 1;
488: }
489: unless ($env{'request.linkprotpburl'} eq $data{'linkprotpburl'}) {
490: $checklaunch = 1;
491: }
492: unless ($env{'request.linkkey'} eq $data{'linkkey'}) {
493: $checklaunch = 1;
494: }
495: unless ($env{'request.deeplink.login'} eq $data{'deeplink.login'}) {
496: $checklaunch = 1;
497: }
498: }
499: if ($data{'linkprot'} ne '') {
500: if (($env{'user.name'} ne $data{'username'}) ||
501: ($env{'user.domain'} ne $data{'domain'})) {
502: my %linkprot_env;
503: foreach my $item ('linkprot','linkprotexit','linkprotpbid','linkprotpburl','deeplink.login') {
504: if ($data{$item}) {
505: $linkprot_env{$item} = $data{$item};
506: }
507: }
508: &logout($r,$ip,$handle,\%data,undef,\%linkprot_env);
509: return OK;
510: }
511: &Apache::lonnet::appenv({'request.linkprot' => $data{'linkprot'}});
512: if ($data{'linkprotexit'}) {
513: &Apache::lonnet::appenv({'request.linkprotexit' => $data{'linkprotexit'}});
514: }
515: if ($env{'request.linkkey'}) {
516: &Apache::lonnet::delenv('request.linkkey');
517: }
518: my ($linkprotector,$deeplink) = split(/:/,$data{'linkprot'},2);
519: if ($env{'user.linkprotector'}) {
520: my @protectors = split(/,/,$env{'user.linkprotector'});
521: unless (grep(/^\Q$linkprotector\E$/,@protectors)) {
522: push(@protectors,$linkprotector);
523: @protectors = sort { $a <=> $b } @protectors;
524: &Apache::lonnet::appenv({'user.linkprotector' => join(',',@protectors)});
525: }
526: } else {
527: &Apache::lonnet::appenv({'user.linkprotector' => $linkprotector });
528: }
529: if ($env{'user.linkproturi'}) {
530: my @proturis = split(/,/,$env{'user.linkproturi'});
531: unless(grep(/^\Q$deeplink\E$/,@proturis)) {
532: push(@proturis,$deeplink);
533: @proturis = sort @proturis;
534: &Apache::lonnet::appenv({'user.linkproturi' => join(',',@proturis)});
535: }
536: } else {
537: &Apache::lonnet::appenv({'user.linkproturi' => $deeplink});
538: }
539: } elsif ($data{'linkkey'} ne '') {
540: &Apache::lonnet::appenv({'request.linkkey' => $data{'linkkey'}});
541: if ($env{'request.linkprot'}) {
542: &Apache::lonnet::delenv('request.linkprot');
543: }
544: my $deeplink = $data{'deeplink.login'};
545: my $linkkey = $data{'linkkey'};
546: if ($env{'user.deeplinkkey'} ne '') {
547: my @linkkeys = split(/,/,$env{'user.deeplinkkey'});
548: unless (grep(/^\Q$linkkey\E$/,@linkkeys)) {
549: push(@linkkeys,$linkkey);
550: &Apache::lonnet::appenv({'user.deeplinkkey' => join(',',sort(@linkkeys))});
551: }
552: } else {
553: &Apache::lonnet::appenv({'user.deeplinkkey' => $linkkey});
554: }
555: if ($deeplink) {
556: if ($env{'user.keyedlinkuri'}) {
557: my @keyeduris = split(/,/,$env{'user.keyedlinkuri'});
558: unless (grep(/^\Q$deeplink\E$/,@keyeduris)) {
559: push(@keyeduris,$deeplink);
560: &Apache::lonnet::appenv({'user.keyedlinkuri' => join(',',sort(@keyeduris))});
561: }
562: } else {
563: &Apache::lonnet::appenv({'user.keyedlinkuri' => $deeplink});
564: }
565: }
566: }
567: if ($data{'deeplink.login'}) {
568: &Apache::lonnet::appenv({'request.deeplink.login' => $data{'deeplink.login'}});
569: if ($env{'environment.remote'} eq 'on') {
570: &Apache::lonnet::appenv({'environment.remote' => 'off'});
571: }
572: }
573: if ($data{'lti.login'}) {
574: if ($env{'environment.remote'} eq 'on') {
575: &Apache::lonnet::appenv({'environment.remote' => 'off'});
576: }
577: my $needslogout;
578: if ($env{'request.lti.login'}) {
579: if (($env{'user.name'} ne $data{'username'}) ||
580: ($env{'user.domain'} ne $data{'domain'})) {
581: $needslogout = 1;
582: }
583: } else {
584: $needslogout = 1;
585: }
586: # If access is via LTI, and user already has a non-LTI session cookie
587: # (and session) or has an LTI session cookie for a different username,
588: # logout the existing session, and start a new one
589: if ($needslogout) {
590: &logout($r,$ip,$handle,\%data,$lti_env);
591: } elsif (($data{'lti.reqcrs'}) && ($data{'lti.reqrole'} eq 'cc')) {
592: $form{'lti.reqcrs'} = $data{'lti.reqcrs'};
593: $form{'lti.reqrole'} = $data{'lti.reqrole'};
594: $form{'lti.sourcecrs'} = $data{'lti.sourcecrs'};
595: $form{'lti.uri'} = $data{'lti.uri'};
596: if ($data{'lti.passbackid'}) {
597: $form{'lti.passbackid'} = $data{'lti.passbackid'};
598: }
599: if ($data{'lti.passbackurl'}) {
600: $form{'lti.passbackurl'} = $data{'lti.passbackurl'};
601: }
602: if ($data{'lti.rosterid'}) {
603: $form{'lti.rosterid'} = $data{'lti.rosterid'};
604: }
605: if ($data{'lti.rosterurl'}) {
606: $form{'lti.rosterurl'} = $data{'lti.rosterurl'};
607: }
608: if ($data{'lti.target'}) {
609: $form{'lti.target'} = $data{'lti.target'};
610: }
611: &Apache::loncommon::content_type($r,'text/html');
612: $r->send_http_header;
613: &Apache::ltiauth::lti_reqcrs($r,$data{'domain'},\%form,$data{'username'},$data{'domain'});
614: } else {
615: if (ref($lti_env) eq 'HASH') {
616: delete($lti_env->{'reqcrs'});
617: delete($lti_env->{'reqrole'});
618: delete($lti_env->{'selfenrollrole'});
619: }
620: if ($data{'lti.selfenrollrole'}) {
621: if (&Apache::ltiauth::lti_enroll($data{'username'},$data{'domain'},
622: $data{'lti.selfenrollrole'}) eq 'ok') {
623: my $url = '/adm/roles?selectrole=1&'.
624: &escape($data{'lti.selfenrollrole'}).'=1';
625: if ($data{'origurl'} =~ m{/default_\d+\.sequence$}) {
626: $url .= '&orgurl='.$data{'origurl'}.'&navmap=1';
627: } elsif ($data{'origurl'} ne '') {
628: $url .= '&orgurl='.$data{'origurl'};
629: }
630: if (ref($lti_env) eq 'HASH') {
631: &Apache::lonnet::appenv($lti_env);
632: }
633: $r->internal_redirect($url);
634: } else {
635: &Apache::ltiauth::invalid_request($r,23);
636: }
637: } elsif ($data{'origurl'} ne '') {
638: my $url = $data{'origurl'};
639: if ($url =~ m{/default_\d+\.sequence$}) {
640: $url .= (($url =~/\?/)?'&':'?').'navmap=1';
641: }
642: if (ref($lti_env) eq 'HASH') {
643: &Apache::lonnet::appenv($lti_env);
644: }
645: $r->internal_redirect($url);
646: } else {
647: if (ref($lti_env) eq 'HASH') {
648: &Apache::lonnet::appenv($lti_env);
649: }
650: }
651: }
652: } elsif ($data{'origurl'} ne '') {
653: my $dest = $data{'origurl'};
654: if (($env{'request.deeplink.login'} eq $data{'origurl'}) &&
655: (($env{'request.linkprot'}) || ($env{'request.linkkey'} ne ''))) {
656: my %info;
657: if ($env{'request.linkprot'}) {
658: $info{'linkprot'} = $env{'request.linkprot'};
659: foreach my $item ('linkprotuser','linkprotexit','linkprotpbid','linkprotpburl') {
660: if ($data{$item}) {
661: $info{$item} = $data{$item};
662: }
663: }
664: } elsif ($env{'request.linkkey'} ne '') {
665: $info{'linkkey'} = $env{'request.linkkey'};
666: }
667: $info{'origurl'} = $data{'origurl'};
668: if ($checklaunch) {
669: $info{'checklaunch'} = 1;
670: }
671: my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link');
672: unless (($token eq 'con_lost') || ($token eq 'refused') || ($token =~ /^error:/) ||
673: ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
674: $dest .= (($dest =~ /\?/) ? '&' : '?') . 'ttoken='.$token;
675: }
676: }
677: $r->internal_redirect($dest);
678: } elsif ($env{'request.course.id'}) {
679: $r->internal_redirect('/adm/navmaps');
680: } else {
681: $r->internal_redirect('/adm/roles');
682: }
683: } else {
684: my $desturl = '/adm/roles';
685: if ($data{'lti.login'}) {
686: if ($data{'lti.selfenrollrole'}) {
687: $desturl .= '?selectrole=1&'.
688: &escape($data{'lti.selfenrollrole'}).'=1';
689: if ($data{'origurl'} ne '') {
690: $desturl .= '&orgurl='.$data{'origurl'};
691: if ($data{'origurl'} =~ m{/default_\d+\.sequence$}) {
692: $desturl .= '&navmap=1';
693: }
694: }
695: } elsif ($data{'origurl'} ne '') {
696: $desturl = $data{'origurl'};
697: if ($desturl =~ m{/default_\d+\.sequence$}) {
698: $desturl .= (($desturl =~/\?/)?'&':'?').'navmap=1';
699: }
700: }
701: } elsif ($data{'origurl'} ne '') {
702: $desturl = $data{'origurl'};
703: if ($data{'linkprot'}) {
704: my ($linkprotector,$linkuri) = split(/:/,$data{'linkprot'},2);
705: if ($linkprotector) {
706: if (ref($extra_env) eq 'HASH') {
707: $extra_env->{'user.linkprotector'} = $linkprotector;
708: $extra_env->{'user.linkproturi'} = $linkuri;
709: $extra_env->{'request.linkprot'} = $data{'linkprot'};
710: } else {
711: $extra_env = {'user.linkprotector' => $linkprotector,
712: 'user.linkproturi' => $linkuri,
713: 'request.linkprot' => $data{'linkprot'}};
714: }
715: if ($data{'linkprotexit'}) {
716: $extra_env->{'request.linkprotexit'} = $data{'linkprotexit'};
717: }
718: if ($data{'linkprotpbid'}) {
719: $extra_env->{'request.linkprotpbid'} = $data{'linkprotpbid'};
720: }
721: if ($data{'linkprotpburl'}) {
722: $extra_env->{'request.linkprotpburl'} = $data{'linkprotpburl'};
723: }
724: }
725: foreach my $item ('linkprotuser','linkprotexit','linkprotpbid','linkprotpburl') {
726: if ($data{$item} ne '') {
727: $form{$item} = $data{$item};
728: }
729: }
730: } elsif ($data{'linkkey'} ne '') {
731: if (ref($extra_env) eq 'HASH') {
732: $extra_env->{'user.deeplinkkey'} = $data{'linkkey'};
733: $extra_env->{'user.keyedlinkuri'} = $data{'deeplink.login'};
734: $extra_env->{'request.linkkey'} = $data{'linkkey'};
735: } else {
736: $extra_env = {'user.deeplinkkey' => $data{'linkkey'},
737: 'user.keyedlinkuri' => $data{'deeplink.login'},
738: 'request.linkkey' => $data{'linkkey'}};
739: }
740: }
741: if ($data{'deeplink.login'}) {
742: if (ref($extra_env) eq 'HASH') {
743: $extra_env->{'request.deeplink.login'} = $data{'deeplink.login'};
744: } else {
745: $extra_env = {'request.deeplink.login' => $data{'deeplink.login'}};
746: }
747: }
748: if ($desturl eq '/adm/email') {
749: if (($data{'display'}) && ($data{'mailrecip'})) {
750: if (&unescape($data{'mailrecip'}) eq "$data{'username'}:$data{'domain'}") {
751: $form{'display'} = &unescape($data{'display'});
752: }
753: }
754: }
755: }
756: my $skipcritical;
757: if ($data{'lti.login'}) {
758: if ((($data{'lti.reqcrs'}) &&
759: ($data{'lti.reqrole'} eq 'cc')) ||
760: ($data{'lti.selfenrollrole'})) {
761: $skipcritical = 1;
762: }
763: }
764: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
765: $home,$desturl,$extra_env,\%form,$skipcritical,'',
766: $expirepub,$data{'lcssowin'});
767: }
768: return OK;
769: }
770:
771: if ($data{'lti.login'}) {
772: my $handle = &Apache::lonnet::check_for_valid_session($r);
773: if ($handle) {
774: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
775: $handle);
776: my $needslogout;
777: if ($env{'request.lti.login'}) {
778: if (($env{'user.name'} ne $data{'username'}) ||
779: ($env{'user.domain'} ne $data{'domain'})) {
780: $needslogout = 1;
781: }
782: } else {
783: $needslogout = 1;
784: }
785: if ($needslogout) {
786: &logout($r,$ip,$handle,\%data,$lti_env);
787: return OK;
788: }
789: }
790: }
791:
792: # check if current user, and role match those expected.
793: my ($next_url,$reuse_session);
794: if (($env{'user.name'} eq $data{'username'}) && ($env{'user.domain'} eq $data{'domain'}) &&
795: ($env{'request.role'} eq $data{'role'}) && ($data{'origurl'} ne '')) {
796: $next_url = $data{'origurl'};
797: $reuse_session = 1;
798: } elsif ($data{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) {
799: $next_url=$data{'origurl'};
800: } else {
801: if ($data{'lti.login'}) {
802: $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
803: if ($data{'origurl'} ne '') {
804: $next_url .= '&orgurl='.&escape($data{'origurl'});
805: }
806: } else {
807: $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
808: if ($data{'origurl'} ne '') {
809: $next_url .= '&orgurl='.&escape($data{'origurl'});
810: }
811: }
812: }
813: if ($data{'lti.login'}) {
814: if (($data{'origurl'} =~ m{/default_\d+\.sequence$}) ||
815: ($data{'origurl'} =~ m{^/res/.+\.sequence$})) {
816: if ($reuse_session) {
817: $next_url .= '&navmap=1';
818: } else {
819: $next_url .= '&navmap=1';
820: }
821: }
822: }
823: if ($reuse_session) {
824: $r->internal_redirect($next_url);
825: } else {
826: if ($data{'deeplink.login'}) {
827: if (ref($extra_env) eq 'HASH') {
828: $extra_env->{'request.deeplink.login'} = $data{'deeplink.login'};
829: } else {
830: $extra_env = {'request.deeplink.login' => $data{'deeplink.login'}};
831: }
832: if ($data{'linkprot'}) {
833: $extra_env->{'request.linkprot'} = $data{'linkprot'};
834: if ($data{'linkprotexit'}) {
835: $extra_env->{'request.linkprotexit'} = $data{'linkprotexit'};
836: }
837: if ($data{'linkprotpbid'}) {
838: $extra_env->{'request.linkprotpassbid'} = $data{'linkprotpbid'};
839: }
840: if ($data{'linkprotpburl'}) {
841: $extra_env->{'request.linkprotpassburl'} = $data{'linkprotpburl'};
842: }
843: foreach my $item ('linkprotuser','linkprotexit','linkprotpbid','linkprotpburl') {
844: if ($data{$item}) {
845: $form{'request.'.$item} = $data{$item};
846: }
847: }
848: } elsif ($data{'linkkey'} ne '') {
849: $extra_env->{'request.linkkey'} = $data{'linkkey'};
850: }
851: }
852: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
853: $next_url,$extra_env,\%form,'',$cid,'',$data{'lcssowin'});
854: }
855: return OK;
856: }
857:
858: 1;
859: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>