1: # The LearningOnline Network with CAPA
2: # Create a user
3: #
4: # $Id: loncreateuser.pm,v 1.64 2003/07/19 21:44:51 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 (sort 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: # Is this a custom role? Get role owner and title.
291: my ($croleudom,$croleuname,$croletitle)=
292: ($role_code=~/^cr\/(\w+)\/(\w+)\/(\w+)$/);
293: my $bgcol='ffffff';
294: my $allowed=0;
295: my $delallowed=0;
296: if ($area =~ /^\/(\w+)\/(\d\w+)/ ) {
297: my ($coursedom,$coursedir) = ($1,$2);
298: # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
299: my %coursedata=
300: &Apache::lonnet::coursedescription($1.'_'.$2);
301: my $carea;
302: if (defined($coursedata{'description'})) {
303: $carea='Course: '.$coursedata{'description'}.
304: '<br />Domain: '.$coursedom.(' 'x8).
305: &Apache::loncommon::syllabuswrapper('Syllabus',$coursedir,$coursedom);
306: } else {
307: $carea='Unavailable course: '.$area;
308: }
309: $inccourses{$1.'_'.$2}=1;
310: if ((&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2)) ||
311: (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
312: $allowed=1;
313: }
314: if ((&Apache::lonnet::allowed('dro',$1)) ||
315: (&Apache::lonnet::allowed('dro',$ccdomain))) {
316: $delallowed=1;
317: }
318: # - custom role. Needs more info, too
319: if ($croletitle) {
320: if (&Apache::lonnet::allowed('ccr',$1.'/'.$2)) {
321: $allowed=1;
322: $thisrole.='.'.$role_code;
323: }
324: }
325: # Compute the background color based on $area
326: $bgcol=$1.'_'.$2;
327: $bgcol=~s/[^7-9a-e]//g;
328: $bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',2,6);
329: if ($area=~/^\/(\w+)\/(\d\w+)\/(\w+)/) {
330: $carea.='<br>Section/Group: '.$3;
331: }
332: $area=$carea;
333: } else {
334: # Determine if current user is able to revoke privileges
335: if ($area=~ /^\/(\w+)\//) {
336: if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
337: (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
338: $allowed=1;
339: }
340: if (((&Apache::lonnet::allowed('dro',$1)) ||
341: (&Apache::lonnet::allowed('dro',$ccdomain))) &&
342: ($role_code ne 'dc')) {
343: $delallowed=1;
344: }
345: } else {
346: if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
347: $allowed=1;
348: }
349: }
350: }
351: if ($role_code eq 'ca') {
352: $area=~/\/(\w+)\/(\w+)/;
353: if (&authorpriv($2,$1)) {
354: $allowed=1;
355: } else {
356: $allowed=0;
357: }
358: }
359: my $row = '';
360: $row.='<tr bgcolor="#'.$bgcol.'"><td>';
361: my $active=1;
362: $active=0 if (($role_end_time) && ($now>$role_end_time));
363: if (($active) && ($allowed)) {
364: $row.= '<input type="checkbox" name="rev:'.$thisrole.'">';
365: } else {
366: if ($active) {
367: $row.=' ';
368: } else {
369: $row.='expired or revoked';
370: }
371: }
372: $row.='</td><td>';
373: if ($delallowed) {
374: $row.= '<input type="checkbox" name="del:'.$thisrole.'">';
375: } else {
376: $row.=' ';
377: }
378: my $plaintext='';
379: unless ($croletitle) {
380: $plaintext=&Apache::lonnet::plaintext($role_code);
381: } else {
382: $plaintext=
383: "Customrole '$croletitle' defined by $croleuname\@$croleudom";
384: }
385: $row.= '</td><td>'.$plaintext.
386: '</td><td>'.$area.
387: '</td><td>'.($role_start_time?localtime($role_start_time)
388: : ' ' ).
389: '</td><td>'.($role_end_time ?localtime($role_end_time)
390: : ' ' )
391: ."</td></tr>\n";
392: $r->print($row);
393: } # end of foreach (table building loop)
394: $r->print('</table>');
395: } # End of unless
396: my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
397: if ($currentauth=~/^krb(4|5):/) {
398: $currentauth=~/^krb(4|5):(.*)/;
399: my $krbdefdom=$1;
400: my %param = ( formname => 'document.cu',
401: kerb_def_dom => $krbdefdom
402: );
403: $loginscript = &Apache::loncommon::authform_header(%param);
404: }
405: # Check for a bad authentication type
406: unless ($currentauth=~/^krb(4|5):/ or
407: $currentauth=~/^unix:/ or
408: $currentauth=~/^internal:/ or
409: $currentauth=~/^localauth:/
410: ) { # bad authentication scheme
411: if (&Apache::lonnet::allowed('mau',$ENV{'request.role.domain'})) {
412: $r->print(<<ENDBADAUTH);
413: <hr />
414: <script type="text/javascript" language="Javascript">
415: $loginscript
416: </script>
417: <font color='#ff0000'>ERROR:</font>
418: This user has an unrecognized authentication scheme ($currentauth).
419: Please specify login data below.
420: <h3>Login Data</h3>
421: <p>$generalrule</p>
422: <p>$authformkrb</p>
423: <p>$authformint</p>
424: <p>$authformfsys</p>
425: <p>$authformloc</p>
426: ENDBADAUTH
427: } else {
428: # This user is not allowed to modify the users
429: # authentication scheme, so just notify them of the problem
430: $r->print(<<ENDBADAUTH);
431: <hr />
432: <script type="text/javascript" language="Javascript">
433: $loginscript
434: </script>
435: <font color="#ff0000"> ERROR: </font>
436: This user has an unrecognized authentication scheme ($currentauth).
437: Please alert a domain coordinator of this situation.
438: <hr />
439: ENDBADAUTH
440: }
441: } else { # Authentication type is valid
442: my $authformcurrent='';
443: my $authform_other='';
444: if ($currentauth=~/^krb(4|5):/) {
445: $authformcurrent=$authformkrb;
446: $authform_other="<p>$authformint</p>\n".
447: "<p>$authformfsys</p><p>$authformloc</p>";
448: }
449: elsif ($currentauth=~/^internal:/) {
450: $authformcurrent=$authformint;
451: $authform_other="<p>$authformkrb</p>".
452: "<p>$authformfsys</p><p>$authformloc</p>";
453: }
454: elsif ($currentauth=~/^unix:/) {
455: $authformcurrent=$authformfsys;
456: $authform_other="<p>$authformkrb</p>".
457: "<p>$authformint</p><p>$authformloc;</p>";
458: }
459: elsif ($currentauth=~/^localauth:/) {
460: $authformcurrent=$authformloc;
461: $authform_other="<p>$authformkrb</p>".
462: "<p>$authformint</p><p>$authformfsys</p>";
463: }
464: $authformcurrent.=' <i>(will override current values)</i><br />';
465: if (&Apache::lonnet::allowed('mau',$ENV{'request.role.domain'})) {
466: # Current user has login modification privileges
467: $r->print(<<ENDOTHERAUTHS);
468: <hr />
469: <script type="text/javascript" language="Javascript">
470: $loginscript
471: </script>
472: <h3>Change Current Login Data</h3>
473: <p>$generalrule</p>
474: <p>$authformnop</p>
475: <p>$authformcurrent</p>
476: <h3>Enter New Login Data</h3>
477: $authform_other
478: ENDOTHERAUTHS
479: }
480: } ## End of "check for bad authentication type" logic
481: } ## End of new user/old user logic
482: $r->print('<hr /><h3>Add Roles</h3>');
483: #
484: # Co-Author
485: #
486: if (&authorpriv($ENV{'user.name'},$ENV{'request.role.domain'}) &&
487: ($ENV{'user.name'} ne $ccuname || $ENV{'user.domain'} ne $ccdomain)) {
488: # No sense in assigning co-author role to yourself
489: my $cuname=$ENV{'user.name'};
490: my $cudom=$ENV{'request.role.domain'};
491: $r->print(<<ENDCOAUTH);
492: <h4>Construction Space</h4>
493: <table border=2><tr><th>Activate</th><th>Role</th><th>Extent</th>
494: <th>Start</th><th>End</th></tr>
495: <tr>
496: <td><input type=checkbox name="act_$cudom\_$cuname\_ca"></td>
497: <td>Co-Author</td>
498: <td>$cudom\_$cuname</td>
499: <td><input type=hidden name="start_$cudom\_$cuname\_ca" value=''>
500: <a href=
501: "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>
502: <td><input type=hidden name="end_$cudom\_$cuname\_ca" value=''>
503: <a href=
504: "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>
505: </tr>
506: </table>
507: ENDCOAUTH
508: }
509: #
510: # Domain level
511: #
512: $r->print('<h4>Domain Level</h4>'.
513: '<table border=2><tr><th>Activate</th><th>Role</th><th>Extent</th>'.
514: '<th>Start</th><th>End</th></tr>');
515: foreach ( sort( keys(%incdomains))) {
516: my $thisdomain=$_;
517: foreach ('dc','li','dg','au') {
518: if (&Apache::lonnet::allowed('c'.$_,$thisdomain)) {
519: my $plrole=&Apache::lonnet::plaintext($_);
520: $r->print(<<ENDDROW);
521: <tr>
522: <td><input type=checkbox name="act_$thisdomain\_$_"></td>
523: <td>$plrole</td>
524: <td>$thisdomain</td>
525: <td><input type=hidden name="start_$thisdomain\_$_" value=''>
526: <a href=
527: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$thisdomain\_$_.value,'start_$thisdomain\_$_','cu.pres','dateset')">Set Start Date</a></td>
528: <td><input type=hidden name="end_$thisdomain\_$_" value=''>
529: <a href=
530: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$thisdomain\_$_.value,'end_$thisdomain\_$_','cu.pres','dateset')">Set End Date</a></td>
531: </tr>
532: ENDDROW
533: }
534: }
535: }
536: $r->print('</table>');
537: #
538: # Course level
539: #
540: $r->print(&course_level_table(%inccourses));
541: $r->print("<hr /><input type=submit value=\"Modify User\">\n");
542: $r->print("</form></body></html>");
543: }
544:
545: # ================================================================= Phase Three
546: sub update_user_data {
547: my $r=shift;
548: my $uhome=&Apache::lonnet::homeserver($ENV{'form.ccuname'},
549: $ENV{'form.ccdomain'});
550: # Error messages
551: my $error = '<font color="#ff0000">Error:</font>';
552: my $end = '</body></html>';
553: # Print header
554: $r->print(<<ENDTHREEHEAD);
555: <html>
556: <head>
557: <title>The LearningOnline Network with CAPA</title>
558: </head>
559: ENDTHREEHEAD
560: my $title;
561: if (exists($ENV{'form.makeuser'})) {
562: $title='Set Privileges for New User';
563: } else {
564: $title='Modify User Privileges';
565: }
566: $r->print(&Apache::loncommon::bodytag($title));
567: # Check Inputs
568: if (! $ENV{'form.ccuname'} ) {
569: $r->print($error.'No login name specified.'.$end);
570: return;
571: }
572: if ( $ENV{'form.ccuname'} =~/\W/) {
573: $r->print($error.'Invalid login name. '.
574: 'Only letters, numbers, and underscores are valid.'.
575: $end);
576: return;
577: }
578: if (! $ENV{'form.ccdomain'} ) {
579: $r->print($error.'No domain specified.'.$end);
580: return;
581: }
582: if ( $ENV{'form.ccdomain'} =~/\W/) {
583: $r->print($error.'Invalid domain name. '.
584: 'Only letters, numbers, and underscores are valid.'.
585: $end);
586: return;
587: }
588: if (! exists($ENV{'form.makeuser'})) {
589: # Modifying an existing user, so check the validity of the name
590: if ($uhome eq 'no_host') {
591: $r->print($error.'Unable to determine home server for '.
592: $ENV{'form.ccuname'}.' in domain '.
593: $ENV{'form.ccdomain'}.'.');
594: return;
595: }
596: }
597: # Determine authentication method and password for the user being modified
598: my $amode='';
599: my $genpwd='';
600: if ($ENV{'form.login'} eq 'krb') {
601: $amode='krb';
602: $amode.=$ENV{'form.krbver'};
603: $genpwd=$ENV{'form.krbarg'};
604: } elsif ($ENV{'form.login'} eq 'int') {
605: $amode='internal';
606: $genpwd=$ENV{'form.intarg'};
607: } elsif ($ENV{'form.login'} eq 'fsys') {
608: $amode='unix';
609: $genpwd=$ENV{'form.fsysarg'};
610: } elsif ($ENV{'form.login'} eq 'loc') {
611: $amode='localauth';
612: $genpwd=$ENV{'form.locarg'};
613: $genpwd=" " if (!$genpwd);
614: } elsif (($ENV{'form.login'} eq 'nochange') ||
615: ($ENV{'form.login'} eq '' )) {
616: # There is no need to tell the user we did not change what they
617: # did not ask us to change.
618: # If they are creating a new user but have not specified login
619: # information this will be caught below.
620: } else {
621: $r->print($error.'Invalid login mode or password'.$end);
622: return;
623: }
624: if ($ENV{'form.makeuser'}) {
625: # Create a new user
626: $r->print(<<ENDNEWUSERHEAD);
627: <h3>Creating user "$ENV{'form.ccuname'}" in domain "$ENV{'form.ccdomain'}"</h2>
628: ENDNEWUSERHEAD
629: # Check for the authentication mode and password
630: if (! $amode || ! $genpwd) {
631: $r->print($error.'Invalid login mode or password'.$end);
632: return;
633: }
634: # Determine desired host
635: my $desiredhost = $ENV{'form.hserver'};
636: if (lc($desiredhost) eq 'default') {
637: $desiredhost = undef;
638: } else {
639: my %home_servers = &Apache::loncommon::get_library_servers
640: ($ENV{'form.ccdomain'});
641: if (! exists($home_servers{$desiredhost})) {
642: $r->print($error.'Invalid home server specified');
643: return;
644: }
645: }
646: # Call modifyuser
647: my $result = &Apache::lonnet::modifyuser
648: ($ENV{'form.ccdomain'},$ENV{'form.ccuname'},$ENV{'form.cstid'},
649: $amode,$genpwd,$ENV{'form.cfirst'},
650: $ENV{'form.cmiddle'},$ENV{'form.clast'},$ENV{'form.cgen'},
651: undef,$desiredhost
652: );
653: $r->print('Generating user: '.$result);
654: my $home = &Apache::lonnet::homeserver($ENV{'form.ccuname'},
655: $ENV{'form.ccdomain'});
656: $r->print('<br>Home server: '.$home.' '.
657: $Apache::lonnet::libserv{$home});
658: } elsif (($ENV{'form.login'} ne 'nochange') &&
659: ($ENV{'form.login'} ne '' )) {
660: # Modify user privileges
661: $r->print(<<ENDMODIFYUSERHEAD);
662: <h2>User "$ENV{'form.ccuname'}" in domain "$ENV{'form.ccdomain'}"</h2>
663: ENDMODIFYUSERHEAD
664: if (! $amode || ! $genpwd) {
665: $r->print($error.'Invalid login mode or password'.$end);
666: return;
667: }
668: # Only allow authentification modification if the person has authority
669: if (&Apache::lonnet::allowed('mau',$ENV{'form.ccdomain'})) {
670: $r->print('Modifying authentication: '.
671: &Apache::lonnet::modifyuserauth(
672: $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
673: $amode,$genpwd));
674: $r->print('<br>Home server: '.&Apache::lonnet::homeserver
675: ($ENV{'form.ccuname'},$ENV{'form.ccdomain'}));
676: } else {
677: # Okay, this is a non-fatal error.
678: $r->print($error.'You do not have the authority to modify '.
679: 'this users authentification information.');
680: }
681: }
682: ##
683: if (! $ENV{'form.makeuser'} ) {
684: # Check for need to change
685: my %userenv = &Apache::lonnet::get
686: ('environment',['firstname','middlename','lastname','generation'],
687: $ENV{'form.ccdomain'},$ENV{'form.ccuname'});
688: my ($tmp) = keys(%userenv);
689: if ($tmp =~ /^(con_lost|error)/i) {
690: %userenv = ();
691: }
692: # Check to see if we need to change user information
693: foreach ('firstname','middlename','lastname','generation') {
694: # Strip leading and trailing whitespace
695: $ENV{'form.c'.$_} =~ s/(\s+$|^\s+)//g;
696: }
697: if (&Apache::lonnet::allowed('mau',$ENV{'form.ccdomain'}) &&
698: ($ENV{'form.cfirstname'} ne $userenv{'firstname'} ||
699: $ENV{'form.cmiddlename'} ne $userenv{'middlename'} ||
700: $ENV{'form.clastname'} ne $userenv{'lastname'} ||
701: $ENV{'form.cgeneration'} ne $userenv{'generation'} )) {
702: # Make the change
703: my %changeHash;
704: $changeHash{'firstname'} = $ENV{'form.cfirstname'};
705: $changeHash{'middlename'} = $ENV{'form.cmiddlename'};
706: $changeHash{'lastname'} = $ENV{'form.clastname'};
707: $changeHash{'generation'} = $ENV{'form.cgeneration'};
708: my $putresult = &Apache::lonnet::put
709: ('environment',\%changeHash,
710: $ENV{'form.ccdomain'},$ENV{'form.ccuname'});
711: if ($putresult eq 'ok') {
712: # Tell the user we changed the name
713: $r->print(<<"END");
714: <table border="2">
715: <caption>User Information Changed</caption>
716: <tr><th> </th>
717: <th>first</th>
718: <th>middle</th>
719: <th>last</th>
720: <th>generation</th></tr>
721: <tr><td>Previous</td>
722: <td>$userenv{'firstname'} </td>
723: <td>$userenv{'middlename'} </td>
724: <td>$userenv{'lastname'} </td>
725: <td>$userenv{'generation'} </td></tr>
726: <tr><td>Changed To</td>
727: <td>$ENV{'form.cfirstname'} </td>
728: <td>$ENV{'form.cmiddlename'} </td>
729: <td>$ENV{'form.clastname'} </td>
730: <td>$ENV{'form.cgeneration'} </td></tr>
731: </table>
732: END
733: } else { # error occurred
734: $r->print("<h2>Unable to successfully change environment for ".
735: $ENV{'form.ccuname'}." in domain ".
736: $ENV{'form.ccdomain'}."</h2>");
737: }
738: } else { # End of if ($ENV ... ) logic
739: # They did not want to change the users name but we can
740: # still tell them what the name is
741: $r->print(<<"END");
742: <h2>User "$ENV{'form.ccuname'}" in domain "$ENV{'form.ccdomain'}"</h2>
743: <h4>$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} </h4>
744: <h4>Generation: $userenv{'generation'}</h4>
745: END
746: }
747: }
748: ##
749: my $now=time;
750: $r->print('<h3>Modifying Roles</h3>');
751: foreach (keys (%ENV)) {
752: next if (! $ENV{$_});
753: # Revoke roles
754: if ($_=~/^form\.rev/) {
755: if ($_=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
756: # Revoke standard role
757: $r->print('Revoking '.$2.' in '.$1.': <b>'.
758: &Apache::lonnet::assignrole($ENV{'form.ccdomain'},
759: $ENV{'form.ccuname'},$1,$2,$now).'</b><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: if ($_=~/^form\.rev\:([^\_]+)\_cr\.cr\/(\w+)\/(\w+)\/(\w+)$/) {
774: # Revoke custom role
775: $r->print(
776: 'Revoking custom role '.$4.' by '.$3.'\@'.$2.' in '.$1.': <b>'.
777: '</b><br>');
778: }
779: } elsif ($_=~/^form\.del/) {
780: if ($_=~/^form\.del\:([^\_]+)\_([^\_]+)$/) {
781: $r->print('Deleting '.$2.' in '.$1.': '.
782: &Apache::lonnet::assignrole($ENV{'form.ccdomain'},
783: $ENV{'form.ccuname'},$1,$2,$now,0,1).'<br>');
784: if ($2 eq 'st') {
785: $1=~/^\/(\w+)\/(\w+)/;
786: my $cid=$1.'_'.$2;
787: $r->print('Drop from classlist: <b>'.
788: &Apache::lonnet::critical('put:'.
789: $ENV{'course.'.$cid.'.domain'}.':'.
790: $ENV{'course.'.$cid.'.num'}.':classlist:'.
791: &Apache::lonnet::escape($ENV{'form.ccuname'}.':'.
792: $ENV{'form.ccdomain'}).'='.
793: &Apache::lonnet::escape($now.':'),
794: $ENV{'course.'.$cid.'.home'}).'</b><br>');
795: }
796: }
797: } elsif ($_=~/^form\.act/) {
798: if ($_=~/^form\.act\_([^\_]+)\_([^\_]+)\_([^\_]+)$/) {
799: # Activate roles for sections with 3 id numbers
800: # set start, end times, and the url for the class
801:
802: my $start = ( $ENV{'form.start_'.$1.'_'.$2.'_'.$3} ?
803: $ENV{'form.start_'.$1.'_'.$2.'_'.$3} :
804: $now );
805: my $end = ( $ENV{'form.end_'.$1.'_'.$2.'_'.$3} ?
806: $ENV{'form.end_'.$1.'_'.$2.'_'.$3} :
807: 0 );
808: my $url='/'.$1.'/'.$2;
809: if ($ENV{'form.sec_'.$1.'_'.$2.'_'.$3}) {
810: $url.='/'.$ENV{'form.sec_'.$1.'_'.$2.'_'.$3};
811: }
812: # Assign the role and report it
813: $r->print('Assigning: '.$3.' in '.$url.
814: ($start?', starting '.localtime($start):'').
815: ($end?', ending '.localtime($end):'').': <b>'.
816: &Apache::lonnet::assignrole(
817: $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
818: $url,$3,$end,$start).
819: '</b><br>');
820: # Handle students differently
821: if ($3 eq 'st') {
822: $url=~/^\/(\w+)\/(\w+)/;
823: my $cid=$1.'_'.$2;
824: $r->print('Add to classlist: <b>'.
825: &Apache::lonnet::critical(
826: 'put:'.$ENV{'course.'.$cid.'.domain'}.':'.
827: $ENV{'course.'.$cid.'.num'}.':classlist:'.
828: &Apache::lonnet::escape(
829: $ENV{'form.ccuname'}.':'.
830: $ENV{'form.ccdomain'} ).'='.
831: &Apache::lonnet::escape($end.':'.$start),
832: $ENV{'course.'.$cid.'.home'})
833: .'</b><br>');
834: }
835: } elsif ($_=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
836: # Activate roles for sections with two id numbers
837: # set start, end times, and the url for the class
838: my $start = ( $ENV{'form.start_'.$1.'_'.$2} ?
839: $ENV{'form.start_'.$1.'_'.$2} :
840: $now );
841: my $end = ( $ENV{'form.end_'.$1.'_'.$2} ?
842: $ENV{'form.end_'.$1.'_'.$2} :
843: 0 );
844: my $url='/'.$1.'/';
845: # Assign the role and report it.
846: $r->print('Assigning: '.$2.' in '.$url.': '.
847: ($start?', starting '.localtime($start):'').
848: ($end?', ending '.localtime($end):'').': <b>'.
849: &Apache::lonnet::assignrole(
850: $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
851: $url,$2,$end,$start)
852: .'</b><br>');
853: } else {
854: $r->print('<p>ERROR: Unknown command <tt>'.$_.'</tt></p><br>');
855: }
856: }
857: } # End of foreach (keys(%ENV))
858: $r->print('</body></html>');
859: }
860:
861: # ========================================================== Custom Role Editor
862:
863: sub custom_role_editor {
864: my $r=shift;
865: my $rolename=$ENV{'form.rolename'};
866:
867: if ($rolename eq 'make new role') {
868: $rolename=$ENV{'form.newrolename'};
869: }
870:
871: $rolename=~s/[^A-Za-z0-9]//gs;
872:
873: unless ($rolename) {
874: &print_username_entry_form($r);
875: return;
876: }
877:
878: $r->print(&Apache::loncommon::bodytag(
879: 'Create Users, Change User Privileges').'<h2>');
880: my $syspriv='';
881: my $dompriv='';
882: my $coursepriv='';
883: my ($rdummy,$roledef)=
884: &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
885: # ------------------------------------------------------- Does this role exist?
886: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
887: $r->print('Existing Role "');
888: # ------------------------------------------------- Get current role privileges
889: ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
890: } else {
891: $r->print('New Role "');
892: $roledef='';
893: }
894: $r->print($rolename.'"</h2>');
895: # ------------------------------------------------------- What can be assigned?
896: my %full=();
897: my %courselevel=();
898: my %courselevelcurrent=();
899: foreach (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
900: my ($priv,$restrict)=split(/\&/,$_);
901: unless ($restrict) { $restrict='F'; }
902: $courselevel{$priv}=$restrict;
903: if ($coursepriv=~/\:$priv/) {
904: $courselevelcurrent{$priv}=1;
905: }
906: $full{$priv}=1;
907: }
908: my %domainlevel=();
909: my %domainlevelcurrent=();
910: foreach (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
911: my ($priv,$restrict)=split(/\&/,$_);
912: unless ($restrict) { $restrict='F'; }
913: $domainlevel{$priv}=$restrict;
914: if ($dompriv=~/\:$priv/) {
915: $domainlevelcurrent{$priv}=1;
916: }
917: $full{$priv}=1;
918: }
919: my %systemlevel=();
920: my %systemlevelcurrent=();
921: foreach (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
922: my ($priv,$restrict)=split(/\&/,$_);
923: unless ($restrict) { $restrict='F'; }
924: $systemlevel{$priv}=$restrict;
925: if ($syspriv=~/\:$priv/) {
926: $systemlevelcurrent{$priv}=1;
927: }
928: $full{$priv}=1;
929: }
930: $r->print(<<ENDCCF);
931: <form method="post">
932: <input type="hidden" name="phase" value="set_custom_roles" />
933: <input type="hidden" name="rolename" value="$rolename" />
934: <table border="2">
935: <tr><th>Privilege</th><th>Course Level</th><th>Domain Level</th>
936: <th>System Level</th></tr>
937: ENDCCF
938: foreach (sort keys %full) {
939: $r->print('<tr><td>'.&Apache::lonnet::plaintext($_).'</td><td>'.
940: ($courselevel{$_}?'<input type="checkbox" name="'.$_.':c" '.
941: ($courselevelcurrent{$_}?'checked="1"':'').' />':' ').
942: '</td><td>'.
943: ($domainlevel{$_}?'<input type="checkbox" name="'.$_.':d" '.
944: ($domainlevelcurrent{$_}?'checked="1"':'').' />':' ').
945: '</td><td>'.
946: ($systemlevel{$_}?'<input type="checkbox" name="'.$_.':s" '.
947: ($systemlevelcurrent{$_}?'checked="1"':'').' />':' ').
948: '</td></tr>');
949: }
950: $r->print(
951: '<table><input type="submit" value="Define Role" /></form></body></html>');
952: }
953:
954: # ---------------------------------------------------------- Call to definerole
955: sub set_custom_role {
956: my $r=shift;
957:
958: my $rolename=$ENV{'form.rolename'};
959:
960: $rolename=~s/[^A-Za-z0-9]//gs;
961:
962: unless ($rolename) {
963: &print_username_entry_form($r);
964: return;
965: }
966:
967: $r->print(&Apache::loncommon::bodytag(
968: 'Create Users, Change User Privileges').'<h2>');
969: my ($rdummy,$roledef)=
970: &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
971: # ------------------------------------------------------- Does this role exist?
972: if (($rdummy ne 'con_lost') && ($roledef ne '')) {
973: $r->print('Existing Role "');
974: } else {
975: $r->print('New Role "');
976: $roledef='';
977: }
978: $r->print($rolename.'"</h2>');
979: # ------------------------------------------------------- What can be assigned?
980: my $sysrole='';
981: my $domrole='';
982: my $courole='';
983:
984: foreach (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
985: my ($priv,$restrict)=split(/\&/,$_);
986: unless ($restrict) { $restrict=''; }
987: if ($ENV{'form.'.$priv.':c'}) {
988: $courole.=':'.$_;
989: }
990: }
991:
992: foreach (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
993: my ($priv,$restrict)=split(/\&/,$_);
994: unless ($restrict) { $restrict=''; }
995: if ($ENV{'form.'.$priv.':d'}) {
996: $domrole.=':'.$_;
997: }
998: }
999:
1000: foreach (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
1001: my ($priv,$restrict)=split(/\&/,$_);
1002: unless ($restrict) { $restrict=''; }
1003: if ($ENV{'form.'.$priv.':s'}) {
1004: $sysrole.=':'.$_;
1005: }
1006: }
1007: $r->print('<br />Defining Role: '.
1008: &Apache::lonnet::definerole($rolename,$sysrole,$domrole,$courole));
1009: if ($ENV{'request.course.id'}) {
1010: my $url='/'.$ENV{'request.course.id'};
1011: $url=~s/\_/\//g;
1012: $r->print('<br />Assigning Role to Self: '.
1013: &Apache::lonnet::assigncustomrole($ENV{'user.domain'},
1014: $ENV{'user.name'},
1015: $url,
1016: $ENV{'user.domain'},
1017: $ENV{'user.name'},
1018: $rolename));
1019: }
1020: $r->print('</body></html>');
1021: }
1022:
1023: # ================================================================ Main Handler
1024: sub handler {
1025: my $r = shift;
1026:
1027: if ($r->header_only) {
1028: $r->content_type('text/html');
1029: $r->send_http_header;
1030: return OK;
1031: }
1032:
1033: if ((&Apache::lonnet::allowed('cta',$ENV{'request.course.id'})) ||
1034: (&Apache::lonnet::allowed('cin',$ENV{'request.course.id'})) ||
1035: (&Apache::lonnet::allowed('ccr',$ENV{'request.course.id'})) ||
1036: (&Apache::lonnet::allowed('cep',$ENV{'request.course.id'})) ||
1037: (&Apache::lonnet::allowed('cca',$ENV{'request.role.domain'})) ||
1038: (&Apache::lonnet::allowed('mau',$ENV{'request.role.domain'}))) {
1039: $r->content_type('text/html');
1040: $r->send_http_header;
1041: unless ($ENV{'form.phase'}) {
1042: &print_username_entry_form($r);
1043: }
1044: if ($ENV{'form.phase'} eq 'get_user_info') {
1045: &print_user_modification_page($r);
1046: } elsif ($ENV{'form.phase'} eq 'update_user_data') {
1047: &update_user_data($r);
1048: } elsif ($ENV{'form.phase'} eq 'selected_custom_edit') {
1049: &custom_role_editor($r);
1050: } elsif ($ENV{'form.phase'} eq 'set_custom_roles') {
1051: &set_custom_role($r);
1052: }
1053: } else {
1054: $ENV{'user.error.msg'}=
1055: "/adm/createuser:mau:0:0:Cannot modify user data";
1056: return HTTP_NOT_ACCEPTABLE;
1057: }
1058: return OK;
1059: }
1060:
1061: #-------------------------------------------------- functions for &phase_two
1062: sub course_level_table {
1063: my %inccourses = @_;
1064: my $table = '';
1065: # Custom Roles?
1066:
1067: my %customroles=&my_custom_roles();
1068:
1069: foreach (sort( keys(%inccourses))) {
1070: my $thiscourse=$_;
1071: my $protectedcourse=$_;
1072: $thiscourse=~s:_:/:g;
1073: my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
1074: my $area=$coursedata{'description'};
1075: if (!defined($area)) { $area='Unavailable course: '.$_; }
1076: my $bgcol=$thiscourse;
1077: $bgcol=~s/[^7-9a-e]//g;
1078: $bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',2,6);
1079: foreach ('st','ta','ep','ad','in','cc') {
1080: if (&Apache::lonnet::allowed('c'.$_,$thiscourse)) {
1081: my $plrole=&Apache::lonnet::plaintext($_);
1082: $table .= <<ENDEXTENT;
1083: <tr bgcolor="#$bgcol">
1084: <td><input type="checkbox" name="act_$protectedcourse\_$_"></td>
1085: <td>$plrole</td>
1086: <td>$area</td>
1087: ENDEXTENT
1088: if ($_ ne 'cc') {
1089: $table .= <<ENDSECTION;
1090: <td><input type="text" size="5" name="sec_$protectedcourse\_$_"></td>
1091: ENDSECTION
1092: } else {
1093: $table .= <<ENDSECTION;
1094: <td> </td>
1095: ENDSECTION
1096: }
1097: $table .= <<ENDTIMEENTRY;
1098: <td><input type=hidden name="start_$protectedcourse\_$_" value=''>
1099: <a href=
1100: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$protectedcourse\_$_.value,'start_$protectedcourse\_$_','cu.pres','dateset')">Set Start Date</a></td>
1101: <td><input type=hidden name="end_$protectedcourse\_$_" value=''>
1102: <a href=
1103: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$_.value,'end_$protectedcourse\_$_','cu.pres','dateset')">Set End Date</a></td>
1104: ENDTIMEENTRY
1105: $table.= "</tr>\n";
1106: }
1107: }
1108: foreach (sort keys %customroles) {
1109: my $plrole=$_;
1110: $table .= <<ENDENTRY;
1111: <tr bgcolor="#$bgcol">
1112: <td><input type="checkbox" name="act_$protectedcourse\_$_"></td>
1113: <td>$plrole</td>
1114: <td>$area</td>
1115: <td><input type="text" size="5" name="sec_$protectedcourse\_$_"></td>
1116: <td><input type=hidden name="start_$protectedcourse\_$_" value=''>
1117: <a href=
1118: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$protectedcourse\_$_.value,'start_$protectedcourse\_$_','cu.pres','dateset')">Set Start Date</a></td>
1119: <td><input type=hidden name="end_$protectedcourse\_$_" value=''>
1120: <a href=
1121: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$_.value,'end_$protectedcourse\_$_','cu.pres','dateset')">Set End Date</a></td></tr>
1122: ENDENTRY
1123: }
1124: }
1125: return '' if ($table eq ''); # return nothing if there is nothing
1126: # in the table
1127: my $result = <<ENDTABLE;
1128: <h4>Course Level</h4>
1129: <table border=2><tr><th>Activate</th><th>Role</th><th>Extent</th>
1130: <th>Group/Section</th><th>Start</th><th>End</th></tr>
1131: $table
1132: </table>
1133: ENDTABLE
1134: return $result;
1135: }
1136: #---------------------------------------------- end functions for &phase_two
1137:
1138: #--------------------------------- functions for &phase_two and &phase_three
1139:
1140: #--------------------------end of functions for &phase_two and &phase_three
1141:
1142: 1;
1143: __END__
1144:
1145:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>