diff --git a/Gemfile b/Gemfile index 9bef8cc..4000259 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,8 @@ source 'https://rubygems.org' gem 'rails', '~> 4.1.0' gem 'sanitize' +gem 'savon', '~> 2.2.0' + #assets and templates gem 'sass-rails', '~> 4.0.2' gem 'uglifier', '>= 1.3.0' diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 9c4071c..d3c7e2b 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,5 +1,7 @@ class SessionsController < ApplicationController layout "authentication" + + require 'savon' def new if session[:user_id] @@ -8,23 +10,83 @@ class SessionsController < ApplicationController end def create - user = User.find_by(user_name: params[:user_name]) rescue nil - if (user && user.authenticate(params[:password]) && user.is_confirmed?.eql?(true)) - if user.is_approved? || user.is_admin? - session[:user_id] = user.id - session[:login_referer] = nil - if params[:referer_url] - redirect_to URI.parse(params[:referer_url]).path + + @SYS_USER = "rulingcom" + @SYS_PASS = "pxm87912tkx" + @LDAP_USER = params[:user_name] + @LDAP_PASS = params[:password] + + if @LDAP_USER == 'rulingcom' + + user = User.find_by(user_name: @LDAP_USER) rescue nil + if (user && user.authenticate(@LDAP_PASS) && user.is_confirmed?.eql?(true)) + if user.is_approved? || user.is_admin? + session[:user_id] = user.id + session[:login_referer] = nil + if params[:referer_url] + redirect_to URI.parse(params[:referer_url]).path + else + redirect_to admin_dashboards_path + end else - redirect_to admin_dashboards_path + flash.now.alert = "User not approved." + render "new" end else - flash.now.alert = "User not approved." + flash.now.alert = "Invalid username or password" render "new" end + else - flash.now.alert = "Invalid username or password" - render "new" + + errors = ["很抱歉,您無此權限或帳號登入本站,請洽本站管理員", "Sorry, you don't have the account or authority to login. Please contact the website administrator."] + + client = Savon.client(wsdl: 'http://ap.ydu.edu.tw/LDAP_WS/RulingcomDataService.asmx?wsdl',filters: [:SYS_USER, :SYS_PASS, :LDAP_USER, :LDAP_PASS]) + + client.operations + + response = client.call(:login_chk, message: { + "SYS_USER" => @SYS_USER, + "SYS_PASS" => @SYS_PASS, + "LDAP_USER" => @LDAP_USER, + "LDAP_PASS" => @LDAP_PASS, + "User_IP" => request.remote_ip + }) + + @datas = response.body[:login_chk_response][:login_chk_result][:string] + + if !@datas[0].blank? and ( @datas[0] == 'P' or @datas[0] == 'U' ) #使用者帳號回傳P,單位帳號回傳U + + user = User.find_by(user_name: @LDAP_USER) rescue nil + + if !user.blank? + + if user.is_approved? || user.is_admin? + session[:user_id] = user.id + session[:login_referer] = nil + if params[:referer_url] + redirect_to URI.parse(params[:referer_url]).path + else + redirect_to admin_dashboards_path + end + else + flash.now.alert = "User not approved." + render "new" + end + + else #認證通過無帳號不通過 + + flash.now.alert = errors.join("
").html_safe + render "new" + + end + + else + + flash.now.alert = errors.join("
").html_safe + render "new" + + end end end diff --git a/app/models/user.rb b/app/models/user.rb index e274c6a..f25160d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -24,8 +24,8 @@ class User has_one :desktop, :dependent => :destroy - validates :user_name, uniqueness: true - validates :password, :on => :create, length: {:in => 8..20} + validates :user_name, uniqueness: true, length: {:minimum => 2, :maximum => 50, :message => I18n.t("users.user_id_length") } + validates :password, presence: true, :on => :create, length: {:in => 8..20} #Add getter and setter for email virtual field attr_accessor :email, :first_name, :last_name diff --git a/config/locales/en.yml b/config/locales/en.yml index c45c6e3..ba561a1 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -659,9 +659,9 @@ en: sex: Gender gender: Gender user_id: User Account - user_id_note: Should be more than 3 characters and less than 50 characters + user_id_note: Should be more than 2 characters and less than 50 characters user_id_not_null: User account cannot be null - user_id_length: User account should be more than 3 characters + user_id_length: User account should be more than 2 characters user_id_error: Someone already use that user account user_basic_id_form: Account Info. user_basic_data: Personal Profile diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index c501af4..7bc553a 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -650,9 +650,9 @@ zh_tw: sex: 性別 gender: 性別 user_id: 使用者帳號 - user_id_note: 需介於3個字元至50個字元之間 + user_id_note: 需介於2個字元至50個字元之間 user_id_not_null: 使用者帳號不能是空值 - user_id_length: 使用者帳號至少需要3個字元 + user_id_length: 使用者帳號至少需要2個字元 user_id_error: 該使用者帳號已被使用 user_basic_id_form: 帳號資料 user_basic_data: 個人資料