Ldap_Login_For_Mdu/lib/ldap_login/login.rb

49 lines
1.4 KiB
Ruby
Raw Normal View History

2021-02-21 04:26:15 +00:00
module LdapLogin::Login
2022-11-13 13:59:08 +00:00
ONLY_LDAP = true
2022-01-24 04:41:39 +00:00
LDAP_ADSERVER="https://ap99.mdu.edu.tw/MduDB/api/Auth/token/1"
2022-01-25 03:51:28 +00:00
AppKey = "OCz3t1nQWD"
2021-02-21 04:26:15 +00:00
def ldap_login_auth(user,request,session,flash,params)
error = ''
ldap_user = params[:user_name]
ldap_pass = params[:password]
login_flag = false
2021-02-22 04:19:47 +00:00
url = '/'
url_method = 'redirect_to'
2022-01-24 04:56:17 +00:00
2022-01-24 04:41:39 +00:00
uri = URI(LDAP_ADSERVER)
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
req.body = {AppKey: AppKey,username: ldap_user,password: ldap_pass}.to_json
2022-01-24 04:56:17 +00:00
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
2022-01-24 04:59:59 +00:00
http.read_timeout = 3
2022-01-24 04:56:17 +00:00
res = http.request(req)
2022-01-24 04:41:39 +00:00
if res.code == '200' && JSON.load(res.body)["userID"]==ldap_user
if !user.nil?
2022-01-25 03:51:28 +00:00
session[:user_id] = user.id
2022-01-24 04:41:39 +00:00
session[:login_referer] = nil
if params[:referer_url]
url = URI.parse(params[:referer_url]).path
url_method = 'redirect_to'
2021-02-21 04:26:15 +00:00
else
2022-01-24 04:41:39 +00:00
url = admin_dashboards_path
url_method = 'redirect_to'
2021-02-21 04:26:15 +00:00
end
2022-01-24 04:41:39 +00:00
login_flag = true
else
error = I18n.t('devise.failure.ldap_pass_but_account_not_in_orbit')
2021-02-21 04:26:15 +00:00
end
2022-01-24 04:41:39 +00:00
else
error = '驗證失敗,您輸入的使用者名稱或密碼不正確!'
2021-02-21 04:26:15 +00:00
end
2022-01-24 04:41:39 +00:00
2021-02-21 04:26:15 +00:00
if !login_flag
flash.now.alert = error.html_safe
url = 'new'
url_method = 'render'
end
[login_flag,session,flash,url,url_method]
end
end