File:  [LON-CAPA] / loncom / interface / loncreateuser.pm
Revision 1.72: download - view: text, annotated - select for diffs
Wed Dec 10 20:38:09 2003 UTC (20 years, 7 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Added another part of localization.

    1: # The LearningOnline Network with CAPA
    2: # Create a user
    3: #
    4: # $Id: loncreateuser.pm,v 1.72 2003/12/10 20:38:09 sakharuk 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: ###
   29: 
   30: package Apache::loncreateuser;
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::loncreateuser - handler to create users and custom roles
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Apache::loncreateuser provides an Apache handler for creating users,
   41:     editing their login parameters, roles, and removing roles, and
   42:     also creating and assigning custom roles.
   43: 
   44: =head1 OVERVIEW
   45: 
   46: =head2 Custom Roles
   47: 
   48: In LON-CAPA, roles are actually collections of privileges. "Teaching
   49: Assistant", "Course Coordinator", and other such roles are really just
   50: collection of privileges that are useful in many circumstances.
   51: 
   52: Creating custom roles can be done by the Domain Coordinator through
   53: the Create User functionality. That screen will show all privileges
   54: that can be assigned to users. For a complete list of privileges,
   55: please see C</home/httpd/lonTabs/rolesplain.tab>.
   56: 
   57: Custom role definitions are stored in the C<roles.db> file of the role
   58: author.
   59: 
   60: =cut
   61: 
   62: use strict;
   63: use Apache::Constants qw(:common :http);
   64: use Apache::lonnet;
   65: use Apache::loncommon;
   66: use Apache::lonlocal;
   67: 
   68: my $loginscript; # piece of javascript used in two separate instances
   69: my $generalrule;
   70: my $authformnop;
   71: my $authformkrb;
   72: my $authformint;
   73: my $authformfsys;
   74: my $authformloc;
   75: 
   76: BEGIN {
   77:     $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
   78:     my $krbdefdom=$1;
   79:     $krbdefdom=~tr/a-z/A-Z/;
   80:     my %param = ( formname => 'document.cu',
   81:                   kerb_def_dom => $krbdefdom 
   82:                   );
   83: # no longer static due to configurable kerberos defaults
   84: #    $loginscript  = &Apache::loncommon::authform_header(%param);
   85:     $generalrule  = &Apache::loncommon::authform_authorwarning(%param);
   86:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
   87: # no longer static due to configurable kerberos defaults
   88: #    $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
   89:     $authformint  = &Apache::loncommon::authform_internal(%param);
   90:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
   91:     $authformloc  = &Apache::loncommon::authform_local(%param);
   92: }
   93: 
   94: 
   95: # ======================================================= Existing Custom Roles
   96: 
   97: sub my_custom_roles {
   98:     my %returnhash=();
   99:     my %rolehash=&Apache::lonnet::dump('roles');
  100:     foreach (keys %rolehash) {
  101: 	if ($_=~/^rolesdef\_(\w+)$/) {
  102: 	    $returnhash{$1}=$1;
  103: 	}
  104:     }
  105:     return %returnhash;
  106: }
  107: 
  108: # ==================================================== Figure out author access
  109: 
  110: sub authorpriv {
  111:     my ($auname,$audom)=@_;
  112:     if (($auname ne $ENV{'user.name'}) ||
  113:         (($audom ne $ENV{'user.domain'}) &&
  114:          ($audom ne $ENV{'request.role.domain'}))) { return ''; }
  115:     unless (&Apache::lonnet::allowed('cca',$audom)) { return ''; }
  116:     return 1;
  117: }
  118: 
  119: # =================================================================== Phase one
  120: 
  121: sub print_username_entry_form {
  122:     my $r=shift;
  123:     my $defdom=$ENV{'request.role.domain'};
  124:     my @domains = &Apache::loncommon::get_domains();
  125:     my $domform = &Apache::loncommon::select_dom_form($defdom,'ccdomain');
  126:     my $bodytag =&Apache::loncommon::bodytag(
  127:                                   'Create Users, Change User Privileges');
  128:     my $selscript=&Apache::loncommon::studentbrowser_javascript();
  129:     my $sellink=&Apache::loncommon::selectstudent_link
  130:                                         ('crtuser','ccuname','ccdomain');
  131:     my %existingroles=&my_custom_roles();
  132:     my $choice=&Apache::loncommon::select_form('make new role','rolename',
  133: 		('make new role' => 'Generate new role ...',%existingroles));
  134:     my %lt=&Apache::lonlocal::texthash(
  135: 		    'siur'   => "Set Individual User Roles",
  136: 		    'usr'  => "Username",
  137:                     'dom'  => "Domain",
  138:                     'usrr' => "User Roles",
  139:                     'ecrp' => "Edit Custom Role Privileges",
  140:                     'nr'   => "Name of Role",
  141:                     'cre'  => "Custom Role Editor"
  142: 				       );
  143:     $r->print(<<"ENDDOCUMENT");
  144: <html>
  145: <head>
  146: <title>The LearningOnline Network with CAPA</title>
  147: $selscript
  148: </head>
  149: $bodytag
  150: <form action="/adm/createuser" method="post" name="crtuser">
  151: <input type="hidden" name="phase" value="get_user_info">
  152: <h2>$lt{siur}</h2>
  153: <table>
  154: <tr><td>$lt{usr}:</td><td><input type="text" size="15" name="ccuname">
  155: </td><td rowspan="2">$sellink</td></tr><tr><td>
  156: $lt{'dom'}:</td><td>$domform</td></tr>
  157: </table>
  158: <input name="userrole" type="submit" value="$lt{usrr}" />
  159: </form>
  160: <form action="/adm/createuser" method="post" name="docustom">
  161: <input type="hidden" name="phase" value="selected_custom_edit">
  162: <h2>$lt{'ecrp'}</h2>
  163: $lt{'nr'}: $choice <input type="text" size="15" name="newrolename" /><br />
  164: <input name="customeditor" type="submit" value="$lt{'cre'}" />
  165: </body>
  166: </html>
  167: ENDDOCUMENT
  168: }
  169: 
  170: # =================================================================== Phase two
  171: sub print_user_modification_page {
  172:     my $r=shift;
  173:     my $ccuname=$ENV{'form.ccuname'};
  174:     my $ccdomain=$ENV{'form.ccdomain'};
  175: 
  176:     $ccuname=~s/\W//gs;
  177:     $ccdomain=~s/\W//gs;
  178: 
  179:     unless (($ccuname) && ($ccdomain)) {
  180: 	&print_username_entry_form($r);
  181:         return;
  182:     }
  183: 
  184:     my $defdom=$ENV{'request.role.domain'};
  185: 
  186:     my ($krbdef,$krbdefdom) =
  187:        &Apache::loncommon::get_kerberos_defaults($defdom);
  188: 
  189:     my %param = ( formname => 'document.cu',
  190:                   kerb_def_dom => $krbdefdom,
  191:                   kerb_def_auth => $krbdef
  192:                   );
  193:     $loginscript  = &Apache::loncommon::authform_header(%param);
  194:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
  195: 
  196:     $ccuname=~s/\W//g;
  197:     $ccdomain=~s/\W//g;
  198:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
  199:     my $dochead =<<"ENDDOCHEAD";
  200: <html>
  201: <head>
  202: <title>The LearningOnline Network with CAPA</title>
  203: <script type="text/javascript" language="Javascript">
  204: 
  205:     function pclose() {
  206:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
  207:                  "height=350,width=350,scrollbars=no,menubar=no");
  208:         parmwin.close();
  209:     }
  210: 
  211:     $pjump_def
  212: 
  213:     function dateset() {
  214:         eval("document.cu."+document.cu.pres_marker.value+
  215:             ".value=document.cu.pres_value.value");
  216:         pclose();
  217:     }
  218: 
  219: </script>
  220: </head>
  221: ENDDOCHEAD
  222:     $r->print(&Apache::loncommon::bodytag(
  223:                                      'Create Users, Change User Privileges'));
  224:     my $forminfo =<<"ENDFORMINFO";
  225: <form action="/adm/createuser" method="post" name="cu">
  226: <input type="hidden" name="phase"       value="update_user_data">
  227: <input type="hidden" name="ccuname"     value="$ccuname">
  228: <input type="hidden" name="ccdomain"    value="$ccdomain">
  229: <input type="hidden" name="pres_value"  value="" >
  230: <input type="hidden" name="pres_type"   value="" >
  231: <input type="hidden" name="pres_marker" value="" >
  232: ENDFORMINFO
  233:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
  234:     my %incdomains; 
  235:     my %inccourses;
  236:     foreach (values(%Apache::lonnet::hostdom)) {
  237:        $incdomains{$_}=1;
  238:     }
  239:     foreach (keys(%ENV)) {
  240: 	if ($_=~/^user\.priv\.cm\.\/(\w+)\/(\w+)/) {
  241: 	    $inccourses{$1.'_'.$2}=1;
  242:         }
  243:     }
  244:     if ($uhome eq 'no_host') {
  245:         my $home_server_list=
  246:             '<option value="default" selected>default</option>'."\n".
  247:                 &Apache::loncommon::home_server_option_list($ccdomain);
  248:         
  249:     my %lt=&Apache::lonlocal::texthash(
  250:                     'cnu'  => "Create New User",
  251:                     'nu'   => "New User",
  252:                     'id'   => "in domain",
  253:                     'pd'   => "Personal Data",
  254:                     'fn'   => "First Name",
  255:                     'mn'   => "Middle Name",
  256:                     'ln'   => "Last Name",
  257:                     'gen'  => "Generation",
  258:                     'idsn' => "ID/Student Number",
  259:                     'hs'   => "Home Server",
  260:                     'lg'   => "Login Data"
  261: 				       );
  262: 	$r->print(<<ENDNEWUSER);
  263: $dochead
  264: <h1>$lt{'cnu'}</h1>
  265: $forminfo
  266: <h2>$lt{'nu'} "$ccuname" $lt{'id'} $ccdomain</h2>
  267: <script type="text/javascript" language="Javascript">
  268: $loginscript
  269: </script>
  270: <input type='hidden' name='makeuser' value='1' />
  271: <h3>$lt{'pd'}</h3>
  272: <p>
  273: <table>
  274: <tr><td>$lt{'fn'}  </td>
  275:     <td><input type='text' name='cfirst'  size='15' /></td></tr>
  276: <tr><td>$lt{'mn'} </td> 
  277:     <td><input type='text' name='cmiddle' size='15' /></td></tr>
  278: <tr><td>$lt{'ln'}   </td>
  279:     <td><input type='text' name='clast'   size='15' /></td></tr>
  280: <tr><td>$lt{'gen'}  </td>
  281:     <td><input type='text' name='cgen'    size='5'  /></td></tr>
  282: </table>
  283: $lt{'idsn'} <input type='text' name='cstid'   size='15' /></p>
  284: ${'hs'}: <select name="hserver" size="1"> $home_server_list </select>
  285: <hr />
  286: <h3>$lt{'lg'}</h3>
  287: <p>$generalrule </p>
  288: <p>$authformkrb </p>
  289: <p>$authformint </p>
  290: <p>$authformfsys</p>
  291: <p>$authformloc </p>
  292: ENDNEWUSER
  293:     } else { # user already exists
  294:     my %lt=&Apache::lonlocal::texthash(
  295:                     'cup'  => "Change User Privileges",
  296:                     'usr'  => "User",                    
  297:                     'id'   => "in domain",
  298:                     'fn'   => "first name",
  299:                     'mn'   => "middle name",
  300:                     'ln'   => "last name",
  301:                     'gen'  => "generation"
  302: 				       );
  303: 	$r->print(<<ENDCHANGEUSER);
  304: $dochead
  305: <h1>$lt{'cup'}</h1>
  306: $forminfo
  307: <h2>$lt{'usr'} "$ccuname" $lt{'id'} "$ccdomain"</h2>
  308: ENDCHANGEUSER
  309:         # Get the users information
  310:         my %userenv = &Apache::lonnet::get('environment',
  311:                           ['firstname','middlename','lastname','generation'],
  312:                           $ccdomain,$ccuname);
  313:         my %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
  314:         $r->print(<<END);
  315: <hr />
  316: <table border="2">
  317: <tr>
  318: <th>$lt{'fn'}</th><th>$lt{'mn'}</th><th>$lt{'ln'}</th><th>$lt{'gen'}</th>
  319: </tr>
  320: <tr>
  321: END
  322:         foreach ('firstname','middlename','lastname','generation') {
  323:            if (&Apache::lonnet::allowed('mau',$ccdomain)) {
  324:               $r->print(<<"END");            
  325: <td><input type="text" name="c$_" value="$userenv{$_}" size="15" /></td>
  326: END
  327:            } else {
  328:                $r->print('<td>'.$userenv{$_}.'</td>');
  329:            }
  330:         }
  331:       $r->print(<<END);
  332: </tr>
  333: </table>
  334: END
  335:         # Build up table of user roles to allow revocation of a role.
  336:         my ($tmp) = keys(%rolesdump);
  337:         unless ($tmp =~ /^(con_lost|error)/i) {
  338:            my $now=time;
  339: 	   my %lt=&Apache::lonlocal::texthash(
  340: 		    'rer'  => "Revoke Existing Roles",
  341:                     'rev'  => "Revoke",                    
  342:                     'del'  => "Delete",
  343:                     'rol'  => "Role",
  344:                     'ext'  => "Extent",
  345:                     'sta'  => "Start",
  346:                     'end'  => "End"
  347: 				       );
  348:            $r->print(<<END);
  349: <hr />
  350: <h3>$lt{'rer'}</h3>
  351: <table border=2>
  352: <tr><th>$lt{'rev'}</th><th>$lt{'del'}</th><th>$lt{'rol'}</th><th>$lt{'ext'}</th><th>$lt{'sta'}</th><th>$lt{'end'}</th>
  353: END
  354: 	   foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
  355: 				    my $b1=join('_',(split('_',$b))[1,0]);
  356: 				    return $a1 cmp $b1;
  357: 				} keys(%rolesdump)) {
  358:                next if ($area =~ /^rolesdef/);
  359:                my $role = $rolesdump{$area};
  360:                my $thisrole=$area;
  361:                $area =~ s/\_\w\w$//;
  362:                my ($role_code,$role_end_time,$role_start_time) = 
  363:                    split(/_/,$role);
  364: # Is this a custom role? Get role owner and title.
  365: 	       my ($croleudom,$croleuname,$croletitle)=
  366: 	           ($role_code=~/^cr\/(\w+)\/(\w+)\/(\w+)$/);
  367:                my $bgcol='ffffff';
  368:                my $allowed=0;
  369:                my $delallowed=0;
  370:                if ($area =~ /^\/(\w+)\/(\d\w+)/ ) {
  371:                    my ($coursedom,$coursedir) = ($1,$2);
  372:                    # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
  373:                    my %coursedata=
  374:                        &Apache::lonnet::coursedescription($1.'_'.$2);
  375: 		   my $carea;
  376: 		   if (defined($coursedata{'description'})) {
  377: 		       $carea=&mt('Course').': '.$coursedata{'description'}.
  378:                            '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
  379:      &Apache::loncommon::syllabuswrapper('Syllabus',$coursedir,$coursedom);
  380: 		   } else {
  381: 		       $carea=&mt('Unavailable course').': '.$area;
  382: 		   }
  383:                    $inccourses{$1.'_'.$2}=1;
  384:                    if ((&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2)) ||
  385:                        (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
  386:                        $allowed=1;
  387:                    }
  388:                    if ((&Apache::lonnet::allowed('dro',$1)) ||
  389:                        (&Apache::lonnet::allowed('dro',$ccdomain))) {
  390:                        $delallowed=1;
  391:                    }
  392: # - custom role. Needs more info, too
  393: 		   if ($croletitle) {
  394: 		       if (&Apache::lonnet::allowed('ccr',$1.'/'.$2)) {
  395: 			   $allowed=1;
  396: 			   $thisrole.='.'.$role_code;
  397: 		       }
  398: 		   }
  399:                    # Compute the background color based on $area
  400:                    $bgcol=$1.'_'.$2;
  401:                    $bgcol=~s/[^7-9a-e]//g;
  402:                    $bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',2,6);
  403:                    if ($area=~/^\/(\w+)\/(\d\w+)\/(\w+)/) {
  404:                        $carea.='<br>Section/Group: '.$3;
  405:                    }
  406:                    $area=$carea;
  407:                } else {
  408:                    # Determine if current user is able to revoke privileges
  409:                    if ($area=~ /^\/(\w+)\//) {
  410:                        if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
  411:                        (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
  412:                            $allowed=1;
  413:                        }
  414:                        if (((&Apache::lonnet::allowed('dro',$1))  ||
  415:                             (&Apache::lonnet::allowed('dro',$ccdomain))) &&
  416:                            ($role_code ne 'dc')) {
  417:                            $delallowed=1;
  418:                        }
  419:                    } else {
  420:                        if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
  421:                            $allowed=1;
  422:                        }
  423:                    }
  424:                }
  425:                if ($role_code eq 'ca') {
  426:                    $area=~/\/(\w+)\/(\w+)/;
  427: 		   if (&authorpriv($2,$1)) {
  428: 		       $allowed=1;
  429:                    } else {
  430:                        $allowed=0;
  431:                    }
  432:                }
  433:                my $row = '';
  434:                $row.='<tr bgcolor="#'.$bgcol.'"><td>';
  435:                my $active=1;
  436:                $active=0 if (($role_end_time) && ($now>$role_end_time));
  437:                if (($active) && ($allowed)) {
  438:                    $row.= '<input type="checkbox" name="rev:'.$thisrole.'">';
  439:                } else {
  440:                    if ($active) {
  441:                       $row.='&nbsp;';
  442: 		   } else {
  443:                       $row.=&mt('expired or revoked');
  444: 		   }
  445:                }
  446: 	       $row.='</td><td>';
  447:                if ($delallowed) {
  448:                    $row.= '<input type="checkbox" name="del:'.$thisrole.'">';
  449:                } else {
  450:                    $row.='&nbsp;';
  451:                }
  452: 	       my $plaintext='';
  453: 	       unless ($croletitle) {
  454: 		   $plaintext=&Apache::lonnet::plaintext($role_code);
  455: 	       } else {
  456: 	           $plaintext=
  457: 		"Customrole '$croletitle' defined by $croleuname\@$croleudom";
  458: 	       }
  459:                $row.= '</td><td>'.$plaintext.
  460:                       '</td><td>'.$area.
  461:                       '</td><td>'.($role_start_time?localtime($role_start_time)
  462:                                                    : '&nbsp;' ).
  463:                       '</td><td>'.($role_end_time  ?localtime($role_end_time)
  464:                                                    : '&nbsp;' )
  465:                       ."</td></tr>\n";
  466:                $r->print($row);
  467:            } # end of foreach        (table building loop)
  468: 	   $r->print('</table>');
  469:         }  # End of unless
  470: 	my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
  471: 	if ($currentauth=~/^krb(4|5):/) {
  472: 	    $currentauth=~/^krb(4|5):(.*)/;
  473: 	    my $krbdefdom=$1;
  474:             my %param = ( formname => 'document.cu',
  475:                           kerb_def_dom => $krbdefdom 
  476:                           );
  477:             $loginscript  = &Apache::loncommon::authform_header(%param);
  478: 	}
  479: 	# Check for a bad authentication type
  480:         unless ($currentauth=~/^krb(4|5):/ or
  481: 		$currentauth=~/^unix:/ or
  482: 		$currentauth=~/^internal:/ or
  483: 		$currentauth=~/^localauth:/
  484: 		) { # bad authentication scheme
  485: 	    if (&Apache::lonnet::allowed('mau',$ENV{'request.role.domain'})) {
  486: 		$r->print(<<ENDBADAUTH);
  487: <hr />
  488: <script type="text/javascript" language="Javascript">
  489: $loginscript
  490: </script>
  491: <font color='#ff0000'>ERROR:</font>
  492: This user has an unrecognized authentication scheme ($currentauth).
  493: Please specify login data below.
  494: <h3>Login Data</h3>
  495: <p>$generalrule</p>
  496: <p>$authformkrb</p>
  497: <p>$authformint</p>
  498: <p>$authformfsys</p>
  499: <p>$authformloc</p>
  500: ENDBADAUTH
  501:             } else { 
  502:                 # This user is not allowed to modify the users 
  503:                 # authentication scheme, so just notify them of the problem
  504: 		$r->print(<<ENDBADAUTH);
  505: <hr />
  506: <script type="text/javascript" language="Javascript">
  507: $loginscript
  508: </script>
  509: <font color="#ff0000"> ERROR: </font>
  510: This user has an unrecognized authentication scheme ($currentauth).
  511: Please alert a domain coordinator of this situation.
  512: <hr />
  513: ENDBADAUTH
  514:             }
  515:         } else { # Authentication type is valid
  516: 	    my $authformcurrent='';
  517: 	    my $authform_other='';
  518: 	    if ($currentauth=~/^krb(4|5):/) {
  519: 		$authformcurrent=$authformkrb;
  520: 		$authform_other="<p>$authformint</p>\n".
  521:                     "<p>$authformfsys</p><p>$authformloc</p>";
  522: 	    }
  523: 	    elsif ($currentauth=~/^internal:/) {
  524: 		$authformcurrent=$authformint;
  525: 		$authform_other="<p>$authformkrb</p>".
  526:                     "<p>$authformfsys</p><p>$authformloc</p>";
  527: 	    }
  528: 	    elsif ($currentauth=~/^unix:/) {
  529: 		$authformcurrent=$authformfsys;
  530: 		$authform_other="<p>$authformkrb</p>".
  531:                     "<p>$authformint</p><p>$authformloc;</p>";
  532: 	    }
  533: 	    elsif ($currentauth=~/^localauth:/) {
  534: 		$authformcurrent=$authformloc;
  535: 		$authform_other="<p>$authformkrb</p>".
  536:                     "<p>$authformint</p><p>$authformfsys</p>";
  537: 	    }
  538:             $authformcurrent.=' <i>(will override current values)</i><br />';
  539:             if (&Apache::lonnet::allowed('mau',$ENV{'request.role.domain'})) {
  540: 		# Current user has login modification privileges
  541: 		$r->print(<<ENDOTHERAUTHS);
  542: <hr />
  543: <script type="text/javascript" language="Javascript">
  544: $loginscript
  545: </script>
  546: <h3>Change Current Login Data</h3>
  547: <p>$generalrule</p>
  548: <p>$authformnop</p>
  549: <p>$authformcurrent</p>
  550: <h3>Enter New Login Data</h3>
  551: $authform_other
  552: ENDOTHERAUTHS
  553:             }
  554:         }  ## End of "check for bad authentication type" logic
  555:     } ## End of new user/old user logic
  556:     $r->print('<hr /><h3>'.&mt('Add Roles').'</h3>');
  557: #
  558: # Co-Author
  559: # 
  560:     if (&authorpriv($ENV{'user.name'},$ENV{'request.role.domain'}) &&
  561:         ($ENV{'user.name'} ne $ccuname || $ENV{'user.domain'} ne $ccdomain)) {
  562:         # No sense in assigning co-author role to yourself
  563: 	my $cuname=$ENV{'user.name'};
  564:         my $cudom=$ENV{'request.role.domain'};
  565: 	   my %lt=&Apache::lonlocal::texthash(
  566: 		    'cs'   => "Construction Space",
  567:                     'act'  => "Activate",                    
  568:                     'rol'  => "Role",
  569:                     'ext'  => "Extent",
  570:                     'sta'  => "Start",
  571:                     'end'  => "End".
  572:                     'cau'  => "Co-Author",
  573:                     'ssd'  => "Set Start Date",
  574:                     'sed'  => "Set End Date"
  575: 				       );
  576:        $r->print(<<ENDCOAUTH);
  577: <h4>$lt{'cs'}</h4>
  578: <table border=2><tr><th>${'act'}</th><th>${'rol'}</th><th>${'ext'}</th>
  579: <th>$lt{'sta'}</th><th>$lt{'end'}</th></tr>
  580: <tr>
  581: <td><input type=checkbox name="act_$cudom\_$cuname\_ca"></td>
  582: <td>$lt{'cau'}</td>
  583: <td>$cudom\_$cuname</td>
  584: <td><input type=hidden name="start_$cudom\_$cuname\_ca" value=''>
  585: <a href=
  586: "javascript:pjump('date_start','Start Date Co-Author',document.cu.start_$cudom\_$cuname\_ca.value,'start_$cudom\_$cuname\_ca','cu.pres','dateset')">$lt{'ssd'}</a></td>
  587: <td><input type=hidden name="end_$cudom\_$cuname\_ca" value=''>
  588: <a href=
  589: "javascript:pjump('date_end','End Date Co-Author',document.cu.end_$cudom\_$cuname\_ca.value,'end_$cudom\_$cuname\_ca','cu.pres','dateset')">$lt{'sed'}</a></td>
  590: </tr>
  591: </table>
  592: ENDCOAUTH
  593:     }
  594: #
  595: # Domain level
  596: #
  597:     $r->print('<h4>'.&mt('Domain Level').'</h4>'.
  598:     '<table border=2><tr><th>Activate</th><th>Role</th><th>Extent</th>'.
  599:     '<th>Start</th><th>End</th></tr>');
  600:     foreach ( sort( keys(%incdomains))) {
  601: 	my $thisdomain=$_;
  602:         foreach ('dc','li','dg','au','sc') {
  603:             if (&Apache::lonnet::allowed('c'.$_,$thisdomain)) {
  604:                my $plrole=&Apache::lonnet::plaintext($_);
  605: 	       my %lt=&Apache::lonlocal::texthash(
  606:                     'ssd'  => "Set Start Date",
  607:                     'sed'  => "Set End Date"
  608: 				       );
  609:                $r->print(<<ENDDROW);
  610: <tr>
  611: <td><input type=checkbox name="act_$thisdomain\_$_"></td>
  612: <td>$plrole</td>
  613: <td>$thisdomain</td>
  614: <td><input type=hidden name="start_$thisdomain\_$_" value=''>
  615: <a href=
  616: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$thisdomain\_$_.value,'start_$thisdomain\_$_','cu.pres','dateset')">$lt{'ssd'}</a></td>
  617: <td><input type=hidden name="end_$thisdomain\_$_" value=''>
  618: <a href=
  619: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$thisdomain\_$_.value,'end_$thisdomain\_$_','cu.pres','dateset')">$lt{'sed'}</a></td>
  620: </tr>
  621: ENDDROW
  622:             }
  623:         } 
  624:     }
  625:     $r->print('</table>');
  626: #
  627: # Course level
  628: #
  629:     $r->print(&course_level_table(%inccourses));
  630:     $r->print("<hr /><input type=submit value=\"".&mt('Modify User')."\">\n");
  631:     $r->print("</form></body></html>");
  632: }
  633: 
  634: # ================================================================= Phase Three
  635: sub update_user_data {
  636:     my $r=shift;
  637:     my $uhome=&Apache::lonnet::homeserver($ENV{'form.ccuname'},
  638:                                           $ENV{'form.ccdomain'});
  639:     # Error messages
  640:     my $error     = '<font color="#ff0000">Error:</font>';
  641:     my $end       = '</body></html>';
  642:     # Print header
  643:     $r->print(<<ENDTHREEHEAD);
  644: <html>
  645: <head>
  646: <title>The LearningOnline Network with CAPA</title>
  647: </head>
  648: ENDTHREEHEAD
  649:     my $title;
  650:     if (exists($ENV{'form.makeuser'})) {
  651: 	$title='Set Privileges for New User';
  652:     } else {
  653:         $title='Modify User Privileges';
  654:     }
  655:     $r->print(&Apache::loncommon::bodytag($title));
  656:     # Check Inputs
  657:     if (! $ENV{'form.ccuname'} ) {
  658: 	$r->print($error.'No login name specified.'.$end);
  659: 	return;
  660:     }
  661:     if (  $ENV{'form.ccuname'}  =~/\W/) {
  662: 	$r->print($error.'Invalid login name.  '.
  663: 		  'Only letters, numbers, and underscores are valid.'.
  664: 		  $end);
  665: 	return;
  666:     }
  667:     if (! $ENV{'form.ccdomain'}       ) {
  668: 	$r->print($error.'No domain specified.'.$end);
  669: 	return;
  670:     }
  671:     if (  $ENV{'form.ccdomain'} =~/\W/) {
  672: 	$r->print($error.'Invalid domain name.  '.
  673: 		  'Only letters, numbers, and underscores are valid.'.
  674: 		  $end);
  675: 	return;
  676:     }
  677:     if (! exists($ENV{'form.makeuser'})) {
  678:         # Modifying an existing user, so check the validity of the name
  679:         if ($uhome eq 'no_host') {
  680:             $r->print($error.'Unable to determine home server for '.
  681:                       $ENV{'form.ccuname'}.' in domain '.
  682:                       $ENV{'form.ccdomain'}.'.');
  683:             return;
  684:         }
  685:     }
  686:     # Determine authentication method and password for the user being modified
  687:     my $amode='';
  688:     my $genpwd='';
  689:     if ($ENV{'form.login'} eq 'krb') {
  690: 	$amode='krb';
  691: 	$amode.=$ENV{'form.krbver'};
  692: 	$genpwd=$ENV{'form.krbarg'};
  693:     } elsif ($ENV{'form.login'} eq 'int') {
  694: 	$amode='internal';
  695: 	$genpwd=$ENV{'form.intarg'};
  696:     } elsif ($ENV{'form.login'} eq 'fsys') {
  697: 	$amode='unix';
  698: 	$genpwd=$ENV{'form.fsysarg'};
  699:     } elsif ($ENV{'form.login'} eq 'loc') {
  700: 	$amode='localauth';
  701: 	$genpwd=$ENV{'form.locarg'};
  702: 	$genpwd=" " if (!$genpwd);
  703:     } elsif (($ENV{'form.login'} eq 'nochange') ||
  704:              ($ENV{'form.login'} eq ''        )) { 
  705:         # There is no need to tell the user we did not change what they
  706:         # did not ask us to change.
  707:         # If they are creating a new user but have not specified login
  708:         # information this will be caught below.
  709:     } else {
  710: 	    $r->print($error.'Invalid login mode or password'.$end);    
  711: 	    return;
  712:     }
  713:     if ($ENV{'form.makeuser'}) {
  714:         # Create a new user
  715: 	$r->print(<<ENDNEWUSERHEAD);
  716: <h3>Creating user "$ENV{'form.ccuname'}" in domain "$ENV{'form.ccdomain'}"</h3>
  717: ENDNEWUSERHEAD
  718:         # Check for the authentication mode and password
  719:         if (! $amode || ! $genpwd) {
  720: 	    $r->print($error.'Invalid login mode or password'.$end);    
  721: 	    return;
  722: 	}
  723:         # Determine desired host
  724:         my $desiredhost = $ENV{'form.hserver'};
  725:         if (lc($desiredhost) eq 'default') {
  726:             $desiredhost = undef;
  727:         } else {
  728:             my %home_servers = &Apache::loncommon::get_library_servers
  729:                 ($ENV{'form.ccdomain'});  
  730:             if (! exists($home_servers{$desiredhost})) {
  731:                 $r->print($error.'Invalid home server specified');
  732:                 return;
  733:             }
  734:         }
  735: 	# Call modifyuser
  736: 	my $result = &Apache::lonnet::modifyuser
  737: 	    ($ENV{'form.ccdomain'},$ENV{'form.ccuname'},$ENV{'form.cstid'},
  738:              $amode,$genpwd,$ENV{'form.cfirst'},
  739:              $ENV{'form.cmiddle'},$ENV{'form.clast'},$ENV{'form.cgen'},
  740:              undef,$desiredhost
  741: 	     );
  742: 	$r->print('Generating user: '.$result);
  743:         my $home = &Apache::lonnet::homeserver($ENV{'form.ccuname'},
  744:                                                $ENV{'form.ccdomain'});
  745:         $r->print('<br />Home server: '.$home.' '.
  746:                   $Apache::lonnet::libserv{$home});
  747:     } elsif (($ENV{'form.login'} ne 'nochange') &&
  748:              ($ENV{'form.login'} ne ''        )) {
  749: 	# Modify user privileges
  750: 	$r->print(<<ENDMODIFYUSERHEAD);
  751: <h2>User "$ENV{'form.ccuname'}" in domain "$ENV{'form.ccdomain'}"</h2>
  752: ENDMODIFYUSERHEAD
  753:         if (! $amode || ! $genpwd) {
  754: 	    $r->print($error.'Invalid login mode or password'.$end);    
  755: 	    return;
  756: 	}
  757: 	# Only allow authentification modification if the person has authority
  758: 	if (&Apache::lonnet::allowed('mau',$ENV{'form.ccdomain'})) {
  759: 	    $r->print('Modifying authentication: '.
  760:                       &Apache::lonnet::modifyuserauth(
  761: 		       $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
  762:                        $amode,$genpwd));
  763:             $r->print('<br>Home server: '.&Apache::lonnet::homeserver
  764: 		  ($ENV{'form.ccuname'},$ENV{'form.ccdomain'}));
  765: 	} else {
  766: 	    # Okay, this is a non-fatal error.
  767: 	    $r->print($error.'You do not have the authority to modify '.
  768: 		      'this users authentification information.');    
  769: 	}
  770:     }
  771:     ##
  772:     if (! $ENV{'form.makeuser'} ) {
  773:         # Check for need to change
  774:         my %userenv = &Apache::lonnet::get
  775:             ('environment',['firstname','middlename','lastname','generation'],
  776:              $ENV{'form.ccdomain'},$ENV{'form.ccuname'});
  777:         my ($tmp) = keys(%userenv);
  778:         if ($tmp =~ /^(con_lost|error)/i) { 
  779:             %userenv = ();
  780:         }
  781:         # Check to see if we need to change user information
  782:         foreach ('firstname','middlename','lastname','generation') {
  783:             # Strip leading and trailing whitespace
  784:             $ENV{'form.c'.$_} =~ s/(\s+$|^\s+)//g; 
  785:         }
  786:         if (&Apache::lonnet::allowed('mau',$ENV{'form.ccdomain'}) && 
  787:             ($ENV{'form.cfirstname'}  ne $userenv{'firstname'}  ||
  788:              $ENV{'form.cmiddlename'} ne $userenv{'middlename'} ||
  789:              $ENV{'form.clastname'}   ne $userenv{'lastname'}   ||
  790:              $ENV{'form.cgeneration'} ne $userenv{'generation'} )) {
  791:             # Make the change
  792:             my %changeHash;
  793:             $changeHash{'firstname'}  = $ENV{'form.cfirstname'};
  794:             $changeHash{'middlename'} = $ENV{'form.cmiddlename'};
  795:             $changeHash{'lastname'}   = $ENV{'form.clastname'};
  796:             $changeHash{'generation'} = $ENV{'form.cgeneration'};
  797:             my $putresult = &Apache::lonnet::put
  798:                 ('environment',\%changeHash,
  799:                  $ENV{'form.ccdomain'},$ENV{'form.ccuname'});
  800:             if ($putresult eq 'ok') {
  801:             # Tell the user we changed the name
  802:                 $r->print(<<"END");
  803: <table border="2">
  804: <caption>User Information Changed</caption>
  805: <tr><th>&nbsp;</th>
  806:     <th>first</th>
  807:     <th>middle</th>
  808:     <th>last</th>
  809:     <th>generation</th></tr>
  810: <tr><td>Previous</td>
  811:     <td>$userenv{'firstname'}  </td>
  812:     <td>$userenv{'middlename'} </td>
  813:     <td>$userenv{'lastname'}   </td>
  814:     <td>$userenv{'generation'} </td></tr>
  815: <tr><td>Changed To</td>
  816:     <td>$ENV{'form.cfirstname'}  </td>
  817:     <td>$ENV{'form.cmiddlename'} </td>
  818:     <td>$ENV{'form.clastname'}   </td>
  819:     <td>$ENV{'form.cgeneration'} </td></tr>
  820: </table>
  821: END
  822:             } else { # error occurred
  823:                 $r->print("<h2>Unable to successfully change environment for ".
  824:                       $ENV{'form.ccuname'}." in domain ".
  825:                       $ENV{'form.ccdomain'}."</h2>");
  826:             }
  827:         }  else { # End of if ($ENV ... ) logic
  828:             # They did not want to change the users name but we can
  829:             # still tell them what the name is
  830:                 $r->print(<<"END");
  831: <h2>User "$ENV{'form.ccuname'}" in domain "$ENV{'form.ccdomain'}"</h2>
  832: <h4>$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} </h4>
  833: <h4>Generation: $userenv{'generation'}</h4>
  834: END
  835:         }
  836:     }
  837:     ##
  838:     my $now=time;
  839:     $r->print('<h3>Modifying Roles</h3>');
  840:     foreach (keys (%ENV)) {
  841: 	next if (! $ENV{$_});
  842: 	# Revoke roles
  843: 	if ($_=~/^form\.rev/) {
  844: 	    if ($_=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
  845: # Revoke standard role
  846: 	        $r->print('Revoking '.$2.' in '.$1.': <b>'.
  847:                      &Apache::lonnet::revokerole($ENV{'form.ccdomain'},
  848:                      $ENV{'form.ccuname'},$1,$2).'</b><br>');
  849: 		if ($2 eq 'st') {
  850: 		    $1=~/^\/(\w+)\/(\w+)/;
  851: 		    my $cid=$1.'_'.$2;
  852: 		    $r->print('Drop from classlist: <b>'.
  853: 			 &Apache::lonnet::critical('put:'.
  854:                              $ENV{'course.'.$cid.'.domain'}.':'.
  855: 	                     $ENV{'course.'.$cid.'.num'}.':classlist:'.
  856:                          &Apache::lonnet::escape($ENV{'form.ccuname'}.':'.
  857:                              $ENV{'form.ccdomain'}).'='.
  858:                          &Apache::lonnet::escape($now.':'),
  859: 	                     $ENV{'course.'.$cid.'.home'}).'</b><br>');
  860: 		}
  861: 	    } 
  862: 	    if ($_=~/^form\.rev\:([^\_]+)\_cr\.cr\/(\w+)\/(\w+)\/(\w+)$/) {
  863: # Revoke custom role
  864: 		$r->print(
  865:                 'Revoking custom role '.$4.' by '.$3.'@'.$2.' in '.$1.': <b>'.
  866: &Apache::lonnet::revokecustomrole($ENV{'form.ccdomain'},
  867: 				  $ENV{'form.ccuname'},$1,$2,$3,$4).
  868: 		'</b><br>');
  869: 	    }
  870: 	} elsif ($_=~/^form\.del/) {
  871: 	    if ($_=~/^form\.del\:([^\_]+)\_([^\_]+)$/) {
  872: 	        $r->print('Deleting '.$2.' in '.$1.': '.
  873:                      &Apache::lonnet::assignrole($ENV{'form.ccdomain'},
  874:                      $ENV{'form.ccuname'},$1,$2,$now,0,1).'<br>');
  875: 		if ($2 eq 'st') {
  876: 		    $1=~/^\/(\w+)\/(\w+)/;
  877: 		    my $cid=$1.'_'.$2;
  878: 		    $r->print('Drop from classlist: <b>'.
  879: 			 &Apache::lonnet::critical('put:'.
  880:                              $ENV{'course.'.$cid.'.domain'}.':'.
  881: 	                     $ENV{'course.'.$cid.'.num'}.':classlist:'.
  882:                          &Apache::lonnet::escape($ENV{'form.ccuname'}.':'.
  883:                              $ENV{'form.ccdomain'}).'='.
  884:                          &Apache::lonnet::escape($now.':'),
  885: 	                     $ENV{'course.'.$cid.'.home'}).'</b><br>');
  886: 		}
  887: 	    } 
  888: 	} elsif ($_=~/^form\.act/) {
  889: 	    if 
  890: ($_=~/^form\.act\_([^\_]+)\_([^\_]+)\_cr_cr_([^\_]+)_(\w+)_([^\_]+)$/) {
  891:                 # Activate a custom role
  892: 		my $url='/'.$1.'/'.$2;
  893: 		my $full=$1.'_'.$2.'_cr_cr_'.$3.'_'.$4.'_'.$5;
  894: 		if ($ENV{'form.sec_'.$full}) {
  895: 		    $url.='/'.$ENV{'form.sec_'.$full};
  896: 		}
  897: 
  898: 		my $start = ( $ENV{'form.start_'.$full} ? 
  899: 			      $ENV{'form.start_'.$full} : 
  900: 			      $now );
  901: 		my $end   = ( $ENV{'form.end_'.$full} ? 
  902: 			      $ENV{'form.end_'.$full} :
  903: 			      0 );
  904: 
  905:     $r->print('Assigning custom role "'.$5.'" by '.$4.'@'.$3.' in '.$url.
  906:                          ($start?', starting '.localtime($start):'').
  907:                          ($end?', ending '.localtime($end):'').': <b>'.
  908: 	      &Apache::lonnet::assigncustomrole(
  909: 	$ENV{'form.ccdomain'},$ENV{'form.ccuname'},$url,$3,$4,$5,$end,$start).
  910: 	      '</b><br>');
  911: 	    } elsif ($_=~/^form\.act\_([^\_]+)\_([^\_]+)\_([^\_]+)$/) {
  912: 		# Activate roles for sections with 3 id numbers
  913: 		# set start, end times, and the url for the class
  914: 
  915: 		my $start = ( $ENV{'form.start_'.$1.'_'.$2.'_'.$3} ? 
  916: 			      $ENV{'form.start_'.$1.'_'.$2.'_'.$3} : 
  917: 			      $now );
  918: 		my $end   = ( $ENV{'form.end_'.$1.'_'.$2.'_'.$3} ? 
  919: 			      $ENV{'form.end_'.$1.'_'.$2.'_'.$3} :
  920: 			      0 );
  921: 		my $url='/'.$1.'/'.$2;
  922: 		if ($ENV{'form.sec_'.$1.'_'.$2.'_'.$3}) {
  923: 		    $url.='/'.$ENV{'form.sec_'.$1.'_'.$2.'_'.$3};
  924: 		}
  925: 		# Assign the role and report it
  926: 		$r->print('Assigning '.$3.' in '.$url.
  927:                          ($start?', starting '.localtime($start):'').
  928:                          ($end?', ending '.localtime($end):'').': <b>'.
  929:                           &Apache::lonnet::assignrole(
  930:                               $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
  931:                               $url,$3,$end,$start).
  932: 			  '</b><br>');
  933: 		# Handle students differently
  934: 		if ($3 eq 'st') {
  935: 		    $url=~/^\/(\w+)\/(\w+)/;
  936: 		    my $cid=$1.'_'.$2;
  937: 		    $r->print('Add to classlist: <b>'.
  938: 			      &Apache::lonnet::critical(
  939: 				  'put:'.$ENV{'course.'.$cid.'.domain'}.':'.
  940: 	                           $ENV{'course.'.$cid.'.num'}.':classlist:'.
  941:                                    &Apache::lonnet::escape(
  942:                                        $ENV{'form.ccuname'}.':'.
  943:                                        $ENV{'form.ccdomain'} ).'='.
  944:                                    &Apache::lonnet::escape($end.':'.$start),
  945: 				       $ENV{'course.'.$cid.'.home'})
  946: 			      .'</b><br>');
  947: 		}
  948: 	    } elsif ($_=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
  949: 		# Activate roles for sections with two id numbers
  950: 		# set start, end times, and the url for the class
  951: 		my $start = ( $ENV{'form.start_'.$1.'_'.$2} ? 
  952: 			      $ENV{'form.start_'.$1.'_'.$2} : 
  953: 			      $now );
  954: 		my $end   = ( $ENV{'form.end_'.$1.'_'.$2} ? 
  955: 			      $ENV{'form.end_'.$1.'_'.$2} :
  956: 			      0 );
  957: 		my $url='/'.$1.'/';
  958: 		# Assign the role and report it.
  959: 		$r->print('Assigning '.$2.' in '.$url.': '.
  960:                          ($start?', starting '.localtime($start):'').
  961:                          ($end?', ending '.localtime($end):'').': <b>'.
  962:                           &Apache::lonnet::assignrole(
  963:                               $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
  964:                               $url,$2,$end,$start)
  965: 			  .'</b><br>');
  966: 	    } else {
  967: 		$r->print('<p>ERROR: Unknown command <tt>'.$_.'</tt></p><br>');
  968:             }
  969: 	} 
  970:     } # End of foreach (keys(%ENV))
  971:     $r->print('</body></html>');
  972: }
  973: 
  974: # ========================================================== Custom Role Editor
  975: 
  976: sub custom_role_editor {
  977:     my $r=shift;
  978:     my $rolename=$ENV{'form.rolename'};
  979: 
  980:     if ($rolename eq 'make new role') {
  981: 	$rolename=$ENV{'form.newrolename'};
  982:     }
  983: 
  984:     $rolename=~s/[^A-Za-z0-9]//gs;
  985: 
  986:     unless ($rolename) {
  987: 	&print_username_entry_form($r);
  988:         return;
  989:     }
  990: 
  991:     $r->print(&Apache::loncommon::bodytag(
  992:                      'Create Users, Change User Privileges').'<h2>');
  993:     my $syspriv='';
  994:     my $dompriv='';
  995:     my $coursepriv='';
  996:     my ($rdummy,$roledef)=
  997: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
  998: # ------------------------------------------------------- Does this role exist?
  999:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 1000: 	$r->print('Existing Role "');
 1001: # ------------------------------------------------- Get current role privileges
 1002: 	($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 1003:     } else {
 1004: 	$r->print('New Role "');
 1005: 	$roledef='';
 1006:     }
 1007:     $r->print($rolename.'"</h2>');
 1008: # ------------------------------------------------------- What can be assigned?
 1009:     my %full=();
 1010:     my %courselevel=();
 1011:     my %courselevelcurrent=();
 1012:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 1013: 	my ($priv,$restrict)=split(/\&/,$_);
 1014:         unless ($restrict) { $restrict='F'; }
 1015:         $courselevel{$priv}=$restrict;
 1016:         if ($coursepriv=~/\:$priv/) {
 1017: 	    $courselevelcurrent{$priv}=1;
 1018: 	}
 1019: 	$full{$priv}=1;
 1020:     }
 1021:     my %domainlevel=();
 1022:     my %domainlevelcurrent=();
 1023:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
 1024: 	my ($priv,$restrict)=split(/\&/,$_);
 1025:         unless ($restrict) { $restrict='F'; }
 1026:         $domainlevel{$priv}=$restrict;
 1027:         if ($dompriv=~/\:$priv/) {
 1028: 	    $domainlevelcurrent{$priv}=1;
 1029: 	}
 1030: 	$full{$priv}=1;
 1031:     }
 1032:     my %systemlevel=();
 1033:     my %systemlevelcurrent=();
 1034:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
 1035: 	my ($priv,$restrict)=split(/\&/,$_);
 1036:         unless ($restrict) { $restrict='F'; }
 1037:         $systemlevel{$priv}=$restrict;
 1038:         if ($syspriv=~/\:$priv/) {
 1039: 	    $systemlevelcurrent{$priv}=1;
 1040: 	}
 1041: 	$full{$priv}=1;
 1042:     }
 1043: 
 1044:     $r->print(<<ENDCCF);
 1045: <form method="post">
 1046: <input type="hidden" name="phase" value="set_custom_roles" />
 1047: <input type="hidden" name="rolename" value="$rolename" />
 1048: <table border="2">
 1049: <tr><th>Privilege</th><th>Course Level</th><th>Domain Level</th>
 1050: <th>System Level</th></tr>
 1051: ENDCCF
 1052:     foreach (sort keys %full) {
 1053: 	$r->print('<tr><td>'.&Apache::lonnet::plaintext($_).'</td><td>'.
 1054:     ($courselevel{$_}?'<input type="checkbox" name="'.$_.':c" '.
 1055:     ($courselevelcurrent{$_}?'checked="1"':'').' />':'&nbsp;').
 1056:     '</td><td>'.
 1057:     ($domainlevel{$_}?'<input type="checkbox" name="'.$_.':d" '.
 1058:     ($domainlevelcurrent{$_}?'checked="1"':'').' />':'&nbsp;').
 1059:     '</td><td>'.
 1060:     ($systemlevel{$_}?'<input type="checkbox" name="'.$_.':s" '.
 1061:     ($systemlevelcurrent{$_}?'checked="1"':'').' />':'&nbsp;').
 1062:     '</td></tr>');
 1063:     }
 1064:     $r->print(
 1065:    '<table><input type="submit" value="Define Role" /></form></body></html>');
 1066: }
 1067: 
 1068: # ---------------------------------------------------------- Call to definerole
 1069: sub set_custom_role {
 1070:     my $r=shift;
 1071: 
 1072:     my $rolename=$ENV{'form.rolename'};
 1073: 
 1074:     $rolename=~s/[^A-Za-z0-9]//gs;
 1075: 
 1076:     unless ($rolename) {
 1077: 	&print_username_entry_form($r);
 1078:         return;
 1079:     }
 1080: 
 1081:     $r->print(&Apache::loncommon::bodytag(
 1082:                      'Create Users, Change User Privileges').'<h2>');
 1083:     my ($rdummy,$roledef)=
 1084: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
 1085: # ------------------------------------------------------- Does this role exist?
 1086:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 1087: 	$r->print('Existing Role "');
 1088:     } else {
 1089: 	$r->print('New Role "');
 1090: 	$roledef='';
 1091:     }
 1092:     $r->print($rolename.'"</h2>');
 1093: # ------------------------------------------------------- What can be assigned?
 1094:     my $sysrole='';
 1095:     my $domrole='';
 1096:     my $courole='';
 1097: 
 1098:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 1099: 	my ($priv,$restrict)=split(/\&/,$_);
 1100:         unless ($restrict) { $restrict=''; }
 1101:         if ($ENV{'form.'.$priv.':c'}) {
 1102: 	    $courole.=':'.$_;
 1103: 	}
 1104:     }
 1105: 
 1106:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
 1107: 	my ($priv,$restrict)=split(/\&/,$_);
 1108:         unless ($restrict) { $restrict=''; }
 1109:         if ($ENV{'form.'.$priv.':d'}) {
 1110: 	    $domrole.=':'.$_;
 1111: 	}
 1112:     }
 1113: 
 1114:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
 1115: 	my ($priv,$restrict)=split(/\&/,$_);
 1116:         unless ($restrict) { $restrict=''; }
 1117:         if ($ENV{'form.'.$priv.':s'}) {
 1118: 	    $sysrole.=':'.$_;
 1119: 	}
 1120:     }
 1121:     $r->print('<br />Defining Role: '.
 1122: 	   &Apache::lonnet::definerole($rolename,$sysrole,$domrole,$courole));
 1123:     if ($ENV{'request.course.id'}) {
 1124:         my $url='/'.$ENV{'request.course.id'};
 1125:         $url=~s/\_/\//g;
 1126: 	$r->print('<br />Assigning Role to Self: '.
 1127: 	      &Apache::lonnet::assigncustomrole($ENV{'user.domain'},
 1128: 						$ENV{'user.name'},
 1129: 						$url,
 1130: 						$ENV{'user.domain'},
 1131: 						$ENV{'user.name'},
 1132: 						$rolename));
 1133:     }
 1134:     $r->print('</body></html>');
 1135: }
 1136: 
 1137: # ================================================================ Main Handler
 1138: sub handler {
 1139:     my $r = shift;
 1140: 
 1141:     if ($r->header_only) {
 1142:        &Apache::loncommon::content_type($r,'text/html');
 1143:        $r->send_http_header;
 1144:        return OK;
 1145:     }
 1146: 
 1147:     if ((&Apache::lonnet::allowed('cta',$ENV{'request.course.id'})) ||
 1148:         (&Apache::lonnet::allowed('cin',$ENV{'request.course.id'})) || 
 1149:         (&Apache::lonnet::allowed('ccr',$ENV{'request.course.id'})) || 
 1150:         (&Apache::lonnet::allowed('cep',$ENV{'request.course.id'})) ||
 1151:         (&Apache::lonnet::allowed('cca',$ENV{'request.role.domain'})) ||
 1152:         (&Apache::lonnet::allowed('mau',$ENV{'request.role.domain'}))) {
 1153:        &Apache::loncommon::content_type($r,'text/html');
 1154:        $r->send_http_header;
 1155:        unless ($ENV{'form.phase'}) {
 1156: 	   &print_username_entry_form($r);
 1157:        }
 1158:        if ($ENV{'form.phase'} eq 'get_user_info') {
 1159:            &print_user_modification_page($r);
 1160:        } elsif ($ENV{'form.phase'} eq 'update_user_data') {
 1161:            &update_user_data($r);
 1162:        } elsif ($ENV{'form.phase'} eq 'selected_custom_edit') {
 1163:            &custom_role_editor($r);
 1164:        } elsif ($ENV{'form.phase'} eq 'set_custom_roles') {
 1165: 	   &set_custom_role($r);
 1166:        }
 1167:    } else {
 1168:       $ENV{'user.error.msg'}=
 1169:         "/adm/createuser:mau:0:0:Cannot modify user data";
 1170:       return HTTP_NOT_ACCEPTABLE; 
 1171:    }
 1172:    return OK;
 1173: } 
 1174: 
 1175: #-------------------------------------------------- functions for &phase_two
 1176: sub course_level_table {
 1177:     my %inccourses = @_;
 1178:     my $table = '';
 1179: # Custom Roles?
 1180: 
 1181:     my %customroles=&my_custom_roles();
 1182: 
 1183:     foreach (sort( keys(%inccourses))) {
 1184: 	my $thiscourse=$_;
 1185: 	my $protectedcourse=$_;
 1186: 	$thiscourse=~s:_:/:g;
 1187: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
 1188: 	my $area=$coursedata{'description'};
 1189: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$_; }
 1190: 	my $bgcol=$thiscourse;
 1191: 	$bgcol=~s/[^7-9a-e]//g;
 1192: 	$bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',2,6);
 1193: 	foreach  ('st','ta','ep','ad','in','cc') {
 1194: 	    if (&Apache::lonnet::allowed('c'.$_,$thiscourse)) {
 1195: 		my $plrole=&Apache::lonnet::plaintext($_);
 1196: 		$table .= <<ENDEXTENT;
 1197: <tr bgcolor="#$bgcol">
 1198: <td><input type="checkbox" name="act_$protectedcourse\_$_"></td>
 1199: <td>$plrole</td>
 1200: <td>$area</td>
 1201: ENDEXTENT
 1202: 	        if ($_ ne 'cc') {
 1203: 		    $table .= <<ENDSECTION;
 1204: <td><input type="text" size="5" name="sec_$protectedcourse\_$_"></td>
 1205: ENDSECTION
 1206:                 } else { 
 1207: 		    $table .= <<ENDSECTION;
 1208: <td>&nbsp</td> 
 1209: ENDSECTION
 1210:                 }
 1211: 		$table .= <<ENDTIMEENTRY;
 1212: <td><input type=hidden name="start_$protectedcourse\_$_" value=''>
 1213: <a href=
 1214: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$protectedcourse\_$_.value,'start_$protectedcourse\_$_','cu.pres','dateset')">Set Start Date</a></td>
 1215: <td><input type=hidden name="end_$protectedcourse\_$_" value=''>
 1216: <a href=
 1217: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$_.value,'end_$protectedcourse\_$_','cu.pres','dateset')">Set End Date</a></td>
 1218: ENDTIMEENTRY
 1219:                 $table.= "</tr>\n";
 1220:             }
 1221:         }
 1222:         foreach (sort keys %customroles) {
 1223: 	    if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
 1224: 		my $plrole=$_;
 1225:                 my $customrole=$protectedcourse.'_cr_cr_'.$ENV{'user.domain'}.
 1226: 		    '_'.$ENV{'user.name'}.'_'.$plrole;
 1227: 		$table .= <<ENDENTRY;
 1228: <tr bgcolor="#$bgcol">
 1229: <td><input type="checkbox" name="act_$customrole"></td>
 1230: <td>$plrole</td>
 1231: <td>$area</td>
 1232: <td><input type="text" size="5" name="sec_$customrole"></td>
 1233: <td><input type=hidden name="start_$customrole" value=''>
 1234: <a href=
 1235: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$customrole.value,'start_$customrole','cu.pres','dateset')">Set Start Date</a></td>
 1236: <td><input type=hidden name="end_$customrole" value=''>
 1237: <a href=
 1238: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$customrole.value,'end_$customrole','cu.pres','dateset')">Set End Date</a></td></tr>
 1239: ENDENTRY
 1240:            }
 1241: 	}
 1242:     }
 1243:     return '' if ($table eq ''); # return nothing if there is nothing 
 1244:                                  # in the table
 1245:     my $result = <<ENDTABLE;
 1246: <h4>Course Level</h4>
 1247: <table border=2><tr><th>Activate</th><th>Role</th><th>Extent</th>
 1248: <th>Group/Section</th><th>Start</th><th>End</th></tr>
 1249: $table
 1250: </table>
 1251: ENDTABLE
 1252:     return $result;
 1253: }
 1254: #---------------------------------------------- end functions for &phase_two
 1255: 
 1256: #--------------------------------- functions for &phase_two and &phase_three
 1257: 
 1258: #--------------------------end of functions for &phase_two and &phase_three
 1259: 
 1260: 1;
 1261: __END__
 1262: 
 1263: 

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