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