diff --git a/app/controllers/ci/base_controller.rb b/app/controllers/ci/base_controller.rb index 95c43d5bd..13ff46410 100644 --- a/app/controllers/ci/base_controller.rb +++ b/app/controllers/ci/base_controller.rb @@ -1,4 +1,6 @@ class Ci::BaseController < ApplicationController + include Ci::DbConnectable + before_action :require_login before_action :connect_to_ci_database @@ -44,20 +46,4 @@ class Ci::BaseController < ApplicationController end end - # Dynamically sets the database connection. - def connect_to_ci_database - db_config = Rails.configuration.database_configuration[Rails.env]["ci_server_db"] - return render_error('ci database config missing') if db_config.blank? - - req_params = { - host: db_config["host"], - username: db_config['username'], - password: db_config['password'], - port: db_config['port'], - database: "#{current_user.login}_#{db_config['database']}" - } - db_params = Ci::Database.get_connection_params(req_params) - Ci::Database.set_connection(db_params) - end - end diff --git a/app/controllers/concerns/ci/db_connectable.rb b/app/controllers/concerns/ci/db_connectable.rb new file mode 100644 index 000000000..41cb96bf6 --- /dev/null +++ b/app/controllers/concerns/ci/db_connectable.rb @@ -0,0 +1,23 @@ +module Ci::DbConnectable + extend ActiveSupport::Concern + + include do + end + + # Dynamically sets the database connection. + def connect_to_ci_database + db_config = Rails.configuration.database_configuration[Rails.env]["ci_server_db"] + return render_error('ci database config missing') if db_config.blank? + + req_params = { + host: db_config["host"], + username: db_config['username'], + password: db_config['password'], + port: db_config['port'], + database: "#{current_user.login}_#{db_config['database']}" + } + db_params = Ci::Database.get_connection_params(req_params) + Ci::Database.set_connection(db_params) + end + +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 390fc86b7..c5bc97276 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,7 +1,10 @@ class UsersController < ApplicationController + include Ci::DbConnectable + 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 :connect_to_ci_database, only: :get_user_info skip_before_action :check_sign, only: [:attachment_show] def list diff --git a/app/views/users/get_user_info.json.jbuilder b/app/views/users/get_user_info.json.jbuilder index d9aa44025..b6c239a9e 100644 --- a/app/views/users/get_user_info.json.jbuilder +++ b/app/views/users/get_user_info.json.jbuilder @@ -13,4 +13,4 @@ json.user_phone_binded @user.phone.present? json.profile_completed @user.profile_completed? json.professional_certification @user.professional_certification json.devops_step @user.devops_step -json.ci_certification @user.ci_certification? +json.ci_certification @user.ci_certification? if !@user.is_a?(AnonymousUser)