File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.38: download - view: text, annotated - select for diffs
Tue May 21 13:07:40 2002 UTC (22 years ago) by stredwic
Branches: MAIN
CVS tags: HEAD
See bug 458.  Added true flag to lonnet::homeserver.  Supposed to be used with negative caching to speed up the display of roles page.

    1: # The LearningOnline Network with CAPA
    2: # User Roles Screen
    3: #
    4: # $Id: lonroles.pm,v 1.38 2002/05/21 13:07:40 stredwic 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: # (Directory Indexer
   29: # (Login Screen
   30: # YEAR=1999
   31: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14 Gerd Kortemeyer)
   32: # 11/23 Gerd Kortemeyer)
   33: # YEAR=2000
   34: # 1/14,03/06,06/01,07/22,07/24,07/25,
   35: # 09/04,09/06,09/28,09/29,09/30,10/2,10/5,10/26,10/28,
   36: # 12/08,12/28,
   37: # YEAR=2001
   38: # 01/15/01 Gerd Kortemeyer
   39: # 02/27/01 Scott Harrison
   40: # 03/02,05/03,05/25,05/30,06/01,07/06,08/06 Gerd Kortemeyer
   41: # 12/21 Scott Harrison
   42: # 12/29 Gerd Kortemeyer
   43: #
   44: ###
   45: 
   46: package Apache::lonroles;
   47: 
   48: use strict;
   49: use Apache::lonnet();
   50: use Apache::lonuserstate();
   51: use Apache::Constants qw(:common);
   52: use Apache::File();
   53: use Apache::lonmenu;
   54: use Apache::loncommon;
   55: 
   56: sub handler {
   57: 
   58:     my $r = shift;
   59: 
   60:     my $now=time;
   61:     my $then=$ENV{'user.login.time'};
   62:     my $envkey;
   63: 
   64: 
   65: # ================================================================== Roles Init
   66: 
   67:     if ($ENV{'form.selectrole'}) {
   68: 	if ($ENV{'request.course.id'}) {
   69: 	    my %temp=('logout_'.$ENV{'request.course.id'} => time);
   70: 	    &Apache::lonnet::put('email_status',\%temp);
   71:         }
   72:        &Apache::lonnet::appenv("request.course.id"  => '',
   73:                                "request.course.fn"  => '',
   74:                                "request.course.uri" => '',
   75:                                "request.course.sec" => '',
   76:                                "request.role" => 'cm'); 
   77:         foreach $envkey (keys %ENV) {
   78:          if ($envkey=~/^user\.role\./) {
   79: 	    my ($dum1,$dum2,$role,@pwhere)=split(/\./,$envkey);
   80:             my $where=join('.',@pwhere);
   81:             my $trolecode=$role.'.'.$where;
   82:             if ($ENV{'form.'.$trolecode}) {
   83:                my ($tstart,$tend)=split(/\./,$ENV{$envkey});
   84:                my $tstatus='is';
   85:                if ($tstart) {
   86:       		  if ($tstart>$then) { 
   87:                      $tstatus='future';
   88:                   }
   89:                }
   90:                if ($tend) {
   91:                   if ($tend<$then) { $tstatus='expired'; }
   92:                   if ($tend<$now) { $tstatus='will_not'; }
   93:                }
   94:                if ($tstatus eq 'is') {
   95:                    $where=~s/^\///;
   96:                    my ($cdom,$cnum,$csec)=split(/\//,$where);
   97:                    &Apache::lonnet::appenv('request.role' => $trolecode,
   98:                                            'request.course.sec' => $csec);
   99:                    my $msg='Entering course ...';
  100:                    if (($cnum) && ($role ne 'ca')) {
  101: 		      my ($furl,$ferr)=
  102: 			  &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
  103:                       if (($ENV{'form.orgurl'}) && 
  104:                           ($ENV{'form.orgurl'}!~/^\/adm\/flip/)) {
  105:                          $r->internal_redirect($ENV{'form.orgurl'});
  106:                          return OK;
  107: 		     } else {
  108:                          unless ($ENV{'request.course.id'}) {
  109:                              &Apache::lonnet::appenv(
  110: 				 "request.course.id"  => $cdom.'_'.$cnum);
  111:                              $furl='/adm/notfound.html';
  112:                              $msg=
  113: 	 '<h1><font color=red>Could not initialize top-level map.</font></h1>';
  114:                           }
  115: 	                 $r->content_type('text/html');
  116:                          &Apache::loncommon::no_cache($r);
  117:                          $r->send_http_header;
  118:                          my $swinfo=&Apache::lonmenu::rawconfig($r);
  119:                          print (<<ENDREDIR);
  120: <head><title>Entering Course</title>
  121: <meta HTTP-EQUIV="Refresh" CONTENT="1; url=$furl">
  122: </head>
  123: <html>
  124: <body bgcolor="#FFFFFF">
  125: <script>
  126: $swinfo
  127: </script>
  128: $msg
  129: </body>
  130: </html>
  131: ENDREDIR
  132:                             return OK;
  133:                      }
  134:                    }
  135:                }
  136:             } 
  137: 	  }
  138:         }
  139:    }
  140:         
  141: 
  142: # =============================================================== No Roles Init
  143: 
  144:     $r->content_type('text/html');
  145:     &Apache::loncommon::no_cache($r);
  146:     $r->send_http_header;
  147:     return OK if $r->header_only;
  148: 
  149:     my $swinfo=&Apache::lonmenu::rawconfig($r);
  150:     $r->print(<<ENDHEADER);
  151: <html>
  152: <head>
  153: <title>LON-CAPA User Roles</title>
  154: </head><body bgcolor="#FFFFFF">
  155: <script>
  156: $swinfo
  157: window.focus();
  158: </script>
  159: ENDHEADER
  160: 
  161: # ------------------------------------------ Get Error Message from Environment
  162: 
  163:     my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$ENV{'user.error.msg'});
  164:     if ($ENV{'user.error.msg'}) {
  165:        $r->log_reason(
  166:      "$msg for $ENV{'user.name'} domain $ENV{'user.domain'} access $priv",$fn);
  167:     }
  168: 
  169: # ---------------------------------------------------------------- Who is this?
  170: 
  171:     my $advanced=0;
  172:     foreach $envkey (keys %ENV) {
  173:         if ($envkey=~/^user\.role\./) {
  174: 	    my ($dum1,$dum2,$role,@pwhere)=split(/\./,$envkey);
  175:             if ($role ne 'st') { $advanced=1; }
  176:         }
  177:     }
  178: 
  179: # -------------------------------------------------------- Generate Page Output
  180: # --------------------------------------------------------------- Error Header?
  181:     if ($error) {
  182: 	$r->print("<h1>LON-CAPA Access Control</h1>");
  183:         $r->print("<hr><pre>Access  : ".
  184:                   Apache::lonnet::plaintext($priv)."\n");
  185:         $r->print("Resource: $fn\n");
  186:         $r->print("Action  : $msg\n</pre><hr>");
  187:     } else {
  188:         $r->print("<h1>LON-CAPA User Roles</h1>");
  189:         if ($ENV{'user.error.msg'}) {
  190: 	    $r->print(
  191:  '<h3><font color=red>You need to choose another user role or '.
  192:  'enter a specific course for this function</font></h3>');
  193: 	}
  194:     }
  195: # -------------------------------------------------------- Choice or no choice?
  196:     if ($nochoose) {
  197:         if ($advanced) {
  198: 	   $r->print("<h2>Assigned User Roles</h2>\n");
  199:         } else {
  200:            $r->print("<h2>Sorry ...</h2>\nThis resource might be part of");
  201:            if ($ENV{'request.course.id'}) {
  202: 	       $r->print(' another');
  203:            } else {
  204:                $r->print(' a certain');
  205:            } 
  206:            $r->print(' course.</body></html>');
  207:            return OK;
  208:         } 
  209:     } else {
  210:         if ($advanced) {
  211:            $r->print
  212:                ("<h2>$ENV{'user.name'}, Please Select a Role</h2>\n");
  213:            $r->print("Your home server is ".
  214:                      $Apache::lonnet::hostname{&Apache::lonnet::homeserver
  215:                      ($ENV{'user.name'},$ENV{'user.domain'}, 'true')}.
  216: 		     "<br />\n");
  217:            $r->print("Author and Co-Author roles may not be available on ".
  218:                      "servers other than your home server.");
  219:         } else {
  220: 	   $r->print("<h2>Enter a Course</h2>\n");
  221:         }
  222:         if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
  223:     	    $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
  224:         }
  225:         $r->print('<form method=post action="'.(($fn)?$fn:$r->uri).'">');
  226:         $r->print('<input type=hidden name=orgurl value="'.$fn.'">');
  227:         $r->print('<input type=hidden name=selectrole value=1>');
  228:     }
  229:     $r->print('<br>Show all roles: <input type=checkbox name=showall');
  230:     if ($ENV{'form.showall'}) { $r->print(' checked'); }
  231:     $r->print('><input type=submit value="Display"><br>');
  232: # ----------------------------------------------------------------------- Table
  233:     $r->print('<table><tr>');
  234:     unless ($nochoose) { $r->print('<th>&nbsp;</th>'); }
  235:        $r->print('<th>User Role</th><th colspan=2>Extent</th>'.
  236:                  '<th>Start</th><th>End</th><th>Remark</th></tr>'."\n");
  237: 
  238:     foreach $envkey (sort keys %ENV) {
  239:         my $button = 1;
  240:         if ($envkey=~/^user\.role\./) {
  241: 	    my ($dum1,$dum2,$role,@pwhere)=split(/\./,$envkey);
  242:             my $where=join('.',@pwhere);
  243:             my $trolecode=$role.'.'.$where;
  244:             my ($tstart,$tend)=split(/\./,$ENV{$envkey});
  245:             my $tremark='';
  246:             my $tstatus='is';
  247:             my $tpstart='&nbsp;';
  248:             my $tpend='&nbsp;';
  249:             if ($tstart) {
  250: 		if ($tstart>$then) { 
  251:                     $tstatus='future';
  252:                     if ($tstart<$now) { $tstatus='will'; }
  253:                 }
  254:                 $tpstart=localtime($tstart);
  255:             }
  256:             if ($tend) {
  257:                 if ($tend<$then) { 
  258:                     $tstatus='expired'; 
  259:                 } elsif ($tend<$now) { 
  260:                     $tstatus='will_not'; 
  261:                 }
  262:                 $tpend=localtime($tend);
  263:             }
  264:             if ($ENV{'request.role'} eq $trolecode) {
  265: 		$tstatus='selected';
  266:             }
  267:             my $tbg;
  268:             if (($tstatus eq 'is') || ($tstatus eq 'selected') ||
  269:                 ($ENV{'form.showall'})) {
  270:                 if ($tstatus eq 'is') {
  271:                     $tbg='#77FF77';
  272:                 } elsif ($tstatus eq 'future') {
  273:                     $tbg='#FFFF77';
  274:                 } elsif ($tstatus eq 'will') {
  275:                     $tbg='#FFAA77';
  276:                     $tremark.='Active at next login. ';
  277:                 } elsif ($tstatus eq 'expired') {
  278:                     $tbg='#FF7777';
  279:                 } elsif ($tstatus eq 'will_not') {
  280:                     $tbg='#AAFF77';
  281:                     $tremark.='Expired after logout. ';
  282:                 } elsif ($tstatus eq 'selected') {
  283:                     $tbg='#11CC55';
  284:                     $tremark.='Currently selected. ';
  285:                 }
  286:                 my $trole;
  287:                 if ($role =~ /^cr\//) {
  288:                     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
  289:                     $tremark.='<br>Defined by '.$rauthor.' at '.$rdomain.'.';
  290:                     $trole=$rrole;
  291:                 } else {
  292:                     $trole=Apache::lonnet::plaintext($role);
  293:                 }
  294:                 my $ttype;
  295:                 my $twhere;
  296:                 my ($tdom,$trest,$tsection)=
  297:                     split(/\//,Apache::lonnet::declutter($where));
  298:                 # First, Co-Authorship roles
  299:                 if ($role eq 'ca') {
  300:                     my $home = &Apache::lonnet::homeserver($trest,$tdom, 
  301: 							   'true');
  302:                     $button = 0 if ($home ne $r->dir_config('lonHostID'));
  303:                     #next if ($home eq 'no_host');
  304:                     $home = $Apache::lonnet::hostname{$home};
  305:                     $ttype='Construction Space';
  306:                     $twhere='User: '.$trest.'<br />Domain: '.$tdom.'<br />'.
  307:                         ' Server:&nbsp;'.$home;
  308:                     $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
  309:                 } elsif ($role eq 'au') {
  310:                     # Authors
  311:                     my $home = &Apache::lonnet::homeserver
  312:                         ($ENV{'user.name'},$ENV{'user.domain'}, 'true');
  313:                     $button = 0 if ($home ne $r->dir_config('lonHostID'));
  314:                     #next if ($home eq 'no_host');
  315:                     $home = $Apache::lonnet::hostname{$home};
  316:                     $ttype='Construction Space';
  317:                     $twhere='Domain: '.$tdom.'<br />Server:&nbsp;'.$home;
  318:                     $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
  319:                 } elsif ($trest) {
  320:                     $ttype='Course';
  321:                     if ($tsection) {
  322:                         $ttype.='<br>Section/Group: '.$tsection;
  323: 		    }
  324:                     my $tcourseid=$tdom.'_'.$trest;
  325:                     if ($ENV{'course.'.$tcourseid.'.description'}) {
  326:                         $twhere=$ENV{'course.'.$tcourseid.'.description'};
  327:                     } else {
  328:                         my %newhash=Apache::lonnet::coursedescription
  329:                             ($tcourseid);
  330:                         if (%newhash) {
  331:                             $twhere=$newhash{'description'};
  332:                         } else {
  333:                             $twhere='Currently not available';
  334:                             $ENV{'course.'.$tcourseid.'.description'}=$twhere;
  335:                         }
  336:                     }
  337: 		    if ($role ne 'st') { $twhere.="<br />Domain:".$tdom; }
  338:                 } elsif ($tdom) {
  339:                     $ttype='Domain';
  340:                     $twhere=$tdom;
  341:                 } else {
  342:                     $ttype='System';
  343:                     $twhere='system wide';
  344:                 }
  345:  
  346: # ----- do not trust the indention below here -----              
  347:                 $r->print('<tr bgcolor='.$tbg.'>');
  348:                 unless ($nochoose) {
  349:                     if (!$button) {
  350:                         $r->print('<td>&nbsp;</td>');
  351:                     } elsif ($tstatus eq 'is') {
  352:                         $r->print('<td><input type=submit value=Select name="'.
  353:                                   $trolecode.'"></td>');
  354:                     } elsif ($ENV{'user.adv'}) {
  355:                         $r->print
  356:                             ('<td><input type=submit value="Re-Initialize"'.
  357:                              ' name="'.$trolecode.'"></td>');
  358:                     } else {
  359:                         $r->print('<td>&nbsp;</td>');
  360:                     }
  361:                 }
  362:             $r->print('<td>'.$trole.'</td><td>'.
  363: 		      $ttype.'</td><td>'.$twhere.'</td><td>'.$tpstart.
  364:                       '</td><td>'.$tpend.
  365:                       '</td><td>'.$tremark.'&nbsp;</td></tr>'."\n");
  366: 	}
  367:         }
  368:     }
  369:     my $tremark='';
  370:     if ($ENV{'request.role'} eq 'cm') {
  371: 	$r->print('<tr bgcolor="#11CC55">');
  372:         $tremark='Currently selected.';
  373:     } else {
  374:         $r->print('<tr bgcolor="#77FF77">');
  375:     }
  376:     unless ($nochoose) {
  377:        if ($ENV{'request.role'} ne 'cm') {
  378:           $r->print('<td><input type=submit value=Select name="cm"></td>');
  379:        } else {
  380:           $r->print('<td>&nbsp;</td>');
  381:        }
  382:     }
  383:     $r->print('<td colspan=5>No role specified'.
  384:                       '</td><td>'.$tremark.'&nbsp;</td></tr>'."\n");
  385: 
  386:     $r->print('</table>');
  387:     unless ($nochoose) {
  388: 	$r->print("</form>\n");
  389:     }
  390: # ------------------------------------------------------------ Privileges Info
  391:   if ($advanced) {
  392:     $r->print('<hr><h2>Current Privileges</h2>');
  393: 
  394:     foreach $envkey (sort keys %ENV) {
  395:         if ($envkey=~/^user\.priv\.$ENV{'request.role'}\./) {
  396:             my $where=$envkey;
  397:             $where=~s/^user\.priv\.$ENV{'request.role'}\.//;
  398:             my $ttype;
  399:             my $twhere;
  400:             my ($tdom,$trest,$tsec)=
  401:                split(/\//,Apache::lonnet::declutter($where));
  402:             if ($trest) {
  403: 	      if ($ENV{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
  404: 	        $ttype='Construction Space';
  405:                 $twhere='User: '.$trest.', Domain: '.$tdom;
  406:               } else {
  407: 		$ttype='Course';
  408:                 $twhere=$ENV{'course.'.$tdom.'_'.$trest.'.description'};
  409:                 if ($tsec) {
  410: 		    $twhere.=' (Section/Group: '.$tsec.')';
  411:                 }
  412: 	      }
  413:             } elsif ($tdom) {
  414:                 $ttype='Domain';
  415:                 $twhere=$tdom;
  416:             } else {
  417:                 $ttype='System';
  418:                 $twhere='/';
  419:             }
  420:             $r->print("\n<h3>".$ttype.': '.$twhere.'</h3><ul>');
  421:             foreach (sort split(/:/,$ENV{$envkey})) {
  422:               if ($_) {
  423: 		  my ($prv,$restr)=split(/\&/,$_);
  424:                   my $trestr='';
  425:                   if ($restr ne 'F') {
  426:                       my $i;
  427:                       $trestr.=' (';
  428:                       for ($i=0;$i<length($restr);$i++) {
  429: 		         $trestr.=
  430:                            Apache::lonnet::plaintext(substr($restr,$i,1));
  431:                          if ($i<length($restr)-1) { $trestr.=', '; }
  432: 		      }
  433:                       $trestr.=')';
  434:                   }
  435:                   $r->print('<li>'.Apache::lonnet::plaintext($prv).$trestr.
  436:                             '</li>');
  437: 	      }
  438:             }
  439:             $r->print('</ul>');
  440:         }
  441:     }
  442:   }
  443: 
  444:     $r->print("</body></html>\n");
  445:     return OK;
  446: } 
  447: 
  448: 1;
  449: __END__
  450: 
  451: =head1 NAME
  452: 
  453: Apache::lonroles - User Roles Screen
  454: 
  455: =head1 SYNOPSIS
  456: 
  457: Invoked by /etc/httpd/conf/srm.conf:
  458: 
  459:  <Location /adm/roles>
  460:  PerlAccessHandler       Apache::lonacc
  461:  SetHandler perl-script
  462:  PerlHandler Apache::lonroles
  463:  ErrorDocument     403 /adm/login
  464:  ErrorDocument	  500 /adm/errorhandler
  465:  </Location>
  466: 
  467: =head1 INTRODUCTION
  468: 
  469: This module enables a user to select what role he wishes to
  470: operate under (instructor, student, teaching assistant, course
  471: coordinator, etc).  These roles are pre-established by the actions
  472: of upper-level users.
  473: 
  474: This is part of the LearningOnline Network with CAPA project
  475: described at http://www.lon-capa.org.
  476: 
  477: =head1 HANDLER SUBROUTINE
  478: 
  479: This routine is called by Apache and mod_perl.
  480: 
  481: =over 4
  482: 
  483: =item *
  484: 
  485: Roles Initialization (yes/no)
  486: 
  487: =item *
  488: 
  489: Get Error Message from Environment
  490: 
  491: =item *
  492: 
  493: Who is this?
  494: 
  495: =item *
  496: 
  497: Generate Page Output
  498: 
  499: =item *
  500: 
  501: Choice or no choice
  502: 
  503: =item *
  504: 
  505: Table
  506: 
  507: =item *
  508: 
  509: Privileges
  510: 
  511: =back
  512: 
  513: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>