File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.32: download - view: text, annotated - select for diffs
Fri Dec 21 16:57:54 2001 UTC (22 years, 5 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
pod; remove void map -Scott Harrison

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

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