From 080026fe3843098e06d6885488479656e53d1faf Mon Sep 17 00:00:00 2001 From: anke1460 Date: Tue, 18 Feb 2020 09:40:51 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=8D=95=E7=82=B9=E7=99=BB=E5=BD=95test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 ++ app/controllers/oauth/cas_controller.rb | 13 ++++++++ app/models/open_users/cas.rb | 9 ++++++ .../oauth/create_or_find_cas_user_service.rb | 32 +++++++++++++++++++ config/application.rb | 4 +++ config/routes.rb | 6 ++-- 6 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 app/controllers/oauth/cas_controller.rb create mode 100644 app/models/open_users/cas.rb create mode 100644 app/services/oauth/create_or_find_cas_user_service.rb diff --git a/Gemfile b/Gemfile index 859090feb..e454889f6 100644 --- a/Gemfile +++ b/Gemfile @@ -49,6 +49,8 @@ gem 'rqrcode_png' gem 'acts-as-taggable-on', '~> 6.0' +gem 'omniauth-cas' + group :development, :test do gem 'rspec-rails', '~> 3.8' end diff --git a/app/controllers/oauth/cas_controller.rb b/app/controllers/oauth/cas_controller.rb new file mode 100644 index 000000000..c42e23303 --- /dev/null +++ b/app/controllers/oauth/cas_controller.rb @@ -0,0 +1,13 @@ +class Oauth::CasController < Oauth::BaseController + def create + user, is_new_user = Oauth::CreateORFindCasUserService.call(current_user, auth_hash) + successful_authentication(user) + + render_ok(new_user: is_new_user) + end + + + def auth_hash + request.env['omniauth.auth'] + end +end \ No newline at end of file diff --git a/app/models/open_users/cas.rb b/app/models/open_users/cas.rb new file mode 100644 index 000000000..301a197a0 --- /dev/null +++ b/app/models/open_users/cas.rb @@ -0,0 +1,9 @@ +class OpenUsers::Cas < OpenUser + def nickname + extra&.[]('nickname') + end + + def en_type + 'cas' + end +end \ No newline at end of file diff --git a/app/services/oauth/create_or_find_cas_user_service.rb b/app/services/oauth/create_or_find_cas_user_service.rb new file mode 100644 index 000000000..c36a23404 --- /dev/null +++ b/app/services/oauth/create_or_find_cas_user_service.rb @@ -0,0 +1,32 @@ +class Oauth::CreateORFindCasUserService < ApplicationService + + def initialize(user, params) + @user = user + @params = params + end + + def call + return [@user, false] if @user + + open_user = OpenUsers::Cas.find_or_initialize_by(uid: @params.info['email']) do |u| + u.extra = @params.info + end + + return [open_user.user, false] if open_user.persisted? + + @user = User.new(login: User.generate_login('C'), type: 'User', status: User::STATUS_ACTIVE) + + ActiveRecord::Base.transaction do + gender = @params.dig('extra', 'raw_info', 'xb') == '女' ? 1 : 0 + @user.save! + @user.create_user_extension!(gender: gender) + + open_user.user = @user + open_user.save! + + Rails.cache.write(open_user.can_bind_cache_key, 1, expires_in: 1.hours) + end + + [@user, true] + end +end \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index baa0011f2..db0204d49 100644 --- a/config/application.rb +++ b/config/application.rb @@ -29,6 +29,10 @@ module Educoderplus # job config.active_job.queue_adapter = :sidekiq + config.middleware.use OmniAuth::Builder do + provider :cas, url: 'http://localhost:3200' + end + config.middleware.insert_before 0, Rack::Cors do allow do origins '*' diff --git a/config/routes.rb b/config/routes.rb index 9eef49bfc..6c58024da 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,8 +8,8 @@ Rails.application.routes.draw do get 'attachments/download/:id/:filename', to: 'attachments#show' get 'auth/qq/callback', to: 'oauth/qq#create' get 'auth/failure', to: 'oauth/base#auth_failure' + get 'auth/cas/callback', to: 'oauth/cas#create' - resources :edu_settings scope '/api' do @@ -26,7 +26,7 @@ Rails.application.routes.draw do put 'commons/unhidden', to: 'commons#unhidden' delete 'commons/delete', to: 'commons#delete' - resources :jupyters do + resources :jupyters do collection do get :save_with_tpi get :save_with_tpm @@ -42,7 +42,7 @@ Rails.application.routes.draw do post :import_with_tpm end end - + resources :memos do member do post :sticky_or_cancel From dcc1fc750e9e563c73f1202a1a8af2061d0ebf60 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Tue, 25 Feb 2020 18:08:12 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=97=A5=E5=BF=97=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/games_controller.rb | 20 ++++++++++---------- app/controllers/myshixuns_controller.rb | 11 +++-------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index fcdd81953..4c9e7fc0e 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -524,11 +524,11 @@ class GamesController < ApplicationController else {updated_at: Time.now} end - logger.info("#############myshixuns_update: ##{myshixuns_update}") + #logger.info("#############myshixuns_update: ##{myshixuns_update}") @myshixun.update_attributes!(myshixuns_update) gitUrl = repo_ip_url @myshixun.repo_path - logger.info("#############giturl: ##{gitUrl}") + #logger.info("#############giturl: ##{gitUrl}") gitUrl = Base64.urlsafe_encode64(gitUrl) shixun_tomcat = edu_setting('cloud_bridge') @@ -554,7 +554,7 @@ class GamesController < ApplicationController testSet << test_cases end - logger.info("##############testSet: #{testSet}") + #logger.info("##############testSet: #{testSet}") testCases = Base64.urlsafe_encode64(testSet.to_json) unless testSet.blank? # 评测类型: 0,1,2 用于webssh的评测, 3用于vnc @@ -581,7 +581,7 @@ class GamesController < ApplicationController if secret_rep&.repo_name secretGitUrl = repo_ip_url secret_rep.repo_path br_params.merge!({secretGitUrl: Base64.urlsafe_encode64(secretGitUrl), secretDir: secret_rep.secret_dir_path}) - logger.info("#######br_params:#{br_params}") + #logger.info("#######br_params:#{br_params}") end # 中间层交互 @@ -700,10 +700,10 @@ class GamesController < ApplicationController resubmit_identifier = @game.resubmit_identifier # 如果没有超时并且正在评测中 # 判断评测中的状态有两种:1、如果之前没有通关的,只需判断status为1即可;如果通过关,则判断game的resubmit_identifier是否更新 - uid_logger("################game_status: #{@game.status}") - uid_logger("################params[:resubmit]: #{params[:resubmit]}") - uid_logger("################resubmit_identifier: #{resubmit_identifier}") - uid_logger("################time_out: #{params[:time_out]}") + #uid_logger("################game_status: #{@game.status}") + #uid_logger("################params[:resubmit]: #{params[:resubmit]}") + #uid_logger("################resubmit_identifier: #{resubmit_identifier}") + #uid_logger("################time_out: #{params[:time_out]}") sec_key = params[:sec_key] if (params[:time_out] == "false") && ((params[:resubmit].blank? && @game.status == 1) || (params[:resubmit].present? && (params[:resubmit] != resubmit_identifier))) @@ -762,7 +762,7 @@ class GamesController < ApplicationController end end - uid_logger("game is #{@game.id}, record id is #{e_record.try(:id)}, time is**** #{Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")}") + #uid_logger("game is #{@game.id}, record id is #{e_record.try(:id)}, time is**** #{Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")}") # 记录前端总耗时 record_consume_time = e_record.try(:pod_execute) max_mem = e_record.try(:max_mem) @@ -848,7 +848,7 @@ class GamesController < ApplicationController @allowed_hidden_testset = @identity < User::EDU_GAME_MANAGER || @game.test_sets_view #解锁的用户 if max_query_index > 0 - uid_logger("max_query_index is #{max_query_index} game id is #{@game.id}, challenge_id is #{challenge.id}") + #uid_logger("max_query_index is #{max_query_index} game id is #{@game.id}, challenge_id is #{challenge.id}") @qurey_test_sets = TestSet.find_by_sql("SELECT o.code, o.actual_output, o.out_put, o.result, o.test_set_position, o.ts_time, o.ts_mem, o.query_index, t.is_public, t.input, t.output, o.compile_success FROM outputs o, games g, challenges c, test_sets t where g.id=#{@game.id} and c.id=#{challenge.id} and o.query_index=#{max_query_index} diff --git a/app/controllers/myshixuns_controller.rb b/app/controllers/myshixuns_controller.rb index f4c73a6a4..893bf28d7 100644 --- a/app/controllers/myshixuns_controller.rb +++ b/app/controllers/myshixuns_controller.rb @@ -91,7 +91,7 @@ class MyshixunsController < ApplicationController ActiveRecord::Base.transaction do begin t1 = Time.now - uid_logger_dubug("@@@222222#{params[:jsonTestDetails]}") + #uid_logger_dubug("@@@222222#{params[:jsonTestDetails]}") jsonTestDetails = JSON.parse(params[:jsonTestDetails]) timeCost = JSON.parse(params[:timeCost]) brige_end_time = Time.parse(timeCost['evaluateEnd']) if timeCost['evaluateEnd'].present? @@ -100,7 +100,7 @@ class MyshixunsController < ApplicationController game_id = jsonTestDetails['buildID'] sec_key = jsonTestDetails['sec_key'] - uid_logger_dubug("training_task_status start-#{game_id}-1#{Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")}") + #uid_logger_dubug("training_task_status start-#{game_id}-1#{Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")}") resubmit = jsonTestDetails['resubmit'] outPut = tran_base64_decode64(jsonTestDetails['outPut']) @@ -262,7 +262,7 @@ class MyshixunsController < ApplicationController @sec_key = generate_identifier(EvaluateRecord, 12) record = EvaluateRecord.create!(:user_id => current_user.id, :shixun_id => @myshixun.shixun_id, :game_id => game_id, :identifier => @sec_key, :exec_time => exec_time) - uid_logger_dubug("-- game build: file update #{@sec_key}, record id is #{record.id}, time is **** #{Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")}") + #uid_logger_dubug("-- game build: file update #{@sec_key}, record id is #{record.id}, time is **** #{Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")}") end # 隐藏代码文件 和 VNC的都不需要走版本库 vnc = @myshixun.shixun&.vnc @@ -277,8 +277,6 @@ class MyshixunsController < ApplicationController else params[:content] end - uid_logger_dubug("content_#{@myshixun.identifier}: #{content}") - uid_logger_dubug("###last_content_#{@myshixun.identifier}####{last_content}") if content != last_content @content_modified = 1 @@ -286,9 +284,6 @@ class MyshixunsController < ApplicationController author_name = current_user.real_name author_email = current_user.git_mail message = params[:evaluate] == 0 ? "System automatically submitted" : "User submitted" - uid_logger_dubug("112233#{author_name}") - uid_logger_dubug("112233#{author_email}") - uid_logger_dubug("daiao_debug_#{@myshixun.identifier}: #{@repo_path}: #{path}; #{message}; #{content}; ") @content = GitService.update_file(repo_path: @repo_path, file_path: path, message: message, From ad6821cf68991d1d2ca2e54fd8b961c1b4d8126a Mon Sep 17 00:00:00 2001 From: anke1460 Date: Tue, 18 Feb 2020 09:40:51 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=8D=95=E7=82=B9=E7=99=BB=E5=BD=95test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 ++ app/controllers/oauth/cas_controller.rb | 13 ++++++++ app/models/open_users/cas.rb | 9 ++++++ .../oauth/create_or_find_cas_user_service.rb | 32 +++++++++++++++++++ config/application.rb | 4 +++ config/routes.rb | 6 ++-- 6 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 app/controllers/oauth/cas_controller.rb create mode 100644 app/models/open_users/cas.rb create mode 100644 app/services/oauth/create_or_find_cas_user_service.rb diff --git a/Gemfile b/Gemfile index 859090feb..e454889f6 100644 --- a/Gemfile +++ b/Gemfile @@ -49,6 +49,8 @@ gem 'rqrcode_png' gem 'acts-as-taggable-on', '~> 6.0' +gem 'omniauth-cas' + group :development, :test do gem 'rspec-rails', '~> 3.8' end diff --git a/app/controllers/oauth/cas_controller.rb b/app/controllers/oauth/cas_controller.rb new file mode 100644 index 000000000..c42e23303 --- /dev/null +++ b/app/controllers/oauth/cas_controller.rb @@ -0,0 +1,13 @@ +class Oauth::CasController < Oauth::BaseController + def create + user, is_new_user = Oauth::CreateORFindCasUserService.call(current_user, auth_hash) + successful_authentication(user) + + render_ok(new_user: is_new_user) + end + + + def auth_hash + request.env['omniauth.auth'] + end +end \ No newline at end of file diff --git a/app/models/open_users/cas.rb b/app/models/open_users/cas.rb new file mode 100644 index 000000000..301a197a0 --- /dev/null +++ b/app/models/open_users/cas.rb @@ -0,0 +1,9 @@ +class OpenUsers::Cas < OpenUser + def nickname + extra&.[]('nickname') + end + + def en_type + 'cas' + end +end \ No newline at end of file diff --git a/app/services/oauth/create_or_find_cas_user_service.rb b/app/services/oauth/create_or_find_cas_user_service.rb new file mode 100644 index 000000000..c36a23404 --- /dev/null +++ b/app/services/oauth/create_or_find_cas_user_service.rb @@ -0,0 +1,32 @@ +class Oauth::CreateORFindCasUserService < ApplicationService + + def initialize(user, params) + @user = user + @params = params + end + + def call + return [@user, false] if @user + + open_user = OpenUsers::Cas.find_or_initialize_by(uid: @params.info['email']) do |u| + u.extra = @params.info + end + + return [open_user.user, false] if open_user.persisted? + + @user = User.new(login: User.generate_login('C'), type: 'User', status: User::STATUS_ACTIVE) + + ActiveRecord::Base.transaction do + gender = @params.dig('extra', 'raw_info', 'xb') == '女' ? 1 : 0 + @user.save! + @user.create_user_extension!(gender: gender) + + open_user.user = @user + open_user.save! + + Rails.cache.write(open_user.can_bind_cache_key, 1, expires_in: 1.hours) + end + + [@user, true] + end +end \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index baa0011f2..db0204d49 100644 --- a/config/application.rb +++ b/config/application.rb @@ -29,6 +29,10 @@ module Educoderplus # job config.active_job.queue_adapter = :sidekiq + config.middleware.use OmniAuth::Builder do + provider :cas, url: 'http://localhost:3200' + end + config.middleware.insert_before 0, Rack::Cors do allow do origins '*' diff --git a/config/routes.rb b/config/routes.rb index 33576e1d0..27ba66b05 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,8 +8,8 @@ Rails.application.routes.draw do get 'attachments/download/:id/:filename', to: 'attachments#show' get 'auth/qq/callback', to: 'oauth/qq#create' get 'auth/failure', to: 'oauth/base#auth_failure' + get 'auth/cas/callback', to: 'oauth/cas#create' - resources :edu_settings scope '/api' do @@ -26,7 +26,7 @@ Rails.application.routes.draw do put 'commons/unhidden', to: 'commons#unhidden' delete 'commons/delete', to: 'commons#delete' - resources :jupyters do + resources :jupyters do collection do get :save_with_tpi get :save_with_tpm @@ -42,7 +42,7 @@ Rails.application.routes.draw do post :import_with_tpm end end - + resources :memos do member do post :sticky_or_cancel From 08196faa1bc62f6781844cddbc59bca7bd5dd052 Mon Sep 17 00:00:00 2001 From: anke1460 Date: Tue, 25 Feb 2020 18:16:32 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=8D=95=E7=82=B9=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=85=B3=E8=81=94=E8=B4=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/oauth/cas_controller.rb | 2 +- app/services/oauth/create_or_find_cas_user_service.rb | 9 ++++----- config/application.rb | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/controllers/oauth/cas_controller.rb b/app/controllers/oauth/cas_controller.rb index c42e23303..d5abc8cbe 100644 --- a/app/controllers/oauth/cas_controller.rb +++ b/app/controllers/oauth/cas_controller.rb @@ -8,6 +8,6 @@ class Oauth::CasController < Oauth::BaseController def auth_hash - request.env['omniauth.auth'] + JSON.parse(CGI.unescape(request.env['omniauth.auth'].extra.to_json)) end end \ No newline at end of file diff --git a/app/services/oauth/create_or_find_cas_user_service.rb b/app/services/oauth/create_or_find_cas_user_service.rb index c36a23404..0b6715471 100644 --- a/app/services/oauth/create_or_find_cas_user_service.rb +++ b/app/services/oauth/create_or_find_cas_user_service.rb @@ -8,18 +8,17 @@ class Oauth::CreateORFindCasUserService < ApplicationService def call return [@user, false] if @user - open_user = OpenUsers::Cas.find_or_initialize_by(uid: @params.info['email']) do |u| - u.extra = @params.info + open_user = OpenUsers::Cas.find_or_initialize_by(uid: @params['user']) do |u| + u.extra = @params end return [open_user.user, false] if open_user.persisted? - @user = User.new(login: User.generate_login('C'), type: 'User', status: User::STATUS_ACTIVE) + @user = User.new(login: User.generate_login('C'), type: 'User', status: User::STATUS_ACTIVE, nickname: @params['comsys_name'], lastname: @params['comsys_name']) ActiveRecord::Base.transaction do - gender = @params.dig('extra', 'raw_info', 'xb') == '女' ? 1 : 0 @user.save! - @user.create_user_extension!(gender: gender) + @user.create_user_extension! open_user.user = @user open_user.save! diff --git a/config/application.rb b/config/application.rb index db0204d49..31a94e7ed 100644 --- a/config/application.rb +++ b/config/application.rb @@ -30,7 +30,7 @@ module Educoderplus config.active_job.queue_adapter = :sidekiq config.middleware.use OmniAuth::Builder do - provider :cas, url: 'http://localhost:3200' + provider :cas, url: 'https://urp.tfswufe.edu.cn/cas' end config.middleware.insert_before 0, Rack::Cors do