使用perl解析LDAP过滤器

时间:2015-08-18 12:26:00

标签: perl

我正在尝试使用Net :: LDAP :: Server

编写LDAP服务器

请求路由/处理工作正常,但解压缩ldap过滤器会给我带来一些麻烦。

incomming过滤器具有以下值/结构:

$VAR1 = {
          'and' => [
                     {
                       'or' => [
                                 {
                                   'substrings' => {
                                                     'substrings' => [
                                                                       {
                                                                         'initial' => '233'
                                                                       }
                                                                     ],
                                                     'type' => 'sn'
                                                   }
                                 },
                                 {
                                   'substrings' => {
                                                     'substrings' => [
                                                                       {
                                                                         'initial' => '233'
                                                                       }
                                                                     ],
                                                     'type' => 'sn'
                                                   }
                                 },
                                 {
                                   'equalityMatch' => {
                                                        'assertionValue' => '233',
                                                        'attributeDesc' => 'telephoneNumber'
                                                      }
                                 },
                                 {
                                   'equalityMatch' => {
                                                        'assertionValue' => '233',
                                                        'attributeDesc' => 'telephoneNumber'
                                                      }
                                 }
                               ]
                     }
                   ]
        };

这是我解析LDAP过滤器的代码

my $myFilter= $reqData->{'filter'};
print STDERR "Filter : $myFilter \n";
print STDERR Dumper($myFilter) ."\n";
my @andloop= $myFilter->{'and'};
my $and;
foreach $and(@andloop) 
{
    print STDERR "Filter AND: $and \n";
    print STDERR Dumper($and) ."\n";

    my $orValue;
    foreach $orValue ($and) 
    {
        print STDERR "Inside Filter OR: $orValue : $and\n";
        print STDERR "Keys: ";
        print STDERR Dumper($orValue) . "\n";
        print STDERR "KeysOR: ";


        my @or= $orValue;
        print STDERR Dumper(@or[0]->{'or'}) . "\n";
        print STDERR "OR Value[0]: " . Dumper(@or[0]) . "\n";
    }
}

我可以通过AND循环,但似乎没有潜入'或'部分

1 个答案:

答案 0 :(得分:2)

$orValue是一个数组引用。取消引用它以获得真正的数组:

my @or = @$orValue;

您需要对引用更加小心,它们(通常)不会自动取消引用:

print STDERR "Filter : $myFilter \n";
print STDERR Dumper($myFilter), "\n";

for my $and (@{ $myFilter->{and} }) {
    print STDERR "Filter AND: $and \n";
    print STDERR Dumper($and) ."\n";

    for my $or (@{ $and->{or} }) {
        print STDERR "Inside Filter OR: $or : $and\n";
        print STDERR "Keys: ";
        print STDERR Dumper($or), "\n";
    }
}