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