Handle hostnames with upper-case letters
[webmin.git] / postgresql / edit_grant.cgi
1 #!/usr/local/bin/perl
2 # edit_grant.cgi
3 # Display a form for editing or creating a grant
4
5 require './postgresql-lib.pl';
6 &ReadParse();
7 $access{'users'} || &error($text{'grant_ecannot'});
8 &ui_print_header(undef, $text{'grant_edit'}, "");
9 if (&supports_schemas($in{'db'})) {
10         $s = &execute_sql_safe($in{'db'}, 'select relname, relacl, nspname from pg_class, pg_namespace where relnamespace = pg_namespace.oid and (relkind = \'r\' OR relkind = \'S\') and relname !~ \'^pg_\' order by relname');
11         }
12 else {
13         $s = &execute_sql_safe($in{'db'}, 'select relname, relacl, \'public\' from pg_class where (relkind = \'r\' OR relkind = \'S\') and relname !~ \'^pg_\' order by relname');
14         }
15 foreach $g (@{$s->{'data'}}) {
16         if ($g->[0] eq $in{'table'} &&
17             $g->[2] eq $in{'ns'}) {
18                 $g->[1] =~ s/^\{//; $g->[1] =~ s/\}$//;
19                 @grant = map { /^"(.*)=(.*)"$/ || /^(.*)=(.*)$/; [ $1, $2 ] }
20                              split(/,/, $g->[1]);
21                 }
22         }
23
24 # Start of form block
25 print &ui_form_start("save_grant.cgi", "post");
26 print &ui_hidden("db", $in{'db'});
27 print &ui_hidden("table", $in{'table'});
28 print &ui_hidden("ns", $in{'ns'});
29 print &ui_hidden("type", $in{'type'});
30 print &ui_hidden("search", $in{'search'});
31 print &ui_table_start($text{'grant_header'}, undef, 2);
32
33 # Database name
34 print &ui_table_row($text{'grant_db'}, "<tt>$in{'db'}</tt>");
35
36 # Schema name
37 print &ui_table_row($text{'grant_ns'}, "<tt>$in{'ns'}</tt>");
38
39 # Object name
40 print &ui_table_row($text{"grant_$in{'type'}"}, "<tt>$in{'table'}</tt>");
41
42 # Get users and groups for permissions table
43 $u = &execute_sql_safe($config{'basedb'}, "select usename from pg_shadow");
44 @users = map { $_->[0] } @{$u->{'data'}};
45
46 $r = &execute_sql_safe($config{'basedb'}, "select groname from pg_group");
47 @groups = map { $_->[0] } @{$r->{'data'}};
48
49 # Table of users / groups and permissions
50 $i = 0;
51 @table = ( );
52 foreach $g (@grant, [ undef, undef ]) {
53         # User / group selector
54         local @row;
55         push(@row, &ui_select("user_$i", 
56                         !defined($g->[0]) ? "" :
57                         $g->[0] eq '' ? "public" : $g->[0],
58                         [ [ '', '&nbsp;' ],
59                           [ 'public', $text{'grant_public'} ],
60                           (map { [ "group $_", &text('grant_group', $_) ] }
61                                @groups),
62                           (@users) ]));
63
64         # Permissions
65         ($acl = $g->[1]) =~ s/\/.*//g;
66         $cbs = "";
67         foreach $p ( [ 'SELECT', 'r' ], [ 'UPDATE', 'w' ],
68                      [ 'INSERT', 'a' ], [ 'DELETE', 'd' ],
69                      [ 'RULE', 'R' ], [ 'REFERENCES', 'x' ],
70                      [ 'TRIGGER', 't' ] ) {
71                 $cbs .= &ui_checkbox("what_$i", $p->[0], $p->[0],
72                                      $acl =~ /$p->[1]/)."\n";
73                 }
74         push(@row, $cbs);
75         push(@table, \@row);
76         $i++;
77         }
78 print &ui_table_row($text{'grant_users'},
79         &ui_columns_table([ $text{'grant_user'}, $text{'grant_what'} ],
80                           undef,
81                           \@table));
82
83 print &ui_table_end();
84 print &ui_form_end([ [ undef, $text{'save'} ] ]);
85
86 &ui_print_footer("list_grants.cgi?search=".&urlize($in{'search'}),
87                  $text{'grant_return'});
88