File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.24: download - view: text, annotated - select for diffs
Thu May 3 17:54:12 2001 UTC (23 years, 2 months ago) by www
Branches: MAIN
CVS tags: HEAD
Co-Author listed correctly.

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

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