Last active
August 29, 2015 14:02
-
-
Save caseywatts/ddea3996853050d1e5ad to your computer and use it in GitHub Desktop.
LDAP Example
This file contains hidden or 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 hidden or 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
lol basically the purpose of the YaleLDAP gem is to correct for these terrible titles
https://github.com/YaleSTC/yaleldap