From ae2c6627a9bb94ec2db5bc93a1ee281ea1877754 Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Wed, 22 Apr 2020 15:38:57 +0800 Subject: [PATCH 1/6] FIX add is_sync_pwd column to users table --- db/migrate/20200421092002_add_is_sync_pwd_to_users.rb | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 db/migrate/20200421092002_add_is_sync_pwd_to_users.rb diff --git a/db/migrate/20200421092002_add_is_sync_pwd_to_users.rb b/db/migrate/20200421092002_add_is_sync_pwd_to_users.rb new file mode 100644 index 000000000..2a6c20da5 --- /dev/null +++ b/db/migrate/20200421092002_add_is_sync_pwd_to_users.rb @@ -0,0 +1,6 @@ +# TODO 该字段用于trusite用户登录时,同步用户密码到gitea平台, 默认为未同步 +class AddIsSyncPwdToUsers < ActiveRecord::Migration[5.2] + def change + add_column :users, :is_sync_pwd, :boolean, :default => true + end +end From e6396bdefd6634169781e639693de00318d94cc2 Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Wed, 22 Apr 2020 15:39:34 +0800 Subject: [PATCH 2/6] =?UTF-8?q?FIX=20=E5=AE=8C=E5=96=84gitea=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/forms/gitea/user/update_form.rb | 11 +++++ .../gitea/user/update_interactor.rb | 49 +++++++++++++++++++ app/services/gitea/user/update_service.rb | 13 +++-- 3 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 app/forms/gitea/user/update_form.rb create mode 100644 app/interactors/gitea/user/update_interactor.rb diff --git a/app/forms/gitea/user/update_form.rb b/app/forms/gitea/user/update_form.rb new file mode 100644 index 000000000..7912d51fa --- /dev/null +++ b/app/forms/gitea/user/update_form.rb @@ -0,0 +1,11 @@ +class Gitea::User::UpdateForm + include ActiveModel::Model + EMAIL_REGEX = /^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9_\-.]+(\.[a-zA-Z0-9_-]+)+$/ + + attr_accessor :username, :email, :admin, :allow_create_organization, :allow_git_hook, :allow_import_local, + :full_name, :location, :login_name, :max_repo_creation, :must_change_password, :password, :prohibit_login, + :source_id, :website + + validates :username, presence: true + validates :email, presence: true, format: { with: EMAIL_REGEX, multiline: true } +end diff --git a/app/interactors/gitea/user/update_interactor.rb b/app/interactors/gitea/user/update_interactor.rb new file mode 100644 index 000000000..e2e5a715b --- /dev/null +++ b/app/interactors/gitea/user/update_interactor.rb @@ -0,0 +1,49 @@ +module Gitea::User + class UpdateInteractor + def self.call(username, params={}) + interactor = new(username, params) + interactor.run + interactor + end + + attr_reader :error, :result + + def initialize(username, params) + @username = username + @params = params + end + + def success? + @error.nil? + end + + def result + @result + end + + def run + Gitea::User::UpdateForm.new(valid_params).validate! + response = Gitea::User::UpdateService.new(username, params).call + render_result(response) + rescue Exception => exception + Rails.logger.info "Exception ===========> #{exception.message}" + fail!(exception.message) + end + + + private + attr_reader :params, :username + + def fail!(error) + @error = error + end + + def render_result(response) + @result = response + end + + def valid_params + @params.merge(username: username) + end + end +end diff --git a/app/services/gitea/user/update_service.rb b/app/services/gitea/user/update_service.rb index 3650d3902..ead483a25 100644 --- a/app/services/gitea/user/update_service.rb +++ b/app/services/gitea/user/update_service.rb @@ -1,6 +1,5 @@ class Gitea::User::UpdateService < Gitea::ClientService - # attr_reader :admin_user, :params - attr_reader :token, :old_login, :params + attr_reader :edit_username, :params # 只有管理员才能修改用户信息 # params: # admin boolean @@ -18,10 +17,10 @@ class Gitea::User::UpdateService < Gitea::ClientService # source_id integer($int64) # website string - def initialize(token, old_login, params={}) - @token = token - @params = params - @old_login = old_login + def initialize(edit_username, params={}, token=nil) + @token = token + @params = params + @edit_username = edit_username end def call @@ -31,7 +30,7 @@ class Gitea::User::UpdateService < Gitea::ClientService private def url - "/admin/users/#{old_login}" + "/admin/users/#{edit_username}" end def data_params From 2fd45393913411e82f73c2cffe24e9e084dd81c1 Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Wed, 22 Apr 2020 15:40:15 +0800 Subject: [PATCH 3/6] =?UTF-8?q?FIX=20=E5=AE=8C=E5=96=84=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AFapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/accounts_controller.rb | 33 +++++++++++++------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 86296d3ea..8f3b81e4f 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -71,13 +71,14 @@ class AccountsController < ApplicationController end if sync_params.present? - update_gitea = Gitea::User::UpdateService.call("", params[:old_user_login], sync_params) - Rails.logger.info("########________update_gitea__________###########__status:_#{update_gitea.status}") + interactor = Gitea::User::UpdateInteractor.call(u.login, sync_params) + if interactor.success? + render_ok + else + render_error(interactor.error) + end end end - - - render_ok({}) end rescue Exception => e uid_logger_error(e.message) @@ -98,20 +99,19 @@ class AccountsController < ApplicationController #修改密码 def remote_password @user = User.find_by(login: params[:login]) - if @user && @user.update_attribute(:password, params[:password]) - sync_params = { - password: params[:password], - email: @user.mail - } - update_gitea = Gitea::User::UpdateService.call("", params[:login], sync_params) + return render_error("未找到相关用户!") if @user.blank? - Rails.logger.info("########________update_gitea___status________###########__status:_#{update_gitea.status}") - Rails.logger.info("######________password_update_success____######") + sync_params = { + password: params[:password].to_s, + email: @user.mail + } - render_ok({}) + interactor = Gitea::User::UpdateInteractor.call(@user.login, sync_params) + if interactor.success? + @user.update_attribute(:password, params[:password]) + render_ok else - Rails.logger.info("######________password_update_failed____######") - render_error("更新失败") + render_error(interactor.error) end end @@ -205,7 +205,6 @@ class AccountsController < ApplicationController end successful_authentication(@user) - # session[:user_id] = @user.id end From 3dbdb5ead4fbb588aea0fb55368d18d94d631fd2 Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Wed, 22 Apr 2020 15:41:47 +0800 Subject: [PATCH 4/6] =?UTF-8?q?ADD=20=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E6=97=B6=E5=90=8C=E6=AD=A5=E4=BF=AE=E6=94=B9gitea=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E5=AF=B9=E5=BA=94=E7=94=A8=E6=88=B7=E7=9A=84=E5=AF=86?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/accounts_controller.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 8f3b81e4f..1df65ae4b 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -205,6 +205,9 @@ class AccountsController < ApplicationController end successful_authentication(@user) + # TODO用户密码未同步 + Gitea::User::UpdateInteractor.call(@user.login, {email: @user.mail, password: password.to_s}) unless @user.is_sync_pwd? + # session[:user_id] = @user.id end From 3b6cbacbd15ae87dce16ac22ec005f27153b5f18 Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Wed, 22 Apr 2020 15:59:40 +0800 Subject: [PATCH 5/6] =?UTF-8?q?ADD=20=E5=85=B6=E4=BB=96=E5=B9=B3=E5=8F=B0(?= =?UTF-8?q?=E5=A6=82trusitie=E5=B9=B3=E5=8F=B0)=E7=99=BB=E5=BD=95=E5=90=8E?= =?UTF-8?q?=E5=B0=86=E7=94=A8=E6=88=B7=E5=AF=86=E7=A0=81=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E5=88=B0gitea=E5=B9=B3=E5=8F=B0=E5=AF=B9=E5=BA=94=E7=9A=84?= =?UTF-8?q?=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 15 ++++++++++++++- config/routes.rb | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index cc9d2a63a..995885e8c 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,6 +1,6 @@ class UsersController < ApplicationController - before_action :load_user, only: [:show, :homepage_info, :sync_token] + before_action :load_user, only: [:show, :homepage_info, :sync_token, :sync_gitea_pwd] before_action :check_user_exist, only: [:show, :homepage_info] before_action :require_login, only: %i[me list projects] skip_before_action :check_sign, only: [:attachment_show] @@ -118,6 +118,19 @@ class UsersController < ApplicationController @projects = paginate(scope) end + # TODO 其他平台登录时同步修改gitea平台对应用户的密码 + # 该方法主要用于:别的平台初次部署对接forge平台,同步用户后,gitea平台对应的用户密码与forge平台用户密码不一致是问题 + def sync_gitea_pwd + return render_error("未找到相关的用户") if @user.blank? + + sync_params = { + email: @user.mail, + password: params[:password].to_s + } + interactor = Gitea::User::UpdateInteractor.call(@user.login, sync_params) + interactor.success? ? render_ok : render_error(interactor.error) + 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 88b285b1a..1950b768b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -148,6 +148,7 @@ Rails.application.routes.draw do get :list post :sync_token get :projects + post :sync_gitea_pwd end scope module: :users do From 45b2e5bf96721c892694e3d123d219d13be93faa Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Wed, 22 Apr 2020 17:13:08 +0800 Subject: [PATCH 6/6] ADD sync users salt column --- app/controllers/users_controller.rb | 9 +++++++++ config/routes.rb | 1 + 2 files changed, 10 insertions(+) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 995885e8c..3f5dd3095 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -131,6 +131,15 @@ class UsersController < ApplicationController interactor.success? ? render_ok : render_error(interactor.error) end + # TODO + # 同步trusite平台用户的salt信息,只需同步一次,同步完成后,该方法可以删除 + def sync_salt + user = User.find_by_login params[:login] + return if user.blank? + user.update_column(:salt, params[:salt]) + render_ok + 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 1950b768b..fc2c54328 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -149,6 +149,7 @@ Rails.application.routes.draw do post :sync_token get :projects post :sync_gitea_pwd + post :sync_salt end scope module: :users do