Annotation of loncom/auth/migrateuser.pm, revision 1.26
1.1 albertel 1: # The LearningOnline Network
2: # Starts a user off based of an existing token.
3: #
1.26 ! raeburn 4: # $Id: migrateuser.pm,v 1.25 2016/12/05 00:51:43 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.1 albertel 38:
39: sub goto_login {
1.24 raeburn 40: my ($r,$domain) = @_;
1.2 albertel 41: &Apache::loncommon::content_type($r,'text/html');
42: $r->send_http_header;
1.24 raeburn 43: my $url = '/adm/login';
44: if ($domain) {
45: $url .= '?domain='.$domain;
46: }
1.5 albertel 47: $r->print(&Apache::loncommon::start_page('Going to login',undef,
1.24 raeburn 48: {'redirect' => [0,$url],}).
1.5 albertel 49: '<h1>'.&mt('One moment please...').'</h1>'.
50: '<p>'.&mt('Transferring to login page.').'</p>'.
51: &Apache::loncommon::end_page());
1.2 albertel 52: return OK;
1.1 albertel 53: }
54:
1.2 albertel 55:
1.26 ! raeburn 56: sub sso_lti_check {
1.7 albertel 57: my ($data) = @_;
1.8 albertel 58: my %extra_env;
1.18 raeburn 59: if (ref($data) eq 'HASH') {
60: if ($data->{'sso.login'}) {
61: $extra_env{'request.sso.login'} = $data->{'sso.login'};
62: }
63: if ($data->{'sso.reloginserver'}) {
64: $extra_env{'request.sso.reloginserver'} =
65: $data->{'sso.reloginserver'};
66: }
1.26 ! raeburn 67: if ($data->{'lti.login'}) {
! 68: $extra_env{'request.lti.login'} = $data->{'lti.login'};
! 69: }
! 70: if ($data->{'lti.passbackid'}) {
! 71: $extra_env{'request.lti.passbackid'} = $data->{'lti.passbackid'};
! 72: }
! 73: if ($data->{'lti.passbackurl'}) {
! 74: $extra_env{'request.lti.passbackurl'} = $data->{'lti.passbackurl'};
! 75: }
! 76: if ($data->{'lti.rosterid'}) {
! 77: $extra_env{'request.lti.rosterid'} = $data->{'lti.rosterid'};
! 78: }
! 79: if ($data->{'lti.rosterurl'}) {
! 80: $extra_env{'request.lti.rosterurl'} = $data->{'lti.rosterurl'};
! 81: }
1.7 albertel 82: }
1.18 raeburn 83: return \%extra_env;
84: }
85:
86: sub ip_changed {
87: my ($r,$udom,$camefrom,$dataref) = @_;
88: &Apache::loncommon::content_type($r,'text/html');
89: $r->send_http_header;
90: if (ref($dataref) eq 'HASH') {
91: my $title = 'LON-CAPA Session redirected';
92: my $message = &mt('Your internet address has changed since you logged in.');
93: my $rule_in_effect;
1.22 raeburn 94: if ($dataref->{'balancer'}) {
95: my $baldom = &Apache::lonnet::host_domain($camefrom);
96: my $balprimaryid = &Apache::lonnet::domain($baldom,'primary');
97: my $balintdom = &Apache::lonnet::internet_dom($balprimaryid);
98: my $uprimaryid = &Apache::lonnet::domain($udom,'primary');
99: my $uintdom = &Apache::lonnet::internet_dom($uprimaryid);
100: my $dom_in_use;
101: if (($uintdom ne '') && ($uintdom eq $balintdom)) {
102: $dom_in_use = $udom;
103: } else {
104: $dom_in_use = $baldom;
1.21 raeburn 105: }
1.22 raeburn 106: my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
1.18 raeburn 107: unless (defined($cached)) {
108: my $cachetime = 60*60*24;
109: my %domconfig =
1.22 raeburn 110: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1.18 raeburn 111: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.22 raeburn 112: $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
1.18 raeburn 113: $domconfig{'loadbalancing'},$cachetime);
114: }
115: }
116: if (ref($result) eq 'HASH') {
117: (undef,my $currtargets,my $currrules) =
118: &Apache::lonnet::check_balancer_result($result,$dataref->{'server'});
119: if (ref($currrules) eq 'HASH') {
120: if ($dataref->{'sso.login'}) {
121: if ($currrules->{'_LC_ipchangesso'} ne '') {
122: $rule_in_effect = $currrules->{'_LC_ipchangesso'};
123: }
124: } else {
125: if ($currrules->{'_LC_ipchange'} ne '') {
126: $rule_in_effect = $currrules->{'_LC_ipchange'};
127: }
128: }
129: }
130: }
131: }
132: my $url;
133: my $lonhost= $r->dir_config('lonHostID');
134: my $switchto = $lonhost;
1.23 raeburn 135: if ($rule_in_effect ne 'offloadedto') {
1.18 raeburn 136: my $hosthere;
1.23 raeburn 137: my @ids=&Apache::lonnet::current_machine_ids();
138: unless ($rule_in_effect eq 'balancer') {
139: if (grep(/^\Q$rule_in_effect\E$/,@ids)) {
140: $hosthere = 1;
141: }
142: }
143: unless ($hosthere) {
144: if ($dataref->{'role'}) {
145: my ($adom,$aname);
146: if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) {
147: $adom = $1;
148: $aname = $dataref->{'username'};
149: } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
150: $adom = $1;
151: $aname = $2;
152: }
153: if ($adom ne '' && $aname ne '') {
154: my $ahome = &Apache::lonnet::homeserver($aname,$adom);
155: unless ($ahome eq 'no_host') {
156: if ($ahome && grep(/^\Q$ahome\E$/,@ids)) {
157: $hosthere = 1;
158: }
1.18 raeburn 159: }
160: }
161: }
162: }
1.23 raeburn 163: unless ($hosthere) {
164: my $hostname;
165: if ($rule_in_effect eq 'balancer') {
166: $hostname = &Apache::lonnet::hostname($dataref->{'server'});
167: if ($hostname) {
168: $switchto = $dataref->{'server'};
169: }
170: } else {
171: $hostname = &Apache::lonnet::hostname($rule_in_effect);
172: if ($hostname) {
173: $switchto = $rule_in_effect;
174: }
175: }
1.18 raeburn 176: if ($hostname) {
177: my $protocol = $Apache::lonnet::protocol{$switchto};
178: $protocol = 'http' if ($protocol ne 'https');
179: $url = $protocol.'://'.$hostname;
1.23 raeburn 180: if ($rule_in_effect eq 'balancer') {
181: $message .= '<br />'.
182: &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.');
183: } else {
184: $message .= '<br />'.
185: &mt('As a result, your LON-CAPA session is being redirected.');
186: }
1.18 raeburn 187: }
188: }
189: }
190: if ($dataref->{'sso.login'}) {
1.21 raeburn 191: $url .= '/adm/roles';
1.18 raeburn 192: } else {
1.21 raeburn 193: $url .= '/adm/login';
1.24 raeburn 194: if ($udom) {
195: $url .= '?domain='.$udom;
196: }
1.20 raeburn 197: $message .= '<br />'.&mt('You will need to provide your password one more time.');
1.18 raeburn 198: }
199: my %info= (
1.24 raeburn 200: 'domain' => $udom,
1.18 raeburn 201: 'username' => $dataref->{'username'},
202: 'role' => $dataref->{'role'},
203: 'sessionserver' => $lonhost,
204: );
205: if ($dataref->{'origurl'}) {
206: $info{'origurl'} = $dataref->{'origurl'};
207: }
208: if ($dataref->{'symb'}) {
209: $info{'symb'} = $dataref->{'symb'};
210: }
211: my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto);
212: unless ($iptoken eq 'conlost') {
1.24 raeburn 213: $url .= ($url =~ /\?/) ? '&' : '?';
214: $url .= 'iptoken='.$iptoken;
1.18 raeburn 215: }
216: $r->print(&Apache::loncommon::start_page($title,undef,
217: {'redirect' =>
218: [2,$url],}).
219: '<h1>'.&mt('One moment please...').'</h1>'.
220: '<p class="LC_warning">'.$message.'</p>'.
221: &Apache::loncommon::end_page());
222: } else {
223: return &goto_login($r);
1.10 raeburn 224: }
1.18 raeburn 225: return OK;
1.7 albertel 226: }
227:
1.1 albertel 228: sub handler {
229: my ($r) = @_;
230:
231: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
1.4 albertel 232: my %data = &Apache::lonnet::tmpget($env{'form.token'});
1.13 raeburn 233: if (keys(%data) == 0) {
234: return &goto_login($r);
235: }
1.4 albertel 236: my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
237:
1.6 albertel 238: &Apache::lonlocal::get_language_handle($r);
239:
1.4 albertel 240: if ($delete ne 'ok') {
241: return &goto_login($r);
242: }
1.2 albertel 243:
1.18 raeburn 244: if (!defined($data{'username'}) || !defined($data{'domain'})) {
245: return &goto_login($r);
246: }
247: if ($data{'ip'} ne $ENV{'REMOTE_ADDR'}) {
1.24 raeburn 248: &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
249: $data{'ip'}.'; now: '.$ENV{'REMOTE_ADDR'}.' for '.$data{'username'}.':'.$data{'domain'});
1.18 raeburn 250: return &ip_changed($r,$data{'domain'},$data{'server'},\%data);
1.2 albertel 251: }
252:
1.17 raeburn 253: &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} to $data{'role'}");
1.2 albertel 254: my $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
1.24 raeburn 255: my $udom;
256: if (&Apache::lonnet::domain($data{'domain'})) {
257: $udom=$data{'domain'};
258: }
259: if ($home =~ /(con_lost|no_such_host)/) { return &goto_login($r,$udom); }
1.2 albertel 260:
1.26 ! raeburn 261: my $extra_env = &sso_lti_check(\%data);
1.8 albertel 262:
1.16 raeburn 263: my %form;
264: if ($data{'symb'} ne '') {
265: $form{'symb'} = $data{'symb'};
266: }
1.21 raeburn 267: if ($data{'iptoken'} ne '') {
268: $form{'iptoken'} = $data{'iptoken'};
269: }
1.25 raeburn 270: if ($data{'noloadbalance'} ne '') {
271: $form{'noloadbalance'} = $data{'noloadbalance'};
272: }
1.16 raeburn 273:
1.3 albertel 274: if (!$data{'role'}) {
1.12 albertel 275: my $handle = &Apache::lonnet::check_for_valid_session($r);
276: if ($handle) {
1.11 albertel 277: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
278: $handle);
1.14 raeburn 279: if ($data{'origurl'} ne '') {
280: $r->internal_redirect($data{'origurl'});
281: } elsif ($env{'request.course.id'}) {
282: $r->internal_redirect('/adm/navmaps');
1.11 albertel 283: } else {
284: $r->internal_redirect('/adm/roles');
285: }
286: } else {
1.14 raeburn 287: my $desturl = '/adm/roles';
288: if ($data{'origurl'} ne '') {
289: $desturl = $data{'origurl'};
290: }
1.11 albertel 291: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
1.16 raeburn 292: $home,$desturl,$extra_env,\%form);
1.11 albertel 293:
294: }
1.1 albertel 295: return OK;
1.11 albertel 296:
1.1 albertel 297: }
1.6 albertel 298:
299: my $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
1.15 raeburn 300: if ($data{'origurl'} ne '') {
301: $next_url .= '&orgurl='.&escape($data{'origurl'});
302: }
1.6 albertel 303: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
1.16 raeburn 304: $next_url,$extra_env,\%form);
1.6 albertel 305: return OK;
1.1 albertel 306: }
307:
308: 1;
309: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>