File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.9: download - view: text, annotated - select for diffs
Mon Oct 2 21:34:58 2000 UTC (23 years, 8 months ago) by www
Branches: MAIN
CVS tags: HEAD
Redirects to original page before error/select role

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

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