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