forgeplus/app/controllers/ci/base_controller.rb

77 lines
1.8 KiB
Ruby
Raw Normal View History

2020-08-13 23:38:32 +08:00
class Ci::BaseController < ApplicationController
2020-09-18 15:46:36 +08:00
include Ci::DbConnectable
2020-08-13 23:38:32 +08:00
before_action :require_login
before_action :connect_to_ci_db
2020-08-20 18:37:53 +08:00
def load_repo
namespace = params[:owner]
id = params[:repo] || params[:id]
2022-02-10 14:08:14 +08:00
@ci_user, @repo = Ci::Repo.find_with_namespace(namespace, id, current_user.login)
2020-08-20 18:37:53 +08:00
end
2020-09-10 16:01:24 +08:00
def load_all_repo
namespace = current_user.login
@repos = Ci::Repo.find_all_with_namespace(namespace)
end
2021-01-26 17:15:53 +08:00
def load_repo_by_repo_slug(slug)
@repo_slug = Ci::Repo.load_repo_by_repo_slug(slug)
end
2020-09-10 16:01:24 +08:00
private
def authorize_access_project!
unless @project.manager?(current_user)
return render_forbidden
end
end
2020-10-16 11:57:20 +08:00
def authenticate_manager!
unless @project.manager?(current_user)
2020-09-10 16:01:24 +08:00
return render_forbidden
end
end
def authenticate_admin!
return render_forbidden unless current_user.admin?
end
2020-10-16 11:57:20 +08:00
def authorize_owner!
2020-09-10 16:01:24 +08:00
unless @project.owner?(current_user)
return render_forbidden
end
end
def find_cloud_account
@cloud_account ||= current_user.ci_cloud_account
2020-09-27 15:30:44 +08:00
@cloud_account.blank? ? nil : @cloud_account
2020-09-10 16:01:24 +08:00
end
def load_ci_user
@ci_user ||= Ci::User.find_by(user_login: params[:owner])
@ci_user.blank? ? raise("未找到相关的记录") : @ci_user
2020-09-10 16:01:24 +08:00
end
def connect_to_ci_db(options={})
current = current_user
owner = params[:owner]
if owner
current = User.find_by(login: owner)
end
if !(current && !current.is_a?(AnonymousUser) && !current.devops_uninit?)
return
end
if current.ci_cloud_account.server_type == Ci::CloudAccount::SERVER_TYPE_TRUSTIE
connect_to_trustie_ci_database(options)
else
options = options.merge(db_name: current.login)
connect_to_ci_database(options)
end
end
2020-08-13 23:38:32 +08:00
end