Annotation of loncom/publisher/lonrights.pm, revision 1.10
1.1 www 1: # The LearningOnline Network with CAPA
2: # Handler to show and edit custom distribution rights
3: #
1.10 ! www 4: # $Id: lonrights.pm,v 1.9 2003/03/28 01:53:55 www Exp $
1.1 www 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:
31: package Apache::lonrights;
32:
33: use strict;
34: use Apache::Constants qw(:common :http);
35: use Apache::lonnet();
36: use Apache::loncommon();
1.2 www 37: use HTML::LCParser;
38: use Apache::File;
1.10 ! www 39: use Apache::lonlocal;
1.1 www 40:
41: sub handler {
42:
43: my $r=shift;
1.10 ! www 44: &Apache::loncommon::content_type($r,'text/html');
1.1 www 45: $r->send_http_header;
46:
1.3 www 47: $r->print(
1.8 www 48: '<html><head><title>LON-CAPA Custom Distribution Rights</title>'.
49: &Apache::loncommon::coursebrowser_javascript().'</head>');
1.1 www 50:
51: $r->print(&Apache::loncommon::bodytag('Custom Distribution Rights'));
1.5 www 52: $r->rflush();
1.1 www 53:
1.2 www 54: my $uri=$r->uri;
55: my $fn=&Apache::lonnet::filelocation('',$uri);
56: my $contents='';
57: my $constructmode=($uri=~/^\/\~/);
1.5 www 58:
59: # ============================================================ Modify and store
60: if ($constructmode) {
61: if ($ENV{'form.store'}) {
62: my @newrules=();
63: undef @newrules;
64: # read rules from form
65: foreach (keys %ENV) {
66: if ($_=~/^form\.effect\_(\d+)$/) {
67: my $number=$1;
68: my %rulehash=();
69: foreach ('effect','domain','course','section','role') {
70: $rulehash{$_}=$ENV{'form.'.$_.'_'.$number};
71: }
72: if ($rulehash{'role'} eq 'au') {
73: $rulehash{'course'}='';
74: $rulehash{'section'}='';
75: }
76: if ($rulehash{'role'} eq 'cc') {
77: $rulehash{'section'}='';
78: }
79: unless (($rulehash{'effect'} eq 'deny') ||
80: ($rulehash{'effect'} eq 'allow')) {
81: $rulehash{'effect'}='deny';
82: }
83: $rulehash{'domain'}=~s/\W//g;
84: $rulehash{'course'}=~s/\W//g;
85: $rulehash{'section'}=~s/\W//g;
86: unless ($rulehash{'domain'}) {
87: $rulehash{'domain'}=$ENV{'user.domain'};
88: }
89: my $realm='';
90: if ($number) {
91: $realm=$rulehash{'domain'};
92: if ($rulehash{'course'}) {
93: $realm.='_'.$rulehash{'course'};
94: }
95: if ($rulehash{'section'}) {
96: $realm.='_'.$rulehash{'section'};
97: }
98: }
99: $newrules[$number]=$rulehash{'effect'}.':'.
100: $realm.':'.$rulehash{'role'};
101: }
102: }
103: # edit actions?
1.6 www 104: foreach (keys %ENV) {
105: if ($_=~/^form\.action\_(\d+)$/) {
106: my $number=$1;
107: if ($ENV{$_} eq 'delete') { $newrules[$number]=''; }
108: if (($ENV{$_} eq 'moveup') && ($number>2)) {
109: my $buffer=$newrules[$number];
110: $newrules[$number]=$newrules[$number-1];
111: $newrules[$number-1]=$buffer;
112: }
113: if (($ENV{$_} eq 'movedown') && ($number<$#newrules)) {
114: my $buffer=$newrules[$number];
115: $newrules[$number]=$newrules[$number+1];
116: $newrules[$number+1]=$buffer;
117: }
118: if ($ENV{$_} eq 'insertabove') {
119: for (my $i=$#newrules;$i>=$number;$i--) {
120: $newrules[$i+1]=$newrules[$i];
121: }
122: $newrules[$number]='deny';
123: }
124: if ($ENV{$_} eq 'insertbelow') {
125: for (my $i=$#newrules;$i>$number;$i--) {
126: $newrules[$i+1]=$newrules[$i];
127: }
128: $newrules[$number+1]='deny';
129: }
130: }
131: }
1.5 www 132:
133: # store file
134: my $fh=Apache::File->new('>'.$fn);
135: foreach (my $i=0;$i<=$#newrules;$i++) {
136: if ($newrules[$i]) {
137: my ($effect,$realm,$role)=split(/\:/,$newrules[$i]);
138: print $fh
139: "<accessrule effect='$effect' realm='$realm' role='$role' />\n";
140: }
141: }
142: $fh->close;
143: }
144: }
145: # ============================================================ Read and display
1.2 www 146: unless ($constructmode) {
147: # =========================================== This is not in construction space
148: $contents=&Apache::lonnet::getfile($fn);
149: if ($contents==-1) { $contents=''; }
150: } else {
151: # =============================================== This is in construction space
152: if (-e $fn) {
153: my $fh=Apache::File->new($fn);
154: $contents=join('',<$fh>);
155: $fh->close();
156: }
1.8 www 157: $r->print('<form name="rules" method="post">');
1.2 www 158: }
1.5 www 159: unless ($contents=~/\<accessrule/s) {
160: $contents='<accessrule effect="deny" />';
161: }
1.2 www 162: my $parser=HTML::LCParser->new(\$contents);
163: my $token;
1.3 www 164: my $rulecounter=0;
1.10 ! www 165: my $colzero=&mt($constructmode?'Edit action':'Rule');
! 166: my %lt=&Apache::loncommon::texthash('ef' => 'Effect',
! 167: 'do' => 'Domain',
! 168: 'co' => 'Course',
! 169: 'se' => 'Section/Group',
! 170: 'ro' => 'Role');
1.3 www 171: # ---------------------------------------------------------- Start table output
1.4 www 172: $r->print(<<ENDSTARTTABLE);
173: <table border="2">
1.10 ! www 174: <tr><th>$colzero</th><th>$lt{'ef'}</th><th>$lt{'do'}</th><th>$lt{'co'}</th>
! 175: <th>$lt{'se'}</th><th>$lt{'ro'}</th></tr>
1.4 www 176: ENDSTARTTABLE
1.3 www 177: # --------------------------------------------------------------------- Default
178: # Fast forward to first rule
179: $token=$parser->get_token;
180: while ($token->[1] ne 'accessrule') { $token=$parser->get_token; }
181: # print default
1.7 www 182: $r->print('<tr><td align="right">');
1.6 www 183: if ($constructmode) {
184: $r->print(&Apache::loncommon::select_form('','action_0',
1.7 www 185: ('' => '',
186: 'insertbelow' => 'Insert rule below ')));
1.6 www 187:
188: } else {
189: $r->print(' ');
190: }
191: $r->print('</td><td>');
1.3 www 192: if ($constructmode) {
193: $r->print(&Apache::loncommon::select_form
194: ($token->[2]->{'effect'},'effect_0',
195: ('allow' => 'allow',
196: 'deny' => 'deny')));
197: } else {
198: $r->print($token->[2]->{'effect'});
199: }
1.4 www 200: $r->print('</td><td colspan="4">Default');
1.3 www 201: if (($token->[2]->{'realm'}) || ($token->[2]->{'role'})) {
1.10 ! www 202: $r->print(' - <font color="red">'.&mt('Error! No default set.').
! 203: '</font>');
1.3 www 204: }
205: $r->print('</td></tr>');
206: # Additional roles
1.2 www 207: while ($token=$parser->get_token) {
208: if (($token->[0] eq 'S') && ($token->[1] eq 'accessrule')) {
1.3 www 209: $rulecounter++;
1.8 www 210: $r->print('<tr><td align="right" rowspan="2">');
1.4 www 211: # insert, delete, etc
212: $r->print($rulecounter.'. ');
213: if ($constructmode) {
214: $r->print(&Apache::loncommon::select_form(
215: '','action_'.$rulecounter,
216: ('' => '',
217: 'delete' => 'Delete this rule',
218: 'insertabove' => 'Insert rule above',
1.7 www 219: 'insertbelow' => 'Insert rule below ',
1.4 www 220: 'moveup' => 'Move rule up',
221: 'movedown' => 'Move rule down')));
222: }
1.8 www 223: $r->print('</td><td rowspan="2">');
1.3 www 224: # effect
225: if ($constructmode) {
226: $r->print(&Apache::loncommon::select_form
227: ($token->[2]->{'effect'},
228: 'effect_'.$rulecounter,
229: ('allow' => 'allow',
230: 'deny' => 'deny')));
231: } else {
232: $r->print($token->[2]->{'effect'});
233: }
234: $r->print('</td><td>');
1.4 www 235: # ---- realm
1.3 www 236: my $realm=$token->[2]->{'realm'};
237: $realm=~s/^\W//;
1.4 www 238: my ($rdom,$rcourse,$rsec)=split(/[\/\_]/,$realm);
1.7 www 239: # realm domain
1.3 www 240: if ($constructmode) {
1.7 www 241: unless ($rdom) { $rdom=$ENV{'user.domain'}; }
1.3 www 242: $r->print(&Apache::loncommon::select_dom_form($rdom,
243: 'domain_'.$rulecounter));
244: } else {
1.4 www 245: $r->print($rdom);
246: }
247: $r->print('</td><td>');
248: # realm course
249: if ($constructmode) {
250: $r->print('<input input type="text" size="25" name="course_'.
251: $rulecounter.'" value="'.$rcourse.'" />');
252: } else {
253: $r->print($rcourse);
1.3 www 254: }
1.4 www 255:
1.9 www 256: $r->print('</td><td>');
1.4 www 257: # realm section
258: if ($constructmode) {
259: $r->print('<input input type="text" size="5" name="section_'.
260: $rulecounter.'" value="'.$rsec.'" />');
261: } else {
262: $r->print($rsec);
263: }
264:
1.8 www 265: $r->print('</td><td rowspan="2">');
1.3 www 266: # role
267: if ($constructmode) {
1.4 www 268: my %hash=('' => '');
269: foreach ('au','cc','in','ta','st') {
270: $hash{$_}=&Apache::lonnet::plaintext($_);
271: }
272: my $role=$token->[2]->{'role'};
273: unless ($role) { $role=''; }
274: $r->print(&Apache::loncommon::select_form(
275: $role,'role_'.$rulecounter,%hash));
1.3 www 276: } else {
1.4 www 277: $r->print(&Apache::lonnet::plaintext($token->[2]->{'role'}));
1.3 www 278: }
1.8 www 279: # course selection link
1.9 www 280: $r->print('</td></tr><tr><td colspan="3" align="right">');
1.8 www 281: if ($rcourse) {
282: my %descript=
283: &Apache::lonnet::coursedescription($rdom.'_'.$rcourse);
284: $r->print($descript{'description'}.' ');
285: }
286: $r->print(&Apache::loncommon::selectcourse_link('rules',
287: 'course_'.$rulecounter,'domain_'.$rulecounter));
1.3 www 288: # close row
289: $r->print('</td></tr>');
1.2 www 290: }
1.3 www 291: }
292: $r->print('</table>');
293: # ------------------------------------------------------------ End table output
294: if ($constructmode) {
1.10 ! www 295: $r->print('<input type="submit" name="store" value="'.&mt('Store').'" /></form>');
1.2 www 296: }
1.1 www 297: $r->print('</body></html>');
298: return OK;
299: }
300:
301:
302: 1;
303: __END__
304:
305:
306:
307:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>