From 2df0d65ce7f9ff7ad7c06bc7f89a329611b01127 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 29 May 2019 23:44:00 +0800 Subject: [PATCH] 1 --- app/api/mobile/apis/cnmooc.rb | 16 +++++++++++++++- app/controllers/application_controller.rb | 5 +++-- app/controllers/challenges_controller.rb | 9 +++++++++ app/services/cnmoocs_service.rb | 5 +---- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/app/api/mobile/apis/cnmooc.rb b/app/api/mobile/apis/cnmooc.rb index 96b10101..7745cf8e 100644 --- a/app/api/mobile/apis/cnmooc.rb +++ b/app/api/mobile/apis/cnmooc.rb @@ -43,7 +43,21 @@ module Mobile requires :accessType, type: Integer, desc: "资源类型" end get "source_url" do - CnmoocsService.new.source_url(params) + user = User.find_by_id(params[:userId]) + return {error: -1, messages: "用户不存在,请先创建用户"} unless user + token = Token.get_or_create_permanent_login_token(user) + cookie_options = { + :value => token.value, + :expires => 1.month.from_now, + :path => (Redmine::Configuration['autologin_cookie_path'] || '/'), + :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false), + :httponly => true + } + if Redmine::Configuration['cookie_domain'].present? + cookie_options = cookie_options.merge(domain: Redmine::Configuration['cookie_domain']) + end + cookies[Redmine::Configuration['autologin_cookie_name'].presence || 'autologin'] = cookie_options + CnmoocsService.new.source_url(params, token) end desc "远程登录" diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index dd1f5967..4185e367 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -224,8 +224,9 @@ class ApplicationController < ActionController::Base user = uw.user if uw elsif params[:authToken] user = Token.find_by_value(params[:authToken]).user - set_autologin_cookie(user) - start_user_session(user) + #set_autologin_cookie(user) + #start_user_session(user) + session[:user_id] = user.id end end if user.nil? && Setting.rest_api_enabled? && accept_api_auth? diff --git a/app/controllers/challenges_controller.rb b/app/controllers/challenges_controller.rb index bf01ffb0..3322f1f2 100644 --- a/app/controllers/challenges_controller.rb +++ b/app/controllers/challenges_controller.rb @@ -16,6 +16,7 @@ class ChallengesController < ApplicationController #before_filter :find_shixun_language, :only => [:show, :new, :edit] before_filter :base_index, :only => [:index, :index_down, :index_up, :destroy] before_filter :view_allow, :only => [:show] + # before_filter :check_cnmooc, :only => [:index] include ApplicationHelper @@ -487,4 +488,12 @@ class ChallengesController < ApplicationController response.headers['content--type'] = 'text/javascript' request.format = 'js' end + + # def check_cnmooc + # if params[:authToken] + # user = User.find_by_id(session[:user_id]) + # set_autologin_cookie(user) + # end + # end + end diff --git a/app/services/cnmoocs_service.rb b/app/services/cnmoocs_service.rb index fa6e67ea..41705eb6 100644 --- a/app/services/cnmoocs_service.rb +++ b/app/services/cnmoocs_service.rb @@ -114,10 +114,7 @@ class CnmoocsService end - def source_url(params) - user = User.find_by_id(params[:userId]) - return {error: -1, messages: "用户不存在,请先创建用户"} unless user - token = Token.get_or_create_permanent_login_token(user) + def source_url(params, token) shixun = Shixun.find_by_id(params[:resouceId]) if shixun.blank? return { error: -1, messages: '资源不存在' }