From 78b2353815ed98472688c2f17b31d17bb2450ff4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bernd=20Ble=C3=9Fmann?= Date: Fri, 3 Aug 2018 14:23:09 +0200 Subject: [PATCH] =?utf8?q?SL::Auth:=20evaluate=5Frights=5Fary:=20Negierung?= =?utf8?q?=20(!)=20erm=C3=B6glichen?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Auth.pm | 21 +++++++++++++++++---- t/auth/evaluate_rights_ary.t | 29 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 t/auth/evaluate_rights_ary.t diff --git a/SL/Auth.pm b/SL/Auth.pm index 04145f2cf..82be3b8d8 100644 --- a/SL/Auth.pm +++ b/SL/Auth.pm @@ -1077,23 +1077,36 @@ sub evaluate_rights_ary { my $value = 0; my $action = '|'; + my $negate = 0; foreach my $el (@{$ary}) { if (ref $el eq "ARRAY") { + my $val = evaluate_rights_ary($el); + $val = !$val if $negate; + $negate = 0; if ($action eq '|') { - $value |= evaluate_rights_ary($el); + $value |= $val; } else { - $value &= evaluate_rights_ary($el); + $value &= $val; } } elsif (($el eq '&') || ($el eq '|')) { $action = $el; + } elsif ($el eq '!') { + $negate = !$negate; + } elsif ($action eq '|') { - $value |= $el; + my $val = $el; + $val = !$val if $negate; + $negate = 0; + $value |= $val; } else { - $value &= $el; + my $val = $el; + $val = !$val if $negate; + $negate = 0; + $value &= $val; } } diff --git a/t/auth/evaluate_rights_ary.t b/t/auth/evaluate_rights_ary.t new file mode 100644 index 000000000..7203d0a76 --- /dev/null +++ b/t/auth/evaluate_rights_ary.t @@ -0,0 +1,29 @@ +use strict; + +use Test::More; + +use lib 't'; +use Support::TestSetup; + +Support::TestSetup::login(); + +use_ok 'SL::Auth'; + +ok( SL::Auth::evaluate_rights_ary(['1']), 'simple: right'); +ok(!SL::Auth::evaluate_rights_ary(['0']), 'simple: no right'); +ok( SL::Auth::evaluate_rights_ary(['1', '|', 0]), 'simple: or'); +ok( SL::Auth::evaluate_rights_ary(['0', '|', '1']), 'simple: or 2'); +ok(!SL::Auth::evaluate_rights_ary(['1', '&', '0']), 'simple: and'); +ok(!SL::Auth::evaluate_rights_ary(['0', '&', '1']), 'simple: and 2'); +ok( SL::Auth::evaluate_rights_ary(['1', '&', '1']), 'simple: and 3'); +ok(!SL::Auth::evaluate_rights_ary(['!', '1']), 'simple: not'); +ok( SL::Auth::evaluate_rights_ary(['!', '0']), 'simple: not 2'); +ok(!SL::Auth::evaluate_rights_ary(['!', '!', '0']), 'simple: double not'); +ok( SL::Auth::evaluate_rights_ary(['!', ['0']]), 'not 1'); +ok(!SL::Auth::evaluate_rights_ary(['!', ['1']]), 'not 2'); +ok( SL::Auth::evaluate_rights_ary(['!', '!', ['1']]), 'double not'); +ok( SL::Auth::evaluate_rights_ary([ '!', ['!', ['1', '&', '1'], '&', '!', '!', ['1', '|', '!', '1']] ]), 'something more coplex'); + +done_testing; + +1; -- 2.20.1