Last active
August 29, 2015 14:02
-
-
Save caseywatts/ddea3996853050d1e5ad to your computer and use it in GitHub Desktop.
LDAP Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Is first name actually stored as givenname
and last name stored as sn
(presumably 'surname')? Wow...
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
I've decided I'd rather have all of these as constants, and they probably belong somewhere in the
config
folder (or environment! (?))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.