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