From 0d97d7d4f0ac5c19a3b0cd4136bb205f8a076730 Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Thu, 1 Apr 2021 18:01:51 +0800 Subject: [PATCH 01/12] add: educoder third party --- app/controllers/oauth/base_controller.rb | 22 ++++++++++++ app/controllers/oauth/educoder_controller.rb | 28 +++++++++++++++ app/controllers/settings_controller.rb | 10 +++++- app/libs/educoder_oauth.rb | 18 ++++++++++ app/libs/educoder_oauth/service.rb | 37 ++++++++++++++++++++ app/views/settings/show.json.jbuilder | 1 + config/configuration.yml.example | 4 +++ config/initializers/educoder_oauth_init.rb | 15 ++++++++ config/routes.rb | 1 + 9 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 app/libs/educoder_oauth.rb create mode 100644 app/libs/educoder_oauth/service.rb create mode 100644 config/initializers/educoder_oauth_init.rb diff --git a/app/controllers/oauth/base_controller.rb b/app/controllers/oauth/base_controller.rb index 3fe349bb5..6956c9ce9 100644 --- a/app/controllers/oauth/base_controller.rb +++ b/app/controllers/oauth/base_controller.rb @@ -2,6 +2,7 @@ class Oauth::BaseController < ActionController::Base include RenderHelper include LoginHelper include ControllerRescueHandler + include LoggerHelper # include LaboratoryHelper skip_before_action :verify_authenticity_token @@ -11,6 +12,18 @@ class Oauth::BaseController < ActionController::Base end private + def tip_exception(status = -1, message) + raise Educoder::TipException.new(status, message) + end + + def tip_show_exception(status = -2, message) + raise Educoder::TipException.new(status, message) + end + + def tip_show(exception) + uid_logger("Tip show status is #{exception.status}, message is #{exception.message}") + render json: exception.tip_json + end def session_user_id # session[:user_id] @@ -48,4 +61,13 @@ class Oauth::BaseController < ActionController::Base Rails.logger.info("[wechat] set session unionid: #{unionid}") session[:unionid] = unionid end + + def session_edulogin + session[:edulogin] + end + + def set_session_edulogin(login) + Rails.logger.info("[educoder] set sesstion edulogin: #{login}") + session[:edulogin] = login + end end diff --git a/app/controllers/oauth/educoder_controller.rb b/app/controllers/oauth/educoder_controller.rb index 8ed537d6c..cc9ad8966 100644 --- a/app/controllers/oauth/educoder_controller.rb +++ b/app/controllers/oauth/educoder_controller.rb @@ -32,4 +32,32 @@ class Oauth::EducoderController < Oauth::BaseController render_error(ex.message) end end + + # 需要educoder那边设置回调地址 + def create + begin + code = params['code'].to_s.strip + tip_exception("code不能为空") if code.blank? + new_user = false + result = EducoderOauth::Service.access_token(code, [request.protocol, request.host_with_port, '/api/auth/educoder/callback'].join('')) + result = EducoderOauth::Service.user_info(result[:access_token]) + + # 存在该用户 + open_user = OpenUsers::Educoder.find_by(uid: result['login']) + if open_user.present? && open_user.user.present? + successful_authentication(open_user.user) + else + if current_user.blank? || !current_user.logged? + new_user = true + set_session_edulogin(result['login']) + else + OpenUsers::Educoder.create!(user: current_user, uid: result['login'], extra: result) + end + end + + render_ok(new_user: new_user) + rescue Exception => ex + render_error(ex.message) + end + end end diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index b6662f661..eab27636c 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -4,7 +4,7 @@ class SettingsController < ApplicationController get_add_menu get_common_menu get_personal_menu - + get_third_party end private @@ -40,6 +40,14 @@ class SettingsController < ApplicationController end end + def get_third_party + @third_party = [] + @third_party << { + name: 'educoder', + url: EducoderOauth.oauth_url([request.protocol, request.host_with_port, '/api/auth/educoder/callback'].join('')) + } + end + def get_site_url(key, value) key.to_s === "url" ? append_http(reset_site_url(value)) : reset_site_url(value) end diff --git a/app/libs/educoder_oauth.rb b/app/libs/educoder_oauth.rb new file mode 100644 index 000000000..3fa06dcde --- /dev/null +++ b/app/libs/educoder_oauth.rb @@ -0,0 +1,18 @@ +module EducoderOauth + class << self + attr_accessor :client_id, :client_secret, :base_url + + def logger + @_logger ||= STDOUT + end + + def logger=(l) + @_logger = l + end + + def oauth_url(redirect_uri) + "#{base_url}/oauth2?call_url=/oauth/authorize?client_id=#{client_id}&redirect_uri=#{URI.encode_www_form_component(redirect_uri)}&response_type=code" + end + + end +end \ No newline at end of file diff --git a/app/libs/educoder_oauth/service.rb b/app/libs/educoder_oauth/service.rb new file mode 100644 index 000000000..29f5f1ebb --- /dev/null +++ b/app/libs/educoder_oauth/service.rb @@ -0,0 +1,37 @@ +require 'oauth2' + +module EducoderOauth::Service + module_function + + def request(method, url, params) + begin + Rails.logger.info("[EducoderOauth] [#{method.to_s.upcase}] #{url} || #{params}") + + client = Faraday.new(url: EducoderOauth.base_url) + response = client.public_send(method, url, params) + result = JSON.parse(response.body) + + Rails.logger.info("[EducoderOauth] [#{response.status}] #{result}") + + result + rescue Exception => e + raise Educoder::TipException.new(e.message) + end + end + + def access_token(code, redirect_uri) + begin + Rails.logger.info("[EducoderOauth] [code] #{code} ") + Rails.logger.info("[EducoderOauth] [redirect_uri] #{redirect_uri} ") + client = OAuth2::Client.new(EducoderOauth.client_id, EducoderOauth.client_secret, site: EducoderOauth.base_url) + result = client.auth_code.get_token(code, redirect_uri: redirect_uri).to_hash + return result + rescue Exception => e + raise Educoder::TipException.new(e.message) + end + end + + def user_info(access_token) + request(:get, '/api/users/info.json', {access_token: access_token}) + end +end \ No newline at end of file diff --git a/app/views/settings/show.json.jbuilder b/app/views/settings/show.json.jbuilder index 330966aa1..ea42f20b4 100644 --- a/app/views/settings/show.json.jbuilder +++ b/app/views/settings/show.json.jbuilder @@ -56,4 +56,5 @@ json.setting do end json.common @common + json.third_party @third_party end diff --git a/config/configuration.yml.example b/config/configuration.yml.example index 823d8547e..1bb25ec39 100644 --- a/config/configuration.yml.example +++ b/config/configuration.yml.example @@ -43,6 +43,10 @@ default: &default cate_id: '-1' callback_url: 'callback_url' signature_key: 'test12345678' + educoder: + client_id: 'e9ce4d5ba1698d6f7d01d8ee2959776c7a6d743ebe94da2341e288fd2fbf60aa' + client_secret: '6ff84dd75eddd859c5bd0e7a791b58bc5ad1ba4fbb30bc9db37cb0baf9f33012' + base_url: 'https://test-data.educoder.net' gitea: access_key_id: 'root' diff --git a/config/initializers/educoder_oauth_init.rb b/config/initializers/educoder_oauth_init.rb new file mode 100644 index 000000000..1ef46bc95 --- /dev/null +++ b/config/initializers/educoder_oauth_init.rb @@ -0,0 +1,15 @@ +oauth_config = {} +begin + config = Rails.application.config_for(:configuration) + oauth_config = config.dig('oauth', 'educoder') + 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 + +EducoderOauth.client_id = oauth_config['client_id'] +EducoderOauth.client_secret = oauth_config['client_secret'] +EducoderOauth.base_url = oauth_config['base_url'] diff --git a/config/routes.rb b/config/routes.rb index daef12bb4..fec562ace 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -312,6 +312,7 @@ Rails.application.routes.draw do get '/auth/qq/callback', to: 'oauth/qq#create' get '/auth/wechat/callback', to: 'oauth/wechat#create' + get '/auth/educoder/callback', to: 'oauth/educoder#create' resource :bind_user, only: [:create] resources :hot_keywords, only: [:index] From f732b0374cc8b551b629f3a37de6754bbd532737 Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Fri, 2 Apr 2021 09:23:11 +0800 Subject: [PATCH 02/12] fix: ceshi --- app/controllers/oauth/educoder_controller.rb | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/app/controllers/oauth/educoder_controller.rb b/app/controllers/oauth/educoder_controller.rb index cc9ad8966..a79a3dd03 100644 --- a/app/controllers/oauth/educoder_controller.rb +++ b/app/controllers/oauth/educoder_controller.rb @@ -1,4 +1,6 @@ class Oauth::EducoderController < Oauth::BaseController + include RegisterHelper + def bind begin login = params[:login] @@ -38,8 +40,13 @@ class Oauth::EducoderController < Oauth::BaseController begin code = params['code'].to_s.strip tip_exception("code不能为空") if code.blank? + redirect_uri = [request.protocol, request.host_with_port, '/api/auth/educoder/callback'].join('') + Rails.logger.info redirect_uri + redirect_uri = 'https://testforgeplus.trustie.net/api/auth/educoder/callback' + Rails.logger.info redirect_uri + new_user = false - result = EducoderOauth::Service.access_token(code, [request.protocol, request.host_with_port, '/api/auth/educoder/callback'].join('')) + result = EducoderOauth::Service.access_token(code, redirect_uri) result = EducoderOauth::Service.user_info(result[:access_token]) # 存在该用户 @@ -49,13 +56,20 @@ class Oauth::EducoderController < Oauth::BaseController else if current_user.blank? || !current_user.logged? new_user = true - set_session_edulogin(result['login']) + login = User.generate_login('E') + reg_result = autologin_register(login,"#{login}@forge.com", "Ec#{login}2021#", 'educoder') + if reg_result[:message].blank? + open_user = OpenUsers::Educoder.create!(user_id: reg_result[:user][:id], uid: result['login'], extra: result) + successful_authentication(open_user.user) + else + render_error(reg_result[:message]) + end else OpenUsers::Educoder.create!(user: current_user, uid: result['login'], extra: result) end end - render_ok(new_user: new_user) + redirect_to root_path(new_user: new_user) rescue Exception => ex render_error(ex.message) end From 1153bd5ab66f11e4b61e00d26103b6646e86f880 Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Fri, 2 Apr 2021 09:43:18 +0800 Subject: [PATCH 03/12] fix: set redirect_uri --- app/controllers/oauth/educoder_controller.rb | 6 +----- app/controllers/settings_controller.rb | 2 +- app/libs/educoder_oauth.rb | 4 ++-- app/libs/educoder_oauth/service.rb | 6 +++--- config/configuration.yml.example | 1 + config/initializers/educoder_oauth_init.rb | 1 + 6 files changed, 9 insertions(+), 11 deletions(-) diff --git a/app/controllers/oauth/educoder_controller.rb b/app/controllers/oauth/educoder_controller.rb index a79a3dd03..685e245c0 100644 --- a/app/controllers/oauth/educoder_controller.rb +++ b/app/controllers/oauth/educoder_controller.rb @@ -40,13 +40,9 @@ class Oauth::EducoderController < Oauth::BaseController begin code = params['code'].to_s.strip tip_exception("code不能为空") if code.blank? - redirect_uri = [request.protocol, request.host_with_port, '/api/auth/educoder/callback'].join('') - Rails.logger.info redirect_uri - redirect_uri = 'https://testforgeplus.trustie.net/api/auth/educoder/callback' - Rails.logger.info redirect_uri new_user = false - result = EducoderOauth::Service.access_token(code, redirect_uri) + result = EducoderOauth::Service.access_token(code) result = EducoderOauth::Service.user_info(result[:access_token]) # 存在该用户 diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index eab27636c..b03edb1fc 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -44,7 +44,7 @@ class SettingsController < ApplicationController @third_party = [] @third_party << { name: 'educoder', - url: EducoderOauth.oauth_url([request.protocol, request.host_with_port, '/api/auth/educoder/callback'].join('')) + url: EducoderOauth.oauth_url } end diff --git a/app/libs/educoder_oauth.rb b/app/libs/educoder_oauth.rb index 3fa06dcde..0c3fb61bf 100644 --- a/app/libs/educoder_oauth.rb +++ b/app/libs/educoder_oauth.rb @@ -1,6 +1,6 @@ module EducoderOauth class << self - attr_accessor :client_id, :client_secret, :base_url + attr_accessor :client_id, :client_secret, :base_url, :redirect_uri def logger @_logger ||= STDOUT @@ -10,7 +10,7 @@ module EducoderOauth @_logger = l end - def oauth_url(redirect_uri) + def oauth_url "#{base_url}/oauth2?call_url=/oauth/authorize?client_id=#{client_id}&redirect_uri=#{URI.encode_www_form_component(redirect_uri)}&response_type=code" end diff --git a/app/libs/educoder_oauth/service.rb b/app/libs/educoder_oauth/service.rb index 29f5f1ebb..9d93d314d 100644 --- a/app/libs/educoder_oauth/service.rb +++ b/app/libs/educoder_oauth/service.rb @@ -19,12 +19,12 @@ module EducoderOauth::Service end end - def access_token(code, redirect_uri) + def access_token(code) begin Rails.logger.info("[EducoderOauth] [code] #{code} ") - Rails.logger.info("[EducoderOauth] [redirect_uri] #{redirect_uri} ") + Rails.logger.info("[EducoderOauth] [redirect_uri] #{EducoderOauth.redirect_uri} ") client = OAuth2::Client.new(EducoderOauth.client_id, EducoderOauth.client_secret, site: EducoderOauth.base_url) - result = client.auth_code.get_token(code, redirect_uri: redirect_uri).to_hash + result = client.auth_code.get_token(code, redirect_uri: EducoderOauth.redirect_uri).to_hash return result rescue Exception => e raise Educoder::TipException.new(e.message) diff --git a/config/configuration.yml.example b/config/configuration.yml.example index 1bb25ec39..3c2d90b77 100644 --- a/config/configuration.yml.example +++ b/config/configuration.yml.example @@ -47,6 +47,7 @@ default: &default client_id: 'e9ce4d5ba1698d6f7d01d8ee2959776c7a6d743ebe94da2341e288fd2fbf60aa' client_secret: '6ff84dd75eddd859c5bd0e7a791b58bc5ad1ba4fbb30bc9db37cb0baf9f33012' base_url: 'https://test-data.educoder.net' + redirect_uri: 'https://testforgeplus.trustie.net/api/auth/educoder/callback' gitea: access_key_id: 'root' diff --git a/config/initializers/educoder_oauth_init.rb b/config/initializers/educoder_oauth_init.rb index 1ef46bc95..0f47cc7ce 100644 --- a/config/initializers/educoder_oauth_init.rb +++ b/config/initializers/educoder_oauth_init.rb @@ -13,3 +13,4 @@ end EducoderOauth.client_id = oauth_config['client_id'] EducoderOauth.client_secret = oauth_config['client_secret'] EducoderOauth.base_url = oauth_config['base_url'] +EducoderOauth.redirect_uri = oauth_config['redirect_uri'] From c4a43c439237cba47c8a5555c54294fda57fa15b Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Fri, 2 Apr 2021 16:44:36 +0800 Subject: [PATCH 04/12] add: need edit user password --- app/controllers/application_controller.rb | 1 - app/controllers/concerns/register_helper.rb | 8 ++++++-- app/controllers/oauth/educoder_controller.rb | 2 +- app/models/user.rb | 15 ++++++++++++++- app/services/projects/fork_service.rb | 2 ++ app/views/users/get_user_info.json.jbuilder | 1 + 6 files changed, 24 insertions(+), 5 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ee81765a5..14892ce7a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -329,7 +329,6 @@ class ApplicationController < ActionController::Base User.current = user end end - # if !User.current.logged? && Rails.env.development? # User.current = User.find 1 # end diff --git a/app/controllers/concerns/register_helper.rb b/app/controllers/concerns/register_helper.rb index 3a23a2103..f40872905 100644 --- a/app/controllers/concerns/register_helper.rb +++ b/app/controllers/concerns/register_helper.rb @@ -1,13 +1,17 @@ module RegisterHelper extend ActiveSupport::Concern - def autologin_register(username, email, password, platform= 'forge') + def autologin_register(username, email, password, platform= 'forge', need_edit_password = false) result = {message: nil, user: nil} user = User.new(admin: false, login: username, mail: email, type: "User") user.password = password user.platform = platform - user.activate + if need_edit_password + user.need_edit_password + else + user.activate + end return unless user.valid? diff --git a/app/controllers/oauth/educoder_controller.rb b/app/controllers/oauth/educoder_controller.rb index 685e245c0..51b0e7a86 100644 --- a/app/controllers/oauth/educoder_controller.rb +++ b/app/controllers/oauth/educoder_controller.rb @@ -53,7 +53,7 @@ class Oauth::EducoderController < Oauth::BaseController if current_user.blank? || !current_user.logged? new_user = true login = User.generate_login('E') - reg_result = autologin_register(login,"#{login}@forge.com", "Ec#{login}2021#", 'educoder') + reg_result = autologin_register(login,"#{login}@forge.com", "Ec#{login}2021#", 'educoder', true) if reg_result[:message].blank? open_user = OpenUsers::Educoder.create!(user_id: reg_result[:user][:id], uid: result['login'], extra: result) successful_authentication(open_user.user) diff --git a/app/models/user.rb b/app/models/user.rb index 5bfe3b6e6..5d03498c1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -79,6 +79,7 @@ class User < Owner STATUS_ACTIVE = 1 STATUS_REGISTERED = 2 STATUS_LOCKED = 3 + STATUS_EDIT_PASSWORD = 4 # tpi tpm权限控制 EDU_ADMIN = 1 # 超级管理员 @@ -161,7 +162,7 @@ class User < Owner has_many :organizations, through: :organization_users # Groups and active users - scope :active, lambda { where(status: STATUS_ACTIVE) } + scope :active, lambda { where(status: [STATUS_ACTIVE, STATUS_EDIT_PASSWORD]) } scope :like, lambda { |keywords| where("LOWER(concat(lastname, firstname, login, mail)) LIKE ?", "%#{keywords.split(" ").join('|')}%") unless keywords.blank? } @@ -378,6 +379,10 @@ class User < Owner status == STATUS_LOCKED end + def need_edit_password? + status == STATUS_EDIT_PASSWORD + end + def activate self.status = STATUS_ACTIVE end @@ -390,6 +395,10 @@ class User < Owner self.status = STATUS_LOCKED end + def need_edit_password + self.status = STATUS_EDIT_PASSWORD + end + def activate! update_attribute(:status, STATUS_ACTIVE) end @@ -402,6 +411,10 @@ class User < Owner update_attribute(:status, STATUS_LOCKED) end + def need_edit_password! + update_attribute(:status, STATUS_LOCKED) + end + # 课程用户身份 def course_identity(course) if !logged? diff --git a/app/services/projects/fork_service.rb b/app/services/projects/fork_service.rb index 3204e5fc7..8ae787c52 100644 --- a/app/services/projects/fork_service.rb +++ b/app/services/projects/fork_service.rb @@ -24,6 +24,8 @@ class Projects::ForkService < ApplicationService new_repository.identifier = @project.identifier new_repository.save! + ProjectUnit.init_types(clone_project.id) + result = Gitea::Repository::ForkService.new(@project.owner, @target_owner, @project.identifier, @organization).call @project.update_column('forked_count', @project&.forked_count.to_i + 1) diff --git a/app/views/users/get_user_info.json.jbuilder b/app/views/users/get_user_info.json.jbuilder index d9aa44025..be11d6c0e 100644 --- a/app/views/users/get_user_info.json.jbuilder +++ b/app/views/users/get_user_info.json.jbuilder @@ -8,6 +8,7 @@ json.is_teacher @user.user_extension&.teacher? json.user_identity @user.identity json.tidding_count 0 json.user_phone_binded @user.phone.present? +json.need_edit_password @user.need_edit_password? # json.phone @user.phone # json.email @user.mail json.profile_completed @user.profile_completed? From fba58800a788d1a1d2098434381eeaacec331fc5 Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Fri, 2 Apr 2021 18:08:36 +0800 Subject: [PATCH 05/12] fix --- app/controllers/application_controller.rb | 3 ++ app/controllers/concerns/register_helper.rb | 32 ++++++++++++++++-- app/controllers/oauth/educoder_controller.rb | 1 + app/models/token.rb | 34 ++++++++++---------- app/models/user.rb | 16 ++++----- app/views/users/get_user_info.json.jbuilder | 4 +-- 6 files changed, 60 insertions(+), 30 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 14892ce7a..71c50fed1 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -357,7 +357,10 @@ class ApplicationController < ActionController::Base uid_logger("user setup start: session[:user_id] is #{session[:user_id]}") uid_logger("0000000000000user setup start: default_yun_session is #{default_yun_session}, session[:current_user_id] is #{session[:"#{default_yun_session}"]}") current_domain_session = session[:"#{default_yun_session}"] + Rails.logger.info "#{session[:user_id]}===============" + if current_domain_session + Rails.logger.info "#{current_domain_session}===============" # existing session User.current = (User.active.find(current_domain_session) rescue nil) elsif autologin_user = try_to_autologin diff --git a/app/controllers/concerns/register_helper.rb b/app/controllers/concerns/register_helper.rb index f40872905..35c1adea4 100644 --- a/app/controllers/concerns/register_helper.rb +++ b/app/controllers/concerns/register_helper.rb @@ -1,14 +1,14 @@ module RegisterHelper extend ActiveSupport::Concern - def autologin_register(username, email, password, platform= 'forge', need_edit_password = false) + def autologin_register(username, email, password, platform= 'forge', need_edit_info = false) result = {message: nil, user: nil} user = User.new(admin: false, login: username, mail: email, type: "User") user.password = password user.platform = platform - if need_edit_password - user.need_edit_password + if need_edit_info + user.need_edit_info else user.activate end @@ -31,4 +31,30 @@ module RegisterHelper result end + def autosync_register_trustie(username, password, email) + config = Rails.application.config_for(:configuration).symbolize_keys! + + api_host = config[:sync_url] + + return if api_host.blank? + + url = "#{api_host}/api/v1/users" + sync_json = { + "mail": email, + "password": password, + "login": username + } + uri = URI.parse(url) + + if api_host + http = Net::HTTP.new(uri.hostname, uri.port) + + if api_host.include?("https://") + http.use_ssl = true + end + + http.send_request('POST', uri.path, sync_json.to_json, {'Content-Type' => 'application/json'}) + end + end + end diff --git a/app/controllers/oauth/educoder_controller.rb b/app/controllers/oauth/educoder_controller.rb index 51b0e7a86..9ca4ae49b 100644 --- a/app/controllers/oauth/educoder_controller.rb +++ b/app/controllers/oauth/educoder_controller.rb @@ -56,6 +56,7 @@ class Oauth::EducoderController < Oauth::BaseController reg_result = autologin_register(login,"#{login}@forge.com", "Ec#{login}2021#", 'educoder', true) if reg_result[:message].blank? open_user = OpenUsers::Educoder.create!(user_id: reg_result[:user][:id], uid: result['login'], extra: result) + autosync_register_trustie(login, "Ec#{login}2021#", "#{login}@forge.com") successful_authentication(open_user.user) else render_error(reg_result[:message]) diff --git a/app/models/token.rb b/app/models/token.rb index c71a860af..db778c6b8 100644 --- a/app/models/token.rb +++ b/app/models/token.rb @@ -1,19 +1,19 @@ -# == Schema Information -# -# Table name: tokens -# -# id :integer not null, primary key -# user_id :integer default("0"), not null -# action :string(30) default(""), not null -# value :string(40) default(""), not null -# created_on :datetime not null -# -# Indexes -# -# index_tokens_on_user_id (user_id) -# tokens_value (value) UNIQUE -# - +# == Schema Information +# +# Table name: tokens +# +# id :integer not null, primary key +# user_id :integer default("0"), not null +# action :string(30) default(""), not null +# value :string(40) default(""), not null +# created_on :datetime not null +# +# Indexes +# +# index_tokens_on_user_id (user_id) +# tokens_value (value) UNIQUE +# + # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -76,7 +76,7 @@ class Token < ActiveRecord::Base # Returns the active user who owns the key for the given action def self.find_active_user(action, key, validity_days=nil) user = find_user(action, key, validity_days) - if user && user.active? + if user && (user.active? || user.need_edit_info?) user end end diff --git a/app/models/user.rb b/app/models/user.rb index 5d03498c1..5559da088 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -79,7 +79,7 @@ class User < Owner STATUS_ACTIVE = 1 STATUS_REGISTERED = 2 STATUS_LOCKED = 3 - STATUS_EDIT_PASSWORD = 4 + STATUS_EDIT_INFO = 4 # tpi tpm权限控制 EDU_ADMIN = 1 # 超级管理员 @@ -162,7 +162,7 @@ class User < Owner has_many :organizations, through: :organization_users # Groups and active users - scope :active, lambda { where(status: [STATUS_ACTIVE, STATUS_EDIT_PASSWORD]) } + scope :active, lambda { where(status: [STATUS_ACTIVE, STATUS_EDIT_INFO]) } scope :like, lambda { |keywords| where("LOWER(concat(lastname, firstname, login, mail)) LIKE ?", "%#{keywords.split(" ").join('|')}%") unless keywords.blank? } @@ -379,8 +379,8 @@ class User < Owner status == STATUS_LOCKED end - def need_edit_password? - status == STATUS_EDIT_PASSWORD + def need_edit_info? + status == STATUS_EDIT_INFO end def activate @@ -395,8 +395,8 @@ class User < Owner self.status = STATUS_LOCKED end - def need_edit_password - self.status = STATUS_EDIT_PASSWORD + def need_edit_info + self.status = STATUS_EDIT_INFO end def activate! @@ -411,8 +411,8 @@ class User < Owner update_attribute(:status, STATUS_LOCKED) end - def need_edit_password! - update_attribute(:status, STATUS_LOCKED) + def need_edit_info! + update_attribute(:status, STATUS_EDIT_INFO) end # 课程用户身份 diff --git a/app/views/users/get_user_info.json.jbuilder b/app/views/users/get_user_info.json.jbuilder index be11d6c0e..908801dad 100644 --- a/app/views/users/get_user_info.json.jbuilder +++ b/app/views/users/get_user_info.json.jbuilder @@ -8,9 +8,9 @@ json.is_teacher @user.user_extension&.teacher? json.user_identity @user.identity json.tidding_count 0 json.user_phone_binded @user.phone.present? -json.need_edit_password @user.need_edit_password? +json.need_edit_info @user.need_edit_info? # json.phone @user.phone -# json.email @user.mail +json.email @user.mail json.profile_completed @user.profile_completed? json.professional_certification @user.professional_certification json.devops_step @user.devops_step From 50fb8d39839cc74de71c9f46f7008b5864466c46 Mon Sep 17 00:00:00 2001 From: jasder Date: Fri, 2 Apr 2021 18:18:52 +0800 Subject: [PATCH 06/12] ADD sync user info api --- app/controllers/users_controller.rb | 22 +++++++++++++++++++++- config/routes.rb | 5 +++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 6c5c10fb3..ca0988ea0 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -4,7 +4,7 @@ class UsersController < ApplicationController before_action :load_user, only: [:show, :homepage_info, :sync_token, :sync_gitea_pwd, :projects, :watch_users, :fan_users] before_action :check_user_exist, only: [:show, :homepage_info,:projects, :watch_users, :fan_users] - before_action :require_login, only: %i[me list] + before_action :require_login, only: %i[me list sync_user_info] before_action :connect_to_ci_db, only: [:get_user_info] skip_before_action :check_sign, only: [:attachment_show] @@ -233,6 +233,26 @@ class UsersController < ApplicationController render_ok end + def sync_user_info + user = User.find_by_mail params[:email] + return render_forbidden unless user === current_user + + sync_params = { + email: params[:email], + password: params[:password] + } + + Users::UpdateInfoForm.new(sync_params).validate! + + interactor = Gitea::User::UpdateInteractor.call(user.login, sync_params) + if interactor.success? + user.update!(password: params[:password], mail: params[:email], status: User::STATUS_EDIT_INFO) + render_ok + else + render_error(interactor.error) + end + end + private def load_user @user = User.find_by_login(params[:id]) || User.find_by(id: params[:id]) diff --git a/config/routes.rb b/config/routes.rb index daef12bb4..66c9d97af 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -72,12 +72,12 @@ Rails.application.routes.draw do end resources :statistic, only: [:index] do - collection do + collection do get :platform_profile get :platform_code get :active_project_rank get :active_developer_rank - end + end end resources :sync_forge, only: [:create] do collection do @@ -218,6 +218,7 @@ Rails.application.routes.draw do post :sync_salt get :trustie_projects get :trustie_related_projects + post :sync_user_info scope '/ci', module: :ci do scope do From e022e429d429244710f42660834e65db59a3ee39 Mon Sep 17 00:00:00 2001 From: jasder Date: Fri, 2 Apr 2021 18:22:21 +0800 Subject: [PATCH 07/12] FIX code bug --- app/forms/users/update_info_form.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 app/forms/users/update_info_form.rb diff --git a/app/forms/users/update_info_form.rb b/app/forms/users/update_info_form.rb new file mode 100644 index 000000000..131a6fd93 --- /dev/null +++ b/app/forms/users/update_info_form.rb @@ -0,0 +1,8 @@ +class Users::UpdateInfoForm + include ActiveModel::Model + + attr_accessor :email, :password + + validates :email, presence: true, format: { with: CustomRegexp::EMAIL } + validates :password, presence: true +end From 91f6dae56afa65ea815c3b0232370bf703ed913c Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Fri, 2 Apr 2021 18:24:20 +0800 Subject: [PATCH 08/12] fix --- app/controllers/users_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index ca0988ea0..1fd479c10 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -246,7 +246,7 @@ class UsersController < ApplicationController interactor = Gitea::User::UpdateInteractor.call(user.login, sync_params) if interactor.success? - user.update!(password: params[:password], mail: params[:email], status: User::STATUS_EDIT_INFO) + user.update!(password: params[:password], mail: params[:email], status: User::STATUS_ACTIVE) render_ok else render_error(interactor.error) From 80ce40e49203ea879b8353d20c58928355a63873 Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Fri, 2 Apr 2021 18:33:31 +0800 Subject: [PATCH 09/12] fix --- app/controllers/concerns/register_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/concerns/register_helper.rb b/app/controllers/concerns/register_helper.rb index 35c1adea4..d77a33584 100644 --- a/app/controllers/concerns/register_helper.rb +++ b/app/controllers/concerns/register_helper.rb @@ -38,7 +38,7 @@ module RegisterHelper return if api_host.blank? - url = "#{api_host}/api/v1/users" + url = "#{api_host}/api/v1/users/common" sync_json = { "mail": email, "password": password, From 1d832a172302f0b4d99935641641aaad005acc08 Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Fri, 2 Apr 2021 18:38:08 +0800 Subject: [PATCH 10/12] fix --- app/controllers/application_controller.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 71c50fed1..14892ce7a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -357,10 +357,7 @@ class ApplicationController < ActionController::Base uid_logger("user setup start: session[:user_id] is #{session[:user_id]}") uid_logger("0000000000000user setup start: default_yun_session is #{default_yun_session}, session[:current_user_id] is #{session[:"#{default_yun_session}"]}") current_domain_session = session[:"#{default_yun_session}"] - Rails.logger.info "#{session[:user_id]}===============" - if current_domain_session - Rails.logger.info "#{current_domain_session}===============" # existing session User.current = (User.active.find(current_domain_session) rescue nil) elsif autologin_user = try_to_autologin From 6ce4b2604714d5d97dc75ba023966c942b8b1d28 Mon Sep 17 00:00:00 2001 From: jasder Date: Fri, 2 Apr 2021 18:40:26 +0800 Subject: [PATCH 11/12] FIX api bug --- app/controllers/users_controller.rb | 2 +- app/forms/users/update_info_form.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index ca0988ea0..91258678e 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -234,7 +234,7 @@ class UsersController < ApplicationController end def sync_user_info - user = User.find_by_mail params[:email] + user = User.find_by_login params[:login] return render_forbidden unless user === current_user sync_params = { diff --git a/app/forms/users/update_info_form.rb b/app/forms/users/update_info_form.rb index 131a6fd93..088db06d3 100644 --- a/app/forms/users/update_info_form.rb +++ b/app/forms/users/update_info_form.rb @@ -1,8 +1,9 @@ class Users::UpdateInfoForm include ActiveModel::Model - attr_accessor :email, :password + attr_accessor :email, :password, :login validates :email, presence: true, format: { with: CustomRegexp::EMAIL } validates :password, presence: true + validates :login, presence: true end From 786629dbff67bb59bfde3d6c5454bc76f82fe706 Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Fri, 2 Apr 2021 18:41:48 +0800 Subject: [PATCH 12/12] fix --- app/services/projects/fork_service.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/services/projects/fork_service.rb b/app/services/projects/fork_service.rb index 8ae787c52..3204e5fc7 100644 --- a/app/services/projects/fork_service.rb +++ b/app/services/projects/fork_service.rb @@ -24,8 +24,6 @@ class Projects::ForkService < ApplicationService new_repository.identifier = @project.identifier new_repository.save! - ProjectUnit.init_types(clone_project.id) - result = Gitea::Repository::ForkService.new(@project.owner, @target_owner, @project.identifier, @organization).call @project.update_column('forked_count', @project&.forked_count.to_i + 1)