Guest User

Untitled

a guest
Apr 14th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. ## environment.rb
  2.  
  3. require 'vpim/vcard'
  4. require 'net/ldap'
  5.  
  6. ## Controllers
  7.  
  8. class SessionController < ApplicationController
  9. skip_before_filter :authorize, :only => ["new", "create"]
  10.  
  11. def new
  12. # If it's not a POST action, just show the form
  13. if request.post?
  14. ldap = Person.init_ldap_connection
  15. bound_user = ldap.bind_as(:filter => "(uid=#{params[:uid]})", :password => params[:password])
  16. if bound_user
  17. session[:auth] = true
  18. redirect_to projects_path
  19. else
  20. flash[:warning] = "Could not login with the credentials you supplied."
  21. end
  22. else
  23. session[:auth] = nil
  24. flash[:message] = "Please login"
  25. end
  26. end
  27.  
  28. def destroy
  29. session[:auth] = nil
  30. redirect_to login_path
  31. end
  32.  
  33. end
  34.  
  35.  
  36. ## Model
  37.  
  38.  
  39.  
  40. class Person < ActiveRecord::Base
  41. has_and_belongs_to_many :projects, :order => "title"
  42. file_column :photo, :magick => {
  43. :crop => "1:1",
  44. :versions => { :thumb => "100x100" }
  45. }
  46.  
  47. SINGLED_COLUMNS = %w(cn givenname sn street i postalcode st c labeleduri description)
  48. ARRAYED_COLUMNS = %w(telephonenumber mobile fax pager mail apple-imhandle)
  49. ARRAYED_COLUMNS.each { |v| serialize v.gsub('-','_') }
  50. # Connect to the LDAP and sync our list of people with the LDAP
  51. # [OPTIMIZE] This is incredibly inefficient, but it works for now.
  52. def self.sync!
  53. ldap = init_ldap_connection
  54. people = Array.new
  55. ldap.search(:filter => Net::LDAP::Filter.eq("apple-keyword", "FCIT" )) do |entry|
  56. people << person = find_or_initialize_by_uid(entry.uid.first)
  57. SINGLED_COLUMNS.each { |v| person[v.gsub('-','_')] = entry[v].first }
  58. ARRAYED_COLUMNS.each { |v| person[v.gsub('-','_')] = entry[v] }
  59. begin
  60. unless entry.jpegphoto.nil?
  61. # the file_column plugin makes it easy to handle thumbnails but it wont
  62. # take StringIO, so dump to a file and THEN read it into file_column
  63. File.open(File.join(RAILS_ROOT, "tmp", "#{person.to_param}.jpg"), "w+") do |file|
  64. file.write(entry.jpegphoto.first); person.photo = file; File.unlink(file.path)
  65. end
  66. end
  67. rescue NoMethodError
  68. # the jpegPhoto field isn't created by default so handle it's non-existance
  69. end
  70. person.save
  71. end
  72. # Remove stale db entries that aren't in the LDAP any more.
  73. find(:all).each do |person|
  74. person.destroy unless people.include? person
  75. end
  76. # Return something.
  77. people.size
  78. end
  79.  
  80. def self.init_ldap_connection
  81. Net::LDAP.new :host => "gila.fcit.usf.edu",
  82. :base => "cn=users,dc=gila,dc=fcit,dc=usf,dc=edu"
  83. end
  84. end
Add Comment
Please, Sign In to add comment