diff --git a/Gemfile b/Gemfile index 0174d73ac..f3b06c2d4 100644 --- a/Gemfile +++ b/Gemfile @@ -49,7 +49,7 @@ gem 'elasticsearch-rails' group :development do gem 'grape-swagger' gem 'better_errors', '~> 1.1.0' - gem "query_reviewer" + # gem "query_reviewer" # gem 'rack-mini-profiler', '~> 0.9.3' if RUBY_PLATFORM =~ /w32/ gem 'win32console' diff --git a/app/controllers/sso_controller.rb b/app/controllers/sso_controller.rb deleted file mode 100644 index 73982ae9c..000000000 --- a/app/controllers/sso_controller.rb +++ /dev/null @@ -1,45 +0,0 @@ -#coding=utf-8 - -require 'base64' -require 'json' -require 'openssl' - -## 单点登录 <=> 北斗 -class SsoController < ApplicationController - - skip_before_filter :check_if_login_required - - def index - options = parse(params[:auth]) - - logger.debug options - - ## 认证 - login(options[:id]) - - ## 选择性跳转 - - redirect_to Organization.find(82) - - end - - private - def base64_safe(content) - content = content.gsub('-', '+') - content.gsub('_', '/') - end - - def parse(auth) - crypted_str = Base64.decode64(base64_safe(auth)) - pkey = OpenSSL::PKey::RSA.new(File.new(File.join(Rails.root,"config/private.key"))) - content = pkey.private_decrypt(crypted_str,OpenSSL::PKey::RSA::PKCS1_PADDING) - # content = pkey.private_decrypt(crypted_str) - ActiveSupport::JSON.decode(content) - end - - def login(id) - sso = Sso.find(id) - start_user_session(sso.user) - end - -end diff --git a/app/controllers/ssos_controller.rb b/app/controllers/ssos_controller.rb new file mode 100644 index 000000000..ee48bea9c --- /dev/null +++ b/app/controllers/ssos_controller.rb @@ -0,0 +1,66 @@ +#coding=utf-8 + +require 'base64' +require 'json' +require 'openssl' + +## 单点登录 <=> 北斗 +class SsosController < ApplicationController + + skip_before_filter :check_if_login_required + layout false + + def show + begin + # suRh2nFEJd0Ai_TFbqZ-1yQXnGfIB-YD_f4KTA3O4dQGSBMiXfOMt-0mzizgXekWTjHKfn62nJ60iHM3_eY_KS0Qn8SF8vANfa46GhzZRt4T0iC5ZOSs4cWeK43OU0RoekQLZZAo5OyOVibxabmiPGzEFCnVVtdmRk9d7X_B0Is= + @auth = params[:auth] + @options = parse(params[:auth]) + + if params[:login].present? + @options["name"] = params[:login] + end + + logger.debug @options + ## 认证 + login(@options) + + logger.debug "login over" + + ## 选择性跳转 + + redirect_to Organization.find(1) + rescue => e + logger.error e + if e.message == "exist user" + render 'ssos/show', :layout => false + else + raise e + end + end + end + + ## 改用户名 + def create + show and return + end + + private + def base64_safe(content) + content = content.gsub('-', '+') + content.gsub('_', '/') + end + + def parse(auth) + crypted_str = Base64.decode64(base64_safe(auth)) + pkey = OpenSSL::PKey::RSA.new(File.new(File.join(Rails.root,"config/private.key"))) + content = pkey.private_decrypt(crypted_str,OpenSSL::PKey::RSA::PKCS1_PADDING) + # content = pkey.private_decrypt(crypted_str) + ActiveSupport::JSON.decode(content) + end + + def login(opt) + sso = Sso.sync_user(opt) + start_user_session(sso.user) + end + +end diff --git a/app/models/sso.rb b/app/models/sso.rb index d7cf54dc3..c6b57cd2d 100644 --- a/app/models/sso.rb +++ b/app/models/sso.rb @@ -1,4 +1,56 @@ class Sso < ActiveRecord::Base belongs_to :user - attr_accessible :email, :name, :openid, :password, :school, :sex + attr_accessible :email, :name, :openid, :password, :school, :sex, :user, :user_id + + validates :user_id, :user, :email, :openid, :presence => true + + def self.sync_user(opt) + sso = Sso.where(openid: opt["openid"]).first + return sso if sso + + sso = Sso.new + sso.name = opt["name"] + sso.openid = opt["openid"] + sso.email = opt["email"] + sso.password = opt["password"] + sso.school = opt["school"] + sso.sex = opt["sex"] + + + # 查邮箱 + user = User.where(mail: opt["email"]).first + + unless user + # 查用户名 + + user = User.where(login: opt["name"]).first + if user + # 跳到修改用户名 + raise "exist user" + end + + password = opt["password"] + if password.size < 8 + password = random_pwd + end + us = UsersService.new + user = us.register(login: opt["name"], mail: opt["email"], + password: password, + :should_confirmation_password => false) + if user.new_record? + raise user.errors.full_messages.first + end + end + + sso.user = user + sso.save! + return sso + end + + + private + def self.random_pwd + ('a'..'z').to_a.shuffle[0..7].join + end + end diff --git a/app/views/ssos/show.html.erb b/app/views/ssos/show.html.erb new file mode 100644 index 000000000..456cbe154 --- /dev/null +++ b/app/views/ssos/show.html.erb @@ -0,0 +1,64 @@ + + + + + 跳转页面 + + + + +
+<%= form_tag('/sso') do -%> +
+

请修改您的资料,以确保Trustie为您提供更便捷的服务

+ + + + + + + + + + + + + + + + + +
用户名: + " name="login"/> +
已存在相同用户名,请选择更合适的用户名
邮箱: + " disabled="disabled" /> +
+
+ +<%= hidden_field_tag 'auth', @auth %> +<% end -%> + + + diff --git a/config/routes.rb b/config/routes.rb index 0592041f6..e1c969b0f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1197,7 +1197,7 @@ RedmineApp::Application.routes.draw do end end - get '/sso', to: 'sso#index' + resource :sso, only: [:show, :create] get '/:sub_dir_name', :to => 'org_subfields#show', :as => 'show_subfield_without_id'