From b5eb7c13541b3efcb9358de4d45d5ac72abeb73f Mon Sep 17 00:00:00 2001 From: Alec Zhou Date: Mon, 29 Apr 2019 08:29:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86before=5Ffilter?= =?UTF-8?q?=E7=9A=84=E4=BD=9C=E7=94=A8=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/application_controller.rb | 2 +- app/controllers/challenges_controller.rb | 2 +- app/controllers/oauth_controller.rb | 14 +++++++++----- app/controllers/shixuns_controller.rb | 2 -- app/controllers/subjects_controller.rb | 2 +- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b1a66080..7f1383bc 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1149,7 +1149,7 @@ class ApplicationController < ActionController::Base redirect_to authorize_url end - def user_login_and_from_openi + def user_login_and_from_openi? url = request.referer.nil? ? Redmine::Configuration['educoder_domain'] : request.referer flag = URI(url).host == URI(Redmine::Configuration['openi_domain']).host && !current_user.logged? logger.info "----------------------- request referer: #{request.referer} -------------------------" diff --git a/app/controllers/challenges_controller.rb b/app/controllers/challenges_controller.rb index 13bfc188..685f9f74 100644 --- a/app/controllers/challenges_controller.rb +++ b/app/controllers/challenges_controller.rb @@ -2,7 +2,7 @@ class ChallengesController < ApplicationController layout "base_shixun" # 如要添加或修改before_filter时,请将handle_openi_request这个before_filter放至第一位 - # before_filter :handle_openi_request, if: -> { user_login_and_from_openi } + before_filter :handle_openi_request, if: -> { user_login_and_from_openi? }, only: [:index] before_filter :check_authentication, :except => [:index] before_filter :find_shixun, :only => [:index, :new, :create, :destroy, :challenge_build, :update_evaluation, :add_choose_question, :new_choose_question, :choose_type_show, :edit_choose_question, :update_choose_question, :destroy_challenge_choose] diff --git a/app/controllers/oauth_controller.rb b/app/controllers/oauth_controller.rb index 64867d55..930f2b75 100644 --- a/app/controllers/oauth_controller.rb +++ b/app/controllers/oauth_controller.rb @@ -154,9 +154,9 @@ class OauthController < ApplicationController ####--Start-- 获取Openi的授权码,access_token,以及用户信息。为在openi登录的用户创建相关的educoder用户 #### IDENTITY_SITE = Redmine::Configuration['openi_domain'] ROOT_URL = Redmine::Configuration['educoder_domain'] - DEFAULT_PASSWORD = "a12345678" - TOKEN_CALL_BACK = "/oauth/get_token_callback" - USER_INFO = "/oauth/userinfo" + DEFAULT_PASSWORD = 'a12345678'.freeze + TOKEN_CALL_BACK = '/oauth/get_token_callback'.freeze + USER_INFO = '/oauth/userinfo'.freeze def get_code # 从OpenI发过来的回调中获取授权码 @@ -183,6 +183,10 @@ class OauthController < ApplicationController openi = Openi.find_by_login(login) unless openi ActiveRecord::Base.transaction do + # 如果Educoder中已存在与该OpenI用户的邮箱相同的用户,则会直接跳转到登录educoder的登录页面 + existing_user = User.find_by_mail(email) + break if existing_user.present? + user = User.new(lastname: name, mail: email, mail_notification: email) user.login = custom_openi_login(login) user.password = DEFAULT_PASSWORD @@ -196,7 +200,7 @@ class OauthController < ApplicationController end end - self.logged_user = openi.user + self.logged_user = openi.user if openi.present? && openi.user.present? original_url = params[:original_url] redirect_to original_url end @@ -208,7 +212,7 @@ class OauthController < ApplicationController private # 为了保证新创建的用户用户名不与系统中已存在的用户冲突,加上 _openi 后缀 def custom_openi_login(login) - login + "_openi" + login + '_openi' end def require_login diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index 20f0f203..f6578f18 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -2,8 +2,6 @@ # REDO: 创建版本库权限控制 class ShixunsController < ApplicationController layout 'base_shixun' - # 如要添加或修改before_filter时,请将handle_openi_request这个before_filter放至第一位 - # before_filter :handle_openi_request, if: -> { user_login_and_from_openi } before_filter :require_login, :except => [:ghook, :download_file, :show, :index] before_filter :check_authentication, :except => [:ghook, :download_file, :show, :index] before_filter :find_shixun, :except => [ :index, :new, :create, :index, :search, :shixun_courses, :new_disscuss, :shixun_migrate, :qrcode, :download_file, :departments, :get_mirror_script, :send_message_to_administrator] diff --git a/app/controllers/subjects_controller.rb b/app/controllers/subjects_controller.rb index d9d4a24c..61e56f9a 100644 --- a/app/controllers/subjects_controller.rb +++ b/app/controllers/subjects_controller.rb @@ -2,7 +2,7 @@ class SubjectsController < ApplicationController layout 'base_subject' # 如要添加或修改before_filter时,请将handle_openi_request这个before_filter放至第一位 - # before_filter :handle_openi_request, if: -> { user_login_and_from_openi } + before_filter :handle_openi_request, if: -> { user_login_and_from_openi? }, only: [:show] before_filter :require_login, :except => [:show, :index] before_filter :check_authentication, :except => [:show, :index] before_filter :find_subject, :except => [:index, :new, :create, :create_subject, :new_subject, :append_to_stage, :send_to_course]