File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.12: download - view: text, annotated - select for diffs
Thu Oct 5 19:15:34 2000 UTC (23 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Don't log an error message if there is no error

    1: # The LearningOnline Network with CAPA
    2: # User Roles Screen
    3: # (Directory Indexer
    4: # (Login Screen
    5: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14 Gerd Kortemeyer)
    6: # 11/23 Gerd Kortemeyer)
    7: # 1/14,03/06,06/01,07/22,07/24,07/25,
    8: # 09/04,09/06,09/28,09/29,09/30,10/2,10/5 Gerd Kortemeyer
    9: #
   10: package Apache::lonroles;
   11: 
   12: use strict;
   13: use Apache::lonnet();
   14: use Apache::lonuserstate();
   15: use Apache::Constants qw(:common);
   16: use Apache::File();
   17: 
   18: sub handler {
   19: 
   20:     my $r = shift;
   21: 
   22:     my $now=time;
   23:     my $then=$ENV{'user.login.time'};
   24:     my $envkey;
   25: 
   26: 
   27: # ================================================================== Roles Init
   28: 
   29:     if ($ENV{'form.selectrole'}) {
   30:        foreach $envkey (keys %ENV) {
   31:           if ($envkey=~/^user\.role\./) {
   32: 	    my ($dum1,$dum2,$role,@pwhere)=split(/\./,$envkey);
   33:             my $where=join('.',@pwhere);
   34:             my $trolecode=$role.'.'.$where;
   35:             if ($ENV{'form.'.$trolecode}) {
   36:                my ($tstart,$tend)=split(/\./,$ENV{$envkey});
   37:                my $tstatus='is';
   38:                if ($tstart) {
   39:       		  if ($tstart>$then) { 
   40:                      $tstatus='future';
   41:                   }
   42:                }
   43:                if ($tend) {
   44:                   if ($tend<$then) { $tstatus='expired'; }
   45:                   if ($tend>$now) { $tstatus='will_not'; }
   46:                }
   47:                if ($tstatus eq 'is') {
   48:                    &Apache::lonnet::appenv('request.role' => $trolecode);
   49:                    my ($cdom,$cnum)=split(/\//,$where);
   50:                    if ($cnum) {
   51: 		      &Apache::lonuserstate::readmap($where);
   52:                       if ($ENV{'form.orgurl'}) {
   53:                          $r->internal_redirect($ENV{'form.orgurl'});
   54:                          return OK;
   55: 		      }
   56:                    }
   57:                }
   58:             } 
   59: 	  }
   60:         }
   61:     }
   62:         
   63: 
   64: # =============================================================== No Roles Init
   65: 
   66:     $r->content_type('text/html');
   67:     $r->send_http_header;
   68:     return OK if $r->header_only;
   69: 
   70:     $r->print(<<ENDHEADER);
   71: <html>
   72: <head>
   73: <title>LON-CAPA User Roles</title>
   74: </head><body bgcolor="#FFFFFF">
   75: ENDHEADER
   76: 
   77: # ------------------------------------------ Get Error Message from Environment
   78: 
   79:     my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$ENV{'user.error.msg'});
   80:     if ($ENV{'user.error.msg'}) {
   81:        $r->log_reason(
   82:      "$msg for $ENV{'user.name'} domain $ENV{'user.domain'} access $priv",$fn);
   83:     }
   84: 
   85: # ---------------------------------------------------------------- Who is this?
   86: 
   87:     my $advanced=0;
   88:     foreach $envkey (keys %ENV) {
   89:         if ($envkey=~/^user\.role\./) {
   90: 	    my ($dum1,$dum2,$role,@pwhere)=split(/\./,$envkey);
   91:             if ($role ne 'st') { $advanced=1; }
   92:         }
   93:     }
   94: 
   95: # ---------------------------------------------- Get cached course descriptions
   96: 
   97:     my %cdes=Apache::lonnet::dump('coursedescriptions');
   98: 
   99: # -------------------------------------------------------- Generate Page Output
  100: # --------------------------------------------------------------- Error Header?
  101:     if ($error) {
  102: 	$r->print("<h1>LON-CAPA Access Control</h1>");
  103:         $r->print("<hr><pre>Access  : ".
  104:                   Apache::lonnet::plaintext($priv)."\n");
  105:         $r->print("Resource: $fn\n");
  106:         $r->print("Action  : $msg\n</pre><hr>");
  107:     } else {
  108:         $r->print("<h1>LON-CAPA User Roles</h1>");
  109:     }
  110: # -------------------------------------------------------- Choice or no choice?
  111:     if ($nochoose) {
  112:         if ($advanced) {
  113: 	   $r->print("<h2>Assigned User Roles</h2>\n");
  114:         } else {
  115:            $r->print("<h2>Sorry ...</h2>\nThis resource might be part of");
  116:            if ($ENV{'request.course.id'}) {
  117: 	       $r->print(' another');
  118:            } else {
  119:                $r->print(' a certain');
  120:            } 
  121:            $r->print(' course.</body></html>');
  122:            return OK;
  123:         } 
  124:     } else {
  125:         if ($advanced) {
  126:            $r->print("<h2>Select a User Role</h2>\n");
  127:         } else {
  128: 	   $r->print("<h2>Enter a Course</h2>\n");
  129:         }
  130:         $r->print('<form method=post action="'.(($fn)?$fn:$r->uri).'">');
  131:         $r->print('<input type=hidden name=orgurl value="'.$fn.'">');
  132:         $r->print('<input type=hidden name=selectrole value=1>');
  133:     }
  134: # ----------------------------------------------------------------------- Table
  135:     $r->print('<table><tr>');
  136:     unless ($nochoose) { $r->print('<th>&nbsp;</th>'); }
  137:     if ($advanced) {
  138:        $r->print('<th>User Role</th><th colspan=2>Extent</th>'.
  139:                  '<th>Start</th><th>End</th><th>Remark</th></tr>'."\n");
  140:     } else {
  141: 	$r->print('<th>Course</th></tr>'."\n");
  142:     }
  143: 
  144:     foreach $envkey (sort keys %ENV) {
  145:         if ($envkey=~/^user\.role\./) {
  146: 	    my ($dum1,$dum2,$role,@pwhere)=split(/\./,$envkey);
  147:             my $where=join('.',@pwhere);
  148:             my $trolecode=$role.'.'.$where;
  149:             my ($tstart,$tend)=split(/\./,$ENV{$envkey});
  150:             my $tremark='';
  151:             my $tstatus='is';
  152:             my $tpstart='&nbsp;';
  153:             my $tpend='&nbsp;';
  154:             if ($tstart) {
  155: 		if ($tstart>$then) { 
  156:                    $tstatus='future';
  157:                    if ($tstart<$now) { $tstatus='will'; }
  158:                 }
  159:                 $tpstart=localtime($tstart);
  160:             }
  161:             if ($tend) {
  162:                 if ($tend<$then) { $tstatus='expired'; }
  163:                 if ($tend>$now) { $tstatus='will_not'; }
  164:                 $tpend=localtime($tend);
  165:             }
  166:             if ($ENV{'request.role'} eq $trolecode) {
  167: 		$tstatus='selected';
  168:             }
  169:             my $tbg;
  170:             if ($tstatus eq 'is') {
  171: 		$tbg='#77FF77';
  172:             } elsif ($tstatus eq 'future') {
  173:                 $tbg='#FFFF77';
  174:             } elsif ($tstatus eq 'will') {
  175:                 $tbg='#FFAA77';
  176:                 $tremark.='Active at next login. ';
  177:             } elsif ($tstatus eq 'expired') {
  178:                 $tbg='#FF7777';
  179: 	    } elsif ($tstatus eq 'will_not') {
  180:                 $tbg='#AAFF77';
  181:                 $tremark.='Expired after logout. ';
  182:             } elsif ($tstatus eq 'selected') {
  183:                 $tbg='#33FF33';
  184:                 $tremark.='Currently selected. ';
  185:             }
  186:             my $trole;
  187:             if ($role =~ /^cr\//) {
  188: 	       my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
  189:                $tremark.='<br>Defined by '.$rauthor.' at '.$rdomain.'.';
  190:                $trole=$rrole;
  191: 	    } else {
  192:                $trole=Apache::lonnet::plaintext($role);
  193:             }
  194:             my $ttype;
  195:             my $twhere;
  196:             my ($tdom,$trest)=
  197:                split(/\//,Apache::lonnet::declutter($where));
  198:             if ($trest) {
  199: 		$ttype='Course';
  200:                 my $tcourseid=$tdom.'/'.$trest;
  201:                 if ($cdes{$tcourseid}) {
  202: 		    $twhere=$cdes{$tcourseid};
  203:                 } else {
  204:                     my %newhash=Apache::lonnet::coursedescription($tcourseid);
  205:                     if (%newhash) {
  206: 			$twhere=$newhash{'description'};
  207:                     } else {
  208:                         $twhere='Currently not available';
  209:                     }
  210:                     $cdes{$tcourseid}=$twhere;
  211:                 }     
  212:             } elsif ($tdom) {
  213:                 $ttype='Domain';
  214:                 $twhere=$tdom;
  215:             } else {
  216:                 $ttype='System';
  217:                 $twhere='system wide';
  218:             }
  219:                
  220:             $r->print('<tr bgcolor='.$tbg.'>');
  221:             unless ($nochoose) {
  222: 		if ($tstatus eq 'is') {
  223:                     $r->print('<td><input type=submit value=Select name="'.
  224:                               $trolecode.'"></td>');
  225:                 } else {
  226:                     $r->print('<td>&nbsp;</td>');
  227:                 }
  228:             }
  229:             $r->print('<td>'.$trole.'</td><td>'.
  230: 		      $ttype.'</td><td>'.$twhere.'</td><td>'.$tpstart.
  231:                       '</td><td>'.$tpend.
  232:                       '</td><td>'.$tremark.'&nbsp;</td></tr>'."\n");
  233:         }
  234:     }
  235: 
  236:     $r->print('</table>');
  237:     unless ($nochoose) {
  238: 	$r->print("</form>\n");
  239:     }
  240: # ------------------------------------------------------------ Priviledges Info
  241:   if ($advanced) {
  242:     $r->print('<hr><h2>Priviledges</h2>');
  243: 
  244:     foreach $envkey (sort keys %ENV) {
  245:         if ($envkey=~/^user\.priv\./) {
  246: 	    my ($dum1,$dum2,@pwhere)=split(/\./,$envkey);
  247:             my $where=join('.',@pwhere);
  248:             my $ttype;
  249:             my $twhere;
  250:             my ($tdom,$trest)=
  251:                split(/\//,Apache::lonnet::declutter($where));
  252:             if ($trest) {
  253: 		$ttype='Course';
  254:                 $twhere=$cdes{$tdom.'/'.$trest};
  255:             } elsif ($tdom) {
  256:                 $ttype='Domain';
  257:                 $twhere=$tdom;
  258:             } else {
  259:                 $ttype='System';
  260:                 $twhere='/';
  261:             }
  262:             $r->print("\n<h3>".$ttype.': '.$twhere.'</h3><ul>');
  263:             map {
  264:               if ($_) {
  265: 		  my ($prv,$restr)=split(/\&/,$_);
  266:                   my $trestr='';
  267:                   if ($restr ne 'F') {
  268:                       my $i;
  269:                       $trestr.=' (';
  270:                       for ($i=0;$i<length($restr);$i++) {
  271: 		         $trestr.=
  272:                            Apache::lonnet::plaintext(substr($restr,$i,1));
  273:                          if ($i<length($restr)-1) { $trestr.=', '; }
  274: 		      }
  275:                       $trestr.=')';
  276:                   }
  277:                   $r->print('<li>'.Apache::lonnet::plaintext($prv).$trestr.
  278:                             '</li>');
  279: 	      }
  280:             } sort split(/:/,$ENV{$envkey});
  281:             $r->print('</ul>');
  282:         }
  283:     }
  284:   }
  285: # -------------------------------------------------------------- Debug - remove
  286: 
  287:     $->print("<hr><h1>Debugging</h1><hr>\n");
  288:     
  289:     foreach $envkey (sort keys %ENV) {
  290: 	$r->print("$envkey ---- $ENV{$envkey}<br>");
  291:     }
  292: 
  293: # ------------------------------------------------------------------- End Debug
  294: 
  295:     $r->print("</body></html>\n");
  296:     return OK;
  297: } 
  298: 
  299: 1;
  300: __END__

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