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