File:
[LON-CAPA] /
loncom /
auth /
lonroles.pm
Revision
1.120:
download - view:
text,
annotated -
select for diffs
Sun Jun 5 07:10:32 2005 UTC (19 years, 2 months ago) by
albertel
Branches:
MAIN
CVS tags:
HEAD
- trying to be moe helpful in access control problem cases, throw up the 're-init course' button if the get the access control message and have modified the course with reinit (Kinda BUG# 3646)
1: # The LearningOnline Network with CAPA
2: # User Roles Screen
3: #
4: # $Id: lonroles.pm,v 1.120 2005/06/05 07:10:32 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: ###
29:
30: package Apache::lonroles;
31:
32: use strict;
33: use Apache::lonnet;
34: use Apache::lonuserstate();
35: use Apache::Constants qw(:common);
36: use Apache::File();
37: use Apache::lonmenu;
38: use Apache::loncommon;
39: use Apache::lonhtmlcommon;
40: use Apache::lonannounce;
41: use Apache::lonlocal;
42: use GDBM_File;
43:
44: sub redirect_user {
45: my ($r,$title,$url,$msg,$launch_nav) = @_;
46: $msg = $title if (! defined($msg));
47: &Apache::loncommon::content_type($r,'text/html');
48: &Apache::loncommon::no_cache($r);
49: $r->send_http_header;
50: my $swinfo=&Apache::lonmenu::rawconfig();
51: my $navwindow;
52: if ($launch_nav eq 'on') {
53: $navwindow.=&Apache::lonnavmaps::launch_win('now');
54: } else {
55: $navwindow.=&Apache::lonnavmaps::close();
56: }
57: my $bodytag=&Apache::loncommon::bodytag('Switching Role');
58: # Note to style police:
59: # This must only replace the spaces, nothing else, or it bombs elsewhere.
60: $url=~s/ /\%20/g;
61: $r->print(<<ENDREDIR);
62: <head><title>$title</title>
63: <meta HTTP-EQUIV="Refresh" CONTENT="1; url=$url">
64: </head>
65: <html>
66: $bodytag
67: <script type="text/javascript">
68: $swinfo
69: </script>
70: $navwindow
71: <h1>$msg</h1>
72: <a href="$url">Continue</a>
73: </body>
74: </html>
75: ENDREDIR
76: return;
77: }
78:
79: sub handler {
80:
81: my $r = shift;
82:
83: my $now=time;
84: my $then=$env{'user.login.time'};
85: my $envkey;
86: my %dcroles = ();
87: my $numdc = &check_fordc(\%dcroles,$then);
88:
89: # ================================================================== Roles Init
90: if ($env{'form.selectrole'}) {
91: if ($env{'request.course.id'}) {
92: my %temp=('logout_'.$env{'request.course.id'} => time);
93: &Apache::lonnet::put('email_status',\%temp);
94: &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'});
95: }
96: &Apache::lonnet::appenv("request.course.id" => '',
97: "request.course.fn" => '',
98: "request.course.uri" => '',
99: "request.course.sec" => '',
100: "request.role" => 'cm',
101: "request.role.adv" => $env{'user.adv'},
102: "request.role.domain" => $env{'user.domain'});
103:
104: # Check if user is a DC trying to enter a course and needs privs to be created
105: if ($numdc > 0) {
106: foreach my $envkey (keys %env) {
107: if ($envkey =~ m-^form\.cc\./(\w+)/(\w+)$-) {
108: if ($dcroles{$1}) {
109: my $cckey = 'user.role.cc./'.$1.'/'.$2;
110: &check_privs($cckey,$then,$now);
111: }
112: last;
113: }
114: }
115: }
116:
117: foreach $envkey (keys %env) {
118: next if ($envkey!~/^user\.role\./);
119: my ($where,$trolecode,$role,$tstatus,$tend,$tstart);
120: &role_status($envkey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
121: if ($env{'form.'.$trolecode}) {
122: if ($tstatus eq 'is') {
123: $where=~s/^\///;
124: my ($cdom,$cnum,$csec)=split(/\//,$where);
125: # store role if recent_role list being kept
126: if ($env{'environment.recentroles'}) {
127: &Apache::lonhtmlcommon::store_recent('roles',
128: $trolecode,' ');
129: }
130:
131:
132: # check for keyed access
133: if (($role eq 'st') &&
134: ($env{'course.'.$cdom.'_'.$cnum.'.keyaccess'} eq 'yes')) {
135: # who is key authority?
136: my $authdom=$cdom;
137: my $authnum=$cnum;
138: if ($env{'course.'.$cdom.'_'.$cnum.'.keyauth'}) {
139: ($authnum,$authdom)=
140: split(/\W/,$env{'course.'.$cdom.'_'.$cnum.'.keyauth'});
141: }
142: # check with key authority
143: unless (&Apache::lonnet::validate_access_key(
144: $env{'environment.key.'.$cdom.'_'.$cnum},
145: $authdom,$authnum)) {
146: # there is no valid key
147: if ($env{'form.newkey'}) {
148: # student attempts to register a new key
149: &Apache::loncommon::content_type($r,'text/html');
150: &Apache::loncommon::no_cache($r);
151: $r->send_http_header;
152: my $swinfo=&Apache::lonmenu::rawconfig();
153: my $bodytag=&Apache::loncommon::bodytag
154: ('Verifying Access Key to Unlock this Course');
155: my $buttontext=&mt('Enter Course');
156: my $message=&mt('Successfully registered key');
157: my $assignresult=
158: &Apache::lonnet::assign_access_key(
159: $env{'form.newkey'},
160: $authdom,$authnum,
161: $cdom,$cnum,
162: $env{'user.domain'},
163: $env{'user.name'},
164: 'Assigned from '.$ENV{'REMOTE_ADDR'}.' at '.localtime().' for '.
165: $trolecode);
166: unless ($assignresult eq 'ok') {
167: $assignresult=~s/^error\:\s*//;
168: $message=&mt($assignresult).
169: '<br /><a href="/adm/logout">'.
170: &mt('Logout').'</a>';
171: $buttontext=&mt('Re-Enter Key');
172: }
173: $r->print(<<ENDENTEREDKEY);
174: <head><title>Verifying Course Access Key</title>
175: </head>
176: <html>
177: $bodytag
178: <script>
179: $swinfo
180: </script>
181: <form method="post">
182: <input type="hidden" name="selectrole" value="1" />
183: <input type="hidden" name="$trolecode" value="1" />
184: <font size="+2">$message</font><br />
185: <input type="submit" value="$buttontext" />
186: </form>
187: </body></html>
188: ENDENTEREDKEY
189: return OK;
190: } else {
191: # print form to enter a new key
192: &Apache::loncommon::content_type($r,'text/html');
193: &Apache::loncommon::no_cache($r);
194: $r->send_http_header;
195: my $swinfo=&Apache::lonmenu::rawconfig();
196: my $bodytag=&Apache::loncommon::bodytag
197: ('Enter Access Key to Unlock this Course');
198: $r->print(<<ENDENTERKEY);
199: <head><title>Entering Course Access Key</title>
200: </head>
201: <html>
202: $bodytag
203: <script>
204: $swinfo
205: </script>
206: <form method="post">
207: <input type="hidden" name="selectrole" value="1" />
208: <input type="hidden" name="$trolecode" value="1" />
209: <input type="text" size="20" name="newkey" value="$env{'form.newkey'}" />
210: <input type="submit" value="Enter key" />
211: </form>
212: </body></html>
213: ENDENTERKEY
214: return OK;
215: }
216: }
217: }
218: &Apache::lonnet::log($env{'user.domain'},
219: $env{'user.name'},
220: $env{'user.home'},
221: "Role ".$trolecode);
222:
223: &Apache::lonnet::appenv(
224: 'request.role' => $trolecode,
225: 'request.role.domain' => $cdom,
226: 'request.course.sec' => $csec);
227: my $tadv=0;
228: my $msg=&mt('Entering course ...');
229:
230: if (($cnum) && ($role ne 'ca')) {
231: my ($furl,$ferr)=
232: &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
233: if (($env{'form.orgurl'}) &&
234: ($env{'form.orgurl'}!~/^\/adm\/flip/)) {
235: my $dest=$env{'form.orgurl'};
236: if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
237: &Apache::lonnet::appenv('request.role.adv'=>$tadv);
238: $r->internal_redirect($dest);
239: return OK;
240: } else {
241: unless ($env{'request.course.id'}) {
242: &Apache::lonnet::appenv(
243: "request.course.id" => $cdom.'_'.$cnum);
244: $furl='/adm/roles?tryagain=1';
245: $msg=
246: '<h1><font color=red>'.
247: &mt('Could not initialize course at this time.').
248: '</font></h1><h3>'.&mt('Please try again.').'</h3>'.$ferr;
249: }
250: if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
251: &Apache::lonnet::appenv('request.role.adv'=>$tadv);
252:
253: # Check to see if the user is a CC entering a course
254: # for the first time
255: my (undef, undef, $role, $courseid) = split(/\./, $envkey);
256: if (substr($courseid, 0, 1) eq '/') {
257: $courseid = substr($courseid, 1);
258: }
259: $courseid =~ s/\//_/;
260: if ($role eq 'cc' && $env{'course.' . $courseid .
261: '.course.helper.not.run'}) {
262: $furl = "/adm/helper/course.initialization.helper";
263: }
264: # Send the user to the course they selected
265: &redirect_user($r,&mt('Entering Course'),
266: $furl,$msg,
267: $env{'environment.remotenavmap'});
268: return OK;
269: }
270: }
271: #
272: # Send the user to the construction space they selected
273: if ($role =~ /^(au|ca)$/) {
274: my $redirect_url = '/priv/';
275: if ($role eq 'au') {
276: $redirect_url.=$env{'user.name'};
277: } else {
278: $where =~ /\/(.*)$/;
279: $redirect_url .= $1;
280: }
281: $redirect_url .= '/';
282: &redirect_user($r,&mt('Entering Construction Space'),
283: $redirect_url);
284: return OK;
285: }
286: if ($role eq 'dc') {
287: my $redirect_url = '/adm/menu/';
288: &redirect_user($r,&mt('Loading Domain Coordinator Menu'),
289: $redirect_url);
290: return OK;
291: }
292: }
293: }
294: }
295: }
296:
297:
298: # =============================================================== No Roles Init
299:
300: &Apache::loncommon::content_type($r,'text/html');
301: &Apache::loncommon::no_cache($r);
302: $r->send_http_header;
303: return OK if $r->header_only;
304:
305: my $swinfo=&Apache::lonmenu::rawconfig();
306: my $bodytag=&Apache::loncommon::bodytag('User Roles');
307: my $helptag='<table><tr><td>'.&Apache::loncommon::help_open_menu('','General Intro','General_Intro','User Roles',1,undef,undef,undef,undef,,&mt("Click here for help")).'</td></td></tr></table>';
308: $r->print(<<ENDHEADER);
309: <html>
310: <head>
311: <title>LON-CAPA User Roles</title>
312: </head>
313: $bodytag
314: $helptag<br />
315: <script>
316: $swinfo
317: window.focus();
318: </script>
319: ENDHEADER
320:
321: # ------------------------------------------ Get Error Message from Environment
322:
323: my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$env{'user.error.msg'});
324: if ($env{'user.error.msg'}) {
325: $r->log_reason(
326: "$msg for $env{'user.name'} domain $env{'user.domain'} access $priv",$fn);
327: }
328:
329: # ------------------------------------------------- Can this user re-init, etc?
330:
331: my $advanced=$env{'user.adv'};
332: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['tryagain']);
333: my $tryagain=$env{'form.tryagain'};
334:
335: # -------------------------------------------------------- Generate Page Output
336: # --------------------------------------------------------------- Error Header?
337: if ($error) {
338: $r->print("<h1>LON-CAPA Access Control</h1>");
339: $r->print("<hr><pre>Access : ".
340: Apache::lonnet::plaintext($priv)."\n");
341: $r->print("Resource: ".&Apache::lonenc::check_encrypt($fn)."\n");
342: $r->print("Action : $msg\n</pre><hr />");
343: my $url=$fn;
344: my $last;
345: if (tie(my %hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
346: &GDBM_READER(),0640)) {
347: $last=$hash{'last_known'};
348: untie(%hash);
349: }
350: if ($last) { $fn.='?symb='.&Apache::lonnet::escape($last); }
351:
352: &Apache::londocs::changewarning($r,undef,'You have modified your course recently, [_1] may fix this access problem.',
353: &Apache::lonenc::check_encrypt($fn));
354: } else {
355: if ($env{'user.error.msg'}) {
356: $r->print(
357: '<h3><font color=red>'.
358: &mt('You need to choose another user role or enter a specific course for this function').'</font></h3>');
359: }
360: }
361: # -------------------------------------------------------- Choice or no choice?
362: if ($nochoose) {
363: if ($advanced) {
364: $r->print("<h2>".&mt('Assigned User Roles')."</h2>\n");
365: } else {
366: $r->print("<h2>".&mt('Sorry ...')."</h2>\n".
367: &mt('This resource might be part of'));
368: if ($env{'request.course.id'}) {
369: $r->print(&mt(' another'));
370: } else {
371: $r->print(&mt(' a certain'));
372: }
373: $r->print(&mt(' course.').'</body></html>');
374: return OK;
375: }
376: } else {
377: if ($advanced) {
378: $r->print(&mt("Your home server is ").
379: $Apache::lonnet::hostname{&Apache::lonnet::homeserver
380: ($env{'user.name'},$env{'user.domain'})}.
381: "<br />\n");
382: $r->print(&mt(
383: "Author and Co-Author roles may not be available on servers other than your home server."));
384: }
385: if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
386: $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
387: }
388: $r->print('<form method="post" name="rolechoice" action="'.(($fn)?$fn:$r->uri).'">');
389: $r->print('<input type="hidden" name="orgurl" value="'.$fn.'" />');
390: $r->print('<input type="hidden" name="selectrole" value="1" />');
391: }
392: if ($env{'user.adv'}) {
393: $r->print(
394: '<br /><label>'.&mt('Show all roles').': <input type="checkbox" name="showall"');
395: if ($env{'form.showall'}) { $r->print(' checked="checked" '); }
396: $r->print(' /></label><input type="submit" value="'.&mt('Display').'" />');
397: }
398:
399: my (%roletext,%sortrole,%roleclass);
400: my $countactive=0;
401: my $inrole=0;
402: my $possiblerole='';
403: foreach $envkey (sort keys %env) {
404: my $button = 1;
405: my $switchserver='';
406: my $roletext;
407: my $sortkey;
408: if ($envkey=~/^user\.role\./) {
409: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend,$tfont);
410: &role_status($envkey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
411: next if (!defined($role) || $role eq '');
412: $tremark='';
413: $tpstart=' ';
414: $tpend=' ';
415: $tfont='#000000';
416: if ($tstart) {
417: $tpstart=&Apache::lonlocal::locallocaltime($tstart);
418: }
419: if ($tend) {
420: $tpend=&Apache::lonlocal::locallocaltime($tend);
421: }
422: if ($env{'request.role'} eq $trolecode) {
423: $tstatus='selected';
424: }
425: my $tbg;
426: if (($tstatus eq 'is') || ($tstatus eq 'selected') ||
427: ($env{'form.showall'})) {
428: if ($tstatus eq 'is') {
429: $tbg='#77FF77';
430: $tfont='#003300';
431: $possiblerole=$trolecode;
432: $countactive++;
433: } elsif ($tstatus eq 'future') {
434: $tbg='#FFFF77';
435: $button=0;
436: } elsif ($tstatus eq 'will') {
437: $tbg='#FFAA77';
438: $tremark.=&mt('Active at next login. ');
439: } elsif ($tstatus eq 'expired') {
440: $tbg='#FF7777';
441: $tfont='#330000';
442: $button=0;
443: } elsif ($tstatus eq 'will_not') {
444: $tbg='#AAFF77';
445: $tremark.=&mt('Expired after logout. ');
446: } elsif ($tstatus eq 'selected') {
447: $tbg='#11CC55';
448: $tfont='#002200';
449: $inrole=1;
450: $countactive++;
451: $tremark.=&mt('Currently selected. ');
452: }
453: my $trole;
454: if ($role =~ /^cr\//) {
455: my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
456: $tremark.='<br>'.&mt('Defined by ').$rauthor.
457: &mt(' at ').$rdomain.'.';
458: $trole=$rrole;
459: } else {
460: $trole=Apache::lonnet::plaintext($role);
461: }
462: my $ttype;
463: my $twhere;
464: my ($tdom,$trest,$tsection)=
465: split(/\//,Apache::lonnet::declutter($where));
466: # First, Co-Authorship roles
467: if ($role eq 'ca') {
468: my $home = &Apache::lonnet::homeserver($trest,$tdom);
469: my $allowed=0;
470: my @ids=&Apache::lonnet::current_machine_ids();
471: foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
472: if (!$allowed) {
473: $button=0;
474: $switchserver=&Apache::lonnet::escape('http://'.
475: $Apache::lonnet::hostname{$home}.
476: '/adm/login?domain='.$env{'user.domain'}.
477: '&username='.$env{'user.name'}.
478: '&firsturl=/priv/'.$trest.'/');
479: }
480: #next if ($home eq 'no_host');
481: $home = $Apache::lonnet::hostname{$home};
482: $ttype='Construction Space';
483: $twhere=&mt('User').': '.$trest.'<br />'.&mt('Domain').
484: ': '.$tdom.'<br />'.
485: ' '.&mt('Server').': '.$home;
486: $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
487: $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$trest.'/');
488: $sortkey=$role."$trest:$tdom";
489: } elsif ($role eq 'au') {
490: # Authors
491: my $home = &Apache::lonnet::homeserver
492: ($env{'user.name'},$env{'user.domain'});
493: my $allowed=0;
494: my @ids=&Apache::lonnet::current_machine_ids();
495: foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
496: if (!$allowed) {
497: $button=0;
498: $switchserver=&Apache::lonnet::escape('http://'.
499: $Apache::lonnet::hostname{$home}.
500: '/adm/login?domain='.$env{'user.domain'}.
501: '&username='.$env{'user.name'}.
502: '&firsturl=/priv/'.$env{'user.name'}.'/');
503: }
504: #next if ($home eq 'no_host');
505: $home = $Apache::lonnet::hostname{$home};
506: $ttype='Construction Space';
507: $twhere=&mt('Domain').': '.$tdom.'<br />'.&mt('Server').
508: ': '.$home;
509: $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
510: $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$env{'user.name'}.'/');
511: $sortkey=$role;
512: } elsif ($trest) {
513: $ttype='Course';
514: if ($tsection) {
515: $ttype.='<br>'.&mt('Section/Group').': '.$tsection;
516: }
517: my $tcourseid=$tdom.'_'.$trest;
518: if ($env{'course.'.$tcourseid.'.description'}) {
519: $twhere=$env{'course.'.$tcourseid.'.description'};
520: $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
521: unless ($twhere eq &mt('Currently not available')) {
522: $twhere.=' <font size="-2">'.
523: &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
524: '</font>';
525: }
526: } else {
527: my %newhash=&Apache::lonnet::coursedescription($tcourseid);
528: if (%newhash) {
529: $sortkey=$role."\0".$tdom."\0".$newhash{'description'}.
530: "\0".$envkey;
531: $twhere=$newhash{'description'}.
532: ' <font size="-2">'.
533: &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
534: '</font>';
535: } else {
536: $twhere=&mt('Currently not available');
537: $env{'course.'.$tcourseid.'.description'}=$twhere;
538: $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
539: }
540: }
541: if ($role ne 'st') { $twhere.="<br />".&mt('Domain').":".$tdom; }
542: } elsif ($tdom) {
543: $ttype='Domain';
544: $twhere=$tdom;
545: $sortkey=$role.$twhere;
546: } else {
547: $ttype='System';
548: $twhere=&mt('system wide');
549: $sortkey=$role.$twhere;
550: }
551:
552: $roletext.=&build_roletext($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$tfont,$trole,$ttype,$twhere,$tpstart,$tpend,$nochoose,$button,$switchserver);
553: $roletext{$envkey}=$roletext;
554: if (!$sortkey) {$sortkey=$twhere."\0".$envkey;}
555: $sortrole{$sortkey}=$envkey;
556: $roleclass{$envkey}=$ttype;
557: }
558: }
559: }
560: # No active roles
561: if ($countactive==0) {
562: if ($inrole) {
563: $r->print('<h2>'.&mt('Currently no additional roles or courses').'</h2>');
564: } else {
565: $r->print('<h2>'.&mt('Currently no active roles or courses').'</h2>');
566: }
567: $r->print('</form></body></html>');
568: return OK;
569: # Is there only one choice?
570: } elsif (($countactive==1) && ($env{'request.role'} eq 'cm')) {
571: $r->print('<h3>'.&mt('Please stand by.').'</h3>'.
572: '<input type="hidden" name="'.$possiblerole.'" value="1" />');
573: $r->print("</form>\n");
574: $r->rflush();
575: $r->print('<script>document.forms.rolechoice.submit();</script>');
576: $r->print('</body></html>');
577: return OK;
578: }
579: # More than one possible role
580: # ----------------------------------------------------------------------- Table
581: unless (($advanced) || ($nochoose)) {
582: $r->print("<h2>".&mt('Select a Course to Enter')."</h2>\n");
583: }
584: $r->print('<br /><table><tr>');
585: unless ($nochoose) { $r->print('<th> </th>'); }
586: $r->print('<th>'.&mt('User Role').'</th><th colspan=2>'.&mt('Extent').
587: '</th><th>'.&mt('Start').'</th><th>'.&mt('End').'</th><th>'.
588: &mt('Remarks and Calendar Announcements').'</th></tr>'."\n");
589: my $doheaders=-1;
590: foreach my $type ('Construction Space','Course','Domain','System') {
591: my $haverole=0;
592: foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
593: if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) {
594: $haverole=1;
595: }
596: }
597: if ($haverole) { $doheaders++; }
598: }
599:
600: if ($env{'environment.recentroles'}) {
601: my %recent_roles =
602: &Apache::lonhtmlcommon::get_recent('roles',$env{'environment.recentrolesn'});
603: my $output='';
604: foreach (sort(keys(%recent_roles))) {
605: if (defined($roletext{'user.role.'.$_})) {
606: $output.=$roletext{'user.role.'.$_};
607: } elsif ($numdc > 0) {
608: unless ($_ =~/^error\:/) {
609: $output.=&display_cc_role('user.role.'.$_);
610: }
611: }
612: }
613: if ($output) {
614: $r->print("<tr bgcolor='#BBffBB'><td align='center' colspan='7'>".
615: &mt('Recent Roles')."</td>");
616: $r->print($output);
617: $r->print("</tr>");
618: $doheaders ++;
619: }
620: }
621:
622: if ($numdc > 0) {
623: $r->print(&coursepick_jscript());
624: $r->print(&Apache::loncommon::coursebrowser_javascript());
625: }
626: foreach my $type ('Construction Space','Course','Domain','System') {
627: my $output;
628: foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
629: if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) {
630: $output.=$roletext{$sortrole{$which}};
631: if ($sortrole{$which} =~ m-dc\./(\w+)/-) {
632: if ($dcroles{$1}) {
633: $output .= &allcourses_row($1);
634: }
635: }
636: }
637: }
638: if ($output) {
639: if ($doheaders > 0) {
640: $r->print("<tr bgcolor='#BBffBB'>".
641: "<td align='center' colspan='7'>".&mt($type)."</td></tr>");
642: }
643: $r->print($output);
644: }
645: }
646: my $tremark='';
647: my $tfont='#003300';
648: if ($env{'request.role'} eq 'cm') {
649: $r->print('<tr bgcolor="#11CC55">');
650: $tremark=&mt('Currently selected. ');
651: $tfont='#002200';
652: } else {
653: $r->print('<tr bgcolor="#77FF77">');
654: }
655: unless ($nochoose) {
656: if ($env{'request.role'} ne 'cm') {
657: $r->print('<td><input type=submit value="'.
658: &mt('Select').'" name="cm"></td>');
659: } else {
660: $r->print('<td> </td>');
661: }
662: }
663: $r->print('<td colspan=5><font color="'.$tfont.'">'.&mt('No role specified').
664: '</font></td><td><font color="'.$tfont.'">'.$tremark.
665: ' </font></td></tr>'."\n");
666:
667: $r->print('</table>');
668: unless ($nochoose) {
669: $r->print("</form>\n");
670: }
671: # ------------------------------------------------------------ Privileges Info
672: if (($advanced) && (($env{'user.error.msg'}) || ($error))) {
673: $r->print('<hr><h2>Current Privileges</h2>');
674:
675: foreach $envkey (sort keys %env) {
676: if ($envkey=~/^user\.priv\.$env{'request.role'}\./) {
677: my $where=$envkey;
678: $where=~s/^user\.priv\.$env{'request.role'}\.//;
679: my $ttype;
680: my $twhere;
681: my ($tdom,$trest,$tsec)=
682: split(/\//,Apache::lonnet::declutter($where));
683: if ($trest) {
684: if ($env{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
685: $ttype='Construction Space';
686: $twhere='User: '.$trest.', Domain: '.$tdom;
687: } else {
688: $ttype='Course';
689: $twhere=$env{'course.'.$tdom.'_'.$trest.'.description'};
690: if ($tsec) {
691: $twhere.=' (Section/Group: '.$tsec.')';
692: }
693: }
694: } elsif ($tdom) {
695: $ttype='Domain';
696: $twhere=$tdom;
697: } else {
698: $ttype='System';
699: $twhere='/';
700: }
701: $r->print("\n<h3>".$ttype.': '.$twhere.'</h3><ul>');
702: foreach (sort split(/:/,$env{$envkey})) {
703: if ($_) {
704: my ($prv,$restr)=split(/\&/,$_);
705: my $trestr='';
706: if ($restr ne 'F') {
707: my $i;
708: $trestr.=' (';
709: for ($i=0;$i<length($restr);$i++) {
710: $trestr.=
711: Apache::lonnet::plaintext(substr($restr,$i,1));
712: if ($i<length($restr)-1) { $trestr.=', '; }
713: }
714: $trestr.=')';
715: }
716: $r->print('<li>'.
717: Apache::lonnet::plaintext($prv).$trestr.
718: '</li>');
719: }
720: }
721: $r->print('</ul>');
722: }
723: }
724: }
725: $r->print(&Apache::lonnet::getannounce());
726: if ($advanced) {
727: $r->print('<p><small><i>This is LON-CAPA '.
728: $r->dir_config('lonVersion').'</i><br />'.
729: '<a href="/adm/logout">'.&mt('Logout').'</a></small></p>');
730: }
731: $r->print("</body></html>\n");
732: return OK;
733: }
734:
735: sub role_status {
736: my ($rolekey,$then,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
737: my @pwhere = ();
738: if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
739: (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
740: unless (!defined($$role) || $$role eq '') {
741: $$where=join('.',@pwhere);
742: $$trolecode=$$role.'.'.$$where;
743: ($$tstart,$$tend)=split(/\./,$env{$rolekey});
744: $$tstatus='is';
745: if ($$tstart && $$tstart>$then) {
746: $$tstatus='future';
747: if ($$tstart<$now) { $$tstatus='will'; }
748: }
749: if ($$tend) {
750: if ($$tend<$then) {
751: $$tstatus='expired';
752: } elsif ($$tend<$now) {
753: $$tstatus='will_not';
754: }
755: }
756: }
757: }
758: }
759:
760: sub build_roletext {
761: my ($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$tfont,$trole,$ttype,$twhere,$tpstart,$tpend,$nochoose,$button,$switchserver) = @_;
762: my $roletext='<tr bgcolor="'.$tbg.'">';
763: unless ($nochoose) {
764: if (!$button) {
765: if ($switchserver) {
766: $roletext.='<td><a href="/adm/logout?handover='.
767: $switchserver.'">'.&mt('Switch Server').'</a></td>';
768: } else {
769: $roletext.=('<td> </td>');
770: }
771: } elsif ($tstatus eq 'is') {
772: $roletext.=('<td><input type=submit value="'.
773: &mt('Select').'" name="'.
774: $trolecode.'"></td>');
775: } elsif ($tryagain) {
776: $roletext.=
777: '<td><input type=submit value="'.
778: &mt('Try Selecting Again').'" name="'.$trolecode.'"></td>';
779: } elsif ($advanced) {
780: $roletext.=
781: '<td><input type=submit value="'.
782: &mt('Re-Initialize').'" name="'.$trolecode.'"></td>';
783: } else {
784: $roletext.='<td> </td>';
785: }
786: }
787: $tremark.=&Apache::lonannounce::showday(time,1,
788: &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
789:
790:
791: $roletext.='<td><font color="'.$tfont.'">'.$trole.
792: '</font></td><td><font color="'.$tfont.'">'.$ttype.
793: '</font></td><td><font color="'.$tfont.'">'.$twhere.
794: '</font></td><td><font color="'.$tfont.'">'.$tpstart.
795: '</font></td><td><font color="'.$tfont.'">'.$tpend.
796: '</font></td><td><font color="'.$tfont.'">'.$tremark.
797: ' </font></td></tr>'."\n";
798: return $roletext;
799: }
800:
801: sub check_privs {
802: my ($cckey,$then,$now) = @_;
803: if ($env{$cckey}) {
804: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend,$tfont);
805: &role_status($cckey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
806: unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
807: &set_privileges($1,$2);
808: }
809: } else {
810: &set_privileges($1,$2);
811: }
812: }
813:
814: sub check_fordc {
815: my ($dcroles,$then) = @_;
816: my $numdc = 0;
817: if ($env{'user.adv'}) {
818: foreach my $envkey (sort keys %env) {
819: if ($envkey=~/^user\.role\.dc\.\/(\w+)\/$/) {
820: my $dcdom = $1;
821: my $livedc = 1;
822: my ($tstart,$tend)=split(/\./,$env{$envkey});
823: if ($tstart && $tstart>$then) { $livedc = 0; }
824: if ($tend && $tend <$then) { $livedc = 0; }
825: if ($livedc) {
826: $$dcroles{$dcdom} = $envkey;
827: $numdc++;
828: }
829: }
830: }
831: }
832: return $numdc;
833: }
834:
835: sub courselink {
836: my ($dcdom) = @_;
837: my $courseform=&Apache::loncommon::selectcourse_link
838: ('rolechoice','dccourse_'.$dcdom,'dcdomain_'.$dcdom,'coursedesc_'.$dcdom,$dcdom);
839: my $hiddenitems = '<input type="hidden" name="dcdomain_'.$dcdom.'" value="'.$dcdom.'" />'.
840: '<input type="hidden" name="origdom_'.$dcdom.'" value="'.$dcdom.'" />'.
841: '<input type="hidden" name="dccourse_'.$dcdom.'" value="" />'.
842: '<input type="hidden" name="coursedesc_'.$dcdom.'" value="" />';
843: return $courseform.$hiddenitems;
844: }
845:
846: sub coursepick_jscript {
847: my $verify_script = <<"END";
848: <script>
849: function verifyCoursePick(caller) {
850: var numbutton = getIndex(caller)
851: var pickedCourse = document.rolechoice.elements[numbutton+4].value
852: var pickedDomain = document.rolechoice.elements[numbutton+2].value
853: if (document.rolechoice.elements[numbutton+2].value == document.rolechoice.elements[numbutton+3].value) {
854: if (pickedCourse != '') {
855: if (numbutton != -1) {
856: var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
857: document.rolechoice.elements[numbutton+1].name = courseTarget
858: document.rolechoice.submit()
859: }
860: }
861: else {
862: alert("Please use the 'Select Course' link to open a separate pick course window where you may select the course you wish to enter.");
863: }
864: }
865: else {
866: alert("You can only use this screen to select courses in the current domain")
867: }
868: }
869: function getIndex(caller) {
870: for (var i=0;i<document.rolechoice.elements.length;i++) {
871: if (document.rolechoice.elements[i] == caller) {
872: return i;
873: }
874: }
875: return -1;
876: }
877: </script>
878: END
879: return $verify_script;
880: }
881:
882: sub processpick {
883: my $dcdom = shift;
884: my $process_pick = <<"END";
885: <script>
886: function process_pick(dom) {
887: var numbutton = getIndex(dom)
888: var pickedCourse = opener.document.rolechoice.dccourse_$dcdom.value
889: var pickedDomain = opener.document.rolechoice.dcdomain_$dcdom.value
890: if (opener.document.rolechoice.dcdomain_$dcdom.value == opener.document.rolechoice.origdom_$dcdom.value) {
891: if (pickedCourse != '') {
892: if (numbutton != -1) {
893: var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
894: opener.document.rolechoice.elements[numbutton+1].name = courseTarget
895: opener.document.rolechoice.submit()
896: }
897: }
898: }
899: }
900:
901: function getIndex(dom) {
902: var callername = 'ccpick_'+dom
903: for (var i=0;i<opener.document.rolechoice.elements.length;i++) {
904: var elemname = opener.document.rolechoice.elements[i].name
905: if (elemname == callername) {
906: return i;
907: }
908: }
909: return -1;
910: }
911: </script>
912: END
913: return $process_pick;
914: }
915:
916: sub display_cc_role {
917: my $rolekey = shift;
918: my $roletext;
919: my $advanced = $env{'user.adv'};
920: my $tryagain = $env{'form.tryagain'};
921: unless ($rolekey =~/^error\:/) {
922: if ($rolekey =~ m-^user\.role.cc\./(\w+)/(\w+)$-) {
923: my $tcourseid = $1.'_'.$2;
924: my $trolecode = 'cc./'.$1.'/'.$2;
925: my $trole = Apache::lonnet::plaintext('cc');
926: my $twhere;
927: my $tbg='#77FF77';
928: my $tfont='#003300';
929: my %newhash=&Apache::lonnet::coursedescription($tcourseid);
930: if (%newhash) {
931: $twhere=$newhash{'description'}.
932: ' <font size="-2">'.
933: &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$2,$1,$tfont).
934: '</font>';
935: } else {
936: $twhere=&mt('Currently not available');
937: $env{'course.'.$tcourseid.'.description'}=$twhere;
938: }
939: $twhere.="<br />".&mt('Domain').":".$1;
940: $roletext = &build_roletext($trolecode,$1,$2,'is',$tryagain,$advanced,'',$tbg,$tfont,$trole,&mt('Course'),$twhere,'','','',1,'');
941: }
942: }
943: return $roletext;
944: }
945:
946: sub allcourses_row {
947: my $dcdom = shift;
948: my $ccrole = Apache::lonnet::plaintext('cc');
949: my $selectlink = &courselink($dcdom);
950: my $output = '<tr bgcolor="#77FF77">'.
951: '<td><input type="button" value="'.
952: &mt('Select').'" name="ccpick_'.$dcdom.'"'.
953: 'onClick="verifyCoursePick(this)">'.
954: '<input type="hidden" name="pick_'.$dcdom.'" value="1"></td>'.
955: '<td><font color="#002200">'.
956: $ccrole.'</font></td><td>'.&mt('Course').'</td>'.
957: '<td><font color="#002200">'.&mt('All courses').':<b> '.
958: $selectlink.'</b>'.
959: '<br />'.&mt('Domain').':'.$dcdom.'</font>'.
960: '<td colspan="4"><font color="#002200">'.
961: &mt('Course Coordinator access to all courses in domain').
962: ': <b>'.$dcdom.'</b></font></td></tr>'."\n";
963: return $output;
964: }
965:
966: sub recent_filename {
967: my $area=shift;
968: return 'nohist_recent_'.&Apache::lonnet::escape($area);
969: }
970:
971: sub set_privileges {
972: my ($dcdom,$pickedcourse) = @_;
973: my $area = '/'.$dcdom.'/'.$pickedcourse;
974: my $role = 'cc';
975: my $spec = $role.'.'.$area;
976: my $userroles = &Apache::lonnet::set_arearole($role,$area,'','',$dcdom,$env{'user.name'});
977: my %ccrole = ();
978: &Apache::lonnet::standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
979: my ($author,$adv)= &Apache::lonnet::set_userprivs(\$userroles,\%ccrole);
980: my @newprivs = split/\n/,$userroles;
981: my %newccroles = ();
982: foreach (@newprivs) {
983: my ($key,$val) = split/=/,$_;
984: $newccroles{$key} = $val;
985: }
986: &Apache::lonnet::appenv(%newccroles);
987: &Apache::lonnet::log($env{'user.domain'},
988: $env{'user.name'},
989: $env{'user.home'},
990: "Role ".$role);
991: &Apache::lonnet::appenv(
992: 'request.role' => $role,
993: 'request.role.domain' => $dcdom,
994: 'request.course.sec' => '');
995: my $tadv=0;
996: if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
997: &Apache::lonnet::appenv('request.role.adv' => $tadv);
998: }
999:
1000: 1;
1001: __END__
1002:
1003: =head1 NAME
1004:
1005: Apache::lonroles - User Roles Screen
1006:
1007: =head1 SYNOPSIS
1008:
1009: Invoked by /etc/httpd/conf/srm.conf:
1010:
1011: <Location /adm/roles>
1012: PerlAccessHandler Apache::lonacc
1013: SetHandler perl-script
1014: PerlHandler Apache::lonroles
1015: ErrorDocument 403 /adm/login
1016: ErrorDocument 500 /adm/errorhandler
1017: </Location>
1018:
1019: =head1 OVERVIEW
1020:
1021: =head2 Choosing Roles
1022:
1023: C<lonroles> is a handler that allows a user to switch roles in
1024: mid-session. LON-CAPA attempts to work with "No Role Specified", the
1025: default role that a user has before selecting a role, as widely as
1026: possible, but certain handlers for example need specification which
1027: course they should act on, etc. Both in this scenario, and when the
1028: handler determines via C<lonnet>'s C<&allowed> function that a certain
1029: action is not allowed, C<lonroles> is used as error handler. This
1030: allows the user to select another role which may have permission to do
1031: what they were trying to do. C<lonroles> can also be accessed via the
1032: B<CRS> button in the Remote Control.
1033:
1034: =begin latex
1035:
1036: \begin{figure}
1037: \begin{center}
1038: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
1039: \caption{\label{Sample_Roles_Screen}Sample Roles Screen}
1040: \end{center}
1041: \end{figure}
1042:
1043: =end latex
1044:
1045: =head2 Role Initialization
1046:
1047: The privileges for a user are established at login time and stored in the session environment. As a consequence, a new role does not become active till the next login. Handlers are able to query for privileges using C<lonnet>'s C<&allowed> function. When a user first logs in, their role is the "common" role, which means that they have the sum of all of their privileges. During a session it might become necessary to choose a particular role, which as a consequence also limits the user to only the privileges in that particular role.
1048:
1049: =head1 INTRODUCTION
1050:
1051: This module enables a user to select what role he wishes to
1052: operate under (instructor, student, teaching assistant, course
1053: coordinator, etc). These roles are pre-established by the actions
1054: of upper-level users.
1055:
1056: This is part of the LearningOnline Network with CAPA project
1057: described at http://www.lon-capa.org.
1058:
1059: =head1 HANDLER SUBROUTINE
1060:
1061: This routine is called by Apache and mod_perl.
1062:
1063: =over 4
1064:
1065: =item *
1066:
1067: Roles Initialization (yes/no)
1068:
1069: =item *
1070:
1071: Get Error Message from Environment
1072:
1073: =item *
1074:
1075: Who is this?
1076:
1077: =item *
1078:
1079: Generate Page Output
1080:
1081: =item *
1082:
1083: Choice or no choice
1084:
1085: =item *
1086:
1087: Table
1088:
1089: =item *
1090:
1091: Privileges
1092:
1093: =back
1094:
1095: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>