Merge pull request '木兰第三方登录授权登录' (#167) from gua2048/forgeplus:mulanoss_server into mulanoss_server
This commit is contained in:
commit
a721968938
|
@ -0,0 +1,35 @@
|
||||||
|
class Oauth::MulanossController < Oauth::BaseController
|
||||||
|
include RegisterHelper
|
||||||
|
# 需要educoder那边设置回调地址
|
||||||
|
def create
|
||||||
|
# user, new_user = Oauth::CreateOrFindWechatAccountService.call(current_user ,params)
|
||||||
|
|
||||||
|
begin
|
||||||
|
code = params['code'].to_s.strip
|
||||||
|
tip_exception("code不能为空") if code.blank?
|
||||||
|
new_user = false
|
||||||
|
result = MulanossOauth::Service.access_token(code)
|
||||||
|
result = MulanossOauth::Service.user_info(result[:access_token])
|
||||||
|
|
||||||
|
# 存在该用户
|
||||||
|
open_user = OpenUsers::Mulan.find_by(uid: result['id'])
|
||||||
|
if open_user.present? && open_user.user.present?
|
||||||
|
successful_authentication(open_user.user)
|
||||||
|
else
|
||||||
|
if current_user.blank? || !current_user.logged?
|
||||||
|
new_user = true
|
||||||
|
login = User.generate_login('ML')
|
||||||
|
reg_result = autologin_register(login, "#{login}@forge.com" , "M#{login}2021#", 'mulan', true)
|
||||||
|
open_user = OpenUsers::Mulan.create!(user_id: reg_result[:user][:id], uid: result['id'], extra: result)
|
||||||
|
successful_authentication(open_user.user)
|
||||||
|
else
|
||||||
|
OpenUsers::Mulan.create!(user: current_user, uid: result["id"])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
redirect_to "/explore"
|
||||||
|
rescue Exception => ex
|
||||||
|
render_error(ex.message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -43,8 +43,8 @@ class SettingsController < ApplicationController
|
||||||
def get_third_party
|
def get_third_party
|
||||||
@third_party = []
|
@third_party = []
|
||||||
@third_party << {
|
@third_party << {
|
||||||
name: 'educoder',
|
name: 'mulan',
|
||||||
url: EducoderOauth.oauth_url
|
url: MulanossOauth.oauth_url
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
module MulanossOauth
|
||||||
|
class << self
|
||||||
|
attr_accessor :client_id, :client_secret, :base_url, :redirect_uri
|
||||||
|
|
||||||
|
def logger
|
||||||
|
@_logger ||= STDOUT
|
||||||
|
end
|
||||||
|
|
||||||
|
def logger=(l)
|
||||||
|
@_logger = l
|
||||||
|
end
|
||||||
|
|
||||||
|
def oauth_url
|
||||||
|
"#{base_url}/action/oauth2/authorize?client_id=#{client_id}&redirect_uri=#{URI.encode_www_form_component(redirect_uri)}&response_type=code"
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,38 @@
|
||||||
|
require 'oauth2'
|
||||||
|
|
||||||
|
module MulanossOauth::Service
|
||||||
|
module_function
|
||||||
|
|
||||||
|
def request(method, url, params)
|
||||||
|
begin
|
||||||
|
Rails.logger.info("[MulanossOauth] [#{method.to_s.upcase}] #{url} || #{params}")
|
||||||
|
|
||||||
|
client = Faraday.new(url: MulanossOauth.base_url)
|
||||||
|
response = client.public_send(method, url, params)
|
||||||
|
result = JSON.parse(response.body)
|
||||||
|
|
||||||
|
Rails.logger.info("[MulanossOauth] [#{response.status}] #{result}")
|
||||||
|
|
||||||
|
result
|
||||||
|
rescue Exception => e
|
||||||
|
raise Educoder::TipException.new(e.message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def access_token(code)
|
||||||
|
begin
|
||||||
|
Rails.logger.info("[MulanossOauth] [code] #{code} ")
|
||||||
|
Rails.logger.info("[MulanossOauth] [redirect_uri] #{MulanossOauth.redirect_uri} ")
|
||||||
|
client = OAuth2::Client.new(MulanossOauth.client_id, MulanossOauth.client_secret, site: MulanossOauth.base_url, token_url:"/action/openapi/token")
|
||||||
|
result = client.auth_code.get_token(code, redirect_uri: MulanossOauth.redirect_uri).to_hash
|
||||||
|
Rails.logger.info("[MulanossOauth] [GetToken] #{result[:access_token]}")
|
||||||
|
return result
|
||||||
|
rescue Exception => e
|
||||||
|
raise Educoder::TipException.new(e.message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def user_info(access_token)
|
||||||
|
request(:get, '/action/openapi/user', {access_token: access_token})
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,27 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: open_users
|
||||||
|
#
|
||||||
|
# id :integer not null, primary key
|
||||||
|
# user_id :integer
|
||||||
|
# type :string(255)
|
||||||
|
# uid :string(255)
|
||||||
|
# created_at :datetime not null
|
||||||
|
# updated_at :datetime not null
|
||||||
|
# extra :text(65535)
|
||||||
|
#
|
||||||
|
# Indexes
|
||||||
|
#
|
||||||
|
# index_open_users_on_type_and_uid (type,uid) UNIQUE
|
||||||
|
# index_open_users_on_user_id (user_id)
|
||||||
|
#
|
||||||
|
|
||||||
|
class OpenUsers::Mulan < OpenUser
|
||||||
|
def nickname
|
||||||
|
extra&.[]('name')
|
||||||
|
end
|
||||||
|
|
||||||
|
def en_type
|
||||||
|
'mulan'
|
||||||
|
end
|
||||||
|
end
|
|
@ -114,7 +114,7 @@ class User < Owner
|
||||||
# trustie: 来自Trustie平台
|
# trustie: 来自Trustie平台
|
||||||
# forge: 平台本身注册的用户
|
# forge: 平台本身注册的用户
|
||||||
# military: 军科的用户
|
# military: 军科的用户
|
||||||
enumerize :platform, in: [:forge, :educoder, :trustie, :military], default: :forge, scope: :shallow
|
enumerize :platform, in: [:forge, :educoder, :trustie, :military, :mulan], default: :forge, scope: :shallow
|
||||||
|
|
||||||
belongs_to :laboratory, optional: true
|
belongs_to :laboratory, optional: true
|
||||||
has_many :composes, dependent: :destroy
|
has_many :composes, dependent: :destroy
|
||||||
|
|
|
@ -25,6 +25,7 @@ json.issues do
|
||||||
|
|
||||||
json.id issue.id
|
json.id issue.id
|
||||||
json.name issue.subject
|
json.name issue.subject
|
||||||
|
json.description issue.description
|
||||||
json.pr_time time_from_now(pr.status == 1 ? pr.updated_at : issue.updated_on)
|
json.pr_time time_from_now(pr.status == 1 ? pr.updated_at : issue.updated_on)
|
||||||
json.assign_user_name issue.get_assign_user.try(:show_real_name)
|
json.assign_user_name issue.get_assign_user.try(:show_real_name)
|
||||||
json.assign_user_login issue.get_assign_user.try(:login)
|
json.assign_user_login issue.get_assign_user.try(:login)
|
||||||
|
@ -37,4 +38,3 @@ json.issues do
|
||||||
json.issue_tags issue.get_issue_tags
|
json.issue_tags issue.get_issue_tags
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
oauth_config = {}
|
||||||
|
begin
|
||||||
|
config = Rails.application.config_for(:configuration)
|
||||||
|
oauth_config = config.dig('oauth', 'mulan')
|
||||||
|
raise 'oauth educoder config missing' if oauth_config.blank?
|
||||||
|
rescue => ex
|
||||||
|
raise ex if Rails.env.production?
|
||||||
|
|
||||||
|
puts %Q{\033[33m [warning] wechat oauth config or configuration.yml missing,
|
||||||
|
please add it or execute 'cp config/configuration.yml.example config/configuration.yml' \033[0m}
|
||||||
|
end
|
||||||
|
|
||||||
|
MulanossOauth.client_id = oauth_config['client_id']
|
||||||
|
MulanossOauth.client_secret = oauth_config['client_secret']
|
||||||
|
MulanossOauth.base_url = oauth_config['base_url']
|
||||||
|
MulanossOauth.redirect_uri = oauth_config['redirect_uri']
|
Loading…
Reference in New Issue