1: # The LearningOnline Network with CAPA
2: # User Roles Screen
3: #
4: # $Id: lonroles.pm,v 1.57 2003/05/21 20:10:53 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: # (Directory Indexer
29: # (Login Screen
30: # YEAR=1999
31: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14 Gerd Kortemeyer)
32: # 11/23 Gerd Kortemeyer)
33: # YEAR=2000
34: # 1/14,03/06,06/01,07/22,07/24,07/25,
35: # 09/04,09/06,09/28,09/29,09/30,10/2,10/5,10/26,10/28,
36: # 12/08,12/28,
37: # YEAR=2001
38: # 01/15/01 Gerd Kortemeyer
39: # 03/02,05/03,05/25,05/30,06/01,07/06,08/06 Gerd Kortemeyer
40: # 12/29 Gerd Kortemeyer
41: #
42: ###
43:
44: package Apache::lonroles;
45:
46: use strict;
47: use Apache::lonnet();
48: use Apache::lonuserstate();
49: use Apache::Constants qw(:common);
50: use Apache::File();
51: use Apache::lonmenu;
52: use Apache::loncommon;
53: use Apache::lonannounce;
54:
55: sub handler {
56:
57: my $r = shift;
58:
59: my $now=time;
60: my $then=$ENV{'user.login.time'};
61: my $envkey;
62:
63:
64: # ================================================================== Roles Init
65:
66: if ($ENV{'form.selectrole'}) {
67: if ($ENV{'request.course.id'}) {
68: my %temp=('logout_'.$ENV{'request.course.id'} => time);
69: &Apache::lonnet::put('email_status',\%temp);
70: }
71: &Apache::lonnet::appenv("request.course.id" => '',
72: "request.course.fn" => '',
73: "request.course.uri" => '',
74: "request.course.sec" => '',
75: "request.role" => 'cm',
76: "request.role.adv" => $ENV{'user.adv'},
77: "request.role.domain" => $ENV{'user.domain'});
78: foreach $envkey (keys %ENV) {
79: next if ($envkey!~/^user\.role\./);
80: my (undef,undef,$role,@pwhere)=split(/\./,$envkey);
81: my $where=join('.',@pwhere);
82: my $trolecode=$role.'.'.$where;
83: if ($ENV{'form.'.$trolecode}) {
84: my ($tstart,$tend)=split(/\./,$ENV{$envkey});
85: my $tstatus='is';
86: if ($tstart) {
87: if ($tstart>$then) {
88: $tstatus='future';
89: }
90: }
91: if ($tend) {
92: if ($tend<$then) { $tstatus='expired'; }
93: if ($tend<$now) { $tstatus='will_not'; }
94: }
95: if ($tstatus eq 'is') {
96: $where=~s/^\///;
97: my ($cdom,$cnum,$csec)=split(/\//,$where);
98: # check for keyed access
99: if (($role eq 'st') &&
100: ($ENV{'course.'.$cdom.'_'.$cnum.'.keyaccess'} eq 'yes')) {
101: unless (&Apache::lonnet::validate_access_key(
102: $ENV{'environment.key.'.$cdom.'_'.$cnum},
103: $cdom,$cnum)) {
104: # there is no valid key
105: if ($ENV{'form.newkey'}) {
106: # student attempts to register a new key
107: } else {
108: # print form to enter a new key
109: $r->content_type('text/html');
110: &Apache::loncommon::no_cache($r);
111: $r->send_http_header;
112: my $swinfo=&Apache::lonmenu::rawconfig();
113: my $bodytag=&Apache::loncommon::bodytag
114: ('Enter Access Key to Unlock this Course');
115: $r->print(<<ENDENTERKEY);
116: <head><title>Entering Course Access Key</title>
117: </head>
118: <html>
119: $bodytag
120: <script>
121: $swinfo
122: </script>
123: <form method="post">
124: <input type="hidden" name="selectrole" value="$ENV{'form.selectrole'}" />
125: <input type="text" size="20" name="newkey" value="$ENV{'form.newkey'}" />
126: <input type="submit" value="Enter key" />
127: </form>
128: </body></html>
129: ENDENTERKEY
130: return OK;
131: }
132: }
133: }
134: my $tadv=0;
135: if (($trolecode!~/^st/) &&
136: ($trolecode!~/^ta/) &&
137: ($trolecode!~/^cm/)) { $tadv=1; }
138: &Apache::lonnet::appenv(
139: 'request.role' => $trolecode,
140: 'request.role.adv' => $tadv,
141: 'request.role.domain' => $cdom,
142: 'request.course.sec' => $csec);
143: my $msg='Entering course ...';
144: if (($cnum) && ($role ne 'ca')) {
145: my ($furl,$ferr)=
146: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
147: if (($ENV{'form.orgurl'}) &&
148: ($ENV{'form.orgurl'}!~/^\/adm\/flip/)) {
149: $r->internal_redirect($ENV{'form.orgurl'});
150: return OK;
151: } else {
152: unless ($ENV{'request.course.id'}) {
153: &Apache::lonnet::appenv(
154: "request.course.id" => $cdom.'_'.$cnum);
155: $furl='/adm/notfound.html';
156: $msg=
157: '<h1><font color=red>Could not initialize top-level map.</font></h1>';
158: }
159: $r->content_type('text/html');
160: &Apache::loncommon::no_cache($r);
161: $r->send_http_header;
162: my $swinfo=&Apache::lonmenu::rawconfig();
163: my $bodytag=&Apache::loncommon::bodytag('Switching Role');
164: print (<<ENDREDIR);
165: <head><title>Entering Course</title>
166: <meta HTTP-EQUIV="Refresh" CONTENT="1; url=$furl">
167: </head>
168: <html>
169: $bodytag
170: <script>
171: $swinfo
172: </script>
173: <h1>$msg</h1>
174: </body>
175: </html>
176: ENDREDIR
177: return OK;
178: }
179: }
180: }
181: }
182: }
183: }
184:
185:
186: # =============================================================== No Roles Init
187:
188: $r->content_type('text/html');
189: &Apache::loncommon::no_cache($r);
190: $r->send_http_header;
191: return OK if $r->header_only;
192:
193: my $swinfo=&Apache::lonmenu::rawconfig();
194: my $bodytag=&Apache::loncommon::bodytag('User Roles');
195: my $helptag=&Apache::loncommon::help_open_topic
196: ("General_Intro","Click here for help");
197: $r->print(<<ENDHEADER);
198: <html>
199: <head>
200: <title>LON-CAPA User Roles</title>
201: </head>
202: $bodytag
203: $helptag<br />
204: <script>
205: $swinfo
206: window.focus();
207: </script>
208: ENDHEADER
209:
210: # ------------------------------------------ Get Error Message from Environment
211:
212: my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$ENV{'user.error.msg'});
213: if ($ENV{'user.error.msg'}) {
214: $r->log_reason(
215: "$msg for $ENV{'user.name'} domain $ENV{'user.domain'} access $priv",$fn);
216: }
217:
218: # ---------------------------------------------------------------- Who is this?
219:
220: my $advanced=0;
221: foreach $envkey (keys %ENV) {
222: if ($envkey=~/^user\.role\./) {
223: my (undef,undef,$role,@pwhere)=split(/\./,$envkey);
224: if ($role ne 'st') { $advanced=1; }
225: }
226: }
227:
228: # -------------------------------------------------------- Generate Page Output
229: # --------------------------------------------------------------- Error Header?
230: if ($error) {
231: $r->print("<h1>LON-CAPA Access Control</h1>");
232: $r->print("<hr><pre>Access : ".
233: Apache::lonnet::plaintext($priv)."\n");
234: $r->print("Resource: $fn\n");
235: $r->print("Action : $msg\n</pre><hr>");
236: } else {
237: if ($ENV{'user.error.msg'}) {
238: $r->print(
239: '<h3><font color=red>You need to choose another user role or '.
240: 'enter a specific course for this function</font></h3>');
241: }
242: }
243: # -------------------------------------------------------- Choice or no choice?
244: if ($nochoose) {
245: if ($advanced) {
246: $r->print("<h2>Assigned User Roles</h2>\n");
247: } else {
248: $r->print("<h2>Sorry ...</h2>\nThis resource might be part of");
249: if ($ENV{'request.course.id'}) {
250: $r->print(' another');
251: } else {
252: $r->print(' a certain');
253: }
254: $r->print(' course.</body></html>');
255: return OK;
256: }
257: } else {
258: if ($advanced) {
259: $r->print("Your home server is ".
260: $Apache::lonnet::hostname{&Apache::lonnet::homeserver
261: ($ENV{'user.name'},$ENV{'user.domain'})}.
262: "<br />\n");
263: $r->print("Author and Co-Author roles may not be available on ".
264: "servers other than your home server.");
265: } else {
266: $r->print("<h2>Enter a Course</h2>\n");
267: }
268: if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
269: $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
270: }
271: $r->print('<form method=post action="'.(($fn)?$fn:$r->uri).'">');
272: $r->print('<input type=hidden name=orgurl value="'.$fn.'">');
273: $r->print('<input type=hidden name=selectrole value=1>');
274: }
275: $r->print('<br>Show all roles: <input type=checkbox name=showall');
276: if ($ENV{'form.showall'}) { $r->print(' checked'); }
277: $r->print('><input type=submit value="Display"><br>');
278: # ----------------------------------------------------------------------- Table
279: $r->print('<table><tr>');
280: unless ($nochoose) { $r->print('<th> </th>'); }
281: $r->print('<th>User Role</th><th colspan=2>Extent</th>'.
282: '<th>Start</th><th>End</th><th>Remark</th></tr>'."\n");
283:
284: foreach $envkey (sort keys %ENV) {
285: my $button = 1;
286: my $switchserver='';
287: if ($envkey=~/^user\.role\./) {
288: my (undef,undef,$role,@pwhere)=split(/\./,$envkey);
289: next if (!defined($role) || $role eq '');
290: my $where=join('.',@pwhere);
291: my $trolecode=$role.'.'.$where;
292: my ($tstart,$tend)=split(/\./,$ENV{$envkey});
293: my $tremark='';
294: my $tstatus='is';
295: my $tpstart=' ';
296: my $tpend=' ';
297: my $tfont='#000000';
298: if ($tstart) {
299: if ($tstart>$then) {
300: $tstatus='future';
301: if ($tstart<$now) { $tstatus='will'; }
302: }
303: $tpstart=localtime($tstart);
304: }
305: if ($tend) {
306: if ($tend<$then) {
307: $tstatus='expired';
308: } elsif ($tend<$now) {
309: $tstatus='will_not';
310: }
311: $tpend=localtime($tend);
312: }
313: if ($ENV{'request.role'} eq $trolecode) {
314: $tstatus='selected';
315: }
316: my $tbg;
317: if (($tstatus eq 'is') || ($tstatus eq 'selected') ||
318: ($ENV{'form.showall'})) {
319: if ($tstatus eq 'is') {
320: $tbg='#77FF77';
321: $tfont='#003300';
322: } elsif ($tstatus eq 'future') {
323: $tbg='#FFFF77';
324: $button=0;
325: } elsif ($tstatus eq 'will') {
326: $tbg='#FFAA77';
327: $tremark.='Active at next login. ';
328: } elsif ($tstatus eq 'expired') {
329: $tbg='#FF7777';
330: $tfont='#330000';
331: $button=0;
332: } elsif ($tstatus eq 'will_not') {
333: $tbg='#AAFF77';
334: $tremark.='Expired after logout. ';
335: } elsif ($tstatus eq 'selected') {
336: $tbg='#11CC55';
337: $tfont='#002200';
338: $tremark.='Currently selected. ';
339: }
340: my $trole;
341: if ($role =~ /^cr\//) {
342: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
343: $tremark.='<br>Defined by '.$rauthor.' at '.$rdomain.'.';
344: $trole=$rrole;
345: } else {
346: $trole=Apache::lonnet::plaintext($role);
347: }
348: my $ttype;
349: my $twhere;
350: my ($tdom,$trest,$tsection)=
351: split(/\//,Apache::lonnet::declutter($where));
352: # First, Co-Authorship roles
353: if ($role eq 'ca') {
354: my $home = &Apache::lonnet::homeserver($trest,$tdom);
355: if ($home ne $r->dir_config('lonHostID')) {
356: $button=0;
357: $switchserver=&Apache::lonnet::escape('http://'.
358: $Apache::lonnet::hostname{$home}.
359: '/adm/login?domain='.$ENV{'user.domain'}.
360: '&username='.$ENV{'user.name'}.
361: '&firsturl=/priv/'.$trest);
362: }
363: #next if ($home eq 'no_host');
364: $home = $Apache::lonnet::hostname{$home};
365: $ttype='Construction Space';
366: $twhere='User: '.$trest.'<br />Domain: '.$tdom.'<br />'.
367: ' Server: '.$home;
368: $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
369: } elsif ($role eq 'au') {
370: # Authors
371: my $home = &Apache::lonnet::homeserver
372: ($ENV{'user.name'},$ENV{'user.domain'});
373: if ($home ne $r->dir_config('lonHostID')) {
374: $button=0;
375: $switchserver=&Apache::lonnet::escape('http://'.
376: $Apache::lonnet::hostname{$home}.
377: '/adm/login?domain='.$ENV{'user.domain'}.
378: '&username='.$ENV{'user.name'}.
379: '&firsturl=/priv/'.$ENV{'user.name'});
380: }
381: #next if ($home eq 'no_host');
382: $home = $Apache::lonnet::hostname{$home};
383: $ttype='Construction Space';
384: $twhere='Domain: '.$tdom.'<br />Server: '.$home;
385: $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
386: } elsif ($trest) {
387: $ttype='Course';
388: if ($tsection) {
389: $ttype.='<br>Section/Group: '.$tsection;
390: }
391: my $tcourseid=$tdom.'_'.$trest;
392: if ($ENV{'course.'.$tcourseid.'.description'}) {
393: $twhere=$ENV{'course.'.$tcourseid.'.description'};
394: unless ($twhere eq 'Currently not available') {
395: $twhere.=' <font size="-2">'.
396: &Apache::loncommon::syllabuswrapper('Syllabus',$trest,$tdom,$tfont).
397: '</font>';
398: }
399: } else {
400: my %newhash=Apache::lonnet::coursedescription
401: ($tcourseid);
402: if (%newhash) {
403: $twhere=$newhash{'description'}.
404: ' <font size="-2">'.
405: &Apache::loncommon::syllabuswrapper('Syllabus',$trest,$tdom,$tfont).
406: '</font>';
407: } else {
408: $twhere='Currently not available';
409: $ENV{'course.'.$tcourseid.'.description'}=$twhere;
410: }
411: }
412: if ($role ne 'st') { $twhere.="<br />Domain:".$tdom; }
413: } elsif ($tdom) {
414: $ttype='Domain';
415: $twhere=$tdom;
416: } else {
417: $ttype='System';
418: $twhere='system wide';
419: }
420:
421: $r->print('<tr bgcolor='.$tbg.'>');
422: unless ($nochoose) {
423: if (!$button) {
424: if ($switchserver) {
425: $r->print('<td><a href="/adm/logout?handover='.
426: $switchserver.'">Switch Server</a></td>');
427: } else {
428: $r->print('<td> </td>');
429: }
430: } elsif ($tstatus eq 'is') {
431: $r->print('<td><input type=submit value=Select name="'.
432: $trolecode.'"></td>');
433: } elsif ($ENV{'user.adv'}) {
434: $r->print
435: ('<td><input type=submit value="Re-Initialize"'.
436: ' name="'.$trolecode.'"></td>');
437: } else {
438: $r->print('<td> </td>');
439: }
440: }
441: $tremark.=&Apache::lonannounce::showday(time,1,
442: &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
443:
444: $r->print('<td><font color="'.$tfont.'">'.$trole.
445: '</font></td><td><font color="'.$tfont.'">'.$ttype.
446: '</font></td><td><font color="'.$tfont.'">'.$twhere.
447: '</font></td><td><font color="'.$tfont.'">'.$tpstart.
448: '</font></td><td><font color="'.$tfont.'">'.$tpend.
449: '</font></td><td><font color="'.$tfont.'">'.$tremark.
450: ' </font></td></tr>'."\n");
451: }
452: }
453: }
454: my $tremark='';
455: my $tfont='#003300';
456: if ($ENV{'request.role'} eq 'cm') {
457: $r->print('<tr bgcolor="#11CC55">');
458: $tremark='Currently selected.';
459: $tfont='#002200';
460: } else {
461: $r->print('<tr bgcolor="#77FF77">');
462: }
463: unless ($nochoose) {
464: if ($ENV{'request.role'} ne 'cm') {
465: $r->print('<td><input type=submit value=Select name="cm"></td>');
466: } else {
467: $r->print('<td> </td>');
468: }
469: }
470: $r->print('<td colspan=5><font color="'.$tfont.'">No role specified'.
471: '</font></td><td><font color="'.$tfont.'">'.$tremark.
472: ' </font></td></tr>'."\n");
473:
474: $r->print('</table>');
475: unless ($nochoose) {
476: $r->print("</form>\n");
477: }
478: # ------------------------------------------------------------ Privileges Info
479: if (($advanced) && (($ENV{'user.error.msg'}) || ($error))) {
480: $r->print('<hr><h2>Current Privileges</h2>');
481:
482: foreach $envkey (sort keys %ENV) {
483: if ($envkey=~/^user\.priv\.$ENV{'request.role'}\./) {
484: my $where=$envkey;
485: $where=~s/^user\.priv\.$ENV{'request.role'}\.//;
486: my $ttype;
487: my $twhere;
488: my ($tdom,$trest,$tsec)=
489: split(/\//,Apache::lonnet::declutter($where));
490: if ($trest) {
491: if ($ENV{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
492: $ttype='Construction Space';
493: $twhere='User: '.$trest.', Domain: '.$tdom;
494: } else {
495: $ttype='Course';
496: $twhere=$ENV{'course.'.$tdom.'_'.$trest.'.description'};
497: if ($tsec) {
498: $twhere.=' (Section/Group: '.$tsec.')';
499: }
500: }
501: } elsif ($tdom) {
502: $ttype='Domain';
503: $twhere=$tdom;
504: } else {
505: $ttype='System';
506: $twhere='/';
507: }
508: $r->print("\n<h3>".$ttype.': '.$twhere.'</h3><ul>');
509: foreach (sort split(/:/,$ENV{$envkey})) {
510: if ($_) {
511: my ($prv,$restr)=split(/\&/,$_);
512: my $trestr='';
513: if ($restr ne 'F') {
514: my $i;
515: $trestr.=' (';
516: for ($i=0;$i<length($restr);$i++) {
517: $trestr.=
518: Apache::lonnet::plaintext(substr($restr,$i,1));
519: if ($i<length($restr)-1) { $trestr.=', '; }
520: }
521: $trestr.=')';
522: }
523: $r->print('<li>'.
524: Apache::lonnet::plaintext($prv).$trestr.
525: '</li>');
526: }
527: }
528: $r->print('</ul>');
529: }
530: }
531: }
532:
533: $r->print("</body></html>\n");
534: return OK;
535: }
536:
537: 1;
538: __END__
539:
540: =head1 NAME
541:
542: Apache::lonroles - User Roles Screen
543:
544: =head1 SYNOPSIS
545:
546: Invoked by /etc/httpd/conf/srm.conf:
547:
548: <Location /adm/roles>
549: PerlAccessHandler Apache::lonacc
550: SetHandler perl-script
551: PerlHandler Apache::lonroles
552: ErrorDocument 403 /adm/login
553: ErrorDocument 500 /adm/errorhandler
554: </Location>
555:
556: =head1 INTRODUCTION
557:
558: This module enables a user to select what role he wishes to
559: operate under (instructor, student, teaching assistant, course
560: coordinator, etc). These roles are pre-established by the actions
561: of upper-level users.
562:
563: This is part of the LearningOnline Network with CAPA project
564: described at http://www.lon-capa.org.
565:
566: =head1 HANDLER SUBROUTINE
567:
568: This routine is called by Apache and mod_perl.
569:
570: =over 4
571:
572: =item *
573:
574: Roles Initialization (yes/no)
575:
576: =item *
577:
578: Get Error Message from Environment
579:
580: =item *
581:
582: Who is this?
583:
584: =item *
585:
586: Generate Page Output
587:
588: =item *
589:
590: Choice or no choice
591:
592: =item *
593:
594: Table
595:
596: =item *
597:
598: Privileges
599:
600: =back
601:
602: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>