2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
class Projects::DeployKeysController < Projects::ApplicationController
|
|
|
|
respond_to :html
|
|
|
|
|
|
|
|
# Authorize
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :authorize_admin_project!
|
2017-09-10 17:25:29 +05:30
|
|
|
before_action :authorize_update_deploy_key!, only: [:edit, :update]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
layout 'project_settings'
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
def index
|
2017-08-17 22:00:37 +05:30
|
|
|
respond_to do |format|
|
2020-04-08 14:13:33 +05:30
|
|
|
format.html { redirect_to_ci_cd_settings }
|
2017-08-17 22:00:37 +05:30
|
|
|
format.json do
|
|
|
|
render json: Projects::Settings::DeployKeysPresenter.new(@project, current_user: current_user).as_json
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2020-04-08 14:13:33 +05:30
|
|
|
redirect_to_ci_cd_settings
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2019-02-15 15:39:39 +05:30
|
|
|
@key = DeployKeys::CreateService.new(current_user, create_params).execute(project: @project)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
unless @key.valid?
|
2017-08-17 22:00:37 +05:30
|
|
|
flash[:alert] = @key.errors.full_messages.join(', ').html_safe
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
redirect_to_ci_cd_settings
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2018-11-18 11:00:15 +05:30
|
|
|
if deploy_key.update(update_params)
|
2019-07-07 11:18:12 +05:30
|
|
|
flash[:notice] = _('Deploy key was successfully updated.')
|
2020-04-08 14:13:33 +05:30
|
|
|
redirect_to_ci_cd_settings
|
2017-09-10 17:25:29 +05:30
|
|
|
else
|
|
|
|
render 'edit'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def enable
|
2019-02-15 15:39:39 +05:30
|
|
|
key = Projects::EnableDeployKeyService.new(@project, current_user, params).execute
|
|
|
|
|
|
|
|
return render_404 unless key
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
respond_to do |format|
|
2020-04-08 14:13:33 +05:30
|
|
|
format.html { redirect_to_ci_cd_settings }
|
2017-08-17 22:00:37 +05:30
|
|
|
format.json { head :ok }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def disable
|
2019-02-15 15:39:39 +05:30
|
|
|
deploy_key_project = Projects::DisableDeployKeyService.new(@project, current_user, params).execute
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
return render_404 unless deploy_key_project
|
2019-01-03 12:48:30 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
respond_to do |format|
|
2020-04-08 14:13:33 +05:30
|
|
|
format.html { redirect_to_ci_cd_settings }
|
2017-08-17 22:00:37 +05:30
|
|
|
format.json { head :ok }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def deploy_key
|
|
|
|
@deploy_key ||= DeployKey.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
def deploy_keys_project
|
|
|
|
@deploy_keys_project ||= deploy_key.deploy_keys_project_for(@project)
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def create_params
|
2018-03-17 18:26:18 +05:30
|
|
|
create_params = params.require(:deploy_key)
|
|
|
|
.permit(:key, :title, deploy_keys_projects_attributes: [:can_push])
|
|
|
|
create_params.dig(:deploy_keys_projects_attributes, '0')&.merge!(project_id: @project.id)
|
|
|
|
create_params
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
def update_params
|
2019-12-21 20:55:43 +05:30
|
|
|
permitted_params = [deploy_keys_projects_attributes: [:id, :can_push]]
|
|
|
|
permitted_params << :title if can?(current_user, :update_deploy_key, deploy_key)
|
|
|
|
|
|
|
|
params.require(:deploy_key).permit(*permitted_params)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_update_deploy_key!
|
2019-12-21 20:55:43 +05:30
|
|
|
if !can?(current_user, :update_deploy_key, deploy_key) &&
|
|
|
|
!can?(current_user, :update_deploy_keys_project, deploy_keys_project)
|
|
|
|
access_denied!
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
def redirect_to_ci_cd_settings
|
|
|
|
redirect_to project_settings_ci_cd_path(@project, anchor: 'js-deploy-keys-settings')
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|