修改了before_filter的作用范围

dev_partners
Alec Zhou 6 years ago
parent 7cee6ac4ce
commit b5eb7c1354

@ -1149,7 +1149,7 @@ class ApplicationController < ActionController::Base
redirect_to authorize_url redirect_to authorize_url
end end
def user_login_and_from_openi def user_login_and_from_openi?
url = request.referer.nil? ? Redmine::Configuration['educoder_domain'] : request.referer url = request.referer.nil? ? Redmine::Configuration['educoder_domain'] : request.referer
flag = URI(url).host == URI(Redmine::Configuration['openi_domain']).host && !current_user.logged? flag = URI(url).host == URI(Redmine::Configuration['openi_domain']).host && !current_user.logged?
logger.info "----------------------- request referer: #{request.referer} -------------------------" logger.info "----------------------- request referer: #{request.referer} -------------------------"

@ -2,7 +2,7 @@
class ChallengesController < ApplicationController class ChallengesController < ApplicationController
layout "base_shixun" layout "base_shixun"
# 如要添加或修改before_filter时请将handle_openi_request这个before_filter放至第一位 # 如要添加或修改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 :check_authentication, :except => [:index]
before_filter :find_shixun, :only => [:index, :new, :create, :destroy, :challenge_build, :update_evaluation, :add_choose_question, :new_choose_question, 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] :choose_type_show, :edit_choose_question, :update_choose_question, :destroy_challenge_choose]

@ -154,9 +154,9 @@ class OauthController < ApplicationController
####--Start-- 获取Openi的授权码access_token以及用户信息。为在openi登录的用户创建相关的educoder用户 #### ####--Start-- 获取Openi的授权码access_token以及用户信息。为在openi登录的用户创建相关的educoder用户 ####
IDENTITY_SITE = Redmine::Configuration['openi_domain'] IDENTITY_SITE = Redmine::Configuration['openi_domain']
ROOT_URL = Redmine::Configuration['educoder_domain'] ROOT_URL = Redmine::Configuration['educoder_domain']
DEFAULT_PASSWORD = "a12345678" DEFAULT_PASSWORD = 'a12345678'.freeze
TOKEN_CALL_BACK = "/oauth/get_token_callback" TOKEN_CALL_BACK = '/oauth/get_token_callback'.freeze
USER_INFO = "/oauth/userinfo" USER_INFO = '/oauth/userinfo'.freeze
def get_code def get_code
# 从OpenI发过来的回调中获取授权码 # 从OpenI发过来的回调中获取授权码
@ -183,6 +183,10 @@ class OauthController < ApplicationController
openi = Openi.find_by_login(login) openi = Openi.find_by_login(login)
unless openi unless openi
ActiveRecord::Base.transaction do 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 = User.new(lastname: name, mail: email, mail_notification: email)
user.login = custom_openi_login(login) user.login = custom_openi_login(login)
user.password = DEFAULT_PASSWORD user.password = DEFAULT_PASSWORD
@ -196,7 +200,7 @@ class OauthController < ApplicationController
end end
end end
self.logged_user = openi.user self.logged_user = openi.user if openi.present? && openi.user.present?
original_url = params[:original_url] original_url = params[:original_url]
redirect_to original_url redirect_to original_url
end end
@ -208,7 +212,7 @@ class OauthController < ApplicationController
private private
# 为了保证新创建的用户用户名不与系统中已存在的用户冲突,加上 _openi 后缀 # 为了保证新创建的用户用户名不与系统中已存在的用户冲突,加上 _openi 后缀
def custom_openi_login(login) def custom_openi_login(login)
login + "_openi" login + '_openi'
end end
def require_login def require_login

@ -2,8 +2,6 @@
# REDO: 创建版本库权限控制 # REDO: 创建版本库权限控制
class ShixunsController < ApplicationController class ShixunsController < ApplicationController
layout 'base_shixun' 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 :require_login, :except => [:ghook, :download_file, :show, :index]
before_filter :check_authentication, :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] 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]

@ -2,7 +2,7 @@
class SubjectsController < ApplicationController class SubjectsController < ApplicationController
layout 'base_subject' layout 'base_subject'
# 如要添加或修改before_filter时请将handle_openi_request这个before_filter放至第一位 # 如要添加或修改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 :require_login, :except => [:show, :index]
before_filter :check_authentication, :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] before_filter :find_subject, :except => [:index, :new, :create, :create_subject, :new_subject, :append_to_stage, :send_to_course]

Loading…
Cancel
Save