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