Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## environment.rb
- require 'vpim/vcard'
- require 'net/ldap'
- ## Controllers
- class SessionController < ApplicationController
- skip_before_filter :authorize, :only => ["new", "create"]
- def new
- # If it's not a POST action, just show the form
- if request.post?
- ldap = Person.init_ldap_connection
- bound_user = ldap.bind_as(:filter => "(uid=#{params[:uid]})", :password => params[:password])
- if bound_user
- session[:auth] = true
- redirect_to projects_path
- else
- flash[:warning] = "Could not login with the credentials you supplied."
- end
- else
- session[:auth] = nil
- flash[:message] = "Please login"
- end
- end
- def destroy
- session[:auth] = nil
- redirect_to login_path
- end
- end
- ## Model
- class Person < ActiveRecord::Base
- has_and_belongs_to_many :projects, :order => "title"
- file_column :photo, :magick => {
- :crop => "1:1",
- :versions => { :thumb => "100x100" }
- }
- SINGLED_COLUMNS = %w(cn givenname sn street i postalcode st c labeleduri description)
- ARRAYED_COLUMNS = %w(telephonenumber mobile fax pager mail apple-imhandle)
- ARRAYED_COLUMNS.each { |v| serialize v.gsub('-','_') }
- # Connect to the LDAP and sync our list of people with the LDAP
- # [OPTIMIZE] This is incredibly inefficient, but it works for now.
- def self.sync!
- ldap = init_ldap_connection
- people = Array.new
- ldap.search(:filter => Net::LDAP::Filter.eq("apple-keyword", "FCIT" )) do |entry|
- people << person = find_or_initialize_by_uid(entry.uid.first)
- SINGLED_COLUMNS.each { |v| person[v.gsub('-','_')] = entry[v].first }
- ARRAYED_COLUMNS.each { |v| person[v.gsub('-','_')] = entry[v] }
- begin
- unless entry.jpegphoto.nil?
- # the file_column plugin makes it easy to handle thumbnails but it wont
- # take StringIO, so dump to a file and THEN read it into file_column
- File.open(File.join(RAILS_ROOT, "tmp", "#{person.to_param}.jpg"), "w+") do |file|
- file.write(entry.jpegphoto.first); person.photo = file; File.unlink(file.path)
- end
- end
- rescue NoMethodError
- # the jpegPhoto field isn't created by default so handle it's non-existance
- end
- person.save
- end
- # Remove stale db entries that aren't in the LDAP any more.
- find(:all).each do |person|
- person.destroy unless people.include? person
- end
- # Return something.
- people.size
- end
- def self.init_ldap_connection
- Net::LDAP.new :host => "gila.fcit.usf.edu",
- :base => "cn=users,dc=gila,dc=fcit,dc=usf,dc=edu"
- end
- end
Add Comment
Please, Sign In to add comment