This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/app/controllers/sessions_controller.rb

63 lines
2.5 KiB
Ruby
Raw Normal View History

2012-02-22 06:43:24 +00:00
class SessionsController < Devise::SessionsController
prepend_before_filter :require_no_authentication, :only => [ :new, :create ]
include Devise::Controllers::InternalHelpers
# POST /resource/sign_in
def create
2012-03-06 08:41:06 +00:00
# login_password = params[:user][:password]
# login_uid = params[:user][:nccu_ldap_uid]
2012-02-22 06:43:24 +00:00
login_password = params[:user][:password]
login_uid = params[:user][:nccu_ldap_uid]
result = false
ldap = Net::LDAP.new
2012-03-06 08:41:06 +00:00
ldap.port = '8001'
ldap.host = '127.0.0.1'
# ldap.port = '389'
# ldap.host = '140.119.166.23'
2012-02-22 06:43:24 +00:00
ldap_filter = "(uid=#{login_uid})"
ldap_base = 'ou=People,dc=nccu,dc=edu,dc=tw'
ldap.authenticate("cn=uccn,ou=profile,dc=nccu,dc=edu,dc=tw","nccu2ucc")
if ldap.bind
result = ldap.bind_as(:base => ldap_base,:filter => ldap_filter,:password=> login_password)
if result
2012-02-24 11:07:47 +00:00
nccu_id = get_nccu_id_from_mid_site(login_uid)
resource = User.first(conditions:{ nccu_id: nccu_id })
2012-02-22 06:43:24 +00:00
# resource = env['warden'].authenticate!(:check_nccu_ldap)
# resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
2012-03-06 08:41:06 +00:00
unless resource_name.nil?
resource_name = resource._type.downcase
sign_in(resource_name, resource)
respond_with resource, :location => redirect_location(resource_name, resource)
else
flash[:notice] = t('devise.failure.ldap_pass_but_account_not_in_orbit')
render :action => "new"
end
2012-02-22 06:43:24 +00:00
else
flash[:notice] = t('devise.failure.ldap_invalid')
render :action => "new"
end
else
flash[:notice] = t('devise.failure.ldap_connection_failed')
render :action => "new"
end
end
2012-02-24 11:07:47 +00:00
private
def get_nccu_id_from_mid_site(ldap_id)
nccu_id = MID_CLIENT.query("SELECT nccu_id FROM rss_aaldap_view WHERE ldap_id='#{ldap_id}' LIMIT 1").first['nccu_id'] rescue nil
#
# if nccu_id.nil?
# #show_error
# p 'account not exist'
# #should return?
# end
# # User.first(conditions: { })
# rss_pautlst_ut = MID_CLIENT.query("SELECT * FROM rss_pautlst_ut WHERE nccu_id='#{nccu_id}' LIMIT 1").first rescue nil
# # rss_paunit = client.query("SELECT * FROM rss_paunit LIMIT 1").first rescue nil
# user = User.find_or_create_by(:nccu_id => nccu_id)
# p user
# # p rss_paunit
#
end
2012-02-22 06:43:24 +00:00
end