Skip to content

Instantly share code, notes, and snippets.

@caseywatts caseywatts/ldapexample.rb
Last active Aug 29, 2015

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

caseywatts commented Sep 16, 2014

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

This comment has been minimized.

Copy link

orenyk commented Sep 16, 2014

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

@caseywatts

This comment has been minimized.

Copy link
Owner Author

caseywatts commented Sep 27, 2014

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
You can’t perform that action at this time.