run_list: new argument syntax: [<options>] [<filter>] [<attributes>]

From 232fbd24ff43c9c0d0691cf0e1b51a82ef099489 Mon Sep 17 00:00:00 2001
Make run_list work with a properly defined argument syntax:
- start with (optional) options: -R -l
- continue with filter ['(objectclass=*)' as fallback if none given]
- end with attributes (also optional)

Add method is_valid_filter() to check whether a strig is a legal LDAP filter.

FossilOrigin-Name: ec19e834a29e23820ee6088da8ff4d80edb14eb3668050edcbb9c32f8ed5297c
This commit is contained in:
peter@adpm.de 2011-03-05 21:13:27 +00:00
parent 8d0841dfae
commit 02414e8982

View file

@ -775,6 +775,18 @@ sub make_filter
return $filter;
}
# check whether a given string may be a filter
# Synopsis: $yesNo = $self->is_valid_filter($string);
sub is_valid_filter
{
my $self = shift;
my $filter = shift or return;
my $filterObject = Net::LDAP::Filter->new($filter);
return $filterObject ? 1 : 0
}
# little. yellow. different. better.
#
sub debug
@ -1405,34 +1417,49 @@ sub run_help
sub run_list
{
my $self = shift;
my @filters = @_;
my @args = @_;
my $base = $self->base();
my $attrs = [ 'hasSubordinates' ];
my @attrs = ();
my $flags = '';
my $filter = '(objectclass=*)';
# setup filters
my ( $flags, $filter );
if ( scalar @filters ) {
# support '-l' or '-R' listings
if ( $filters[0] =~ /\-[lR]|verbose/ ) {
$flags = shift @filters;
# parse arguments: [ <option> ...] [<filter> ...] [<attribute> ...]
if (@args) {
# options: support '-l' or '-R' listings
if ( $args[0] =~ /^\-([lR])/o ) {
$flags .= $1;
shift(@args);
}
my @filters;
# get filter elements from argument list
while (@args && $self->is_valid_filter($args[0])) {
push(@filters, shift(@args));
}
push(@filters, '(objectclass=*)') if (!@filters);
# construct OR'ed filter from filter elements
$filter = $self->make_filter( \@filters );
# remaining arguments must be attributes
push(@attrs, @args);
}
# flag booleans
my ( $recurse, $long );
if ( $flags ) {
$recurse = $flags =~ /R/;
$long = $flags =~ /l/;
$attrs = [ '*', 'hasSubordinates' ] if $long;
$recurse = $flags =~ /R/o;
$long = $flags =~ /l/o;
push(@attrs, '*') if ($long && !@attrs);
}
my $s = $self->search({
scope => $recurse ? 'sub' : 'one',
vals => 1,
filter => $filter,
attrs => $attrs
attrs => [ @attrs, 'hasSubordinates' ]
});
if ( $s->{'code'} ) {
print "$s->{'message'}\n";
@ -1456,24 +1483,30 @@ sub run_list
# iterate and print
#
my $dn_count = 0;
my $dn;
foreach my $e ( sort { $a->dn() cmp $b->dn() } @{ $s->{'entries'} } ) {
$dn = $e->dn();
my $dn = $e->dn();
# only show RDN unless -l was given
$dn = canonical_dn([shift(@{ldap_explode_dn($dn, casefold => 'none')})],
casefold => 'none')
unless ($long);
# if this entry is a container for other entries, append a
# trailing slash.
if ( $e->get_value('hasSubordinates') eq 'TRUE' ) {
$dn .= '/';
$dn .= '/' if ($e->get_value('hasSubordinates') eq 'TRUE');
# additional arguments given; show their values
if (@args) {
my @elements = ( $dn );
foreach my $attr (@args) {
my @vals = $e->get_value($attr);
push(@elements, join(',', @vals));
}
my $rdn = $dn;
$rdn =~ s/,$base//i;
unless ( $long ) {
$dn = $rdn;
next;
print join("\t", @elements)."\n";
}
else {
# show descriptions
my $desc = $e->get_value('description');
if ( $desc ) {
@ -1498,9 +1531,8 @@ sub run_list
next;
}
}
}
continue {
print "$dn\n";
}
$dn_count++;
}