Merge remote-tracking branch 'origin/pre_trustie_server' into trustie_server

This commit is contained in:
xiaoxiaoqiong 2022-07-13 12:51:20 +08:00
commit 63f1cdda00
7 changed files with 112 additions and 45 deletions

View File

@ -3,13 +3,22 @@ class Api::V1::BaseController < ApplicationController
include Api::ProjectHelper include Api::ProjectHelper
include Api::UserHelper include Api::UserHelper
before_action :doorkeeper_authorize! # before_action :doorkeeper_authorize!
skip_before_action :user_setup # skip_before_action :user_setup
protected protected
def current_user # def current_user
User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token # #client方法对接需要一直带着用户标识uid
end # Rails.logger.info doorkeeper_token
# if doorkeeper_token && doorkeeper_token.resource_owner_id.blank?
# # return User.anonymous if params[:uid].nil?
# # tip_exception("2222")
# # return render_error('缺少用户标识!') if params[:uid].nil?
# User.current = User.find(params[:uid])
# else
# User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
# end
# end
def require_manager_above def require_manager_above
@project = load_project @project = load_project

View File

@ -170,10 +170,6 @@ class ApplicationController < ActionController::Base
# 未授权的捕捉407弹试用申请弹框 # 未授权的捕捉407弹试用申请弹框
def require_login def require_login
#6.13 -hs #6.13 -hs
if request.headers["Authorization"].present?
tip_exception(401, "请登录后再操作!") unless valid_doorkeeper_token?
User.current = User.find_by(id: @doorkeeper_token.resource_owner_id) if @doorkeeper_token.present?
end
tip_exception(401, "请登录后再操作") unless User.current.logged? tip_exception(401, "请登录后再操作") unless User.current.logged?
end end
@ -252,42 +248,55 @@ class ApplicationController < ActionController::Base
#return if params[:controller] == "main" #return if params[:controller] == "main"
# Find the current user # Find the current user
#Rails.logger.info("current_laboratory is #{current_laboratory} domain is #{request.subdomain}") #Rails.logger.info("current_laboratory is #{current_laboratory} domain is #{request.subdomain}")
User.current = find_current_user if request.headers["Authorization"].present? && request.headers["Authorization"].start_with?('Bearer')
uid_logger("user_setup: " + (User.current.logged? ? "#{User.current.try(:login)} (id=#{User.current.try(:id)})" : "anonymous")) tip_exception(401, "请登录后再操作!") unless valid_doorkeeper_token?
if @doorkeeper_token.present?
# 开放课程通过链接访问的用户 # client方法对接需要一直带着用户标识uid
if !User.current.logged? && !params[:chinaoocTimestamp].blank? && !params[:websiteName].blank? && !params[:chinaoocKey].blank? if @doorkeeper_token.resource_owner_id.blank?
content = "#{OPENKEY}#{params[:websiteName]}#{params[:chinaoocTimestamp]}" tip_exception(-1, "缺少用户标识!") if params[:uid].nil?
User.current = User.find(params[:uid])
if Digest::MD5.hexdigest(content) == params[:chinaoocKey] else
user = open_class_user User.current = User.find_by(id: @doorkeeper_token.resource_owner_id)
if user
start_user_session(user)
set_autologin_cookie(user)
end end
User.current = user
end end
end else
User.current = find_current_user
uid_logger("user_setup: " + (User.current.logged? ? "#{User.current.try(:login)} (id=#{User.current.try(:id)})" : "anonymous"))
# if !User.current.logged? && Rails.env.development? # 开放课程通过链接访问的用户
# user = User.find 1 if !User.current.logged? && !params[:chinaoocTimestamp].blank? && !params[:websiteName].blank? && !params[:chinaoocKey].blank?
# User.current = user content = "#{OPENKEY}#{params[:websiteName]}#{params[:chinaoocTimestamp]}"
# start_user_session(user)
# end if Digest::MD5.hexdigest(content) == params[:chinaoocKey]
user = open_class_user
if user
start_user_session(user)
set_autologin_cookie(user)
end
User.current = user
end
end
# if !User.current.logged? && Rails.env.development?
# user = User.find 1
# User.current = user
# start_user_session(user)
# end
# 测试版前端需求 # 测试版前端需求
logger.info("subdomain:#{request.subdomain}") logger.info("subdomain:#{request.subdomain}")
if request.subdomain != "www" if request.subdomain != "www"
if params[:debug] == 'teacher' #todo 为了测试,记得讲debug删除 if params[:debug] == 'teacher' #todo 为了测试,记得讲debug删除
User.current = User.find 81403 User.current = User.find 81403
elsif params[:debug] == 'student' elsif params[:debug] == 'student'
User.current = User.find 8686 User.current = User.find 8686
elsif params[:debug] == 'admin' elsif params[:debug] == 'admin'
logger.info "@@@@@@@@@@@@@@@@@@@@@@ debug mode....." logger.info "@@@@@@@@@@@@@@@@@@@@@@ debug mode....."
user = User.find 36480 user = User.find 36480
User.current = user User.current = user
cookies.signed[:user_id] = user.id cookies.signed[:user_id] = user.id
end
end end
end end
# User.current = User.find 81403 # User.current = User.find 81403

View File

@ -0,0 +1,23 @@
class CommitLogsController < ApplicationController
def create
tip_exception "未认证" unless params[:token].to_s == "7917908927b6f1b792f2027a08a8b24a2de42c1692c2fd45da0dee5cf90a5af5"
ref = params[:ref]
commit_id = params[:commits][0][:id]
message = params[:commits][0][:message]
user_name = params[:commits][0][:committer][:username]
user_mail = params[:commits][0][:committer][:email]
user = User.find_by(mail: user_mail)
user = User.find_by(login: user_name) if user.blank?
repository_id = params[:repository][:id]
repository_name = params[:repository][:name]
repository_full_name = params[:repository][:full_name]
project = Project.where(identifier: repository_name).where(user_id: user.id)&.first
project = Project.where(identifier: repository_name).where(gpid: repository_id)&.first if project.blank?
CommitLog.create(user: user, project: project, repository_id: repository_id,
name: repository_name, full_name: repository_full_name,
ref: ref, commit_id: commit_id, message: message)
end
end

6
app/models/commit_log.rb Normal file
View File

@ -0,0 +1,6 @@
class CommitLog < ApplicationRecord
belongs_to :user
belongs_to :project
belongs_to :repository
end

View File

@ -1,6 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
Doorkeeper.configure do Doorkeeper.configure do
base_controller 'ApplicationController'
# Change the ORM that doorkeeper will use (requires ORM extensions installed). # Change the ORM that doorkeeper will use (requires ORM extensions installed).
# Check the list of supported ORMs here: https://github.com/doorkeeper-gem/doorkeeper#orms # Check the list of supported ORMs here: https://github.com/doorkeeper-gem/doorkeeper#orms
orm :active_record orm :active_record
@ -20,7 +21,7 @@ Doorkeeper.configure do
access_token_generator '::Doorkeeper::JWT' access_token_generator '::Doorkeeper::JWT'
admin_authenticator do admin_authenticator do
user = User.find_by_id(session[:www_user_id]) user = current_user
unless user && user.admin_or_business? unless user && user.admin_or_business?
redirect_to root_url redirect_to root_url
end end
@ -513,7 +514,7 @@ Doorkeeper::JWT.configure do
# about the user. Defaults to a randomly generated token in a hash: # about the user. Defaults to a randomly generated token in a hash:
# { token: "RANDOM-TOKEN" } # { token: "RANDOM-TOKEN" }
token_payload do |opts| token_payload do |opts|
user = User.find(opts[:resource_owner_id]) user = User.find_by(id: opts[:resource_owner_id])
{ {
iss: 'GitLink', iss: 'GitLink',
@ -523,9 +524,9 @@ Doorkeeper::JWT.configure do
jti: SecureRandom.uuid, jti: SecureRandom.uuid,
user: { user: {
id: user.id, id: user&.id,
login: user.login, login: user&.login,
mail: user.mail mail: user&.mail
} }
} }
end end

View File

@ -1011,6 +1011,8 @@ Rails.application.routes.draw do
get 'oauth/get_code', to: 'oauth#get_code' get 'oauth/get_code', to: 'oauth#get_code'
get 'oauth/get_token_callback', to: 'oauth#get_token_callback' get 'oauth/get_token_callback', to: 'oauth#get_token_callback'
resources :commit_logs, :only => [:create]
root 'main#index' root 'main#index'

View File

@ -0,0 +1,17 @@
class CreateCommitLogs < ActiveRecord::Migration[5.2]
def change
create_table :commit_logs do |t|
t.references :user
t.references :project
t.integer :repository_id
t.string :name
t.string :full_name
t.string :commit_id
t.string :ref
t.string :message
t.timestamps
end
add_index :commit_logs, :commit_id
end
end