File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.11: download - view: text, annotated - select for diffs
Wed Oct 4 15:29:50 2000 UTC (23 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Now does redirect only when necessary, and leaves URL correct after choosing
role for the redirected page

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

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