-#=====================================================================
-# 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;
+package SL::Menu;
-sub new {
- $main::lxdebug->enter_sub();
-
- my ($type, $menufile, $level) = @_;
+use strict;
- use SL::Inifile;
- my $self = Inifile->new($menufile, $level);
+use SL::Auth;
+use File::Spec;
+use SL::MoreCommon qw(uri_encode);
+use SL::YAML;
- $main::lxdebug->leave_sub();
+our %menu_cache;
- bless $self, $type;
-}
+sub new {
+ my ($package, $domain) = @_;
+
+ if (!$menu_cache{$domain}) {
+ my $path = File::Spec->catdir('menus', $domain);
+
+ opendir my $dir, $path or die "can't open $path: $!";
+ my @files = sort grep -f "$path/$_", grep /\.yaml$/, readdir $dir;
+ close $dir;
+
+ my $nodes = [];
+ my $nodes_by_id = {};
+ for my $file (@files) {
+ my $data;
+ eval {
+ $data = SL::YAML::LoadFile(File::Spec->catfile($path, $file));
+ 1;
+ } or do {
+ die "Error while parsing $file: $@";
+ };
+
+ # check if this file is internally consistent.
+ die 'not an array ref' unless $data && 'ARRAY' eq ref $data; # TODO get better diag to user
+
+ # in particular duplicate ids tend to come up as a user error when editing the menu files
+ #my %uniq_ids;
+ #$uniq_ids{$_->{id}}++ && die "Error in $file: duplicate id $_->{id}" for @$data;
+
+ _merge($nodes, $nodes_by_id, $data);
+ }
-sub menuitem {
- $main::lxdebug->enter_sub();
- my ($self, $myconfig, $form, $item) = @_;
+ my $self = bless {
+ nodes => $nodes,
+ by_id => $nodes_by_id,
+ }, $package;
- my $module = $form->{script};
- my $action = "section_menu";
- my $target = "";
+ $self->build_tree;
- if ($self->{$item}{module}) {
- $module = $self->{$item}{module};
- }
- if ($self->{$item}{action}) {
- $action = $self->{$item}{action};
- }
- if ($self->{$item}{target}) {
- $target = $self->{$item}{target};
+ $menu_cache{$domain} = $self;
+ } else {
+ $menu_cache{$domain}->clear_access;
}
- my $level = $form->escape($item);
-
- my $str =
- qq|<a style="vertical-align:top" href=$module?action=$action&level=$level&login=$form->{login}&password=$form->{password}|;
+ $menu_cache{$domain}->set_access;
- my @vars = qw(module action target href);
+ return $menu_cache{$domain};
+}
- if ($self->{$item}{href}) {
- $str = qq|<a href=$self->{$item}{href}|;
- @vars = qw(module target href);
- }
+sub _merge {
+ my ($nodes, $by_id, $data) = @_;
- map { delete $self->{$item}{$_} } @vars;
+ for my $node (@$data) {
+ my $id = $node->{id};
- # 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);
- }
+ die "menu: node with name '$node->{name}' does not have an id" if !$id;
- if ($target) {
- $str .= qq| target=$target|;
- }
+ my $merge_to = $by_id->{$id};
- $str .= ">";
-
- $main::lxdebug->leave_sub();
+ if (!$merge_to) {
+ push @$nodes, $node;
+ $by_id->{$id} = $node;
+ next;
+ }
- return $str;
+ # TODO make this a real recursive merge
+ # TODO add support for arrays
+
+ # merge keys except params
+ for my $key (keys %$node) {
+ if (ref $node->{$key}) {
+ if ('HASH' eq ref $node->{$key}) {
+ $merge_to->{$key} = {} if !exists $merge_to->{$key} || 'HASH' ne ref $merge_to->{$key};
+ for (keys %{ $node->{params} }) {
+ $merge_to->{$key}{$_} = $node->{params}{$_};
+ }
+ } else {
+ die "unsupported structure @{[ ref $node->{$key} ]}";
+ }
+ } else {
+ $merge_to->{$key} = $node->{$key};
+ }
+ }
+ }
}
-sub menuitem_v3 {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form, $item, $other) = @_;
-
- my $module = $form->{script};
- my $action = "section_menu";
- my $target = "";
+sub build_tree {
+ my ($self) = @_;
- if ($self->{$item}{module}) {
- $module = $self->{$item}{module};
+ # first, some sanity check. are all parents valid ids or empty?
+ for my $node ($self->nodes) {
+ next if !exists $node->{parent} || !$node->{parent} || $self->{by_id}->{$node->{id}};
+ die "menu: node $node->{id} has non-existent parent $node->{parent}";
}
- if ($self->{$item}{action}) {
- $action = $self->{$item}{action};
- }
- if ($self->{$item}{target}) {
- $target = $self->{$item}{target};
+
+ my %by_parent;
+ # order them by parent
+ for my $node ($self->nodes) {
+ push @{ $by_parent{ $node->{parent} // '' } //= [] }, $node;
}
- my $level = $form->escape($item);
+ # autovivify order in by_parent, so that numerical sorting for entries without order
+ # preserves their order and position with respect to entries with order.
+ for (values %by_parent) {
+ my $last_order = 0;
+ for my $node (@$_) {
+ if (defined $node->{order} && $node->{order} * 1) {
+ $last_order = $node->{order};
+ } else {
+ $node->{order} = ++$last_order;
+ }
+ }
+ }
- my $str = qq|<a href="$module?action=| . $form->escape($action) .
- qq|&level=| . $form->escape($level);
- map({ $str .= "&${_}=" . $form->escape($form->{$_}); } qw(login password));
+ my $tree = { };
+ $self->{by_id}{''} = $tree;
- my @vars = qw(module action target href);
- if ($self->{$item}{href}) {
- $str = qq|<a href=$self->{$item}{href}|;
- @vars = qw(module target href);
+ for (keys %by_parent) {
+ my $parent = $self->{by_id}{$_};
+ $parent->{children} = [ sort { $a->{order} <=> $b->{order} } @{ $by_parent{$_} } ];
}
- 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);
- }
+ _set_level_rec($tree->{children}, 0);
- $str .= '"';
+ $self->{tree} = $tree->{children};
+}
- if ($target) {
- $str .= qq| target="| . $form->quote($target) . qq|"|;
- }
+sub _set_level_rec {
+ my ($ary_ref, $level) = @_;
- if ($other) {
- foreach my $key (keys(%{$other})) {
- $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
- }
+ for (@$ary_ref) {
+ $_->{level} = $level;
+ _set_level_rec($_->{children}, $level + 1) if $_->{children};
}
+}
- $str .= ">";
+sub nodes {
+ @{ $_[0]{nodes} }
+}
- $main::lxdebug->leave_sub();
+sub tree_walk {
+ my ($self, $all) = @_;
- return $str;
+ _tree_walk_rec($self->{tree}, $all);
}
-sub menuitemNew {
- my ($self, $myconfig, $form, $item) = @_;
-
- my $module = $form->{script};
- my $action = "section_menu";
+sub _tree_walk_rec {
+ my ($ary_ref, $all) = @_;
+ map { $_->{children} ? ($_, _tree_walk_rec($_->{children}, $all)) : ($_) } grep { $all || $_->{visible} } @$ary_ref;
+}
- #if ($self->{$item}{module}) {
- $module = $self->{$item}{module};
+sub parse_access_string {
+ my ($self, $node) = @_;
- #}
- if ($self->{$item}{action}) {
- $action = $self->{$item}{action};
- }
+ my @stack;
+ my $cur_ary = [];
- 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);
+ push @stack, $cur_ary;
- if ($self->{$item}{href}) {
- $str = qq|$self->{$item}{href}|;
- @vars = qw(module target href);
- }
+ my $access = $node->{access};
- map { delete $self->{$item}{$_} } @vars;
+ while ($access =~ m/^([a-z_\/]+|\!|\||\&|\(|\)|\s+)/) {
+ my $token = $1;
+ substr($access, 0, length($1)) = "";
- # 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);
- }
+ next if ($token =~ /\s/);
- $str .= " ";
+ 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) {
+ die "Error while parsing menu entry $node->{id}: missing '('";
+ }
+ $cur_ary = $stack[-1];
-sub access_control {
- $main::lxdebug->enter_sub(2);
+ } elsif (($token eq "|") || ($token eq "&") || ($token eq "!")) {
+ push @{$cur_ary}, $token;
- my ($self, $myconfig, $menulevel) = @_;
+ } else {
+ if ($token =~ m{^ client / (.*) }x) {
+ push @{$cur_ary}, $self->parse_instance_conf_string($1);
+ } else {
+ push @{$cur_ary}, $::auth->check_right($::myconfig{login}, $token, 1);
+ }
+ }
+ }
- my @menu = ();
+ if ($access) {
+ die "Error while parsing menu entry $node->{id}: unrecognized token at the start of '$access'\n";
+ }
- if ($menulevel eq "") {
- @menu = grep { !/--/ } @{ $self->{ORDER} };
- } else {
- @menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
+ if (1 < scalar @stack) {
+ die "Error while parsing menu entry $node->{id}: Missing ')'\n";
}
- my @a = split(/;/, $myconfig->{acs});
- my $excl = ();
+ return SL::Auth::evaluate_rights_ary($stack[0]);
+}
- # remove --AR, --AP from array
- grep { ($a, $b) = split(/--/); s/--$a$//; } @a;
+sub href_for_node {
+ my ($self, $node) = @_;
- map { $excl{$_} = 1 } @a;
+ return undef if !$node->{href} && !$node->{module} && !$node->{params};
- @a = ();
- map { push @a, $_ unless $excl{$_} } (@menu);
+ return $node->{href_for_node} ||= do {
+ my $href = $node->{href} || $node->{module} || 'controller.pl';
+ my @tokens;
- $main::lxdebug->leave_sub(2);
+ while (my ($key, $value) = each %{ $node->{params} }) {
+ push @tokens, uri_encode($key, 1) . "=" . uri_encode($value, 1);
+ }
- return @a;
+ join '?', $href, grep $_, join '&', @tokens;
+ }
}
-sub generate_acl {
- my ($self, $menulevel, $hash) = @_;
+sub name_for_node {
+ $::locale->text($_[1]{name})
+}
- my @items = $self->access_control(\%main::myconfig, $menulevel);
+sub parse_instance_conf_string {
+ my ($self, $setting) = @_;
+ return $::instance_conf->data->{$setting};
+}
- $menulevel =~ s/[^A-Za-z_\/\.\+\-]/_/g;
- $hash->{"access_" . lc($menulevel)} = 1 if ($menulevel);
+sub clear_access {
+ my ($self) = @_;
+ for my $node ($self->tree_walk("all")) {
+ delete $node->{visible};
+ delete $node->{visible_children};
+ }
+}
- foreach my $item (@items) {
- $self->generate_acl($item, $hash); #unless ($menulevel);
+sub set_access {
+ my ($self) = @_;
+ # 1. evaluate access for all
+ # 2. if a menu has no visible children, its not visible either
+
+ for my $node (reverse $self->tree_walk("all")) {
+ $node->{visible} = $node->{access} ? $self->parse_access_string($node)
+ : !$node->{children} ? 1
+ : $node->{visible_children} ? 1
+ : 0;
+ if ($node->{visible} && $node->{parent}) {
+ $self->{by_id}{ $node->{parent} }{visible_children} = 1;
+ }
}
}
1;
-