Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[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_new {
62   $main::lxdebug->enter_sub(LXDebug::DEBUG2());
63
64   my ($self, $name, $item) = @_;
65
66   my $form        =  $main::form;
67   my $myconfig    = \%main::myconfig;
68
69   my $module      = $self->{$name}->{module} || $form->{script};
70   my $action      = $self->{$name}->{action};
71
72   $item->{target} = $self->{$name}->{target} || "main_window";
73   $item->{href}   = $self->{$name}->{href}   || "${module}?action=" . $form->escape($action);
74
75   my @vars = qw(module target href);
76   push @vars, 'action' unless ($self->{$name}->{href});
77
78   map { delete $self->{$name}{$_} } @vars;
79
80   # add other params
81   foreach my $key (keys %{ $self->{$name} }) {
82     my ($value, $conf)  = split(m/=/, $self->{$name}->{$key}, 2);
83     $value              = $myconfig->{$value} . "/$conf" if ($conf);
84     $item->{href}      .= "&" . $form->escape($key) . "=" . $form->escape($value);
85   }
86
87   $main::lxdebug->leave_sub(LXDebug::DEBUG2());
88 }
89
90 sub access_control {
91   $main::lxdebug->enter_sub(2);
92
93   my ($self, $myconfig, $menulevel) = @_;
94
95   my @menu = ();
96
97   if ($menulevel eq "") {
98     @menu = grep { !/--/ } @{ $self->{ORDER} };
99   } else {
100     @menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
101   }
102
103   $main::lxdebug->leave_sub(2);
104
105   return @menu;
106 }
107
108 sub parse_access_string {
109   my $self   = shift;
110   my $key    = shift;
111   my $access = shift;
112
113   my $form        =  $main::form;
114   my $auth        =  $main::auth;
115   my $myconfig    = \%main::myconfig;
116
117   my @stack;
118   my $cur_ary = [];
119
120   push @stack, $cur_ary;
121
122   while ($access =~ m/^([a-z_]+|\||\&|\(|\)|\s+)/) {
123     my $token = $1;
124     substr($access, 0, length($1)) = "";
125
126     next if ($token =~ /\s/);
127
128     if ($token eq "(") {
129       my $new_cur_ary = [];
130       push @stack, $new_cur_ary;
131       push @{$cur_ary}, $new_cur_ary;
132       $cur_ary = $new_cur_ary;
133
134     } elsif ($token eq ")") {
135       pop @stack;
136       if (!@stack) {
137         $form->error("Error in menu.ini for entry ${key}: missing '('");
138       }
139       $cur_ary = $stack[-1];
140
141     } elsif (($token eq "|") || ($token eq "&")) {
142       push @{$cur_ary}, $token;
143
144     } else {
145       push @{$cur_ary}, $auth->check_right($form->{login}, $token, 1);
146     }
147   }
148
149   if ($access) {
150     $form->error("Error in menu.ini for entry ${key}: unrecognized token at the start of '$access'\n");
151   }
152
153   if (1 < scalar @stack) {
154     $main::form->error("Error in menu.ini for entry ${key}: Missing ')'\n");
155   }
156
157   return SL::Auth::evaluate_rights_ary($stack[0]);
158 }
159
160 sub set_access {
161   my $self = shift;
162
163   my $key;
164
165   foreach $key (@{ $self->{ORDER} }) {
166     my $entry = $self->{$key};
167
168     $entry->{GRANTED}              = $entry->{ACCESS} ? $self->parse_access_string($key, $entry->{ACCESS}) : 1;
169     $entry->{IS_MENU}              = $entry->{submenu} || ($key !~ m/--/);
170     $entry->{NUM_VISIBLE_CHILDREN} = 0;
171
172     if ($key =~ m/--/) {
173       my $parent = $key;
174       substr($parent, rindex($parent, '--')) = '';
175       $entry->{GRANTED} &&= $self->{$parent}->{GRANTED};
176     }
177
178     $entry->{VISIBLE} = $entry->{GRANTED};
179   }
180
181   foreach $key (reverse @{ $self->{ORDER} }) {
182     my $entry = $self->{$key};
183
184     if ($entry->{IS_MENU}) {
185       $entry->{VISIBLE} &&= $entry->{NUM_VISIBLE_CHILDREN} > 0;
186     }
187
188     next if (($key !~ m/--/) || !$entry->{VISIBLE});
189
190     my $parent = $key;
191     substr($parent, rindex($parent, '--')) = '';
192     $self->{$parent}->{NUM_VISIBLE_CHILDREN}++;
193   }
194
195 #   $self->dump_visible();
196
197   $self->{ORDER} = [ grep { $self->{$_}->{VISIBLE} } @{ $self->{ORDER} } ];
198
199   { no strict 'refs';
200   # ToDO: fix this. nuke and pave algorithm without type checking screams for problems.
201   map { delete @{$self->{$_}}{qw(GRANTED IS_MENU NUM_VISIBLE_CHILDREN VISIBLE ACCESS)} if ($_ ne 'ORDER') } keys %{ $self };
202   }
203 }
204
205 sub dump_visible {
206   my $self = shift;
207   foreach my $key (@{ $self->{ORDER} }) {
208     my $entry = $self->{$key};
209     $main::lxdebug->message(0, "$entry->{GRANTED} $entry->{VISIBLE} $entry->{NUM_VISIBLE_CHILDREN} $key");
210   }
211 }
212
213 1;
214