diff --git a/app/controllers/concerns/laboratory_helper.rb b/app/controllers/concerns/laboratory_helper.rb index b231b848..6103b931 100644 --- a/app/controllers/concerns/laboratory_helper.rb +++ b/app/controllers/concerns/laboratory_helper.rb @@ -41,7 +41,7 @@ module LaboratoryHelper my_courses: "https://www.trustie.net/users/#{current_user.try(:login)}/user_courselist", my_projects: "/users/#{current_user.try(:login)}/projects", my_organ: "https://www.trustie.net/users/#{current_user.try(:login)}/user_organizations", - default_url: "https://www.trustie.net/", + default_url: "/", tiding_url: "https://www.trustie.net/users/#{current_user.try(:login)}/user_messages", register_url: "https://www.trustie.net/login?login=false" } diff --git a/app/controllers/concerns/register_helper.rb b/app/controllers/concerns/register_helper.rb index b5d42322..ef827106 100644 --- a/app/controllers/concerns/register_helper.rb +++ b/app/controllers/concerns/register_helper.rb @@ -28,4 +28,34 @@ module RegisterHelper result end + def ccyun_autologin_register(username, email, password, platform= 'forge', options) + result = {message: nil, user: nil} + user = User.new(admin: false, login: username, mail: email, type: "User") + user.password = password + user.platform = platform + user.nickname = options["nickname"] + user.phone = options["mobile"] + user.lastname = options["name"] + user.activate + + ##puts "register user ===== #{user.valid?}" + + #return unless user.valid? + + interactor = Gitea::RegisterInteractor.call({username: username, email: email, password: password}) + if interactor.success? + gitea_user = interactor.result + result = Gitea::User::GenerateTokenService.call(username, password) + user.gitea_token = result['sha1'] + user.gitea_uid = gitea_user[:body]['id'] + if user.save! + UserExtension.create!(user_id: user.id) + result[:user] = {id: user.id, token: user.gitea_token} + end + else + result[:message] = interactor.error + end + result + end + end diff --git a/app/controllers/oauth/ccyun_controller.rb b/app/controllers/oauth/ccyun_controller.rb index a104efe3..2c2e1113 100644 --- a/app/controllers/oauth/ccyun_controller.rb +++ b/app/controllers/oauth/ccyun_controller.rb @@ -46,35 +46,32 @@ class Oauth::CcyunController < Oauth::BaseController new_user = false result = CcyunOauth::Service.access_token(code) result = CcyunOauth::Service.user_info(result[:access_token]) - uid = result["mobile"] + uid = result["personalId"] # 存在该用户 open_user = OpenUsers::Ccyun.find_by(uid: uid) Rails.logger.info("open_user #{open_user}") if open_user.present? && open_user.user.present? - successful_authentication(open_user.user) + ## 同步用户信息 + finalUser = syncRemoteServerUser(open_user.user, result) + successful_authentication(finalUser) + + else - if current_user.blank? || !current_user.logged? - new_user = true + new_user = true + # 使用XC提供的身份ID进行注册 + # login = result['personalId'].to_s + login = User.generate_login('E') - # 使用XC提供的身份ID进行注册 - # login = result['personalId'].to_s - login = User.generate_login('E') - - # 自动注册到平台 - reg_result = autologin_register(login,"#{login}@forge.com", "Ec#{login}2021#", 'ccyun') + # 自动注册到平台 + reg_result = ccyun_autologin_register(login,"#{login}@forge.com", "Ec#{login}2021#", 'ccyun', result) - Rails.logger.info("reg_result=====> #{reg_result}") - - if reg_result[:message].blank? - open_user = OpenUsers::Ccyun.create!(user_id: reg_result[:user][:id], uid: uid, extra: result) - # autosync_register_trustie(login, "Ec#{login}2021#", "#{login}@forge.com") - successful_authentication(open_user.user) - else - render_error(reg_result[:message]) - end + if reg_result[:message].blank? + open_user = OpenUsers::Ccyun.create!(user_id: reg_result[:user][:id], uid: uid, extra: result) + # autosync_register_trustie(login, "Ec#{login}2021#", "#{login}@forge.com") + successful_authentication(open_user.user) else - OpenUsers::Ccyun.create!(user: current_user, uid: uid, extra: result) + render_error(reg_result[:message]) end end @@ -84,4 +81,9 @@ class Oauth::CcyunController < Oauth::BaseController render_error(ex.message) end end -end + + private + def syncRemoteServerUser(user, options) + User.update(user.id, {:nickname=> options["nickname"], :phone=> options["mobile"], :lastname=> options["name"]}) + end +end \ No newline at end of file diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index 26e88c5e..86401c61 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -13,19 +13,21 @@ class SettingsController < ApplicationController @third_party = [] @third_party << { name: 'ccyun', - url: CcyunOauth.oauth_url + url: CcyunOauth.oauth_url, + profile_url:CcyunOauth.profile_url } end def get_add_menu @add = [] - Site.add.select(:id, :name, :url, :key).to_a.map(&:serializable_hash).each do |site| - hash = {} - site.each {|k, v| - hash.merge!("#{k}": get_site_url(k, v)) - } - @add << hash - end + # Site.add.select(:id, :name, :url, :key).to_a.map(&:serializable_hash).each do |site| + # hash = {} + # site.each {|k, v| + # hash.merge!("#{k}": get_site_url(k, v)) + # } + # @add << hash + # end + @add end def get_common_menu diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ccc45df0..1e4f1b7e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -143,7 +143,16 @@ module ApplicationHelper File.join("images/avatars", ["#{source.class}", "#{source.id}"]) + "?t=#{ctime}" end elsif source.class.to_s == 'User' - source.get_letter_avatar_url + if "ccyun" == source[:platform] + openUser = OpenUser.find_by user_id: source[:id] + img = "https://www.ccyunchina.com/appPortal/assets/img/head.eba2ec4e.jpg" + if openUser&.extra["avatar"] + img = openUser&.extra["avatar"] + end + img + else + source.get_letter_avatar_url + end end end diff --git a/app/libs/ccyun_oauth.rb b/app/libs/ccyun_oauth.rb index 51914894..3f0e5695 100644 --- a/app/libs/ccyun_oauth.rb +++ b/app/libs/ccyun_oauth.rb @@ -1,6 +1,6 @@ module CcyunOauth class << self - attr_accessor :client_id, :client_secret, :base_url, :redirect_uri + attr_accessor :client_id, :client_secret, :base_url, :redirect_uri, :profile_url def logger @_logger ||= STDOUT @@ -11,7 +11,7 @@ module CcyunOauth end def oauth_url - "#{base_url}/oauth/authorize?client_id=#{client_id}&redirect_uri=#{URI.encode_www_form_component(redirect_uri)}&response_type=code&state=1" + "#{base_url}/auth/oauth/authorize?client_id=#{client_id}&redirect_uri=#{URI.encode_www_form_component(redirect_uri)}&response_type=code&state=1" end end diff --git a/app/libs/ccyun_oauth/service.rb b/app/libs/ccyun_oauth/service.rb index be898358..efd16c92 100644 --- a/app/libs/ccyun_oauth/service.rb +++ b/app/libs/ccyun_oauth/service.rb @@ -10,7 +10,7 @@ module CcyunOauth::Service response = Faraday.get(url, params, nil) Rails.logger.info("[CcyunOauth] [response] #{response.body}") result = JSON.parse(response.body) - result + result["data"] rescue Exception => e raise CcyunOauth::Error.new(result['error'], result['error_description']) end @@ -19,7 +19,7 @@ module CcyunOauth::Service def access_token(code) begin Rails.logger.info("[CcyunOauth] [code] #{code} ") - client = OAuth2::Client.new(CcyunOauth.client_id, CcyunOauth.client_secret, site: CcyunOauth.base_url) + client = OAuth2::Client.new(CcyunOauth.client_id, CcyunOauth.client_secret, site: CcyunOauth.base_url, token_url: "/auth/oauth/token") result = client.auth_code.get_token(code, redirect_uri: CcyunOauth.redirect_uri).to_hash return result rescue Exception => e @@ -28,6 +28,6 @@ module CcyunOauth::Service end def user_info(access_token) - request(:get, "#{CcyunOauth.base_url}/user-api/userInfo", {access_token: access_token}) + request(:post, "#{CcyunOauth.base_url}/user-api/user/userInfo", {access_token: access_token}) end end \ No newline at end of file diff --git a/app/models/site.rb b/app/models/site.rb index 8528d7c2..327c9e47 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -15,12 +15,13 @@ class Site < ApplicationRecord # add: 添加类链接 # personal: 个人名下类链接, # common: 普通链接 - enum site_type: { add: 0, personal: 1, common: 2 } + enum site_type: { add: 0, personal: 1, common: 2 , third_party: 3} def self.set_default_menu set_add_menu! set_personal_menu! set_common_menu! + set_third_party! end private @@ -69,4 +70,17 @@ class Site < ApplicationRecord end } end + + def self.set_third_party! + third_partys = [ + ] + + third_partys.each { |ele| + Site.find_or_create_by(key: ele[:key]) do |site| + site.name = ele[:name] + site.url = ele[:url] + site.site_type = Site.site_types[:third_party] + end + } + end end diff --git a/app/views/settings/show.json.jbuilder b/app/views/settings/show.json.jbuilder index 58cd597a..cbe1650e 100644 --- a/app/views/settings/show.json.jbuilder +++ b/app/views/settings/show.json.jbuilder @@ -18,8 +18,8 @@ json.setting do # # end - nav_bar = default_setting.navbar + nav_bar = default_setting.navbar # if User.current.logged? # nav_bar[2]["link"] = "https://forgeplus.trustie.net/users/#{current_user.login}/projects" # nav_bar[2]["hidden"] = false diff --git a/config/initializers/ccyun_oauth_init.rb b/config/initializers/ccyun_oauth_init.rb index 9b846f9b..2f93cc06 100644 --- a/config/initializers/ccyun_oauth_init.rb +++ b/config/initializers/ccyun_oauth_init.rb @@ -14,3 +14,4 @@ CcyunOauth.client_id = oauth_config['client_id'] CcyunOauth.client_secret = oauth_config['client_secret'] CcyunOauth.base_url = oauth_config['base_url'] CcyunOauth.redirect_uri = oauth_config['redirect_uri'] +CcyunOauth.profile_url = oauth_config['profile_url']