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