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