File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.42: download - view: text, annotated - select for diffs
Wed Aug 28 14:11:27 2002 UTC (21 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Convenience links to about-me page, syllabus, and email.

    1: # The LearningOnline Network with CAPA
    2: # User Roles Screen
    3: #
    4: # $Id: lonroles.pm,v 1.42 2002/08/28 14:11:27 www 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:                                "request.role.domain" => $ENV{'user.domain'}); 
   78:         foreach $envkey (keys %ENV) {
   79:             next if ($envkey!~/^user\.role\./);
   80: 	    my (undef,undef,$role,@pwhere)=split(/\./,$envkey);
   81:             my $where=join('.',@pwhere);
   82:             my $trolecode=$role.'.'.$where;
   83:             if ($ENV{'form.'.$trolecode}) {
   84:                my ($tstart,$tend)=split(/\./,$ENV{$envkey});
   85:                my $tstatus='is';
   86:                if ($tstart) {
   87:       		  if ($tstart>$then) { 
   88:                      $tstatus='future';
   89:                   }
   90:                }
   91:                if ($tend) {
   92:                   if ($tend<$then) { $tstatus='expired'; }
   93:                   if ($tend<$now) { $tstatus='will_not'; }
   94:                }
   95:                if ($tstatus eq 'is') {
   96:                    $where=~s/^\///;
   97:                    my ($cdom,$cnum,$csec)=split(/\//,$where);
   98:                    &Apache::lonnet::appenv('request.role'        => $trolecode,
   99:                                            'request.role.domain' => $cdom,
  100:                                            'request.course.sec'  => $csec);
  101:                    my $msg='Entering course ...';
  102:                    if (($cnum) && ($role ne 'ca')) {
  103: 		      my ($furl,$ferr)=
  104: 			  &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
  105:                       if (($ENV{'form.orgurl'}) && 
  106:                           ($ENV{'form.orgurl'}!~/^\/adm\/flip/)) {
  107:                          $r->internal_redirect($ENV{'form.orgurl'});
  108:                          return OK;
  109: 		     } else {
  110:                          unless ($ENV{'request.course.id'}) {
  111:                              &Apache::lonnet::appenv(
  112: 				 "request.course.id"  => $cdom.'_'.$cnum);
  113:                              $furl='/adm/notfound.html';
  114:                              $msg=
  115: 	 '<h1><font color=red>Could not initialize top-level map.</font></h1>';
  116:                           }
  117: 	                 $r->content_type('text/html');
  118:                          &Apache::loncommon::no_cache($r);
  119:                          $r->send_http_header;
  120:                          my $swinfo=&Apache::lonmenu::rawconfig($r);
  121:                          print (<<ENDREDIR);
  122: <head><title>Entering Course</title>
  123: <meta HTTP-EQUIV="Refresh" CONTENT="1; url=$furl">
  124: </head>
  125: <html>
  126: <body bgcolor="#FFFFFF">
  127: <script>
  128: $swinfo
  129: </script>
  130: $msg
  131: </body>
  132: </html>
  133: ENDREDIR
  134:                             return OK;
  135:                      }
  136:                    }
  137:                }
  138:             } 
  139:         }
  140:     }
  141:         
  142: 
  143: # =============================================================== No Roles Init
  144: 
  145:     $r->content_type('text/html');
  146:     &Apache::loncommon::no_cache($r);
  147:     $r->send_http_header;
  148:     return OK if $r->header_only;
  149: 
  150:     my $swinfo=&Apache::lonmenu::rawconfig($r);
  151:     my $bodytag=&Apache::loncommon::bodytag('User Roles');
  152:     $r->print(<<ENDHEADER);
  153: <html>
  154: <head>
  155: <title>LON-CAPA User Roles</title>
  156: </head>
  157: $bodytag
  158: <script>
  159: $swinfo
  160: window.focus();
  161: </script>
  162: ENDHEADER
  163: 
  164: # ------------------------------------------ Get Error Message from Environment
  165: 
  166:     my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$ENV{'user.error.msg'});
  167:     if ($ENV{'user.error.msg'}) {
  168:        $r->log_reason(
  169:      "$msg for $ENV{'user.name'} domain $ENV{'user.domain'} access $priv",$fn);
  170:     }
  171: 
  172: # ---------------------------------------------------------------- Who is this?
  173: 
  174:     my $advanced=0;
  175:     foreach $envkey (keys %ENV) {
  176:         if ($envkey=~/^user\.role\./) {
  177: 	    my (undef,undef,$role,@pwhere)=split(/\./,$envkey);
  178:             if ($role ne 'st') { $advanced=1; }
  179:         }
  180:     }
  181: 
  182: # -------------------------------------------------------- Generate Page Output
  183: # --------------------------------------------------------------- Error Header?
  184:     if ($error) {
  185: 	$r->print("<h1>LON-CAPA Access Control</h1>");
  186:         $r->print("<hr><pre>Access  : ".
  187:                   Apache::lonnet::plaintext($priv)."\n");
  188:         $r->print("Resource: $fn\n");
  189:         $r->print("Action  : $msg\n</pre><hr>");
  190:     } else {
  191:         if ($ENV{'user.error.msg'}) {
  192: 	    $r->print(
  193:  '<h3><font color=red>You need to choose another user role or '.
  194:  'enter a specific course for this function</font></h3>');
  195: 	}
  196:     }
  197: # -------------------------------------------------------- Choice or no choice?
  198:     if ($nochoose) {
  199:         if ($advanced) {
  200: 	   $r->print("<h2>Assigned User Roles</h2>\n");
  201:         } else {
  202:            $r->print("<h2>Sorry ...</h2>\nThis resource might be part of");
  203:            if ($ENV{'request.course.id'}) {
  204: 	       $r->print(' another');
  205:            } else {
  206:                $r->print(' a certain');
  207:            } 
  208:            $r->print(' course.</body></html>');
  209:            return OK;
  210:         } 
  211:     } else {
  212:         if ($advanced) {
  213:            $r->print("Your home server is ".
  214:                      $Apache::lonnet::hostname{&Apache::lonnet::homeserver
  215:                      ($ENV{'user.name'},$ENV{'user.domain'})}.
  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 (undef,undef,$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:                     $button = 0 if ($home ne $r->dir_config('lonHostID'));
  302:                     #next if ($home eq 'no_host');
  303:                     $home = $Apache::lonnet::hostname{$home};
  304:                     $ttype='Construction Space';
  305:                     $twhere='User: '.$trest.'<br />Domain: '.$tdom.'<br />'.
  306:                         ' Server:&nbsp;'.$home;
  307:                     $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
  308:                 } elsif ($role eq 'au') {
  309:                     # Authors
  310:                     my $home = &Apache::lonnet::homeserver
  311:                         ($ENV{'user.name'},$ENV{'user.domain'});
  312:                     $button = 0 if ($home ne $r->dir_config('lonHostID'));
  313:                     #next if ($home eq 'no_host');
  314:                     $home = $Apache::lonnet::hostname{$home};
  315:                     $ttype='Construction Space';
  316:                     $twhere='Domain: '.$tdom.'<br />Server:&nbsp;'.$home;
  317:                     $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
  318:                 } elsif ($trest) {
  319:                     $ttype='Course';
  320:                     if ($tsection) {
  321:                         $ttype.='<br>Section/Group: '.$tsection;
  322: 		    }
  323:                     my $tcourseid=$tdom.'_'.$trest;
  324:                     if ($ENV{'course.'.$tcourseid.'.description'}) {
  325:                         $twhere=
  326: &Apache::loncommon::syllabuswrapper($ENV{'course.'.$tcourseid.'.description'},
  327: $trest,$tdom);
  328:                     } else {
  329:                         my %newhash=Apache::lonnet::coursedescription
  330:                             ($tcourseid);
  331:                         if (%newhash) {
  332:                             $twhere=$newhash{'description'};
  333:                         } else {
  334:                             $twhere='Currently not available';
  335:                             $ENV{'course.'.$tcourseid.'.description'}=$twhere;
  336:                         }
  337:                     }
  338: 		    if ($role ne 'st') { $twhere.="<br />Domain:".$tdom; }
  339:                 } elsif ($tdom) {
  340:                     $ttype='Domain';
  341:                     $twhere=$tdom;
  342:                 } else {
  343:                     $ttype='System';
  344:                     $twhere='system wide';
  345:                 }
  346:  
  347: # ----- do not trust the indention below here -----              
  348:                 $r->print('<tr bgcolor='.$tbg.'>');
  349:                 unless ($nochoose) {
  350:                     if (!$button) {
  351:                         $r->print('<td>&nbsp;</td>');
  352:                     } elsif ($tstatus eq 'is') {
  353:                         $r->print('<td><input type=submit value=Select name="'.
  354:                                   $trolecode.'"></td>');
  355:                     } elsif ($ENV{'user.adv'}) {
  356:                         $r->print
  357:                             ('<td><input type=submit value="Re-Initialize"'.
  358:                              ' name="'.$trolecode.'"></td>');
  359:                     } else {
  360:                         $r->print('<td>&nbsp;</td>');
  361:                     }
  362:                 }
  363:             $r->print('<td>'.$trole.'</td><td>'.
  364: 		      $ttype.'</td><td>'.$twhere.'</td><td>'.$tpstart.
  365:                       '</td><td>'.$tpend.
  366:                       '</td><td>'.$tremark.'&nbsp;</td></tr>'."\n");
  367: 	}
  368:         }
  369:     }
  370:     my $tremark='';
  371:     if ($ENV{'request.role'} eq 'cm') {
  372: 	$r->print('<tr bgcolor="#11CC55">');
  373:         $tremark='Currently selected.';
  374:     } else {
  375:         $r->print('<tr bgcolor="#77FF77">');
  376:     }
  377:     unless ($nochoose) {
  378:        if ($ENV{'request.role'} ne 'cm') {
  379:           $r->print('<td><input type=submit value=Select name="cm"></td>');
  380:        } else {
  381:           $r->print('<td>&nbsp;</td>');
  382:        }
  383:     }
  384:     $r->print('<td colspan=5>No role specified'.
  385:                       '</td><td>'.$tremark.'&nbsp;</td></tr>'."\n");
  386: 
  387:     $r->print('</table>');
  388:     unless ($nochoose) {
  389: 	$r->print("</form>\n");
  390:     }
  391: # ------------------------------------------------------------ Privileges Info
  392:   if ($advanced) {
  393:     $r->print('<hr><h2>Current Privileges</h2>');
  394: 
  395:     foreach $envkey (sort keys %ENV) {
  396:         if ($envkey=~/^user\.priv\.$ENV{'request.role'}\./) {
  397:             my $where=$envkey;
  398:             $where=~s/^user\.priv\.$ENV{'request.role'}\.//;
  399:             my $ttype;
  400:             my $twhere;
  401:             my ($tdom,$trest,$tsec)=
  402:                split(/\//,Apache::lonnet::declutter($where));
  403:             if ($trest) {
  404: 	      if ($ENV{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
  405: 	        $ttype='Construction Space';
  406:                 $twhere='User: '.$trest.', Domain: '.$tdom;
  407:               } else {
  408: 		$ttype='Course';
  409:                 $twhere=$ENV{'course.'.$tdom.'_'.$trest.'.description'};
  410:                 if ($tsec) {
  411: 		    $twhere.=' (Section/Group: '.$tsec.')';
  412:                 }
  413: 	      }
  414:             } elsif ($tdom) {
  415:                 $ttype='Domain';
  416:                 $twhere=$tdom;
  417:             } else {
  418:                 $ttype='System';
  419:                 $twhere='/';
  420:             }
  421:             $r->print("\n<h3>".$ttype.': '.$twhere.'</h3><ul>');
  422:             foreach (sort split(/:/,$ENV{$envkey})) {
  423:               if ($_) {
  424: 		  my ($prv,$restr)=split(/\&/,$_);
  425:                   my $trestr='';
  426:                   if ($restr ne 'F') {
  427:                       my $i;
  428:                       $trestr.=' (';
  429:                       for ($i=0;$i<length($restr);$i++) {
  430: 		         $trestr.=
  431:                            Apache::lonnet::plaintext(substr($restr,$i,1));
  432:                          if ($i<length($restr)-1) { $trestr.=', '; }
  433: 		      }
  434:                       $trestr.=')';
  435:                   }
  436:                   $r->print('<li>'.Apache::lonnet::plaintext($prv).$trestr.
  437:                             '</li>');
  438: 	      }
  439:             }
  440:             $r->print('</ul>');
  441:         }
  442:     }
  443:   }
  444: 
  445:     $r->print("</body></html>\n");
  446:     return OK;
  447: } 
  448: 
  449: 1;
  450: __END__
  451: 
  452: =head1 NAME
  453: 
  454: Apache::lonroles - User Roles Screen
  455: 
  456: =head1 SYNOPSIS
  457: 
  458: Invoked by /etc/httpd/conf/srm.conf:
  459: 
  460:  <Location /adm/roles>
  461:  PerlAccessHandler       Apache::lonacc
  462:  SetHandler perl-script
  463:  PerlHandler Apache::lonroles
  464:  ErrorDocument     403 /adm/login
  465:  ErrorDocument	  500 /adm/errorhandler
  466:  </Location>
  467: 
  468: =head1 INTRODUCTION
  469: 
  470: This module enables a user to select what role he wishes to
  471: operate under (instructor, student, teaching assistant, course
  472: coordinator, etc).  These roles are pre-established by the actions
  473: of upper-level users.
  474: 
  475: This is part of the LearningOnline Network with CAPA project
  476: described at http://www.lon-capa.org.
  477: 
  478: =head1 HANDLER SUBROUTINE
  479: 
  480: This routine is called by Apache and mod_perl.
  481: 
  482: =over 4
  483: 
  484: =item *
  485: 
  486: Roles Initialization (yes/no)
  487: 
  488: =item *
  489: 
  490: Get Error Message from Environment
  491: 
  492: =item *
  493: 
  494: Who is this?
  495: 
  496: =item *
  497: 
  498: Generate Page Output
  499: 
  500: =item *
  501: 
  502: Choice or no choice
  503: 
  504: =item *
  505: 
  506: Table
  507: 
  508: =item *
  509: 
  510: Privileges
  511: 
  512: =back
  513: 
  514: =cut

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