Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I can't test this, but LDAP queries use <em>prefix</em> notation while we're use to using <em>infix</em> notation. Imagine if you want something that's either a <em>dog</em> or a <em>cat</em>. In <em>infix</em> notation, it would look something like this:</p> <pre><code>((ANIMAL = "cat") OR (ANIMAL = "dog")) </code></pre> <p>With <em>prefix</em> notation, the boolean operator goes at the <em>beginning</em> of the query:</p> <pre><code>(OR (ANIMAL = "cat") (ANIMAL = "dog")) </code></pre> <p>The advantage to <em>prefixed</em> notation comes when you do more than two checks per boolean. Here I'm looking for something that's either a <em>cat</em>, a <em>dog</em> or a <em>wombat</em>:</p> <pre><code>(OR (ANIMAL = "cat") (ANIMAL = "dog") (ANIMAL = "wombat")) </code></pre> <p>Notice that I only needed a single boolean operator in the front of my statement. This will <em>OR</em> together all three statements. With our standard <em>infix</em> notation, I would have to have a second <em>OR</em> operator:</p> <pre><code>((ANIMAL = "cat") OR (ANIMAL = "dog") OR (ANIMAL = "wombat")) </code></pre> <blockquote> <p><em>Prefix</em> notation was created by a Polish Mathematician named Jan Lukasiewicz back in 1924 in Warsaw Univeristy and thus became known as <em><a href="http://chc60.fgcu.edu/EN/HistoryDetail.aspx?c=8" rel="noreferrer">Polish Notation</a></em>. Later on, it was discovered that computers could work an equation from front to back if the equation was written in <em>postfix</em> notation which is the reverse of Polish Notation. Thus, <strong><em>Reverse</strong> Polish Notation</em> (or RPN) was born.</p> <p>Early HP calculators used <em>RPN</em> notation which became the Geek Sheik thing back in the early 1970s. Imagine the sense of brain superiority you get when you hand your calculator to someone and they have no early idea how to use it. The only way to be cooler back then was to have a <a href="http://en.wikipedia.org/wiki/Curta_calculator" rel="noreferrer">Curta</a>.</p> </blockquote> <p><em>Okay, enough walking down nostalgia lane. Let's get back to the problem...</em></p> <p>The easiest way to construct an <em>infix</em> operation is to build a tree diagram of what you want. Thus, you should sketch out your LDAP query as a tree:</p> <pre><code> AND / | \ / | \ / | \ / | \ / | \ / | \ / | \ OR employee!=9* mail=* / \ / \ / \ / \ / \ phyDelOfficeName=100 phyDelOfficeName=274 </code></pre> <p>To build a query based upon this tree, start with the bottom of the tree, and work your way up each layer. The bottom part of our tree is the <em>OR</em> part of our query:</p> <pre><code>(OR (physicalDeliveryOfficeName = 100) (physicalDeliveryOfficeName = 274)) </code></pre> <p>Using LDAP's <em>OR</em> operator, the pipe (<code>|</code>) and removing the extra spaces, we get:</p> <pre><code>(|(physicalDeliveryOfficeName = 100)(physicalDeliveryOfficeName = 274)) </code></pre> <p>When I build an LDAP query, I like to save each section as a Perl scalar variable. It makes it a bit easier to use:</p> <pre><code>$or_part = "|(physicalDeliveryOfficeName=100)(physicalDeliveryOfficeName=274)"; </code></pre> <p>Notice I've left off the outer pair or parentheses. The outer set of parentheses return when you string all the queries back together. However, some people put them anyway. An extra set of parentheses doesn't hurt an LDAP query.</p> <p>Now for the other two parts of the query:</p> <pre><code>$mailAddrExists = "mail=*"; $not_emp_starts_9 = "!(employee=9*)"; </code></pre> <p>And, now we <em>AND</em> all three sections together:</p> <pre><code>"(&amp;($mailAddrExists)($not_emp_starts_9)($or_part))" </code></pre> <p>Note that a single ampersand weaves it all together. I can substitute back each section to see the full query:</p> <pre><code>(&amp;(mail=*)(!(employee=9*))(|(physicalDeliveryOfficeName=100)(physicalDeliveryOfficeName=274))) </code></pre> <p>Or like this:</p> <pre><code>my $psearch-$ldap-&gt;search( base =&gt; $my_base, attr =&gt; ["mail","employeeNumber","physicalDeliveryOfficeName"], filter =&gt; "(&amp;(mail=*)(!(employee=9*))(|(physicalDeliveryOfficeName=100)(physicalDeliveryOfficeName=274)))", ); </code></pre> <p>Or piecemeal:</p> <pre><code>my $mail = "mail=*"; my $employee = "!(employee=9*)"; my $physicalAddress = "|(physicalDeliveryOfficeName=100)" . "(physicalDeliveryOfficeName=274)"; my $psearch-$ldap-&gt;search( base =&gt; $my_base, attr =&gt; ["mail","employeeNumber","physicalDeliveryOfficeName"], filter =&gt; "(&amp;($mail)($employee)($physicalAddress))", ); </code></pre> <p>As I said before, I can't test this. I hope it works. If nothing else, I hope you understand how to create an LDAP query and can figure out how to do it yourself.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload