Login und Passwort müssen nicht mehr übergeben werden.
[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_v3 {
111   $main::lxdebug->enter_sub();
112
113   my ($self, $myconfig, $form, $item, $other) = @_;
114
115   my $module = $form->{script};
116   my $action = "section_menu";
117   my $target = "";
118
119   if ($self->{$item}{module}) {
120     $module = $self->{$item}{module};
121   }
122   if ($self->{$item}{action}) {
123     $action = $self->{$item}{action};
124   }
125   if ($self->{$item}{target}) {
126     $target = $self->{$item}{target};
127   }
128
129   my $level = $form->escape($item);
130
131   my $str = qq|<a href="$module?action=| . $form->escape($action) . qq|&level=| . $form->escape($level);
132
133   my @vars = qw(module action target href);
134
135   if ($self->{$item}{href}) {
136     $str  = qq|<a href=$self->{$item}{href}|;
137     @vars = qw(module target href);
138   }
139
140   map { delete $self->{$item}{$_} } @vars;
141
142   # add other params
143   foreach my $key (keys %{ $self->{$item} }) {
144     $str .= "&" . $form->escape($key, 1) . "=";
145     ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
146     $value = $myconfig->{$value} . "/$conf" if ($conf);
147     $str .= $form->escape($value, 1);
148   }
149
150   $str .= '"';
151
152   if ($target) {
153     $str .= qq| target="| . $form->quote($target) . qq|"|;
154   }
155
156   if ($other) {
157     foreach my $key (keys(%{$other})) {
158       $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
159     }
160   }
161
162   $str .= ">";
163
164   $main::lxdebug->leave_sub();
165
166   return $str;
167 }
168
169 sub menuitem_XML {
170   $main::lxdebug->enter_sub();
171
172   my ($self, $myconfig, $form, $item, $other) = @_;
173
174   my $module = $form->{script};
175   my $action = "section_menu";
176   my $target = "";
177
178   if ($self->{$item}{module}) {
179     $module = $self->{$item}{module};
180   }
181   if ($self->{$item}{action}) {
182     $action = $self->{$item}{action};
183   }
184   if ($self->{$item}{target}) {
185     $target = $self->{$item}{target};
186   }
187
188   my $level = $form->escape($item);
189
190   my $str = qq| link="$module?action=| . $form->escape($action) .
191     qq|&amp;level=| . $form->escape($level);
192
193   my @vars = qw(module action target href);
194
195   if ($self->{$item}{href}) {
196     $str  = qq| link=$self->{$item}{href}|;
197     @vars = qw(module target href);
198   }
199
200   map { delete $self->{$item}{$_} } @vars;
201
202   # add other params
203   foreach my $key (keys %{ $self->{$item} }) {
204     $str .= "&amp;" . $form->escape($key, 1) . "=";
205     ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
206     $value = $myconfig->{$value} . "/$conf" if ($conf);
207     $str .= $form->escape($value, 1);
208   }
209
210   $str .= '"';
211
212
213
214   if ($other) {
215     foreach my $key (keys(%{$other})) {
216       $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
217     }
218   }
219
220
221   $main::lxdebug->leave_sub();
222
223   return $str;
224 }
225
226 sub menuitemNew {
227   my ($self, $myconfig, $form, $item) = @_;
228
229   my $module = $form->{script};
230   my $action = "section_menu";
231
232   #if ($self->{$item}{module}) {
233   $module = $self->{$item}{module};
234
235   #}
236   if ($self->{$item}{action}) {
237     $action = $self->{$item}{action};
238   }
239
240   my $level = $form->escape($item);
241   my $str   = qq|$module?action=$action&level=$level|;
242   my @vars = qw(module action target href);
243
244   if ($self->{$item}{href}) {
245     $str  = qq|$self->{$item}{href}|;
246     @vars = qw(module target href);
247   }
248
249   map { delete $self->{$item}{$_} } @vars;
250
251   # add other params
252   foreach my $key (keys %{ $self->{$item} }) {
253     $str .= "&" . $form->escape($key, 1) . "=";
254     ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
255     $value = $myconfig->{$value} . "/$conf" if ($conf);
256     $str .= $form->escape($value, 1);
257   }
258
259   $str .= " ";
260
261 }
262
263 sub access_control {
264   $main::lxdebug->enter_sub(2);
265
266   my ($self, $myconfig, $menulevel) = @_;
267
268   my @menu = ();
269
270   if ($menulevel eq "") {
271     @menu = grep { !/--/ } @{ $self->{ORDER} };
272   } else {
273     @menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
274   }
275
276   my @a    = split(/;/, $myconfig->{acs});
277   my $excl = ();
278
279   # remove --AR, --AP from array
280   grep { ($a, $b) = split(/--/); s/--$a$//; } @a;
281
282   map { $excl{$_} = 1 } @a;
283
284   @a = ();
285   map { push @a, $_ unless $excl{$_} } (@menu);
286
287   $main::lxdebug->leave_sub(2);
288
289   return @a;
290 }
291
292 sub generate_acl {
293   my ($self, $menulevel, $hash) = @_;
294
295   my @items = $self->access_control(\%main::myconfig, $menulevel);
296
297   $menulevel =~ s/[^A-Za-z_\/\.\+\-]/_/g;
298   $hash->{"access_" . lc($menulevel)} = 1 if ($menulevel);
299
300   foreach my $item (@items) {
301     $self->generate_acl($item, $hash); #unless ($menulevel);
302   }
303 }
304
305 sub parse_access_string {
306   my $self   = shift;
307   my $key    = shift;
308   my $access = shift;
309
310   my @stack;
311   my $cur_ary = [];
312
313   push @stack, $cur_ary;
314
315   while ($access =~ m/^([a-z_]+|\||\&|\(|\)|\s+)/) {
316     my $token = $1;
317     substr($access, 0, length($1)) = "";
318
319     next if ($token =~ /\s/);
320
321     if ($token eq "(") {
322       my $new_cur_ary = [];
323       push @stack, $new_cur_ary;
324       push @{$cur_ary}, $new_cur_ary;
325       $cur_ary = $new_cur_ary;
326
327     } elsif ($token eq ")") {
328       pop @stack;
329       if (!@stack) {
330         $main::form->error("Error in menu.ini for entry ${key}: missing '('");
331       }
332       $cur_ary = $stack[-1];
333
334     } elsif (($token eq "|") || ($token eq "&")) {
335       push @{$cur_ary}, $token;
336
337     } else {
338       push @{$cur_ary}, $main::auth->check_right($main::form->{login}, $token, 1);
339     }
340   }
341
342   if ($access) {
343     $main::form->error("Error in menu.ini for entry ${name}: unrecognized token at the start of '$access'\n");
344   }
345
346   if (1 < scalar @stack) {
347     $main::form->error("Error in menu.ini for entry ${name}: Missing ')'\n");
348   }
349
350   return SL::Auth::evaluate_rights_ary($stack[0]);
351 }
352
353 sub set_access {
354   my $self = shift;
355
356   my $key;
357
358   foreach $key (@{ $self->{ORDER} }) {
359     my $entry = $self->{$key};
360
361     $entry->{GRANTED}              = $entry->{ACCESS} ? $self->parse_access_string($key, $entry->{ACCESS}) : 1;
362     $entry->{IS_MENU}              = $entry->{submenu} || ($key !~ m/--/);
363     $entry->{NUM_VISIBLE_CHILDREN} = 0;
364
365     if ($key =~ m/--/) {
366       my $parent = $key;
367       substr($parent, rindex($parent, '--')) = '';
368       $entry->{GRANTED} &&= $self->{$parent}->{GRANTED};
369     }
370
371     $entry->{VISIBLE} = $entry->{GRANTED};
372   }
373
374   foreach $key (reverse @{ $self->{ORDER} }) {
375     my $entry = $self->{$key};
376
377     if ($entry->{IS_MENU}) {
378       $entry->{VISIBLE} &&= $entry->{NUM_VISIBLE_CHILDREN} > 0;
379     }
380
381     next if (($key !~ m/--/) || !$entry->{VISIBLE});
382
383     my $parent = $key;
384     substr($parent, rindex($parent, '--')) = '';
385     $self->{$parent}->{NUM_VISIBLE_CHILDREN}++;
386   }
387
388 #   $self->dump_visible();
389
390   $self->{ORDER} = [ grep { $self->{$_}->{VISIBLE} } @{ $self->{ORDER} } ];
391
392   map { delete @{$self->{$_}}{qw(GRANTED IS_MENU NUM_VISIBLE_CHILDREN VISIBLE ACCESS)} if ($_ ne 'ORDER') } keys %{ $self };
393 }
394
395 sub dump_visible {
396   my $self = shift;
397   foreach my $key (@{ $self->{ORDER} }) {
398     my $entry = $self->{$key};
399     $main::lxdebug->message(0, "$entry->{GRANTED} $entry->{VISIBLE} $entry->{NUM_VISIBLE_CHILDREN} $key");
400   }
401 }
402
403 1;
404