Inifile.pm auch dazu benutzen, um die memers/users einzulesen und neu zu schreiben.
[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::Inifile;
38
39 sub new {
40   $main::lxdebug->enter_sub();
41
42   my ($type, $menufile) = @_;
43
44   my $self    = {};
45   my $inifile = Inifile->new($menufile);
46
47   map { $self->{$_} = $inifile->{$_} } keys %{ $inifile };
48
49   $main::lxdebug->leave_sub();
50
51   bless $self, $type;
52 }
53
54 sub menuitem {
55   $main::lxdebug->enter_sub();
56
57   my ($self, $myconfig, $form, $item) = @_;
58
59   my $module = $form->{script};
60   my $action = "section_menu";
61   my $target = "";
62
63   if ($self->{$item}{module}) {
64     $module = $self->{$item}{module};
65   }
66   if ($self->{$item}{action}) {
67     $action = $self->{$item}{action};
68   }
69   if ($self->{$item}{target}) {
70     $target = $self->{$item}{target};
71   }
72
73   my $level = $form->escape($item);
74
75   my $str =
76     qq|<a style="vertical-align:top" href=$module?action=$action&level=$level&login=$form->{login}&password=$form->{password}|;
77
78   my @vars = qw(module action target href);
79
80   if ($self->{$item}{href}) {
81     $str  = qq|<a href=$self->{$item}{href}|;
82     @vars = qw(module target href);
83   }
84
85   map { delete $self->{$item}{$_} } @vars;
86
87   # add other params
88   foreach my $key (keys %{ $self->{$item} }) {
89     $str .= "&" . $form->escape($key, 1) . "=";
90     ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
91     $value = $myconfig->{$value} . "/$conf" if ($conf);
92     $str .= $form->escape($value, 1);
93   }
94
95   if ($target) {
96     $str .= qq| target=$target|;
97   }
98
99   $str .= ">";
100
101   $main::lxdebug->leave_sub();
102
103   return $str;
104 }
105
106 sub menuitem_v3 {
107   $main::lxdebug->enter_sub();
108
109   my ($self, $myconfig, $form, $item, $other) = @_;
110
111   my $module = $form->{script};
112   my $action = "section_menu";
113   my $target = "";
114
115   if ($self->{$item}{module}) {
116     $module = $self->{$item}{module};
117   }
118   if ($self->{$item}{action}) {
119     $action = $self->{$item}{action};
120   }
121   if ($self->{$item}{target}) {
122     $target = $self->{$item}{target};
123   }
124
125   my $level = $form->escape($item);
126
127   my $str = qq|<a href="$module?action=| . $form->escape($action) .
128     qq|&level=| . $form->escape($level);
129   map({ $str .= "&${_}=" . $form->escape($form->{$_}); } qw(login password));
130
131   my @vars = qw(module action target href);
132
133   if ($self->{$item}{href}) {
134     $str  = qq|<a href=$self->{$item}{href}|;
135     @vars = qw(module target href);
136   }
137
138   map { delete $self->{$item}{$_} } @vars;
139
140   # add other params
141   foreach my $key (keys %{ $self->{$item} }) {
142     $str .= "&" . $form->escape($key, 1) . "=";
143     ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
144     $value = $myconfig->{$value} . "/$conf" if ($conf);
145     $str .= $form->escape($value, 1);
146   }
147
148   $str .= '"';
149
150   if ($target) {
151     $str .= qq| target="| . $form->quote($target) . qq|"|;
152   }
153
154   if ($other) {
155     foreach my $key (keys(%{$other})) {
156       $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
157     }
158   }
159
160   $str .= ">";
161
162   $main::lxdebug->leave_sub();
163
164   return $str;
165 }
166
167 sub menuitemNew {
168   my ($self, $myconfig, $form, $item) = @_;
169
170   my $module = $form->{script};
171   my $action = "section_menu";
172
173   #if ($self->{$item}{module}) {
174   $module = $self->{$item}{module};
175
176   #}
177   if ($self->{$item}{action}) {
178     $action = $self->{$item}{action};
179   }
180
181   my $level = $form->escape($item);
182   my $str   =
183     qq|$module?action=$action&level=$level&login=$form->{login}&password=$form->{password}|;
184   my @vars = qw(module action target href);
185
186   if ($self->{$item}{href}) {
187     $str  = qq|$self->{$item}{href}|;
188     @vars = qw(module target href);
189   }
190
191   map { delete $self->{$item}{$_} } @vars;
192
193   # add other params
194   foreach my $key (keys %{ $self->{$item} }) {
195     $str .= "&" . $form->escape($key, 1) . "=";
196     ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
197     $value = $myconfig->{$value} . "/$conf" if ($conf);
198     $str .= $form->escape($value, 1);
199   }
200
201   $str .= " ";
202
203 }
204
205 sub access_control {
206   $main::lxdebug->enter_sub(2);
207
208   my ($self, $myconfig, $menulevel) = @_;
209
210   my @menu = ();
211
212   if ($menulevel eq "") {
213     @menu = grep { !/--/ } @{ $self->{ORDER} };
214   } else {
215     @menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
216   }
217
218   my @a    = split(/;/, $myconfig->{acs});
219   my $excl = ();
220
221   # remove --AR, --AP from array
222   grep { ($a, $b) = split(/--/); s/--$a$//; } @a;
223
224   map { $excl{$_} = 1 } @a;
225
226   @a = ();
227   map { push @a, $_ unless $excl{$_} } (@menu);
228
229   $main::lxdebug->leave_sub(2);
230
231   return @a;
232 }
233
234 sub generate_acl {
235   my ($self, $menulevel, $hash) = @_;
236
237   my @items = $self->access_control(\%main::myconfig, $menulevel);
238
239   $menulevel =~ s/[^A-Za-z_\/\.\+\-]/_/g;
240   $hash->{"access_" . lc($menulevel)} = 1 if ($menulevel);
241
242   foreach my $item (@items) {
243     $self->generate_acl($item, $hash); #unless ($menulevel);
244   }
245 }
246
247 1;
248