Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is sample code I use with the <a href="http://net-ldap.rubyforge.org/">net-ldap</a> gem to verify user logins from the ActiveDirectory server at my work:</p> <pre><code>require 'net/ldap' # gem install net-ldap def name_for_login( email, password ) email = email[/\A\w+/].downcase # Throw out the domain, if it was there email &lt;&lt; "@mycompany.com" # I only check people in my company ldap = Net::LDAP.new( host: 'ldap.mycompany.com', # Thankfully this is a standard name auth: { method: :simple, email: email, password:password } ) if ldap.bind # Yay, the login credentials were valid! # Get the user's full name and return it ldap.search( base: "OU=Users,OU=Accounts,DC=mycompany,DC=com", filter: Net::LDAP::Filter.eq( "mail", email ), attributes: %w[ displayName ], return_result:true ).first.displayName.first end end </code></pre> <p>The <code>first.displayName.first</code> code at the end looks a little goofy, and so might benefit from some explanation:</p> <ul> <li><p><a href="http://net-ldap.rubyforge.org/Net/LDAP.html#method-i-search"><code>Net::LDAP#search</code></a> always returns an array of results, even if you end up matching only one entry. The first call to <code>first</code> finds the first (and presumably only) entry that matched the email address.</p></li> <li><p>The <a href="http://net-ldap.rubyforge.org/Net/LDAP/Entry.html"><code>Net::LDAP::Entry</code></a> returned by the search conveniently lets you access attributes via method name, so <code>some_entry.displayName</code> is the same as <code>some_entry['displayName']</code>.</p></li> <li><p>Every attribute in a <code>Net::LDAP::Entry</code> is always an array of values, even when only one value is present. Although it might be silly to have a user with multiple "displayName" values, LDAP's generic nature means that it's possible. The final <code>first</code> invocation turns the array-of-one-string into just the string for the user's full name.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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