[LON-CAPA-cvs] cvs: loncom /auth lonauth.pm lonlogin.pm lonrelaunch.pm lonroles.pm migrateuser.pm /interface lontiny.pm /lti ltiauth.pm

raeburn raeburn at source.lon-capa.org
Sun Jun 26 00:03:48 EDT 2022


raeburn		Sun Jun 26 04:03:48 2022 EDT

  Modified files:              
    /loncom/auth	lonauth.pm lonlogin.pm lonrelaunch.pm lonroles.pm 
                	migrateuser.pm 
    /loncom/interface	lontiny.pm 
    /loncom/lti	ltiauth.pm 
  Log:
  - Bug 6907
    - Set request.linkprotuser in %env for students accessing deep-linked items
      with LTI link protection which includes username from other LMS.
    - Set "only_body" for intermediate pages shown while session is being set up
      for LTI link-protected access.
    - Provide appropriate feedback when a deep link is followed but the user
      only has a future and/or expired role in the target course. 
  
  
-------------- next part --------------
Index: loncom/auth/lonauth.pm
diff -u loncom/auth/lonauth.pm:1.174 loncom/auth/lonauth.pm:1.175
--- loncom/auth/lonauth.pm:1.174	Sat Jun 18 02:10:18 2022
+++ loncom/auth/lonauth.pm	Sun Jun 26 04:03:47 2022
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # User Authentication Module
 #
-# $Id: lonauth.pm,v 1.174 2022/06/18 02:10:18 raeburn Exp $
+# $Id: lonauth.pm,v 1.175 2022/06/26 04:03:47 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -179,11 +179,19 @@
         $destination .= 'source=login';
     }
 
+    my $brcrum = [{'href' => '',
+                   'text' => 'Successful Login'},];
+    my $args = {'no_inline_link' => 1,
+                'bread_crumbs' => $brcrum,};
     if (($env{'request.deeplink.login'} eq $lowerurl) &&
         (($env{'request.linkprot'}) || ($env{'request.linkkey'} ne ''))) {
         my %info;
         if ($env{'request.linkprot'}) {
             $info{'linkprot'} = $env{'request.linkprot'};
+            if ($form->{'linkprotuser'}) {
+                $info{'linkprotuser'} = $form->{'linkprotuser'};
+            }
+            $args = {'only_body' => 1,};
         } elsif ($env{'request.linkkey'} ne '') {
             $info{'linkkey'} = $env{'request.linkkey'};
         }
@@ -200,9 +208,6 @@
         $windowname .= 'lti';
     }
     my $windowinfo = Apache::lonhtmlcommon::scripttag('self.name="'.$windowname.'";');
-    my $brcrum = [{'href' => '',
-                   'text' => 'Successful Login'},];
-    my $args = {'bread_crumbs' => $brcrum,};
     unless ((defined($form->{role})) || (defined($form->{symb}))) {
         my $update=$env{'user.update.time'};
         if (!$update) {
@@ -276,18 +281,19 @@
         $args->{'redirect'} = [0,$destination];
         $start_page=&Apache::loncommon::start_page('Successful Login',
                                                    $js,$args);
-
-        my %lt=&Apache::lonlocal::texthash(
-				           'wel' => 'Welcome',
-				           'pro' => 'Login problems?',
+        unless ($env{'request.linkprot'}) {
+            my %lt=&Apache::lonlocal::texthash(
+		    		               'wel' => 'Welcome',
+				               'pro' => 'Login problems?',
 				          );
-        $pagebody = "<h1>$lt{'wel'}</h1>\n".
-                    &mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>');
-        my $loginhelp = &loginhelpdisplay($domain);
-        if ($loginhelp) {
-            $pagebody .= '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';
+            $pagebody = "<h1>$lt{'wel'}</h1>\n".
+                        &mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>');
+            my $loginhelp = &loginhelpdisplay($domain);
+            if ($loginhelp) {
+                $pagebody .= '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';
+            }
         }
-    }
+    } 
     $end_page = &Apache::loncommon::end_page();
     $r->print(<<ENDSUCCESS);
 $start_page
@@ -809,6 +815,9 @@
             }
             if ($form{'linkprot'}) {
                 $env{'request.linkprot'} = $form{'linkprot'};
+                if ($form{'linkprotuser'}) {
+                    $env{'request.linkprotuser'} = $form{'linkprotuser'};
+                }
             } elsif ($form{'linkkey'} ne '') {
                 $env{'request.linkkey'} = $form{'linkkey'};
             }
@@ -840,6 +849,9 @@
                 }
                 if ($form{'linkprot'}) {
                     $env{'request.linkprot'} = $form{'linkprot'};
+                    if ($form{'linkprotuser'}) {
+                        $env{'request.linkprotuser'} = $form{'linkprotuser'};
+                    }
                 } elsif ($form{'linkkey'} ne '') {
                     $env{'request.linkkey'} = $form{'linkkey'};
                 }
Index: loncom/auth/lonlogin.pm
diff -u loncom/auth/lonlogin.pm:1.199 loncom/auth/lonlogin.pm:1.200
--- loncom/auth/lonlogin.pm:1.199	Sat Jun 18 02:10:18 2022
+++ loncom/auth/lonlogin.pm	Sun Jun 26 04:03:47 2022
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Login Screen
 #
-# $Id: lonlogin.pm,v 1.199 2022/06/18 02:10:18 raeburn Exp $
+# $Id: lonlogin.pm,v 1.200 2022/06/26 04:03:47 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -262,7 +262,6 @@
                 if ($info{'linkprotuser'} ne '') {
                     $linkprotuser = $info{'linkprotuser'};
                 } 
-                my $delete = &Apache::lonnet::tmpdel($env{'form.ltoken'});
             } else {
                 $linkprot = $env{'form.linkprot'};
                 $linkprotuser = $env{'form.linkprotuser'};
@@ -277,6 +276,9 @@
                                           linkprot => $linkprot,
                                           linkprotuser => $linkprotuser,
                                        );    
+                    if ($env{'form.ltoken'}) {
+                        my $delete = &Apache::lonnet::tmpdel($env{'form.ltoken'});
+                    }
                     &Apache::migrateuser::logout($r,$ip,$handle,undef,undef,\%linkprotinfo);
                     return OK;
                 }
@@ -325,6 +327,9 @@
                 }
             }
         }
+        if ($env{'form.ltoken'}) {
+            my $delete = &Apache::lonnet::tmpdel($env{'form.ltoken'});
+        }
 	$r->print(
                   $start_page
                  .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
@@ -1149,6 +1154,7 @@
         $path = '/'.$path;
     }
     my $url = $protocol.'://'.$hostname.$path;
+    my $args = {};
     if ($env{'form.firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
         $url = $protocol.'://'.$hostname.$env{'form.firsturl'};
         if (($env{'form.ltoken'}) || ($env{'form.linkprot'} ne '') ||
@@ -1157,11 +1163,13 @@
             if ($env{'form.ltoken'}) {
                 %link_info = &Apache::lonnet::tmpget($env{'form.ltoken'});
                 &Apache::lonnet::tmpdel($env{'form.ltoken'});
+                $args->{'only_body'} = 1;
             } elsif ($env{'form.linkprot'}) {
                 $link_info{'linkprot'} = $env{'form.linkprot'};
                 if ($env{'form.linkprotuser'}) {
                     $link_info{'linkprotuser'} = $env{'form.linkprotuser'};
                 }
+                $args->{'only_body'} = 1;
             } elsif ($env{'form.linkkey'} ne '') {
                 $link_info{'linkkey'} = $env{'form.linkkey'};
             }
@@ -1196,8 +1204,8 @@
         }
         $url .= $querystring;
     }
-    my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,
-                                                    {'redirect' => [0,$url],});
+    $args->{'redirect'} = [0,$url];
+    my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,$args);
     my $end_page   = &Apache::loncommon::end_page();
     return $start_page.$end_page;
 }
Index: loncom/auth/lonrelaunch.pm
diff -u loncom/auth/lonrelaunch.pm:1.1 loncom/auth/lonrelaunch.pm:1.2
--- loncom/auth/lonrelaunch.pm:1.1	Sat Jun 18 02:10:18 2022
+++ loncom/auth/lonrelaunch.pm	Sun Jun 26 04:03:47 2022
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Re-launch guidance for deep linked access with username mismatch
 #
-# $Id: lonrelaunch.pm,v 1.1 2022/06/18 02:10:18 raeburn Exp $
+# $Id: lonrelaunch.pm,v 1.2 2022/06/26 04:03:47 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -45,7 +45,6 @@
         &Apache::loncommon::get_unprocessed_cgi($r->args,['rtoken']);
         if (exists($env{'form.rtoken'})) {
             %data = &Apache::lonnet::tmpget($env{'form.rtoken'});
-            my $delete = &Apache::lonnet::tmpdel($env{'form.rtoken'});
         }
     }
     my $handle = &Apache::lonnet::check_for_valid_session($r);
@@ -85,22 +84,34 @@
 
     if ((keys(%data)) && ($data{'origurl'} =~ m{^/tiny/$match_domain/\w+$})) {
         my $url = $data{'origurl'};
-        if (($data{'linkprot'} =~ m{^\w+(c|d):\Q$url\E$}) &&
-            ($data{'linkprotuser'} =~ m{^($match_username):($match_domain)$})) {
-            my $brcrum = [{'href' => '','text' => 'Update session'},];
-            $url .= '?ltoken='.$env{'form.rtoken'};
-            $r->print(&Apache::loncommon::start_page('Updating session',undef,
-                                                     {'only_body'    => 1,
-                                                      'redirect'     => [1,$url],
-                                                      'bread_crumbs' => $brcrum,}).
-                      '<p>'.&mt('Updating old session information').'</p>'.
-                      &Apache::loncommon::end_page());
+        if ($data{'linkprot'} =~ m{^\w+(c|d):\Q$url\E$}) {
+            if ((!exists($data{'linkprotuser'})) ||
+                (($data{'linkprotuser'}) &&
+                 ($data{'linkprotuser'} =~ m{^($match_username):($match_domain)$}))) {
+                $url .= '?ltoken='.$env{'form.rtoken'};
+                $r->print(&Apache::loncommon::start_page('Updating session',undef,
+                                                         {'only_body'    => 1,
+                                                          'redirect'     => [1,$url]}).
+                          '<p>'.&mt('Updating old session information').'</p>'.
+                          &Apache::loncommon::end_page());
             return OK;
         }
     }
-    $r->print(&Apache::loncommon::start_page('Username mismatch',undef,{'only_body' => 1}));
-    $r->print(&mt('Although your credentials were authenticated, the username you entered did not match what was expected [_1] from the link you followed',"($data{'linkprotuser'})"));
+    if ($data{'linkprot'}) {
+        $r->print(&Apache::loncommon::start_page('Updating session',undef,{'only_body' => 1}));
+        if ($data{'linkprotuser'} ne '') {
+            $r->print(&mt('Although your credentials were authenticated, the username you entered did not match what was expected [_1] from the link you followed',"($data{'linkprotuser'})"));
+        } else {
+            $r->print(&mt('Expired any existing session'));
+        }
+    } else {
+        $r->print(&Apache::loncommon::start_page('Session removed',undef,{'only_body' => 1}));
+        $r->print(&mt('Expired any existing session'));
+    }
     $r->print(&Apache::loncommon::end_page());
+    if (exists($env{'form.rtoken'})) {
+        &Apache::lonnet::tmpdel($env{'form.rtoken'});       
+    }
     return OK;
 }
 
Index: loncom/auth/lonroles.pm
diff -u loncom/auth/lonroles.pm:1.364 loncom/auth/lonroles.pm:1.365
--- loncom/auth/lonroles.pm:1.364	Wed May 25 18:04:17 2022
+++ loncom/auth/lonroles.pm	Sun Jun 26 04:03:47 2022
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # User Roles Screen
 #
-# $Id: lonroles.pm,v 1.364 2022/05/25 18:04:17 raeburn Exp $
+# $Id: lonroles.pm,v 1.365 2022/06/26 04:03:47 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -149,19 +149,23 @@
 my $rosterupdates;
 
 sub start_loading_course {
-    my ($r,$title) = @_;
+    my ($r,$title,$only_body) = @_;
     &Apache::loncommon::content_type($r,'text/html');
     &Apache::loncommon::no_cache($r);
     $r->send_http_header;
-    my $swinfo=&Apache::lonmenu::rawconfig();
-    # Breadcrumbs
-    my $brcrum = [{'href' => '',
-                   'text' => $title},];
-    my $start_page = &Apache::loncommon::start_page($title,undef,
-                                                    {'bread_crumbs' => $brcrum,
-                                                     'bread_crumbs_nomenu' => 1,
-                                                     'links_disabled' => 1});
-    $r->print(<<ENDREDIR);
+    if ($only_body) {
+        $r->print(&Apache::loncommon::start_page($title,undef,{'only_body' => 1,
+                                                               'add_progressbar' => 1}));
+    } else {
+        my $swinfo=&Apache::lonmenu::rawconfig();
+        # Breadcrumbs
+        my $brcrum = [{'href' => '',
+                       'text' => $title},];
+        my $start_page = &Apache::loncommon::start_page($title,undef,
+                                                        {'bread_crumbs' => $brcrum,
+                                                         'bread_crumbs_nomenu' => 1,
+                                                         'links_disabled' => 1});
+        $r->print(<<ENDREDIR);
 $start_page
 <script type="text/javascript">
 // <![CDATA[
@@ -176,23 +180,30 @@
 // ]]>
 </script>
 ENDREDIR
+    }
     return;
 }
 
 sub finish_loading_course {
-    my ($r,$msg,$url) = @_;
+    my ($r,$msg,$url,$only_body) = @_;
     my $link = '<div id="LC_course_loaded" style="display:none"><a href="'.
                &HTML::Entities::encode($url,'"<>&').'">'.&mt('Continue').'</a></div>';
     my $end_page = &Apache::loncommon::end_page();
     my $js_url = &js_escape($url);
+    my $reenable;
+    unless ($only_body) {
+        $reenable = <<REENABLE;
+    \$('.isDisabled > a').removeAttr("aria-disabled");
+    \$('.isDisabled').removeClass("isDisabled");
+REENABLE
+    }
     $r->print(<<END);
 $msg
 <script type="text/javascript">
 // <![CDATA[
 \$(document).ready(function() {
     \$("#LC_course_loaded").css("display","block");
-    \$('.isDisabled > a').removeAttr("aria-disabled");
-    \$('.isDisabled').removeClass("isDisabled");
+    $reenable
     var url = "$js_url";
     \$(location).attr('href',url);
 });
@@ -763,9 +774,21 @@
                         }
                         my $crstype = &Apache::loncommon::course_type($cdom.'_'.$cnum);
                         $crstype = lc($crstype);
+                        my ($msg,$critmsg_check,$title,$loadmsg,$only_body);
+                        $critmsg_check = 1;
+                        $title = &mt("Loading $crstype");
+                        $loadmsg = &mt("Please be patient while your $crstype loads");
+                        if (($env{'request.deeplink.login'}) && ($env{'request.linkprot'})) {
+                            if ($env{'request.linkprot'} =~ /^\d+(c|d):\Q$env{'form.destinationurl'}\E$/) {
+                                $title = &mt('Loading LON-CAPA session');
+                                $loadmsg = &mt('Please be patient while LON-CAPA loads');
+                                $only_body = 1;
+                                $critmsg_check = 0;
+                            }
+                        }
                         my $preamble = '<div id="LC_update_'.$cdom.'_'.$cnum.'" class="LC_info">'.
                                        '<br />'.
-                                       &mt("Please be patient while your $crstype loads").
+                                       $loadmsg.
                                        '<br /></div>'.
                                        '<div style="padding:0;clear:both;margin:0;border:0"></div>';
                         my $closure = <<ENDCLOSE;
@@ -775,22 +798,21 @@
 // ]]>
 </script>
 ENDCLOSE
-                        my $title = &mt("Loading $crstype");
-                        &start_loading_course($r,$title);
+                        &start_loading_course($r,$title,$only_body);
                         my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,undef,$preamble);
                         &Apache::lonhtmlcommon::Update_PrgWin($r,\%prog_state,&mt('Loading ...'));
                         $r->rflush();
-                        my ($msg,$blockcrit,$critmsg_check);
-                        $critmsg_check = 1;
-                        $blockcrit = &Apache::loncommon::blocking_status('alert',$clientip,$cnum,$cdom,undef,1);
-                        if ($blockcrit) {
-                            my $checkrole = "cm./$cdom/$cnum";
-                            if ($csec ne '') {
-                                $checkrole .= "/$csec";
-                            }
-                            unless ((&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) &&
-                                    ($trolecode !~ m{^st\./$cdom/$cnum})) {
-                                $critmsg_check = 0;
+                        if ($critmsg_check) {
+                            my $blockcrit = &Apache::loncommon::blocking_status('alert',$clientip,$cnum,$cdom,undef,1);
+                            if ($blockcrit) {
+                                my $checkrole = "cm./$cdom/$cnum";
+                                if ($csec ne '') {
+                                    $checkrole .= "/$csec";
+                                }
+                                unless ((&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) &&
+                                        ($trolecode !~ m{^st\./$cdom/$cnum})) {
+                                    $critmsg_check = 0;
+                                }
                             }
                         }
                         my ($furl,$ferr)=
@@ -905,7 +927,7 @@
                                     $msg = '<p>'.&mt('Entering [_1] ...',
                                                      $env{'course.'.$cdom.'_'.$cnum.'.description'}).
                                            '</p>';
-                                    &finish_loading_course($r,$msg,$dest);
+                                    &finish_loading_course($r,$msg,$dest,$only_body);
                                 }
 			    }
                             $r->rflush();
@@ -951,7 +973,7 @@
                                             $msg = '<p>'.&mt('Entering [_1] ...',
                                                              $env{'course.'.$cdom.'_'.$cnum.'.description'}).
                                                    '</p>';
-                                            &finish_loading_course($r,$msg,'/adm/placement');
+                                            &finish_loading_course($r,$msg,'/adm/placement',$only_body);
                                             $r->rflush();
                                             return OK;
                                         }
@@ -1023,7 +1045,7 @@
                                                              $env{'course.'.$cdom.'_'.$cnum.'.description'}).
                                                    '</p>';
                                         }
-                                        &finish_loading_course($r,$msg,$dest);
+                                        &finish_loading_course($r,$msg,$dest,$only_body);
                                         $r->rflush();
                                         return OK;
                                     }
@@ -1038,7 +1060,7 @@
 					    $msg = '<p>'.&mt('Entering [_1] ...',
 						             $env{'course.'.$cdom.'_'.$cnum.'.description'}).
                                                    '</p>';
-                                            &finish_loading_course($r,$msg,'/adm/whatsnew?refpage=start');
+                                            &finish_loading_course($r,$msg,'/adm/whatsnew?refpage=start',$only_body);
                                             $r->rflush();
 					    return OK;
 					}
@@ -1086,12 +1108,12 @@
                                 }
                                 if ($env{'request.lti.login'}) {
                                     undef($msg);
-                                    &finish_loading_course($r,$msg,$furl);
+                                    &finish_loading_course($r,$msg,$furl,$only_body);
                                 } else {
                                     $msg = '<p>'.&mt('Entering [_1] ...',
 					              $env{'course.'.$cdom.'_'.$cnum.'.description'}).
                                            '</p>';
-                                    &finish_loading_course($r,$msg,$furl);
+                                    &finish_loading_course($r,$msg,$furl,$only_body);
                                 }
 			    }
                             $r->rflush();
Index: loncom/auth/migrateuser.pm
diff -u loncom/auth/migrateuser.pm:1.61 loncom/auth/migrateuser.pm:1.62
--- loncom/auth/migrateuser.pm:1.61	Sat Jun 18 02:10:18 2022
+++ loncom/auth/migrateuser.pm	Sun Jun 26 04:03:47 2022
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Starts a user off based of an existing token.
 #
-# $Id: migrateuser.pm,v 1.61 2022/06/18 02:10:18 raeburn Exp $
+# $Id: migrateuser.pm,v 1.62 2022/06/26 04:03:47 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -336,7 +336,7 @@
         $r->headers_out->add('Set-cookie' => $c);
     }
     my $lonhost = $r->dir_config('lonHostID');
-    my ($url,$token,%info,%user_info);
+    my ($url,$token,$delay,%info,%user_info);
     if (ref($data) eq 'HASH') {
         %user_info=('ip'       => $ip,
                     'domain'   => $data->{'domain'},
@@ -347,6 +347,7 @@
                     'symb'     => $data->{'symb'},
                     'server'   => $lonhost);
     }
+    $delay = 0.1;
     if (ref($linkprot_info) eq 'HASH') {
         if ($linkprot_info->{'linkprotuser'} ne '') {
             $token = &Apache::lonnet::tmpput($linkprot_info,$lonhost);
@@ -361,6 +362,7 @@
             $token = &Apache::lonnet::tmpput(\%info,$lonhost);
             $url = '/adm/migrateuser?token='.$token;
         }
+        $delay = 0;
     } else {
         my %lti_info;
         if (ref($lti_env) eq 'HASH') {
@@ -377,7 +379,7 @@
     $r->send_http_header;
     $r->print(
         &Apache::loncommon::start_page('Updating Session ...',undef,
-                                       {'redirect'       => [0.1,$url],
+                                       {'redirect'       => [$delay,$url],
                                         'only_body'      => 1,}).
         &Apache::loncommon::end_page());
     if ($env{'request.balancercookie'}) {
@@ -900,6 +902,9 @@
                     my %info;
                     if ($env{'request.linkprot'}) {
                         $info{'linkprot'} = $env{'request.linkprot'};
+                        if ($data{'linkprotuser'}) {
+                            $info{'linkprotuser'} = $data{'linkprotuser'};
+                        }
                     } elsif ($env{'request.linkkey'} ne '') {
                         $info{'linkkey'} = $env{'request.linkkey'};
                     }
@@ -952,6 +957,9 @@
                                           'request.linkprot' => $data{'linkprot'}};
                         }
                     }
+                    if ($data{'linkprotuser'} ne '') {
+                        $form{'linkprotuser'} = $data{'linkprotuser'};
+                    }
                 } elsif ($data{'linkkey'} ne '') {
                     if (ref($extra_env) eq 'HASH') {
                         $extra_env->{'user.deeplinkkey'} = $data{'linkkey'};
@@ -1049,6 +1057,9 @@
             }
             if ($data{'linkprot'}) {
                 $extra_env->{'request.linkprot'} = $data{'linkprot'};
+                if ($data{'linkprotuser'}) {
+                    $form{'request.linkprotuser'} = $data{'linkprotuser'};
+                }
             } elsif ($data{'linkkey'} ne '') {
                 $extra_env->{'request.linkkey'} = $data{'linkkey'};
             }
Index: loncom/interface/lontiny.pm
diff -u loncom/interface/lontiny.pm:1.10 loncom/interface/lontiny.pm:1.11
--- loncom/interface/lontiny.pm:1.10	Wed May 25 18:03:43 2022
+++ loncom/interface/lontiny.pm	Sun Jun 26 04:03:48 2022
@@ -2,7 +2,7 @@
 # Extract domain, courseID, and symb from a shortened URL,
 # and switch role to a role in designated course.
 #
-# $Id: lontiny.pm,v 1.10 2022/05/25 18:03:43 raeburn Exp $
+# $Id: lontiny.pm,v 1.11 2022/06/26 04:03:48 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -68,6 +68,23 @@
                         my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
                         if ($chome ne 'no_host') {
                             &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['ttoken']);
+                            my ($linkprot,$linkprotuser,$ltoken);
+                            if ($env{'form.ttoken'}) {
+                                my %link_info = &Apache::lonnet::tmpget($env{'form.ttoken'});
+                                if ($link_info{'origurl'} eq $r->uri) {
+                                    if ($link_info{'ltoken'}) {
+                                        $ltoken = $link_info{'ltoken'};
+                                        my %ltoken_info = &Apache::lonnet::tmpget($link_info{'ltoken'});
+                                        $linkprot = $ltoken_info{'linkprot'};
+                                        $linkprotuser = $ltoken_info{'linkprotuser'};
+                                    } elsif ($link_info{'linkprot'}) {
+                                        $linkprot = $link_info{'linkprot'};
+                                        if ($link_info{'linkprotuser'}) {
+                                            $linkprotuser = $link_info{'linkprotuser'};
+                                        }
+                                    }
+                                }
+                            }
                             if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
                                 # Check for ttoken
                                 my $newlauncher = &launch_check($r->uri,$symb);
@@ -156,22 +173,39 @@
                                 } else {
                                     unshift(@possroles,'cc');
                                 }
-                                my %roleshash = &Apache::lonnet::get_my_roles($env{'user.uname'},
-                                                                              $env{'user.domain'},
-                                                                              'userroles',undef,
-                                                                              \@possroles,[$cdom],1);
-                                my (%possroles,$hassection);
+                                my %roleshash =
+                                    &Apache::lonnet::get_my_roles($env{'user.uname'},$env{'user.domain'},
+                                                                  'userroles',['previous','active','future'],
+                                                                  \@possroles,[$cdom],1);
+                                my (%possroles,$hassection,%active,%expired,%future);
                                 if (keys(%roleshash)) {
+                                    my $now = time;
                                     foreach my $entry (keys(%roleshash)) {
                                         if ($entry =~ /^\Q$cnum:$cdom:\E([^:]+):([^:]*)$/) {
-                                            $possroles{$1} = $2;
-                                            if ($2 ne '') {
+                                            my ($role,$sec) = ($1,$2);
+                                            $possroles{$role} = $sec;
+                                            if ($sec ne '') {
                                                 $hassection = 1;
                                             }
+                                            my ($tstart,$tend)=split(/\:/,$roleshash{$entry});
+                                            my $status = 'active';
+                                            if (($tend) && ($tend<=$now)) {
+                                                $status = 'previous';
+                                            }
+                                            if (($tstart) && ($now<$tstart)) {
+                                                $status = 'future';
+                                            }
+                                            if ($status eq 'active') {
+                                                $active{$role} = $sec;
+                                            } elsif ($status eq 'previous') {
+                                                $expired{$tend} = $role.':'.$sec;
+                                            } elsif ($status eq 'future') {
+                                                $future{$tstart} = $role.':'.$sec;
+                                            }
                                         }
                                     }
                                 }
-                                my @allposs = keys(%possroles);
+                                my @allposs = keys(%active);
                                 if ($env{'request.lti.login'}) {
                                     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
                                     if ($env{'request.lti.target'} eq '') {
@@ -188,7 +222,7 @@
                                             }
                                             if ($env{"form.$newrole"}) {
                                                 my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
-                                                                   '&destinationurl='.&HTML::Entities::encode($r->uri);
+                                                                   '&destinationurl='.&HTML::Entities::encode($r->uri,'&<>"');
                                                 if ($env{'form.ltitarget'} eq 'iframe') {
                                                     $destination .= '&ltitarget=iframe';
                                                 }
@@ -199,19 +233,19 @@
                                     }
                                 }
                                 if (@allposs == 0) {
-                                    &show_roles($r,\%crsenv,\%possroles);
+                                    &show_roles($r,\%crsenv,\%active,'','',\%future,\%expired,$linkprot,$linkprotuser,$ltoken);
                                 } elsif (@allposs == 1) {
                                     my $newrole = "$allposs[0]./$cdom/$cnum";
                                     $newrole = "$allposs[0]./$cdom/$cnum";
                                     if ($possroles{$allposs[0]} ne '') {
-                                        $newrole .= "/$possroles{$allposs[0]}"; 
+                                        $newrole .= "/$possroles{$allposs[0]}";
                                     }
                                     my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
-                                                       '&destinationurl='.&HTML::Entities::encode($r->uri);
+                                                       '&destinationurl='.&HTML::Entities::encode($r->uri,'&<>"');
                                     if ($env{'form.ttoken'}) {
                                         $destination .= '&ttoken='.$env{'form.ttoken'};
                                     }
-                                    &do_redirect($r,$destination);
+                                    &do_redirect($r,$destination,$linkprot);
                                 } elsif (keys(%possroles) > 1) {
                                     if (grep(/^(cc|co)$/, at allposs)) {
                                         my $newrole;
@@ -222,11 +256,11 @@
                                         }
                                         $newrole .= "./$cdom/$cnum";
                                         my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
-                                                           '&destinationurl='.&HTML::Entities::encode($r->uri);
+                                                           '&destinationurl='.&HTML::Entities::encode($r->uri,'&<>"');
                                         if ($env{'form.ttoken'}) {
                                             $destination .= '&ttoken='.$env{'form.ttoken'};
                                         }
-                                        &do_redirect($r,$destination);
+                                        &do_redirect($r,$destination,$linkprot);
                                     } else {
                                         my $hascustom;
                                         if (grep(/^cr\//, at allposs)) {
@@ -265,8 +299,7 @@
                     $link_info{'linkkey'} = $ltoken_info{'linkkey'};
                 }
             }
-            &Apache::lonnet::tmpdel($env{'form.ltoken'});
-            delete($env{'form.ltoken'});
+            &Apache::lonnet::tmpdel($link_info{'ltoken'});
         }
         if ($link_info{'linkprot'}) {
             ($linkprotector,$linkproturi) = split(/:/,$link_info{'linkprot'},2);
@@ -401,28 +434,35 @@
 }
 
 sub do_redirect {
-    my ($r,$destination) = @_;
+    my ($r,$destination,$linkprot) = @_;
     my $windowname = 'loncapaclient';
     if ($env{'request.lti.login'}) {
         $windowname .= 'lti';
     }
     my $header = '<meta HTTP-EQUIV="Refresh" CONTENT="0; url='.$destination.'" />';
     my $args = {'bread_crumbs' => [{'href' => '','text' => 'Role initialization'},],};
+    if ($linkprot) {
+        $args = {'only_body' => 1,
+                 'redirect'  => [0,$destination],};
+    }
     &Apache::loncommon::content_type($r,'text/html');
     $r->send_http_header;
-    $r->print(&Apache::loncommon::start_page('Valid link',$header,$args).
-              &Apache::lonhtmlcommon::scripttag('self.name="'.$windowname.'";').
-              '<h1>'.&mt('Welcome').'</h1>'.
-              '<p>'.&mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>').'</p><p>'.
-              '<a href="'.$destination.'">'.&mt('Continue').'</a></p>'.
-              &Apache::loncommon::end_page());
+    if ($linkprot) {
+        $r->print(&Apache::loncommon::start_page('Valid link','',$args).
+                  &Apache::loncommon::end_page());
+    } else {
+        $r->print(&Apache::loncommon::start_page('Valid link',$header,$args).
+                  &Apache::lonhtmlcommon::scripttag('self.name="'.$windowname.'";').
+                  '<h1>'.&mt('Welcome').'</h1>'.
+                  '<p>'.&mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>').'</p><p>'.
+                  '<a href="'.$destination.'">'.&mt('Continue').'</a></p>'.
+                  &Apache::loncommon::end_page());
+    }
     return;
 }
 
 sub show_roles {
-    my ($r,$crsenv,$possroles,$hassection,$hascustom) = @_;
-    &Apache::loncommon::content_type($r,'text/html');
-    $r->send_http_header;
+    my ($r,$crsenv,$possroles,$hassection,$hascustom,$futureroles,$expiredroles,$linkprot,$linkprotuser,$ltoken) = @_;
     my ($crsdesc,$crstype,$cdom,$cnum,$header,$title,$preamble,$datatable,$js,$args);
     if (ref($crsenv) eq 'HASH') {
         $crsdesc = $crsenv->{'description'};
@@ -441,6 +481,9 @@
     if (ref($possroles) eq 'HASH') {
         if (keys(%{$possroles}) > 0) {
             $args = {'bread_crumbs' => [{'href' => '','text' => "Choose role in $lc_crstype"},],};
+            if ($linkprot) {
+                $args = {'only_body' => 1};
+            }
             $title = 'Choose a role'; #Do not localize.
             if ($crstype eq 'Community') {
                 $preamble = &mt('You have the following active roles in this community:');
@@ -450,7 +493,7 @@
             $datatable = '<form name="" action="/adm/roles">'.
                          '<input type="hidden" name="newrole" value="" />'."\n".
                          '<input type="hidden" name="selectrole" value="1" />'."\n".
-                         '<input type="hidden" name="destinationurl" value="'.$r->uri.'" />'."\n";
+                         '<input type="hidden" name="destinationurl" value="'.&HTML::Entities::encode($r->uri,'&<>"').'" />'."\n";
             if ($env{'form.ttoken'}) {
                 $datatable .= '<input type="hidden" name="ttoken" value="'.$env{'form.ttoken'}.'" />'."\n";
             }
@@ -532,9 +575,54 @@
 </script>
 ENDJS
         } else {
-            $title = 'No active role';
-            $preamble = &mt("You have no active roles in this $lc_crstype so the page is currently unavailable to you.");
-            $args = {'bread_crumbs' => [{'href' => '','text' => 'Role status'},],};
+            if ($linkprot) {
+                $title = 'No access';
+                $preamble = '<p>'.&mt('Access unavailable for this LON-CAPA content.').'</p>';
+                $args->{'only_body'} = 1;
+            } else {
+                $title = 'No active role';
+                $preamble = '<p>'.&mt("You have no active roles in this $lc_crstype so the page is currently unavailable to you.").'</p>';
+                $args = {'bread_crumbs' => [{'href' => '','text' => 'Role status'},],};
+            }
+            $header = &mt('No access for: [_1]','<b>'.&Apache::loncommon::plainname($env{'user.name'},
+                                                                                    $env{'user.domain'}).'</b>');
+            if ((ref($futureroles) eq 'HASH') && (keys(%{$futureroles}) > 0)) {
+                my @future = sort { $a <=> $b } (keys(%{$futureroles}));
+                $preamble .= '<p>'.&mt('Access will begin: [_1].',&Apache::lonlocal::locallocaltime($future[0])).
+                             ' '.&mt('Please try again then.').'</p>';
+            } elsif ((ref($expiredroles) eq 'HASH') && (keys(%{$expiredroles}) > 0)) {
+                my @expired = sort { $b <=> $a } (keys(%{$expiredroles}));
+                $preamble .= '<p>'.&mt('Access ended: [_1].',&Apache::lonlocal::locallocaltime($expired[0])).'</p>';
+            } elsif ($linkprot) {
+                if ($linkprotuser) {
+                    my ($uname,$udom) = split(/:/,$linkprotuser,2);
+                    $preamble .= '<p>'.&mt('As you followed a link from another system, while logged into that other system with the username: [_1], it is recommended that you contact your instructor.','<i>'.$uname.'</i>').'</p>';
+                } else {
+                    my $relogin;
+                    my %data = (
+                                origurl => $r->uri,
+                                linkprot => $linkprot,
+                    );
+                    my $token =
+                        &Apache::lonnet::tmpput(\%data,$r->dir_config('lonHostID'),'retry');
+                    unless (($token eq 'con_lost') || ($token eq 'refused') || ($token =~ /^error:/) ||
+                            ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
+                            $relogin = '/adm/relaunch?rtoken='.$token;
+                    }
+                    $preamble .= '<p>'.&mt('You might try logging in with a different username and/or domain.').' '.
+                                       &mt('You are currently logged in as: [_1] in domain: [_2]',
+                                           '<i>'.$env{'user.name'}.'</i>','<i>'.$env{'user.domain'}.'</i>').'</p>';
+                    if ($relogin) {
+                        $preamble .= '<p>'.&mt('[_1]Log-in again[_2]','<a href="'.$relogin.'" target="_self">','</a>').'</p>';
+                    }
+                }
+            }
+            if ($env{'form.ttoken'}) {
+                &Apache::lonnet::tmpdel($env{'form.ttoken'});
+            }
+            if ($ltoken) {
+                &Apache::lonnet::tmpdel($ltoken);
+            }
         }
     }
     &Apache::loncommon::content_type($r,'text/html');
Index: loncom/lti/ltiauth.pm
diff -u loncom/lti/ltiauth.pm:1.37 loncom/lti/ltiauth.pm:1.38
--- loncom/lti/ltiauth.pm:1.37	Sat Jun 18 02:10:19 2022
+++ loncom/lti/ltiauth.pm	Sun Jun 26 04:03:48 2022
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Basic LTI Authentication Module
 #
-# $Id: ltiauth.pm,v 1.37 2022/06/18 02:10:19 raeburn Exp $
+# $Id: ltiauth.pm,v 1.38 2022/06/26 04:03:48 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1074,6 +1074,7 @@
         &Apache::lonauth::success($r,$uname,$cdom,$uhome,'noredirect');
         $env{'form.origurl'} = $dest;
         $env{'request.linkprot'} = $itemid.$ltitype.':'.$dest;
+        $env{'request.linkprotuser'} = $uname.':'.$cdom;
         $env{'request.deeplink.login'} = $dest;
         my $redirecturl = '/adm/switchserver';
         if ($otherserver ne '') {
@@ -1089,6 +1090,7 @@
                   'username'       => $uname,
                   'server'         => $lonhost,
                   'linkprot'       => $itemid.$ltitype.':'.$dest,
+                  'linkprotuser'   => $uname.':'.$cdom,
                   'home'           => $uhome,
                   'origurl'        => $dest,
                   'deeplink.login' => $dest,


More information about the LON-CAPA-cvs mailing list