strictness für Menu und Ca
[kivitendo-erp.git] / SL / Menu.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #=====================================================================
30 #
31 # routines for menu items
32 #
33 #=====================================================================
34
35 package Menu;
36
37 use SL::Auth;
38 use SL::Inifile;
39
40 use strict;
41
42 sub new {
43   $main::lxdebug->enter_sub();
44
45   my ($type, $menufile) = @_;
46
47   my $self    = {};
48   my $inifile = Inifile->new($menufile);
49
50   map { $self->{$_} = $inifile->{$_} } keys %{ $inifile };
51
52   bless $self, $type;
53
54   $self->set_access();
55
56   $main::lxdebug->leave_sub();
57
58   return $self;
59 }
60
61 sub menuitem {
62   $main::lxdebug->enter_sub();
63
64   my ($self, $myconfig, $form, $item) = @_;
65
66   my $module = $form->{script};
67   my $action = "section_menu";
68   my $target = "";
69
70   if ($self->{$item}{module}) {
71     $module = $self->{$item}{module};
72   }
73   if ($self->{$item}{action}) {
74     $action = $self->{$item}{action};
75   }
76   if ($self->{$item}{target}) {
77     $target = $self->{$item}{target};
78   }
79
80   my $level = $form->escape($item);
81
82   my $str = qq|<a style="vertical-align:top" href=$module?action=$action&level=$level|;
83
84   my @vars = qw(module action target href);
85
86   if ($self->{$item}{href}) {
87     $str  = qq|<a href=$self->{$item}{href}|;
88     @vars = qw(module target href);
89   }
90
91   map { delete $self->{$item}{$_} } @vars;
92
93   # add other params
94   foreach my $key (keys %{ $self->{$item} }) {
95     $str .= "&" . $form->escape($key, 1) . "=";
96     my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
97     $value = $myconfig->{$value} . "/$conf" if ($conf);
98     $str .= $form->escape($value, 1);
99   }
100
101   if ($target) {
102     $str .= qq| target=$target|;
103   }
104
105   $str .= ">";
106
107   $main::lxdebug->leave_sub();
108
109   return $str;
110 }
111
112 sub menuitem_js {
113   my ($self, $myconfig, $form, $item) = @_;
114
115   my $module = $form->{script};
116   my $action = "section_menu";
117
118   #if ($self->{$item}{module}) {
119   $module = $self->{$item}{module};
120
121   #}
122   if ($self->{$item}{action}) {
123     $action = $self->{$item}{action};
124   }
125
126   my $level = $form->escape($item);
127   my $str   = qq|$module?action=$action&level=$level|;
128   my @vars  = qw(module action target href);
129
130   if ($self->{$item}{href}) {
131     $str  = qq|$self->{$item}{href}|;
132     @vars = qw(module target href);
133   }
134
135   map { delete $self->{$item}{$_} } @vars;
136
137   # add other params
138   foreach my $key (keys %{ $self->{$item} }) {
139     $str .= "&" . $form->escape($key, 1) . "=";
140     my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
141     $value = $myconfig->{$value} . "/$conf" if ($conf);
142     $str .= $form->escape($value, 1);
143   }
144
145   $str .= " ";
146
147 }
148
149 sub menuitem_new {
150   $main::lxdebug->enter_sub();
151
152   my ($self, $name, $item) = @_;
153
154   my $form        =  $main::form;
155   my $myconfig    = \%main::myconfig;
156
157   my $module      = $self->{$name}->{module} || $form->{script};
158   my $action      = $self->{$name}->{action};
159
160   $item->{target} = $self->{$name}->{target} || "main_window";
161   $item->{href}   = $self->{$name}->{href}   || "${module}?action=" . $form->escape($action);
162
163   my @vars = qw(module target href);
164   push @vars, 'action' unless ($self->{$name}->{href});
165
166   map { delete $self->{$name}{$_} } @vars;
167
168   # add other params
169   foreach my $key (keys %{ $self->{$name} }) {
170     my ($value, $conf)  = split(m/=/, $self->{$name}->{$key}, 2);
171     $value              = $myconfig->{$value} . "/$conf" if ($conf);
172     $item->{href}      .= "&" . $form->escape($key) . "=" . $form->escape($value);
173   }
174
175   $main::lxdebug->leave_sub();
176 }
177
178 sub menuitem_v3 {
179   $main::lxdebug->enter_sub();
180
181   my ($self, $myconfig, $form, $item, $other) = @_;
182
183   my $module = $form->{script};
184   my $action = "section_menu";
185   my $target = "";
186
187   if ($self->{$item}{module}) {
188     $module = $self->{$item}{module};
189   }
190   if ($self->{$item}{action}) {
191     $action = $self->{$item}{action};
192   }
193   if ($self->{$item}{target}) {
194     $target = $self->{$item}{target};
195   }
196
197   my $level = $form->escape($item);
198
199   my $str = qq|<a href="$module?action=| . $form->escape($action) . qq|&level=| . $form->escape($level);
200
201   my @vars = qw(module action target href);
202
203   if ($self->{$item}{href}) {
204     $str  = qq|<a href=$self->{$item}{href}|;
205     @vars = qw(module target href);
206   }
207
208   map { delete $self->{$item}{$_} } @vars;
209
210   # add other params
211   foreach my $key (keys %{ $self->{$item} }) {
212     $str .= "&" . $form->escape($key, 1) . "=";
213     my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
214     $value = $myconfig->{$value} . "/$conf" if ($conf);
215     $str .= $form->escape($value, 1);
216   }
217
218   $str .= '"';
219
220   if ($target) {
221     $str .= qq| target="| . $form->quote($target) . qq|"|;
222   }
223
224   if ($other) {
225     foreach my $key (keys(%{$other})) {
226       $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
227     }
228   }
229
230   $str .= ">";
231
232   $main::lxdebug->leave_sub();
233
234   return $str;
235 }
236
237 sub menuitem_XML {
238   $main::lxdebug->enter_sub();
239
240   my ($self, $myconfig, $form, $item, $other) = @_;
241
242   my $module = $form->{script};
243   my $action = "section_menu";
244   my $target = "";
245
246   if ($self->{$item}{module}) {
247     $module = $self->{$item}{module};
248   }
249   if ($self->{$item}{action}) {
250     $action = $self->{$item}{action};
251   }
252   if ($self->{$item}{target}) {
253     $target = $self->{$item}{target};
254   }
255
256   my $level = $form->escape($item);
257
258   my $str = qq| link="$module?action=| . $form->escape($action) .
259     qq|&amp;level=| . $form->escape($level);
260
261   my @vars = qw(module action target href);
262
263   if ($self->{$item}{href}) {
264     $str  = qq| link=$self->{$item}{href}|;
265     @vars = qw(module target href);
266   }
267
268   map { delete $self->{$item}{$_} } @vars;
269
270   # add other params
271   foreach my $key (keys %{ $self->{$item} }) {
272     $str .= "&amp;" . $form->escape($key, 1) . "=";
273     my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
274     $value = $myconfig->{$value} . "/$conf" if ($conf);
275     $str .= $form->escape($value, 1);
276   }
277
278   $str .= '"';
279
280
281
282   if ($other) {
283     foreach my $key (keys(%{$other})) {
284       $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
285     }
286   }
287
288
289   $main::lxdebug->leave_sub();
290
291   return $str;
292 }
293
294 sub access_control {
295   $main::lxdebug->enter_sub(2);
296
297   my ($self, $myconfig, $menulevel) = @_;
298
299   my @menu = ();
300
301   if ($menulevel eq "") {
302     @menu = grep { !/--/ } @{ $self->{ORDER} };
303   } else {
304     @menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
305   }
306
307   $main::lxdebug->leave_sub(2);
308
309   return @menu;
310 }
311
312 sub parse_access_string {
313   my $self   = shift;
314   my $key    = shift;
315   my $access = shift;
316
317   my $form        =  $main::form;
318   my $auth        =  $main::auth;
319   my $myconfig    = \%main::myconfig;
320
321   my @stack;
322   my $cur_ary = [];
323
324   push @stack, $cur_ary;
325
326   while ($access =~ m/^([a-z_]+|\||\&|\(|\)|\s+)/) {
327     my $token = $1;
328     substr($access, 0, length($1)) = "";
329
330     next if ($token =~ /\s/);
331
332     if ($token eq "(") {
333       my $new_cur_ary = [];
334       push @stack, $new_cur_ary;
335       push @{$cur_ary}, $new_cur_ary;
336       $cur_ary = $new_cur_ary;
337
338     } elsif ($token eq ")") {
339       pop @stack;
340       if (!@stack) {
341         $form->error("Error in menu.ini for entry ${key}: missing '('");
342       }
343       $cur_ary = $stack[-1];
344
345     } elsif (($token eq "|") || ($token eq "&")) {
346       push @{$cur_ary}, $token;
347
348     } else {
349       push @{$cur_ary}, $auth->check_right($form->{login}, $token, 1);
350     }
351   }
352
353   if ($access) {
354     $form->error("Error in menu.ini for entry ${key}: unrecognized token at the start of '$access'\n");
355   }
356
357   if (1 < scalar @stack) {
358     $main::form->error("Error in menu.ini for entry ${key}: Missing ')'\n");
359   }
360
361   return SL::Auth::evaluate_rights_ary($stack[0]);
362 }
363
364 sub set_access {
365   my $self = shift;
366
367   my $key;
368
369   foreach $key (@{ $self->{ORDER} }) {
370     my $entry = $self->{$key};
371
372     $entry->{GRANTED}              = $entry->{ACCESS} ? $self->parse_access_string($key, $entry->{ACCESS}) : 1;
373     $entry->{IS_MENU}              = $entry->{submenu} || ($key !~ m/--/);
374     $entry->{NUM_VISIBLE_CHILDREN} = 0;
375
376     if ($key =~ m/--/) {
377       my $parent = $key;
378       substr($parent, rindex($parent, '--')) = '';
379       $entry->{GRANTED} &&= $self->{$parent}->{GRANTED};
380     }
381
382     $entry->{VISIBLE} = $entry->{GRANTED};
383   }
384
385   foreach $key (reverse @{ $self->{ORDER} }) {
386     my $entry = $self->{$key};
387
388     if ($entry->{IS_MENU}) {
389       $entry->{VISIBLE} &&= $entry->{NUM_VISIBLE_CHILDREN} > 0;
390     }
391
392     next if (($key !~ m/--/) || !$entry->{VISIBLE});
393
394     my $parent = $key;
395     substr($parent, rindex($parent, '--')) = '';
396     $self->{$parent}->{NUM_VISIBLE_CHILDREN}++;
397   }
398
399 #   $self->dump_visible();
400
401   $self->{ORDER} = [ grep { $self->{$_}->{VISIBLE} } @{ $self->{ORDER} } ];
402
403   map { delete @{$self->{$_}}{qw(GRANTED IS_MENU NUM_VISIBLE_CHILDREN VISIBLE ACCESS)} if ($_ ne 'ORDER') } keys %{ $self };
404 }
405
406 sub dump_visible {
407   my $self = shift;
408   foreach my $key (@{ $self->{ORDER} }) {
409     my $entry = $self->{$key};
410     $main::lxdebug->message(0, "$entry->{GRANTED} $entry->{VISIBLE} $entry->{NUM_VISIBLE_CHILDREN} $key");
411   }
412 }
413
414 1;
415