Umstellung der Benutzerverwaltung von Dateien im Verzeichnis "users" auf die Verwendu...
[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   map({ $str .= "&amp;${_}=" . $form->escape($form->{$_}); } qw(login password));
193
194   my @vars = qw(module action target href);
195
196   if ($self->{$item}{href}) {
197     $str  = qq| link=$self->{$item}{href}|;
198     @vars = qw(module target href);
199   }
200
201   map { delete $self->{$item}{$_} } @vars;
202
203   # add other params
204   foreach my $key (keys %{ $self->{$item} }) {
205     $str .= "&amp;" . $form->escape($key, 1) . "=";
206     ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
207     $value = $myconfig->{$value} . "/$conf" if ($conf);
208     $str .= $form->escape($value, 1);
209   }
210
211   $str .= '"';
212
213
214
215   if ($other) {
216     foreach my $key (keys(%{$other})) {
217       $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
218     }
219   }
220
221
222   $main::lxdebug->leave_sub();
223
224   return $str;
225 }
226
227 sub menuitemNew {
228   my ($self, $myconfig, $form, $item) = @_;
229
230   my $module = $form->{script};
231   my $action = "section_menu";
232
233   #if ($self->{$item}{module}) {
234   $module = $self->{$item}{module};
235
236   #}
237   if ($self->{$item}{action}) {
238     $action = $self->{$item}{action};
239   }
240
241   my $level = $form->escape($item);
242   my $str   = qq|$module?action=$action&level=$level|;
243   my @vars = qw(module action target href);
244
245   if ($self->{$item}{href}) {
246     $str  = qq|$self->{$item}{href}|;
247     @vars = qw(module target href);
248   }
249
250   map { delete $self->{$item}{$_} } @vars;
251
252   # add other params
253   foreach my $key (keys %{ $self->{$item} }) {
254     $str .= "&" . $form->escape($key, 1) . "=";
255     ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
256     $value = $myconfig->{$value} . "/$conf" if ($conf);
257     $str .= $form->escape($value, 1);
258   }
259
260   $str .= " ";
261
262 }
263
264 sub access_control {
265   $main::lxdebug->enter_sub(2);
266
267   my ($self, $myconfig, $menulevel) = @_;
268
269   my @menu = ();
270
271   if ($menulevel eq "") {
272     @menu = grep { !/--/ } @{ $self->{ORDER} };
273   } else {
274     @menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
275   }
276
277   my @a    = split(/;/, $myconfig->{acs});
278   my $excl = ();
279
280   # remove --AR, --AP from array
281   grep { ($a, $b) = split(/--/); s/--$a$//; } @a;
282
283   map { $excl{$_} = 1 } @a;
284
285   @a = ();
286   map { push @a, $_ unless $excl{$_} } (@menu);
287
288   $main::lxdebug->leave_sub(2);
289
290   return @a;
291 }
292
293 sub generate_acl {
294   my ($self, $menulevel, $hash) = @_;
295
296   my @items = $self->access_control(\%main::myconfig, $menulevel);
297
298   $menulevel =~ s/[^A-Za-z_\/\.\+\-]/_/g;
299   $hash->{"access_" . lc($menulevel)} = 1 if ($menulevel);
300
301   foreach my $item (@items) {
302     $self->generate_acl($item, $hash); #unless ($menulevel);
303   }
304 }
305
306 sub parse_access_string {
307   my $self   = shift;
308   my $key    = shift;
309   my $access = shift;
310
311   my @stack;
312   my $cur_ary = [];
313
314   push @stack, $cur_ary;
315
316   while ($access =~ m/^([a-z_]+|\||\&|\(|\)|\s+)/) {
317     my $token = $1;
318     substr($access, 0, length($1)) = "";
319
320     next if ($token =~ /\s/);
321
322     if ($token eq "(") {
323       my $new_cur_ary = [];
324       push @stack, $new_cur_ary;
325       push @{$cur_ary}, $new_cur_ary;
326       $cur_ary = $new_cur_ary;
327
328     } elsif ($token eq ")") {
329       pop @stack;
330       if (!@stack) {
331         $main::form->error("Error in menu.ini for entry ${key}: missing '('");
332       }
333       $cur_ary = $stack[-1];
334
335     } elsif (($token eq "|") || ($token eq "&")) {
336       push @{$cur_ary}, $token;
337
338     } else {
339       push @{$cur_ary}, $main::auth->check_right($main::form->{login}, $token, 1);
340     }
341   }
342
343   if ($access) {
344     $main::form->error("Error in menu.ini for entry ${name}: unrecognized token at the start of '$access'\n");
345   }
346
347   if (1 < scalar @stack) {
348     $main::form->error("Error in menu.ini for entry ${name}: Missing ')'\n");
349   }
350
351   return SL::Auth::evaluate_rights_ary($stack[0]);
352 }
353
354 sub set_access {
355   my $self = shift;
356
357   my $key;
358
359   foreach $key (@{ $self->{ORDER} }) {
360     my $entry = $self->{$key};
361
362     $entry->{GRANTED}              = $entry->{ACCESS} ? $self->parse_access_string($key, $entry->{ACCESS}) : 1;
363     $entry->{IS_MENU}              = $entry->{submenu} || ($key !~ m/--/);
364     $entry->{NUM_VISIBLE_CHILDREN} = 0;
365
366     if ($key =~ m/--/) {
367       my $parent = $key;
368       substr($parent, rindex($parent, '--')) = '';
369       $entry->{GRANTED} &&= $self->{$parent}->{GRANTED};
370     }
371
372     $entry->{VISIBLE} = $entry->{GRANTED};
373   }
374
375   foreach $key (reverse @{ $self->{ORDER} }) {
376     my $entry = $self->{$key};
377
378     if ($entry->{IS_MENU}) {
379       $entry->{VISIBLE} &&= $entry->{NUM_VISIBLE_CHILDREN} > 0;
380     }
381
382     next if (($key !~ m/--/) || !$entry->{VISIBLE});
383
384     my $parent = $key;
385     substr($parent, rindex($parent, '--')) = '';
386     $self->{$parent}->{NUM_VISIBLE_CHILDREN}++;
387   }
388
389 #   $self->dump_visible();
390
391   $self->{ORDER} = [ grep { $self->{$_}->{VISIBLE} } @{ $self->{ORDER} } ];
392
393   map { delete @{$self->{$_}}{qw(GRANTED IS_MENU NUM_VISIBLE_CHILDREN VISIBLE ACCESS)} if ($_ ne 'ORDER') } keys %{ $self };
394 }
395
396 sub dump_visible {
397   my $self = shift;
398   foreach my $key (@{ $self->{ORDER} }) {
399     my $entry = $self->{$key};
400     $main::lxdebug->message(0, "$entry->{GRANTED} $entry->{VISIBLE} $entry->{NUM_VISIBLE_CHILDREN} $key");
401   }
402 }
403
404 1;
405