Skip to content

Instantly share code, notes, and snippets.

@caseywatts
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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))
@caseywatts
Copy link
Author

I've decided I'd rather have all of these as constants, and they probably belong somewhere in the config folder (or environment! (?))

  LDAP_HOST = 'directory.yale.edu'
  LDAP_PORT = 389
  LDAP_BASE = 'ou=People,o=yale.edu'
  LDAP_ATTRS = %w(uid givenname sn mail collegename college class UPI)

I'm having trouble deciding a good way to represent the LDAP_ATTRS in a way that isn't Yale-specific so this code is more usable (especially when we use it in actual applications). Maybe just a simple way to map first_name and other variable names that make sense to the Yale attribute (givenname) that doesn't always make sense.

@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