Guest User

Untitled

a guest
Feb 20th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. require 'net/ldap'
  2.  
  3. # how to "bind" to your ldap/ad server...
  4. LDAP_HOST = 'server'
  5. LDAP_PORT = 389
  6. LDAP_USERNAME = 'cn=Username;cn=Users;dc=domain;dc=com'
  7. LDAP_PASSWORD = 'your user password'
  8. LDAP_BASE = 'dc=domain;dc=com'
  9.  
  10. # replace "domain" and "com" above with your AD domain
  11.  
  12. class User < ActiveRecord::Base
  13. def member_of?(group)
  14. @@ldap ||= Net::LDAP.new(
  15. :host => LDAP_HOST,
  16. :port => LDAP_PORT,
  17. :auth => {:method => :simple, :username => LDAP_USERNAME, :password => LDAP_PASSWORD}
  18. )
  19. @@ldap.search(
  20. :base => LDAP_BASE,
  21. :filter => Net::LDAP::Filter.eq('sAMAccountName', self.username),
  22. :attributes => %w(memberOf)
  23. ).first.memberOf.include? "CN=#{group},OU=Groups,DC=domain,DC=com"
  24. end
  25. end
  26.  
  27. # this code allows us to test that a user is in a group
  28. @user = User.find(1)
  29. @user.member_of?('staff')
Add Comment
Please, Sign In to add comment