Skip to content

Instantly share code, notes, and snippets.

@caseywatts
Last active August 29, 2015 14:02
Show Gist options
  • Save caseywatts/ddea3996853050d1e5ad to your computer and use it in GitHub Desktop.
Save caseywatts/ddea3996853050d1e5ad to your computer and use it in GitHub Desktop.
LDAP Example
#Yale LDAP Example
require 'csv'
require 'net-ldap'
# Hashrocket Format
{:host => 'directory.yale.edu', :port => 389}
# "New" Format, requires keys be symbols
{host: 'directory.yale.edu', port: 389}
:symbolinmostofruby
{symbolonlywhenbeingusedinthenewhashformat: "value"}
# You can get away with omitting a lot in Ruby
# hash brackets are optional when being passed to a function
# functions don't need parenthesis (except when being defined)
# Some people do, you should be aware
ldap = Net::LDAP.new({:host => 'directory.yale.edu', :port => 389})
ldap = Net::LDAP.new({host: 'directory.yale.edu', port: 389})
ldap = Net::LDAP.new(host: 'directory.yale.edu', port: 389)
ldap = Net::LDAP.new host: 'directory.yale.edu', port: 389
# I AM SEARCHING FOR PEOPLE AT YALE
base = 'ou=People,o=yale.edu'
# I only want these attributes
LDAP_ATTRS = %w(uid givenname sn mail collegename college class UPI)
#Some things I might filter by
netid = "csw3"
firstname = "Casey"
lastname = "Watts"
#Creating some basic filters
netidfilter = Net::LDAP::Filter.eq('uid', netid)
firstnamefilter = Net::LDAP::Filter.eq('givenname', firstname)
lastnamefilter = Net::LDAP::Filter.eq('sn', lastname)
# Combines two filters with join
bigfilter = Net::LDAP::Filter.join(firstnamefilter, lastnamefilter)
# Do the search!
result = ldap.search(base: base,
filter: bigfilter)
# Return only the attributes specified in LDAP_ATTRS
result = ldap.search(base: base,
filter: lastnamefilter,
attributes: LDAP_ATTRS)
require 'csv'
require 'net-ldap'
ldap = Net::LDAP.new host: 'directory.yale.edu', port: 389
base = 'ou=People,o=yale.edu'
netid = "csw3"
result = ldap.search(base: base, filter: Net::LDAP::Filter.eq('uid', netid))
@orenyk
Copy link

orenyk commented Sep 16, 2014

Is first name actually stored as givenname and last name stored as sn (presumably 'surname')? Wow...

@caseywatts
Copy link
Author

lol basically the purpose of the YaleLDAP gem is to correct for these terrible titles
https://github.com/YaleSTC/yaleldap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment