Zugangskontrolle mithilfe des Eintrags "acs" in der Benutzerkonfiguration wird nicht...
[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 sub new {
41   $main::lxdebug->enter_sub();
42
43   my ($type, $menufile) = @_;
44
45   my $self    = {};
46   my $inifile = Inifile->new($menufile);
47
48   map { $self->{$_} = $inifile->{$_} } keys %{ $inifile };
49
50   bless $self, $type;
51
52   $self->set_access();
53
54   $main::lxdebug->leave_sub();
55
56   return $self;
57 }
58
59 sub menuitem {
60   $main::lxdebug->enter_sub();
61
62   my ($self, $myconfig, $form, $item) = @_;
63
64   my $module = $form->{script};
65   my $action = "section_menu";
66   my $target = "";
67
68   if ($self->{$item}{module}) {
69     $module = $self->{$item}{module};
70   }
71   if ($self->{$item}{action}) {
72     $action = $self->{$item}{action};
73   }
74   if ($self->{$item}{target}) {
75     $target = $self->{$item}{target};
76   }
77
78   my $level = $form->escape($item);
79
80   my $str = qq|<a style="vertical-align:top" href=$module?action=$action&level=$level|;
81
82   my @vars = qw(module action target href);
83
84   if ($self->{$item}{href}) {
85     $str  = qq|<a href=$self->{$item}{href}|;
86     @vars = qw(module target href);
87   }
88
89   map { delete $self->{$item}{$_} } @vars;
90
91   # add other params
92   foreach my $key (keys %{ $self->{$item} }) {
93     $str .= "&" . $form->escape($key, 1) . "=";
94     ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
95     $value = $myconfig->{$value} . "/$conf" if ($conf);
96     $str .= $form->escape($value, 1);
97   }
98
99   if ($target) {
100     $str .= qq| target=$target|;
101   }
102
103   $str .= ">";
104
105   $main::lxdebug->leave_sub();
106
107   return $str;
108 }
109
110 sub menuitem_new {
111   $main::lxdebug->enter_sub();
112
113   my ($self, $name, $item) = @_;
114
115   my $form        = $main::form;
116
117   my $module      = $self->{$name}->{module} || $form->{script};
118   my $action      = $self->{$name}->{action};
119
120   $item->{target} = $self->{$name}->{target} || "main_window";
121   $item->{href}   = $self->{$name}->{href}   || "${module}?action=" . $form->escape($action);
122
123   my @vars = qw(module target href);
124   push @vars, 'action' unless ($self->{$name}->{href});
125
126   map { delete $self->{$name}{$_} } @vars;
127
128   # add other params
129   foreach my $key (keys %{ $self->{$name} }) {
130     my ($value, $conf)  = split(m/=/, $self->{$name}->{$key}, 2);
131     $value              = $myconfig->{$value} . "/$conf" if ($conf);
132     $item->{href}      .= "&" . $form->escape($key) . "=" . $form->escape($value);
133   }
134
135   $main::lxdebug->leave_sub();
136 }
137
138 sub menuitem_v3 {
139   $main::lxdebug->enter_sub();
140
141   my ($self, $myconfig, $form, $item, $other) = @_;
142
143   my $module = $form->{script};
144   my $action = "section_menu";
145   my $target = "";
146
147   if ($self->{$item}{module}) {
148     $module = $self->{$item}{module};
149   }
150   if ($self->{$item}{action}) {
151     $action = $self->{$item}{action};
152   }
153   if ($self->{$item}{target}) {
154     $target = $self->{$item}{target};
155   }
156
157   my $level = $form->escape($item);
158
159   my $str = qq|<a href="$module?action=| . $form->escape($action) . qq|&level=| . $form->escape($level);
160
161   my @vars = qw(module action target href);
162
163   if ($self->{$item}{href}) {
164     $str  = qq|<a href=$self->{$item}{href}|;
165     @vars = qw(module target href);
166   }
167
168   map { delete $self->{$item}{$_} } @vars;
169
170   # add other params
171   foreach my $key (keys %{ $self->{$item} }) {
172     $str .= "&" . $form->escape($key, 1) . "=";
173     ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
174     $value = $myconfig->{$value} . "/$conf" if ($conf);
175     $str .= $form->escape($value, 1);
176   }
177
178   $str .= '"';
179
180   if ($target) {
181     $str .= qq| target="| . $form->quote($target) . qq|"|;
182   }
183
184   if ($other) {
185     foreach my $key (keys(%{$other})) {
186       $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
187     }
188   }
189
190   $str .= ">";
191
192   $main::lxdebug->leave_sub();
193
194   return $str;
195 }
196
197 sub menuitem_XML {
198   $main::lxdebug->enter_sub();
199
200   my ($self, $myconfig, $form, $item, $other) = @_;
201
202   my $module = $form->{script};
203   my $action = "section_menu";
204   my $target = "";
205
206   if ($self->{$item}{module}) {
207     $module = $self->{$item}{module};
208   }
209   if ($self->{$item}{action}) {
210     $action = $self->{$item}{action};
211   }
212   if ($self->{$item}{target}) {
213     $target = $self->{$item}{target};
214   }
215
216   my $level = $form->escape($item);
217
218   my $str = qq| link="$module?action=| . $form->escape($action) .
219     qq|&amp;level=| . $form->escape($level);
220
221   my @vars = qw(module action target href);
222
223   if ($self->{$item}{href}) {
224     $str  = qq| link=$self->{$item}{href}|;
225     @vars = qw(module target href);
226   }
227
228   map { delete $self->{$item}{$_} } @vars;
229
230   # add other params
231   foreach my $key (keys %{ $self->{$item} }) {
232     $str .= "&amp;" . $form->escape($key, 1) . "=";
233     ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
234     $value = $myconfig->{$value} . "/$conf" if ($conf);
235     $str .= $form->escape($value, 1);
236   }
237
238   $str .= '"';
239
240
241
242   if ($other) {
243     foreach my $key (keys(%{$other})) {
244       $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
245     }
246   }
247
248
249   $main::lxdebug->leave_sub();
250
251   return $str;
252 }
253
254 sub access_control {
255   $main::lxdebug->enter_sub(2);
256
257   my ($self, $myconfig, $menulevel) = @_;
258
259   my @menu = ();
260
261   if ($menulevel eq "") {
262     @menu = grep { !/--/ } @{ $self->{ORDER} };
263   } else {
264     @menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
265   }
266
267   $main::lxdebug->leave_sub(2);
268
269   return @menu;
270 }
271
272 sub parse_access_string {
273   my $self   = shift;
274   my $key    = shift;
275   my $access = shift;
276
277   my @stack;
278   my $cur_ary = [];
279
280   push @stack, $cur_ary;
281
282   while ($access =~ m/^([a-z_]+|\||\&|\(|\)|\s+)/) {
283     my $token = $1;
284     substr($access, 0, length($1)) = "";
285
286     next if ($token =~ /\s/);
287
288     if ($token eq "(") {
289       my $new_cur_ary = [];
290       push @stack, $new_cur_ary;
291       push @{$cur_ary}, $new_cur_ary;
292       $cur_ary = $new_cur_ary;
293
294     } elsif ($token eq ")") {
295       pop @stack;
296       if (!@stack) {
297         $main::form->error("Error in menu.ini for entry ${key}: missing '('");
298       }
299       $cur_ary = $stack[-1];
300
301     } elsif (($token eq "|") || ($token eq "&")) {
302       push @{$cur_ary}, $token;
303
304     } else {
305       push @{$cur_ary}, $main::auth->check_right($main::form->{login}, $token, 1);
306     }
307   }
308
309   if ($access) {
310     $main::form->error("Error in menu.ini for entry ${name}: unrecognized token at the start of '$access'\n");
311   }
312
313   if (1 < scalar @stack) {
314     $main::form->error("Error in menu.ini for entry ${name}: Missing ')'\n");
315   }
316
317   return SL::Auth::evaluate_rights_ary($stack[0]);
318 }
319
320 sub set_access {
321   my $self = shift;
322
323   my $key;
324
325   foreach $key (@{ $self->{ORDER} }) {
326     my $entry = $self->{$key};
327
328     $entry->{GRANTED}              = $entry->{ACCESS} ? $self->parse_access_string($key, $entry->{ACCESS}) : 1;
329     $entry->{IS_MENU}              = $entry->{submenu} || ($key !~ m/--/);
330     $entry->{NUM_VISIBLE_CHILDREN} = 0;
331
332     if ($key =~ m/--/) {
333       my $parent = $key;
334       substr($parent, rindex($parent, '--')) = '';
335       $entry->{GRANTED} &&= $self->{$parent}->{GRANTED};
336     }
337
338     $entry->{VISIBLE} = $entry->{GRANTED};
339   }
340
341   foreach $key (reverse @{ $self->{ORDER} }) {
342     my $entry = $self->{$key};
343
344     if ($entry->{IS_MENU}) {
345       $entry->{VISIBLE} &&= $entry->{NUM_VISIBLE_CHILDREN} > 0;
346     }
347
348     next if (($key !~ m/--/) || !$entry->{VISIBLE});
349
350     my $parent = $key;
351     substr($parent, rindex($parent, '--')) = '';
352     $self->{$parent}->{NUM_VISIBLE_CHILDREN}++;
353   }
354
355 #   $self->dump_visible();
356
357   $self->{ORDER} = [ grep { $self->{$_}->{VISIBLE} } @{ $self->{ORDER} } ];
358
359   map { delete @{$self->{$_}}{qw(GRANTED IS_MENU NUM_VISIBLE_CHILDREN VISIBLE ACCESS)} if ($_ ne 'ORDER') } keys %{ $self };
360 }
361
362 sub dump_visible {
363   my $self = shift;
364   foreach my $key (@{ $self->{ORDER} }) {
365     my $entry = $self->{$key};
366     $main::lxdebug->message(0, "$entry->{GRANTED} $entry->{VISIBLE} $entry->{NUM_VISIBLE_CHILDREN} $key");
367   }
368 }
369
370 1;
371