Guest User

Untitled

a guest
Feb 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. ## in environment.rb
  2. # For ActiveLDAP control.
  3. if ENV['rrvcldap']
  4. require RAILS_ROOT + "/lib/rrvcldap.rb"
  5. require "password"
  6. ldapuser = ENV['ldapuser'] ? ENV['ldapuser'] : ENV['USER']
  7. ldappass = Proc.new { Password.get("Ldap Password for #{ldapuser}: ") }
  8. ActiveLDAP::Base.connect(
  9. :host => 'ldap.example.com',
  10. :port => 636,
  11. :method => :ssl,
  12. :base => 'dc=rapidreporting,dc=com',
  13. :bind_format => "uid=#{ldapuser},ou=People,dc=rapidreporting,dc=com",
  14. :user => ldapuser,
  15. :password_block => ldappass,
  16. :allow_anonymous => false,
  17. :try_sasl => false
  18. )
  19. end
  20.  
  21. ## in lib/rrvcldap.rb
  22. require "rubygems"
  23. require_gem "ruby-activeldap"
  24.  
  25. class LdapGroup < ActiveLDAP::Base
  26. ldap_mapping :prefix => 'ou=Group', :classes => ['top', 'posixGroup']
  27. belongs_to :primary_members, :class_name => 'LdapUser', :foreign_key => 'gidNumber',:local_key => 'gidNumber'
  28. has_many :members, :class_name => 'LdapUser', :local_key => 'memberUid'
  29. end
  30.  
  31. require "base64"
  32. require "md5"
  33. require "digest/sha1"
  34. class LdapUser < ActiveLDAP::Base
  35. ldap_mapping :dnattr => 'uid', :prefix => 'ou=People', :classes => ['top', 'posixAccount']
  36. belongs_to :groups, :class_name => 'LdapGroup', :foreign_key => 'memberUid'
  37. def change_password(newpass)
  38. salt = [Array.new(6){rand(256).chr}.join].pack("m")[0..7]; # 2^48 combos
  39. self.userPassword = "{SSHA}" + Base64.encode64(Digest::SHA1.new(newpass.chomp + salt).digest + salt).chomp
  40. end
  41. end
  42.  
  43. ## User model
  44. require RAILS_ROOT + "/lib/rrvcldap.rb"
  45. require_gem "ruby-activeldap"
  46. require "common_extensions" unless Object.const_defined?("RRVCErrors")
  47.  
  48. # this model expects a certain database layout and its based on the name/login pattern.
  49. class RRVCUser
  50. include RRVCErrors
  51.  
  52. def self.authenticate(login, pass)
  53. # turn User into Dc::User
  54. case self.to_s
  55. when "RRVCUser"
  56. ActiveLDAP::Base.connect(
  57. :host => 'darkone.rapidreporting.com',
  58. :port => 636,
  59. :base => 'dc=rapidreporting,dc=com',
  60. :bind_format => "uid=#{login},ou=People,dc=rapidreporting,dc=com",
  61. :user => login,
  62. :password_block => Proc.new { pass },
  63. :allow_anonymous => false,
  64. :try_sasl => false
  65. )
  66. LdapUser.new(login)
  67. end
  68. rescue ActiveLDAP::AuthenticationError
  69. false
  70. end
  71. end
Add Comment
Please, Sign In to add comment