X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;ds=sidebyside;f=SL%2FMenu.pm;h=48ac6229daa2b672aa74ad6fb7cdbb24883c1fed;hb=c2f911bdbad03f7b81e0121e4e866bb96586168c;hp=0bdf945e4a888690a45dece9f0e684755849b06e;hpb=de763d70b4b2e0501565e579fb4726e033594589;p=kivitendo-erp.git
diff --git a/SL/Menu.pm b/SL/Menu.pm
index 0bdf945e4..48ac6229d 100644
--- a/SL/Menu.pm
+++ b/SL/Menu.pm
@@ -34,136 +34,180 @@
package Menu;
+use SL::Auth;
+use SL::Inifile;
+
+use strict;
+
sub new {
$main::lxdebug->enter_sub();
- my ($type, $menufile, $level) = @_;
+ my ($type, $menufile) = @_;
- use SL::Inifile;
- my $self = Inifile->new($menufile, $level);
+ my $self = {};
+ my $inifile = Inifile->new($menufile);
- $main::lxdebug->leave_sub();
+ map { $self->{$_} = $inifile->{$_} } keys %{ $inifile };
bless $self, $type;
+
+ $self->set_access();
+
+ $main::lxdebug->leave_sub();
+
+ return $self;
}
-sub menuitem {
+sub menuitem_new {
$main::lxdebug->enter_sub();
- my ($self, $myconfig, $form, $item) = @_;
+ my ($self, $name, $item) = @_;
- my $module = $form->{script};
- my $action = "section_menu";
- my $target = "";
+ my $form = $main::form;
+ my $myconfig = \%main::myconfig;
- 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 $module = $self->{$name}->{module} || $form->{script};
+ my $action = $self->{$name}->{action};
- my $level = $form->escape($item);
+ $item->{target} = $self->{$name}->{target} || "main_window";
+ $item->{href} = $self->{$name}->{href} || "${module}?action=" . $form->escape($action);
- my $str =
- qq|{path}&action=$action&level=$level&login=$form->{login}&password=$form->{password}|;
+ my @vars = qw(module target href);
+ push @vars, 'action' unless ($self->{$name}->{href});
- my @vars = qw(module action target href);
+ map { delete $self->{$name}{$_} } @vars;
- if ($self->{$item}{href}) {
- $str = qq|{$item}{href}|;
- @vars = qw(module target href);
+ # 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);
}
- map { delete $self->{$item}{$_} } @vars;
+ $main::lxdebug->leave_sub();
+}
+
+sub access_control {
+ $main::lxdebug->enter_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);
- }
+ my ($self, $myconfig, $menulevel) = @_;
- if ($target) {
- $str .= qq| target=$target|;
- }
+ my @menu = ();
- $str .= ">";
+ if ($menulevel eq "") {
+ @menu = grep { !/--/ } @{ $self->{ORDER} };
+ } else {
+ @menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
+ }
- $main::lxdebug->leave_sub();
+ $main::lxdebug->leave_sub(2);
- return $str;
+ return @menu;
}
-sub menuitemNew {
- my ($self, $myconfig, $form, $item) = @_;
+sub parse_access_string {
+ my $self = shift;
+ my $key = shift;
+ my $access = shift;
- my $module = $form->{script};
- my $action = "section_menu";
+ my $form = $main::form;
+ my $auth = $main::auth;
+ my $myconfig = \%main::myconfig;
- #if ($self->{$item}{module}) {
- $module = $self->{$item}{module};
+ my @stack;
+ my $cur_ary = [];
- #}
- if ($self->{$item}{action}) {
- $action = $self->{$item}{action};
- }
+ push @stack, $cur_ary;
- my $level = $form->escape($item);
- my $str =
- qq|$module?path=$form->{path}&action=$action&level=$level&login=$form->{login}&password=$form->{password}|;
- my @vars = qw(module action target href);
+ while ($access =~ m/^([a-z_]+|\||\&|\(|\)|\s+)/) {
+ my $token = $1;
+ substr($access, 0, length($1)) = "";
- if ($self->{$item}{href}) {
- $str = qq|$self->{$item}{href}|;
- @vars = qw(module target href);
- }
+ next if ($token =~ /\s/);
- map { delete $self->{$item}{$_} } @vars;
+ if ($token eq "(") {
+ my $new_cur_ary = [];
+ push @stack, $new_cur_ary;
+ push @{$cur_ary}, $new_cur_ary;
+ $cur_ary = $new_cur_ary;
- # 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);
+ } elsif ($token eq ")") {
+ pop @stack;
+ if (!@stack) {
+ $form->error("Error in menu.ini for entry ${key}: missing '('");
+ }
+ $cur_ary = $stack[-1];
+
+ } elsif (($token eq "|") || ($token eq "&")) {
+ push @{$cur_ary}, $token;
+
+ } else {
+ push @{$cur_ary}, $auth->check_right($form->{login}, $token, 1);
+ }
}
- $str .= " ";
+ if ($access) {
+ $form->error("Error in menu.ini for entry ${key}: unrecognized token at the start of '$access'\n");
+ }
+ if (1 < scalar @stack) {
+ $main::form->error("Error in menu.ini for entry ${key}: Missing ')'\n");
+ }
+
+ return SL::Auth::evaluate_rights_ary($stack[0]);
}
-sub access_control {
- $main::lxdebug->enter_sub();
+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;
+
+ if ($key =~ m/--/) {
+ my $parent = $key;
+ substr($parent, rindex($parent, '--')) = '';
+ $entry->{GRANTED} &&= $self->{$parent}->{GRANTED};
+ }
+
+ $entry->{VISIBLE} = $entry->{GRANTED};
}
- my @a = split /;/, $myconfig->{acs};
- my $excl = ();
+ foreach $key (reverse @{ $self->{ORDER} }) {
+ my $entry = $self->{$key};
+
+ if ($entry->{IS_MENU}) {
+ $entry->{VISIBLE} &&= $entry->{NUM_VISIBLE_CHILDREN} > 0;
+ }
- # remove --AR, --AP from array
- grep { ($a, $b) = split /--/; s/--$a$//; } @a;
+ next if (($key !~ m/--/) || !$entry->{VISIBLE});
- map { $excl{$_} = 1 } @a;
+ my $parent = $key;
+ substr($parent, rindex($parent, '--')) = '';
+ $self->{$parent}->{NUM_VISIBLE_CHILDREN}++;
+ }
- @a = ();
- map { push @a, $_ unless $excl{$_} } (@menu);
+# $self->dump_visible();
- $main::lxdebug->leave_sub();
+ $self->{ORDER} = [ grep { $self->{$_}->{VISIBLE} } @{ $self->{ORDER} } ];
+
+ { 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 };
+ }
+}
- return @a;
+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");
+ }
}
1;