]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/cgi-bin/usr_group-setup.cgi
deterministic ordering of perms
[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 OpenILS::Application::Storage::CDBI->connection('dbi:Pg:host=10.0.0.2;dbname=demo-dev', 'postgres');
13 OpenILS::Application::Storage::CDBI->db_Main->{ AutoCommit } = 1;
14
15 my $cgi = new CGI;
16
17 #-------------------------------------------------------------------------------
18 # setup part
19 #-------------------------------------------------------------------------------
20
21 my %org_cols = ( qw/id GroupID name Name parent ParentGroup/ );
22
23 my @col_display_order = ( qw/id name parent/ );
24
25 if (my $action = $cgi->param('action')) {
26         if ( $action eq 'Update' ) {
27                 for my $id ( ($cgi->param('id')) ) {
28                         my $u = permission::grp_tree->retrieve($id);
29                         for my $col ( keys %org_cols ) {
30                                 $u->$col( $cgi->param($col."_$id") );
31                         }
32                         $u->update;
33                 }
34         } elsif ( $action eq 'Set Permissions' ) {
35                 my $grp = permission::grp_tree->retrieve($cgi->param('perms'));
36                 my @ids = $cgi->param('permission');
37                 for my $perm ( permission::perm_list->retrieve_all ) {
38                         if (my $id = $cgi->param('permission_'.$perm->id) ) {
39                                 my $p = permission::grp_perm_map->search({perm=>$id,grp=>$grp->id})->next;
40                                 my $d = $cgi->param("depth_$id");
41                                 if (!$p) {
42                                         $p = permission::grp_perm_map->create({perm=>$id,grp=>$grp->id,depth=>$d});
43                                 } else {
44                                         $p->depth( $d );
45                                 }
46                                 $p->update;
47                         } else {
48                                 permission::grp_perm_map->search({perm=>$perm->id,grp=>$grp->id})->delete_all;
49                         }
50                 }
51                 $cgi->param('action'=>'child');
52         } elsif ( $action eq 'Add New' ) {
53                 permission::grp_tree->create( { map { defined($cgi->param($_)) ? ($_ => $cgi->param($_)) : () } keys %org_cols } );
54         }
55 }
56
57 #-------------------------------------------------------------------------------
58 # HTML part
59 #-------------------------------------------------------------------------------
60
61 print <<HEADER;
62 Content-type: text/html
63
64 <html>
65
66 <head>
67         <style>
68                 table.table_class {
69                         border: dashed lightgrey 1px;
70                         background-color: #EEE;
71                         border-collapse: collapse;
72                 }
73
74                 deactivated {
75                         color: lightgrey;
76                 }
77
78                 tr.new_row_class {
79                         background: grey;
80                 }
81
82                 tr.row_class td {
83                         border: solid lightgrey 1px;
84                 }
85                 
86                 tr.header_class th {
87                         background-color: lightblue;
88                         border: solid blue 1px;
89                         padding: 2px;
90                 }
91
92         </style>
93         <script language='javascript' src='/js/widgets/xtree.js'></script>
94 </head>
95
96 <body style='padding: 25px;'>
97
98 <h1>User Group Hierarchy Setup</h1>
99 <hr/>
100 HEADER
101
102 my $uri = $cgi->url(-relative=>1);
103
104 my $top;
105 for my $grp ( permission::grp_tree->search( {parent=>undef} ) ) {
106         my $name = $grp->name;
107         $name =~ s/'/\\'/og;
108         print <<"       HEADER";
109 <div style="float: left;">
110         <script language='javascript'>
111
112                 function getById (id) { return document.getElementById(id); }
113                 function createAppElement (el) { return document.createElement(el); }
114                 function createAppTextNode (txt) { return document.createTextNode(txt); }
115         
116                 var node_$grp = new WebFXTree('$name','$uri?action=child&id=$grp');
117         HEADER
118         $top = $grp->id;
119         last;
120 }
121
122 for my $grp ( permission::grp_tree->search_like( {parent => '%'}, {order_by => 'id'} ) ) {
123         my $name = $grp->name;
124         $name =~ s/'/\\'/og;
125         my $parent = $grp->parent;
126         print <<"       JS"
127                 var node_$grp = new WebFXTreeItem('$name','$uri?action=child&id=$grp');
128         JS
129 }
130
131 for my $grp ( sort {$a->name cmp $b->name} permission::grp_tree->retrieve_all ) {
132         my $parent = $grp->parent;
133         next unless $parent;
134         print <<"       JS"
135                 node_$parent.add(node_$grp);
136         JS
137 }
138
139 print <<HEADER;
140                 document.write(node_$top);
141         </script>
142 </div>
143 <div style="float:right; width:50%;">
144
145 HEADER
146
147 #-------------------------------------------------------------------------------
148 # Logic part
149 #-------------------------------------------------------------------------------
150
151 if (my $action = $cgi->param('action')) {
152         if ( $action eq 'child' ) {
153                 my $id = $cgi->param('id');
154                 if ($id) {
155                         my $node = permission::grp_tree->retrieve($id);
156                         #-----------------------------------------------------------------------
157                         # child form
158                         #-----------------------------------------------------------------------
159
160                         print "<h2>Edit Group '".$node->name."'</h2>";
161                         print   "<form method='POST'>".
162                                 "<table class='table_class'><tr class='header_class'>\n";
163         
164                         print Tr(
165                                 th($org_cols{id}),
166                                 td( $node->id() ),
167                         );
168                         print Tr(
169                                 th($org_cols{name}),
170                                 td("<input type='text' name='name_$node' value=\"". $node->name() ."\">"),
171                         );
172                         print Tr(
173                                 th($org_cols{parent}),
174                                 td("<select name='parent_$node'>".do{
175                                                 my $out = '<option>-- Select One --</option>';
176                                                 for my $org ( sort {$a->id <=> $b->id} permission::grp_tree->retrieve_all) {
177                                                         $out .= "<option value='$org' ".do {
178                                                                 if ($node->parent == $org->id) {
179                                                                         "selected";
180                                                                 }}.'>'.$org->name.'</option>'
181                                                 }
182                                                 $out;
183                                         }."</select><input type='hidden' value='$node' name='id'>"),
184                         );
185
186                         print Tr( "<td colspan='2'><input type='submit' name='action' value='Update'/></td>" );
187
188                         print   "</table></form><hr/>";
189
190
191                         print "<h2>Group Permissions</h2>";
192
193                         print   "<form method='POST'>".
194                                 "<table class='table_class'>\n".
195                                 "<tr class='header_class'><th>Permission</th><th>Select</th><th>At Depth</th></tr>";
196
197                         for my $perm ( sort {$a->code cmp $b->code} permission::perm_list->retrieve_all ) {
198                                 my $grp = $node;
199                                 my $out = '<select name="depth_'.$perm->id.'"><option value="">-- Select One --</option>';
200                                 for my $outype ( actor::org_unit_type->retrieve_all ) {
201                                         my $grp = $node;
202                                         $out .= "<option value='".$outype->depth."' ".do{
203                                                 my $stuff = '';
204                                                 do {
205                                                         if ($grp) {
206                                                                 my $setting = permission::grp_perm_map->search(
207                                                                                 { grp  => $grp->id,
208                                                                                   perm => $perm->id }
209                                                                 )->next;
210                                                                 $stuff = "selected " if($setting && $outype->depth == $setting->depth);
211                                                                 if($stuff && $setting && $setting->grp != $node->id) {
212                                                                         $out =~ s/^<select/<select disabled/o;
213                                                                 }
214                                                         }
215                                                 } while (!$stuff && $grp && ($grp = $grp->parent));
216                                                 $stuff;
217                                         }.">".$outype->name."</option>";
218                                 }
219                                 $out .= "</select>";
220                                 $grp = $node;
221                                 print Tr( "<td>".$perm->code."</td><td>".
222                                           "<input type='checkbox' name='permission_$perm' value='$perm' ".
223                                           do{
224                                                 my $stuff = '';
225                                                 do {
226                                                         if ($grp) {
227                                                                 my $setting = permission::grp_perm_map->search(
228                                                                                 { grp  => $grp->id,
229                                                                                   perm => $perm->id }
230                                                                 )->next;
231                                                                 $stuff = "checked " if ($setting);
232                                                                 $stuff .= "disabled " if($setting && $setting->grp != $node->id);
233                                                         }
234                                                 } while (!$stuff && $grp && ($grp = $grp->parent));
235                                                 $stuff;
236                                           }.
237                                           "></td><td>$out</td>"
238
239                                 );
240                         }
241                         
242                         print Tr( "<td colspan='3'><input type='hidden' value='$node' name='perms'>",
243                                   "<input type='submit' name='action' value='Set Permissions'/></td>" );
244                         print   "</table></form><hr/>";
245
246
247
248                         print "<h2>New Child</h2>";
249         
250                         print   "<form method='POST'>".
251                                 "<table class='table_class'>\n";
252
253                         print Tr(
254                                 th($org_cols{name}),
255                                 td("<input type='text' name='name'>"),
256                         );
257                         print Tr( "<td colspan='2'><input type='hidden' value='$node' name='parent'>",
258                                   "<input type='submit' name='action' value='Add New'/></td>" );
259                         print   "</table></form><hr/>";
260
261                 }
262         }
263 }
264         
265 print "</div></body></html>";
266
267