File Coverage

blib/lib/Net/LDAP/Control/MatchedValues.pm
Criterion Covered Total %
statement 12 60 20.0
branch 0 24 0.0
condition 0 8 0.0
subroutine 4 10 40.0
pod 3 3 100.0
total 19 105 18.1


line stmt bran cond sub pod time code
1             # Copyright (c) 2011 Peter Marschall . All rights reserved.
2             # This program is free software; you can redistribute it and/or
3             # modify it under the same terms as Perl itself.
4              
5             package Net::LDAP::Control::MatchedValues;
6              
7 1     1   1036 use Net::LDAP::Control;
  1         3  
  1         56  
8              
9             our @ISA = qw(Net::LDAP::Control);
10             our $VERSION = '0.02';
11              
12 1     1   8 use Net::LDAP::ASN qw(ValuesReturnFilter);
  1         3  
  1         6  
13 1     1   7 use strict;
  1         2  
  1         298  
14              
15             sub init {
16 0     0 1   my($self) = @_;
17              
18 0           delete $self->{asn};
19              
20 0 0         unless (exists $self->{value}) {
21 0   0       $self->{asn} = $self->{matchedValues} || '';
22             }
23              
24 0           $self;
25             }
26              
27             sub matchedValues {
28 0     0 1   my $self = shift;
29              
30 0 0         if (@_) {
    0          
31 0           delete $self->{value};
32 0           return $self->{asn} = shift;
33             }
34             elsif (exists $self->{value}) {
35 0           my $f = $ValuesReturnFilter->decode($self->{value});
36 0 0 0       $self->{asn} ||= Net::LDAP::FilterList::as_string($f)
37             if (ref $f);
38             }
39              
40 0           $self->{asn};
41             }
42              
43             sub value {
44 0     0 1   my $self = shift;
45              
46 0 0         unless (exists $self->{value}) {
47 0           my $f = Net::LDAP::FilterList->new;
48             $self->{value} = $ValuesReturnFilter->encode($f)
49 0 0         if ($f->parse($self->{asn}));
50             }
51              
52 0           $self->{value};
53             }
54              
55             1;
56              
57             =head1 NAME
58              
59             Net::LDAP::Control::MatchedValues - LDAPv3 MatchedValues Control
60              
61             =head1 SYNOPSIS
62              
63             use Net::LDAP;
64             use Net::LDAP::Control::MatchedValues;
65              
66             $ldap = Net::LDAP->new( "ldap.mydomain.eg" );
67              
68             $mv = Net::LDAP::Control::MatchedValues->new( matchedValues => '((sn=Jensen)(sn=Miller))' );
69              
70             # return the entries of all people with first name "Babs",
71             # but only show the sn if it is "Jensen" or "Miller"
72             my $mesg = $ldap->search( base => "o=University of Michigan, c=US",
73             filter => "(givenName=Babs)",
74             attrs => [ qw/sn/ ],
75             control => $mv );
76              
77             =head1 DESCRIPTION
78              
79             C provides an interface for the creation and
80             manipulation of objects that represent the C as described
81             by RFC 3876.
82              
83             The C, which only has a meaning with the C operation,
84             allows the client to specify criteria that restrict the values of attributes returned.
85             It has no effect on the number of objects found, but only allows one to restrict the
86             values of the attributes returned by the search to those matching the criteria.
87              
88              
89             =head1 CONSTRUCTOR ARGUMENTS
90              
91             In addition to the constructor arguments described in
92             L the following are provided.
93              
94             =over 4
95              
96             =item matchedValues => VALUESRETURNFILTER
97              
98             A filter giving the criteria which attribute values shall be returned.
99              
100             VALUESRETURNFILTER is a sequence of simple filter items of the form
101             C<< ( ) >> surrounded by an additional set of parentheses;
102             e.g.
103              
104             =over 4
105              
106             =item ((personsAge<=29))
107              
108             Only return the age if is less than 30 ;-)
109              
110             =item ((cn=*Emergency*)(telephoneNumber=+1*)(telephoneNumber=911))
111              
112             Only return those values of the cn that contain C,
113             and phone numbers from North America including the one for emergency calls.
114              
115             =back
116              
117             =back
118              
119              
120             =head1 METHODS
121              
122             As with L each constructor argument
123             described above is also available as a method on the object which will
124             return the current value for the attribute if called without an argument,
125             and set a new value for the attribute if called with an argument.
126              
127              
128             =head1 SEE ALSO
129              
130             L,
131             L,
132             http://www.ietf.org/rfc/rfc3876.txt
133              
134             =head1 AUTHOR
135              
136             Peter Marschall Epeter@adpm.deE
137              
138             Please report any bugs, or post any suggestions, to the perl-ldap mailing list
139             Eperl-ldap@perl.orgE
140              
141             =head1 COPYRIGHT
142              
143             Copyright (c) 2011 Peter Marschall. All rights reserved. This program is
144             free software; you can redistribute it and/or modify it under the same
145             terms as Perl itself.
146              
147             =cut
148              
149              
150             package Net::LDAP::FilterList;
151              
152 1     1   513 use Net::LDAP::Filter;
  1         2  
  1         335  
153              
154             our @ISA = qw(Net::LDAP::Filter);
155             our $VERSION = '0.03';
156              
157             # filter = "(" 1*item ")"
158             # item = simple / present / substring / extensible
159             # simple = attr filtertype value
160             # filtertype = equal / approx / greater / less
161             # equal = "="
162             # approx = "~="
163             # greater = ">="
164             # less = "<="
165             # extensible = attr [":" matchingrule] ":=" value
166             # / ":" matchingrule ":=" value
167             # present = attr "=*"
168             # substring = attr "=" [initial] any [final]
169             # initial = value
170             # any = "*" *(value "*")
171             # final = value
172             # attr = AttributeDescription from Section 4.1.5 of [1]
173             # matchingrule = MatchingRuleId from Section 4.1.9 of [1]
174             # value = AttributeValue from Section 4.1.6 of [1]
175             #
176             # Special Character encodings
177             # ---------------------------
178             # * \2a, \*
179             # ( \28, \(
180             # ) \29, \)
181             # \ \5c, \\
182             # NUL \00
183              
184              
185             sub new {
186 0     0     my $self = shift;
187 0   0       my $class = ref($self) || $self;
188              
189 0           my $me = bless [], $class;
190              
191 0 0         if (@_) {
192 0 0         $me->parse(shift) or
193             return undef;
194             }
195 0           $me;
196             }
197              
198             my $Attr = '[-;.:\d\w]*[-;\d\w]';
199              
200             my %Op = qw(
201             = equalityMatch
202             ~= approxMatch
203             >= greaterOrEqual
204             <= lessOrEqual
205             := extensibleMatch
206             );
207              
208             my $ErrStr;
209              
210             sub parse {
211 0     0     my $self = shift;
212 0           my $filterlist = shift;
213              
214 0           my @parsed = ();
215              
216 0           undef $ErrStr;
217              
218             # a filterlist is required
219 0 0         if (!defined $filterlist) {
220 0           $ErrStr = "Undefined filterlist";
221 0           return undef;
222             }
223              
224              
225             # remove surrounding braces ((..)(..)(..)) -> (..)(..)(..)
226 0           $filterlist =~s/^\((\(.*)\)$/$1/;
227              
228 0           while (length($filterlist)) {
229              
230             # process (attr op string)
231 0 0         if ($filterlist =~ s/^\(\s*
232             ($Attr)\s*
233             ([:~<>]?=)
234             ((?:\\.|[^\\()]+)*)
235             \)\s*
236             //xo) {
237 0           my $item = Net::LDAP::Filter::_encode($1, $2, $3);
238 0 0         return undef if (!$item);
239 0           push(@parsed, $item);
240 0           next;
241             }
242              
243             # If we get here then there is an error in the filter string
244             # so exit loop with data in $filterlist
245 0           last;
246             }
247              
248 0 0         if (length $filterlist) {
249             # If we have anything left in the filter, then there is a problem
250 0           $ErrStr = "Bad filterlist, error before " . substr($filterlist, 0, 20);
251 0           return undef;
252             }
253              
254 0           @$self = @parsed;
255              
256 0           $self;
257             }
258              
259             sub as_string {
260 0     0     my $l = shift;
261              
262 0           return '(' . join('', map { Net::LDAP::Filter::_string(%{$_}) } @{$l}) . ')';
  0            
  0            
  0            
263             }
264              
265             1;