Projekt

Allgemein

Profil

Herunterladen (5,82 KB) Statistiken
| Zweig: | Markierung: | Revision:
d319704a Moritz Bunkus
#=====================================================================
# LX-Office ERP
# Copyright (C) 2004
# Based on SQL-Ledger Version 2.1.9
# Web http://www.lx-office.org
#
#=====================================================================
# SQL-Ledger Accounting
# Copyright (C) 2001
#
# Author: Dieter Simader
# Email: dsimader@sql-ledger.org
# Web: http://www.sql-ledger.org
#
# Contributors:
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#=====================================================================
#
# routines for menu items
#
#=====================================================================

package Menu;

8c7e4493 Moritz Bunkus
use SL::Auth;
db53dc8a Moritz Bunkus
use SL::Inifile;

c621a918 Sven Schöling
use strict;

d319704a Moritz Bunkus
sub new {
$main::lxdebug->enter_sub();

63a8dae2 Moritz Bunkus
my ($type, @menufiles) = @_;
my $self = bless {}, $type;
db53dc8a Moritz Bunkus
63a8dae2 Moritz Bunkus
my @order;
d319704a Moritz Bunkus
63a8dae2 Moritz Bunkus
foreach my $menufile (grep { -f } @menufiles) {
my $inifile = Inifile->new($menufile);
d319704a Moritz Bunkus
63a8dae2 Moritz Bunkus
push @order, @{ delete($inifile->{ORDER}) || [] };
$self->{$_} = $inifile->{$_} for keys %{ $inifile };
}

$self->{ORDER} = \@order;
8c7e4493 Moritz Bunkus
$self->set_access();

d319704a Moritz Bunkus
$main::lxdebug->leave_sub();

075f1eab Sven Schöling
return $self;
d319704a Moritz Bunkus
}

32fa785e Moritz Bunkus
sub menuitem_new {
eb3549d7 Moritz Bunkus
$main::lxdebug->enter_sub(LXDebug::DEBUG2());
32fa785e Moritz Bunkus
my ($self, $name, $item) = @_;

c621a918 Sven Schöling
my $form = $main::form;
my $myconfig = \%main::myconfig;
32fa785e Moritz Bunkus
my $module = $self->{$name}->{module} || $form->{script};
my $action = $self->{$name}->{action};

$item->{target} = $self->{$name}->{target} || "main_window";
$item->{href} = $self->{$name}->{href} || "${module}?action=" . $form->escape($action);

my @vars = qw(module target href);
7214b604 Moritz Bunkus
push @vars, 'action' unless ($self->{$name}->{href});
32fa785e Moritz Bunkus
map { delete $self->{$name}{$_} } @vars;

# 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);
}

eb3549d7 Moritz Bunkus
$main::lxdebug->leave_sub(LXDebug::DEBUG2());
32fa785e Moritz Bunkus
}

d319704a Moritz Bunkus
sub access_control {
8c6efb2a Moritz Bunkus
$main::lxdebug->enter_sub(2);
d319704a Moritz Bunkus
my ($self, $myconfig, $menulevel) = @_;

my @menu = ();

7288950e Sven Schöling
if (!$menulevel) {
d319704a Moritz Bunkus
@menu = grep { !/--/ } @{ $self->{ORDER} };
} else {
@menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
}

8c6efb2a Moritz Bunkus
$main::lxdebug->leave_sub(2);
d319704a Moritz Bunkus
0dd879bc Moritz Bunkus
return @menu;
ff7976ff Moritz Bunkus
}

8c7e4493 Moritz Bunkus
sub parse_access_string {
my $self = shift;
my $key = shift;
my $access = shift;

c621a918 Sven Schöling
my $form = $main::form;
my $auth = $main::auth;
my $myconfig = \%main::myconfig;

8c7e4493 Moritz Bunkus
my @stack;
my $cur_ary = [];

push @stack, $cur_ary;

while ($access =~ m/^([a-z_]+|\||\&|\(|\)|\s+)/) {
my $token = $1;
substr($access, 0, length($1)) = "";

next if ($token =~ /\s/);

if ($token eq "(") {
my $new_cur_ary = [];
push @stack, $new_cur_ary;
push @{$cur_ary}, $new_cur_ary;
$cur_ary = $new_cur_ary;

} elsif ($token eq ")") {
pop @stack;
if (!@stack) {
c621a918 Sven Schöling
$form->error("Error in menu.ini for entry ${key}: missing '('");
8c7e4493 Moritz Bunkus
}
$cur_ary = $stack[-1];

} elsif (($token eq "|") || ($token eq "&")) {
push @{$cur_ary}, $token;

} else {
4bd1e2f8 Sven Schöling
push @{$cur_ary}, $auth->check_right($::myconfig{login}, $token, 1);
8c7e4493 Moritz Bunkus
}
}

if ($access) {
c621a918 Sven Schöling
$form->error("Error in menu.ini for entry ${key}: unrecognized token at the start of '$access'\n");
8c7e4493 Moritz Bunkus
}

if (1 < scalar @stack) {
c621a918 Sven Schöling
$main::form->error("Error in menu.ini for entry ${key}: Missing ')'\n");
8c7e4493 Moritz Bunkus
}

return SL::Auth::evaluate_rights_ary($stack[0]);
}

f775b88a Moritz Bunkus
sub parse_instance_conf_string {
my ($self, $setting) = @_;
return $::instance_conf->data->{$setting};
}

8c7e4493 Moritz Bunkus
sub set_access {
my $self = shift;

my $key;

foreach $key (@{ $self->{ORDER} }) {
my $entry = $self->{$key};

$entry->{GRANTED} = $entry->{ACCESS} ? $self->parse_access_string($key, $entry->{ACCESS}) : 1;
f775b88a Moritz Bunkus
$entry->{GRANTED} &&= $self->parse_instance_conf_string($entry->{INSTANCE_CONF}) if $entry->{INSTANCE_CONF};
8c7e4493 Moritz Bunkus
$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};
}

foreach $key (reverse @{ $self->{ORDER} }) {
my $entry = $self->{$key};

if ($entry->{IS_MENU}) {
$entry->{VISIBLE} &&= $entry->{NUM_VISIBLE_CHILDREN} > 0;
}

next if (($key !~ m/--/) || !$entry->{VISIBLE});

my $parent = $key;
substr($parent, rindex($parent, '--')) = '';
$self->{$parent}->{NUM_VISIBLE_CHILDREN}++;
}

# $self->dump_visible();

$self->{ORDER} = [ grep { $self->{$_}->{VISIBLE} } @{ $self->{ORDER} } ];

ed944ca3 Sven Schöling
{ no strict 'refs';
# ToDO: fix this. nuke and pave algorithm without type checking screams for problems.
8c7e4493 Moritz Bunkus
map { delete @{$self->{$_}}{qw(GRANTED IS_MENU NUM_VISIBLE_CHILDREN VISIBLE ACCESS)} if ($_ ne 'ORDER') } keys %{ $self };
ed944ca3 Sven Schöling
}
8c7e4493 Moritz Bunkus
}

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");
}
}

d319704a Moritz Bunkus
1;