]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/cgi-bin/usr_group-setup.cgi
adding grant flag manipulation to the bootstrapper
[Evergreen.git] / Open-ILS / src / cgi-bin / usr_group-setup.cgi
1 #!/usr/bin/perl
2 use strict;
3
4 use OpenILS::Application::Storage;
5 use OpenILS::Application::Storage::CDBI;
6
7 # I need to abstract the driver loading away...
8 use OpenILS::Application::Storage::Driver::Pg;
9
10 use CGI qw/:standard start_*/;
11
12 our %config;
13 do '##CONFIG##/live-db-setup.pl';
14
15 OpenILS::Application::Storage::CDBI->connection($config{dsn},$config{usr},$config{pw});
16 OpenILS::Application::Storage::CDBI->db_Main->{ AutoCommit } = 1;
17
18 my $cgi = new CGI;
19
20 #-------------------------------------------------------------------------------
21 # setup part
22 #-------------------------------------------------------------------------------
23
24 my %org_cols = ( qw/id GroupID name Name parent ParentGroup description Description/ );
25
26 my @col_display_order = ( qw/id name parent description/ );
27
28 if (my $action = $cgi->param('action')) {
29         if ( $action eq 'Update' ) {
30                 for my $id ( ($cgi->param('id')) ) {
31                         my $u = permission::grp_tree->retrieve($id);
32                         for my $col ( keys %org_cols ) {
33                                 next if ($cgi->param($col."_$id") =~ /Select One/o);
34                                 $u->$col( $cgi->param($col."_$id") );
35                         }
36                         $u->update;
37                 }
38         } elsif ( $action eq 'Set Permissions' ) {
39                 my $grp = permission::grp_tree->retrieve($cgi->param('perms'));
40                 my @ids = $cgi->param('permission');
41                 for my $perm ( permission::perm_list->retrieve_all ) {
42                         if (my $id = $cgi->param('permission_'.$perm->id) ) {
43                                 my $p = permission::grp_perm_map->search({perm=>$id,grp=>$grp->id})->next;
44                                 my $d = $cgi->param("depth_$id");
45                                 my $g = $cgi->param("grant_$id") || 'f';
46                                 if (!$p) {
47                                         $p = permission::grp_perm_map->create({perm=>$id,grp=>$grp->id,depth=>$d,grantable=>$g});
48                                 } else {
49                                         $p->depth( $d );
50                                         $p->grantable( $g );
51                                 }
52                                 $p->update;
53                         } else {
54                                 permission::grp_perm_map->search({perm=>$perm->id,grp=>$grp->id})->delete_all;
55                         }
56                 }
57                 $cgi->param('action'=>'child');
58         } elsif ( $action eq 'Add New' ) {
59                 permission::grp_tree->create( { map { defined($cgi->param($_)) ? ($_ => $cgi->param($_)) : () } keys %org_cols } );
60         }
61 }
62
63 #-------------------------------------------------------------------------------
64 # HTML part
65 #-------------------------------------------------------------------------------
66
67 print <<HEADER;
68 Content-type: text/html
69
70 <html>
71
72 <head>
73         <style>
74                 table.table_class {
75                         border: dashed lightgrey 1px;
76                         background-color: #EEE;
77                         border-collapse: collapse;
78                 }
79
80                 deactivated {
81                         color: lightgrey;
82                 }
83
84                 tr.new_row_class {
85                         background: grey;
86                 }
87
88                 tr.row_class td {
89                         border: solid lightgrey 1px;
90                 }
91                 
92                 tr.header_class th {
93                         background-color: lightblue;
94                         border: solid blue 1px;
95                         padding: 2px;
96                 }
97
98 /*--------------------------------------------------|
99 | dTree 2.05 | www.destroydrop.com/javascript/tree/ |
100 |---------------------------------------------------|
101 | Copyright (c) 2002-2003 Geir Landrö               |
102 |--------------------------------------------------*/
103
104 .dtree {
105         font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
106         font-size: 11px;
107         color: #666;
108         white-space: nowrap;
109 }
110 .dtree img {
111         border: 0px;
112         vertical-align: middle;
113 }
114 .dtree a {
115         color: #333;
116         text-decoration: none;
117 }
118 .dtree a.node, .dtree a.nodeSel {
119         white-space: nowrap;
120         padding: 1px 2px 1px 2px;
121 }
122 .dtree a.node:hover, .dtree a.nodeSel:hover {
123         color: #333;
124         text-decoration: underline;
125 }
126 .dtree a.nodeSel {
127         background-color: #c0d2ec;
128 }
129 .dtree .clip {
130         overflow: hidden;
131 }
132
133
134         </style>
135         <script language='javascript' src='support/dtree.js'></script>
136 </head>
137
138 <body style='padding: 25px;'>
139
140 <a href="$config{index}">Home</a>
141
142 <h1>User Group Hierarchy Setup</h1>
143 <hr/>
144 HEADER
145
146 my $uri = $cgi->url(-relative=>1);
147
148 my $top;
149 for my $grp ( permission::grp_tree->search( {parent=>undef} ) ) {
150         my $name = $grp->name;
151         my $desc = $grp->description || $grp->name;
152         $top = $grp->id;
153         $name =~ s/'/\\'/og;
154         print <<"       HEADER";
155 <div style="float: left;">
156         <script language='javascript'>
157         var tree = new dTree("tree");
158         tree.add($grp, -1, "$name", "$uri?action=child&id=$grp", "$desc");
159         HEADER
160         $top = $grp->id;
161         last;
162 }
163
164 for my $grp ( permission::grp_tree->search_like( {parent => '%'}, {order_by => 'name'} ) ) {
165         my $name = $grp->name;
166         my $desc = $grp->description || $grp->name;
167         $name =~ s/'/\\'/og;
168         my $parent = $grp->parent;
169         print "\ttree.add($grp, $parent, \"$name\", \"$uri?action=child&id=$grp\", \"$desc\");\n";
170 }
171
172 print <<HEADER;
173         tree.closeAllChildren($top);
174         document.write(tree.toString());
175         </script>
176 </div>
177 <div style="float:right; width:75%;">
178 HEADER
179
180 #-------------------------------------------------------------------------------
181 # Logic part
182 #-------------------------------------------------------------------------------
183
184 if (my $action = $cgi->param('action')) {
185         if ( $action eq 'child' ) {
186                 my $id = $cgi->param('id');
187                 if ($id) {
188                         my $node = permission::grp_tree->retrieve($id);
189                         #-----------------------------------------------------------------------
190                         # child form
191                         #-----------------------------------------------------------------------
192
193                         print "<h2>Edit Group '".$node->name."'</h2>";
194                         print   "<form method='POST'>".
195                                 "<table class='table_class'><tr class='header_class'>\n";
196         
197                         print Tr(
198                                 th($org_cols{id}),
199                                 td( $node->id() ),
200                         );
201                         print Tr(
202                                 th($org_cols{name}),
203                                 td("<input type='text' name='name_$node' value=\"". $node->name() ."\">"),
204                         );
205                         print Tr(
206                                 th($org_cols{parent}),
207                                 td("<select name='parent_$node'>".do{
208                                                 my $out = '<option>-- Select One --</option>';
209                                                 for my $org ( sort {$a->id <=> $b->id} permission::grp_tree->retrieve_all) {
210                                                         $out .= "<option value='$org' ".do {
211                                                                 if ($node->parent == $org->id) {
212                                                                         "selected";
213                                                                 }}.'>'.$org->name.'</option>'
214                                                 }
215                                                 $out;
216                                         }."</select><input type='hidden' value='$node' name='id'>"),
217                         );
218                         print Tr(
219                                 th($org_cols{description}),
220                                 td("<input type='text' name='description_$node' value=\"". $node->description() ."\">"),
221                         );
222
223                         print Tr( "<td colspan='2'><input type='submit' name='action' value='Update'/></td>" );
224
225                         print   "</table></form><hr/>";
226
227
228                         print "<h2>Group Permissions</h2>";
229
230                         print   "<form method='POST'>".
231                                 "<table class='table_class'>\n".
232                                 "<tr class='header_class'><th>Permission</th><th>Select</th><th>At Depth</th><th>Grantable</th></tr>";
233
234                         for my $perm ( sort {$a->code cmp $b->code} permission::perm_list->retrieve_all ) {
235                                 my $grp = $node;
236                                 my $out = '<select name="depth_'.$perm->id.'"><option value="">-- Select One --</option>';
237                                 for my $outype ( actor::org_unit_type->retrieve_all ) {
238                                         my $grp = $node;
239                                         $out .= "<option value='".$outype->depth."' ".do{
240                                                 my $stuff = '';
241                                                 do {
242                                                         if ($grp) {
243                                                                 my $setting = permission::grp_perm_map->search(
244                                                                                 { grp  => $grp->id,
245                                                                                   perm => $perm->id }
246                                                                 )->next;
247                                                                 $stuff = "selected " if($setting && $outype->depth == $setting->depth);
248                                                                 if($stuff && $setting && $setting->grp != $node->id) {
249                                                                         $out =~ s/^<select/<select disabled/o;
250                                                                 }
251                                                         }
252                                                 } while (!$stuff && $grp && ($grp = $grp->parent));
253                                                 $stuff;
254                                         }.">".$outype->name."</option>";
255                                 }
256                                 $out .= "</select>";
257                                 $out .= "</td><td><input type='checkbox' name='grant_$perm' value='t' ".
258                                           do{
259                                                 my $stuff = '';
260                                                 do {
261                                                         if ($grp) {
262                                                                 my $setting = permission::grp_perm_map->search(
263                                                                                 { grp  => $grp->id,
264                                                                                   perm => $perm->id }
265                                                                 )->next;
266                                                                 $stuff = "checked='checked' " if ($setting && $setting->grantable);
267                                                         }
268                                                 } while (!$stuff && $grp && ($grp = $grp->parent));
269                                                 $stuff;
270                                           }.">";
271
272                                 $grp = $node;
273                                 print Tr( "<td>".$perm->code."</td><td>".
274                                           "<input type='checkbox' name='permission_$perm' value='$perm' ".
275                                           do{
276                                                 my $stuff = '';
277                                                 do {
278                                                         if ($grp) {
279                                                                 my $setting = permission::grp_perm_map->search(
280                                                                                 { grp  => $grp->id,
281                                                                                   perm => $perm->id }
282                                                                 )->next;
283                                                                 $stuff = "checked " if ($setting);
284                                                                 $stuff .= "disabled " if($setting && $setting->grp != $node->id);
285                                                         }
286                                                 } while (!$stuff && $grp && ($grp = $grp->parent));
287                                                 $stuff;
288                                           }.
289                                           "></td><td>$out</td>"
290
291                                 );
292                         }
293                         
294                         print Tr( "<td colspan='3'><input type='hidden' value='$node' name='perms'>",
295                                   "<input type='submit' name='action' value='Set Permissions'/></td>" );
296                         print   "</table></form><hr/>";
297
298
299
300                         print "<h2>New Child</h2>";
301         
302                         print   "<form method='POST'>".
303                                 "<table class='table_class'>\n";
304
305                         print Tr(
306                                 th($org_cols{name}),
307                                 td("<input type='text' name='name'>"),
308                         );
309                         print Tr(
310                                 th($org_cols{description}),
311                                 td("<input type='text' name='description'>"),
312                         );
313                         print Tr( "<td colspan='2'><input type='hidden' value='$node' name='parent'>",
314                                   "<input type='submit' name='action' value='Add New'/></td>" );
315                         print   "</table></form><hr/>";
316
317                 }
318         }
319 }
320         
321 print "</div></body></html>";
322
323