X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FMenu.pm;h=06ea6ee43ed44c04b21459694f7e7ebce836b05b;hb=99bbf349b825fc48cdb71288e0979ab687770227;hp=89a6ffd15e06cb1b16b94388e40ccb40186c5bfb;hpb=db53dc8ab328b82c7af17896487674ad98f2af6b;p=kivitendo-erp.git
diff --git a/SL/Menu.pm b/SL/Menu.pm
index 89a6ffd15..06ea6ee43 100644
--- a/SL/Menu.pm
+++ b/SL/Menu.pm
@@ -34,213 +34,184 @@
package Menu;
+use SL::Auth;
use SL::Inifile;
+use strict;
+
sub new {
$main::lxdebug->enter_sub();
- my ($type, $menufile) = @_;
+ my ($type, @menufiles) = @_;
+ my $self = bless {}, $type;
- my $self = {};
- my $inifile = Inifile->new($menufile);
+ my @order;
- map { $self->{$_} = $inifile->{$_} } keys %{ $inifile };
+ foreach my $menufile (grep { -f } @menufiles) {
+ my $inifile = Inifile->new($menufile);
- $main::lxdebug->leave_sub();
+ push @order, @{ delete($inifile->{ORDER}) || [] };
+ $self->{$_} = $inifile->{$_} for keys %{ $inifile };
+ }
- bless $self, $type;
-}
+ $self->{ORDER} = \@order;
-sub menuitem {
- $main::lxdebug->enter_sub();
+ $self->set_access();
- my ($self, $myconfig, $form, $item) = @_;
+ $main::lxdebug->leave_sub();
- my $module = $form->{script};
- my $action = "section_menu";
- my $target = "";
+ return $self;
+}
- if ($self->{$item}{module}) {
- $module = $self->{$item}{module};
- }
- if ($self->{$item}{action}) {
- $action = $self->{$item}{action};
- }
- if ($self->{$item}{target}) {
- $target = $self->{$item}{target};
- }
+sub menuitem_new {
+ $main::lxdebug->enter_sub(LXDebug::DEBUG2());
- my $level = $form->escape($item);
+ my ($self, $name, $item) = @_;
- my $str =
- qq|{login}&password=$form->{password}|;
+ my $form = $main::form;
+ my $myconfig = \%main::myconfig;
- my @vars = qw(module action target href);
+ my $module = $self->{$name}->{module} || $form->{script};
+ my $action = $self->{$name}->{action};
- if ($self->{$item}{href}) {
- $str = qq|{$item}{href}|;
- @vars = qw(module target href);
- }
+ $item->{target} = $self->{$name}->{target} || "main_window";
+ $item->{href} = $self->{$name}->{href} || "${module}?action=" . $form->escape($action);
- map { delete $self->{$item}{$_} } @vars;
+ my @vars = qw(module target href);
+ push @vars, 'action' unless ($self->{$name}->{href});
- # add other params
- foreach my $key (keys %{ $self->{$item} }) {
- $str .= "&" . $form->escape($key, 1) . "=";
- ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
- $value = $myconfig->{$value} . "/$conf" if ($conf);
- $str .= $form->escape($value, 1);
- }
+ map { delete $self->{$name}{$_} } @vars;
- if ($target) {
- $str .= qq| target=$target|;
+ # add other params
+ foreach my $key (keys %{ $self->{$name} }) {
+ my ($value, $conf) = split(m/=/, $self->{$name}->{$key}, 2);
+ $value = $myconfig->{$value} . "/$conf" if ($conf);
+ $item->{href} .= "&" . $form->escape($key) . "=" . $form->escape($value);
}
- $str .= ">";
-
- $main::lxdebug->leave_sub();
-
- return $str;
+ $main::lxdebug->leave_sub(LXDebug::DEBUG2());
}
-sub menuitem_v3 {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form, $item, $other) = @_;
-
- my $module = $form->{script};
- my $action = "section_menu";
- my $target = "";
-
- if ($self->{$item}{module}) {
- $module = $self->{$item}{module};
- }
- if ($self->{$item}{action}) {
- $action = $self->{$item}{action};
- }
- if ($self->{$item}{target}) {
- $target = $self->{$item}{target};
- }
-
- my $level = $form->escape($item);
+sub access_control {
+ $main::lxdebug->enter_sub(2);
- my $str = qq|escape($form->{$_}); } qw(login password));
+ my ($self, $myconfig, $menulevel) = @_;
- my @vars = qw(module action target href);
+ my @menu = ();
- if ($self->{$item}{href}) {
- $str = qq|{$item}{href}|;
- @vars = qw(module target href);
+ if (!$menulevel) {
+ @menu = grep { !/--/ } @{ $self->{ORDER} };
+ } else {
+ @menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
}
- map { delete $self->{$item}{$_} } @vars;
+ $main::lxdebug->leave_sub(2);
- # add other params
- foreach my $key (keys %{ $self->{$item} }) {
- $str .= "&" . $form->escape($key, 1) . "=";
- ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
- $value = $myconfig->{$value} . "/$conf" if ($conf);
- $str .= $form->escape($value, 1);
- }
+ return @menu;
+}
- $str .= '"';
+sub parse_access_string {
+ my $self = shift;
+ my $key = shift;
+ my $access = shift;
- if ($target) {
- $str .= qq| target="| . $form->quote($target) . qq|"|;
- }
+ my $form = $main::form;
+ my $auth = $main::auth;
+ my $myconfig = \%main::myconfig;
- if ($other) {
- foreach my $key (keys(%{$other})) {
- $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
- }
- }
+ my @stack;
+ my $cur_ary = [];
- $str .= ">";
+ push @stack, $cur_ary;
- $main::lxdebug->leave_sub();
+ while ($access =~ m/^([a-z_]+|\||\&|\(|\)|\s+)/) {
+ my $token = $1;
+ substr($access, 0, length($1)) = "";
- return $str;
-}
+ next if ($token =~ /\s/);
-sub menuitemNew {
- my ($self, $myconfig, $form, $item) = @_;
+ if ($token eq "(") {
+ my $new_cur_ary = [];
+ push @stack, $new_cur_ary;
+ push @{$cur_ary}, $new_cur_ary;
+ $cur_ary = $new_cur_ary;
- my $module = $form->{script};
- my $action = "section_menu";
+ } elsif ($token eq ")") {
+ pop @stack;
+ if (!@stack) {
+ $form->error("Error in menu.ini for entry ${key}: missing '('");
+ }
+ $cur_ary = $stack[-1];
- #if ($self->{$item}{module}) {
- $module = $self->{$item}{module};
+ } elsif (($token eq "|") || ($token eq "&")) {
+ push @{$cur_ary}, $token;
- #}
- if ($self->{$item}{action}) {
- $action = $self->{$item}{action};
+ } else {
+ push @{$cur_ary}, $auth->check_right($form->{login}, $token, 1);
+ }
}
- my $level = $form->escape($item);
- my $str =
- qq|$module?action=$action&level=$level&login=$form->{login}&password=$form->{password}|;
- my @vars = qw(module action target href);
-
- if ($self->{$item}{href}) {
- $str = qq|$self->{$item}{href}|;
- @vars = qw(module target href);
+ if ($access) {
+ $form->error("Error in menu.ini for entry ${key}: unrecognized token at the start of '$access'\n");
}
- map { delete $self->{$item}{$_} } @vars;
-
- # add other params
- foreach my $key (keys %{ $self->{$item} }) {
- $str .= "&" . $form->escape($key, 1) . "=";
- ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
- $value = $myconfig->{$value} . "/$conf" if ($conf);
- $str .= $form->escape($value, 1);
+ if (1 < scalar @stack) {
+ $main::form->error("Error in menu.ini for entry ${key}: Missing ')'\n");
}
- $str .= " ";
-
+ return SL::Auth::evaluate_rights_ary($stack[0]);
}
-sub access_control {
- $main::lxdebug->enter_sub(2);
+sub set_access {
+ my $self = shift;
- my ($self, $myconfig, $menulevel) = @_;
+ my $key;
- my @menu = ();
+ foreach $key (@{ $self->{ORDER} }) {
+ my $entry = $self->{$key};
- if ($menulevel eq "") {
- @menu = grep { !/--/ } @{ $self->{ORDER} };
- } else {
- @menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
- }
+ $entry->{GRANTED} = $entry->{ACCESS} ? $self->parse_access_string($key, $entry->{ACCESS}) : 1;
+ $entry->{IS_MENU} = $entry->{submenu} || ($key !~ m/--/);
+ $entry->{NUM_VISIBLE_CHILDREN} = 0;
- my @a = split(/;/, $myconfig->{acs});
- my $excl = ();
+ if ($key =~ m/--/) {
+ my $parent = $key;
+ substr($parent, rindex($parent, '--')) = '';
+ $entry->{GRANTED} &&= $self->{$parent}->{GRANTED};
+ }
- # remove --AR, --AP from array
- grep { ($a, $b) = split(/--/); s/--$a$//; } @a;
+ $entry->{VISIBLE} = $entry->{GRANTED};
+ }
- map { $excl{$_} = 1 } @a;
+ foreach $key (reverse @{ $self->{ORDER} }) {
+ my $entry = $self->{$key};
- @a = ();
- map { push @a, $_ unless $excl{$_} } (@menu);
+ if ($entry->{IS_MENU}) {
+ $entry->{VISIBLE} &&= $entry->{NUM_VISIBLE_CHILDREN} > 0;
+ }
- $main::lxdebug->leave_sub(2);
+ next if (($key !~ m/--/) || !$entry->{VISIBLE});
- return @a;
-}
+ my $parent = $key;
+ substr($parent, rindex($parent, '--')) = '';
+ $self->{$parent}->{NUM_VISIBLE_CHILDREN}++;
+ }
-sub generate_acl {
- my ($self, $menulevel, $hash) = @_;
+# $self->dump_visible();
- my @items = $self->access_control(\%main::myconfig, $menulevel);
+ $self->{ORDER} = [ grep { $self->{$_}->{VISIBLE} } @{ $self->{ORDER} } ];
- $menulevel =~ s/[^A-Za-z_\/\.\+\-]/_/g;
- $hash->{"access_" . lc($menulevel)} = 1 if ($menulevel);
+ { no strict 'refs';
+ # ToDO: fix this. nuke and pave algorithm without type checking screams for problems.
+ map { delete @{$self->{$_}}{qw(GRANTED IS_MENU NUM_VISIBLE_CHILDREN VISIBLE ACCESS)} if ($_ ne 'ORDER') } keys %{ $self };
+ }
+}
- foreach my $item (@items) {
- $self->generate_acl($item, $hash); #unless ($menulevel);
+sub dump_visible {
+ my $self = shift;
+ foreach my $key (@{ $self->{ORDER} }) {
+ my $entry = $self->{$key};
+ $main::lxdebug->message(0, "$entry->{GRANTED} $entry->{VISIBLE} $entry->{NUM_VISIBLE_CHILDREN} $key");
}
}