2018-09-26 03:45:43 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-23 16:47:22 +00:00
|
|
|
class Projects::DeployKeysController < Projects::ApplicationController
|
2020-04-24 18:09:46 +00:00
|
|
|
include RepositorySettingsRedirect
|
2012-01-17 22:46:13 +00:00
|
|
|
respond_to :html
|
2011-12-29 21:46:18 +00:00
|
|
|
|
|
|
|
# Authorize
|
2015-04-16 12:03:37 +00:00
|
|
|
before_action :authorize_admin_project!
|
2017-03-31 12:54:38 +00:00
|
|
|
before_action :authorize_update_deploy_key!, only: [:edit, :update]
|
2011-12-29 19:44:16 +00:00
|
|
|
|
2017-06-20 09:37:07 +00:00
|
|
|
layout 'project_settings'
|
2013-06-19 19:44:57 +00:00
|
|
|
|
2020-10-08 18:08:32 +00:00
|
|
|
feature_category :continuous_delivery
|
|
|
|
|
2011-12-29 19:44:16 +00:00
|
|
|
def index
|
2017-03-24 10:11:36 +00:00
|
|
|
respond_to do |format|
|
2020-04-24 18:09:46 +00:00
|
|
|
format.html { redirect_to_repository }
|
2017-03-24 10:11:36 +00:00
|
|
|
format.json do
|
|
|
|
render json: Projects::Settings::DeployKeysPresenter.new(@project, current_user: current_user).as_json
|
|
|
|
end
|
|
|
|
end
|
2011-12-29 19:44:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2020-04-24 18:09:46 +00:00
|
|
|
redirect_to_repository
|
2011-12-29 19:44:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2018-12-18 16:40:05 +00:00
|
|
|
@key = DeployKeys::CreateService.new(current_user, create_params).execute(project: @project)
|
2013-05-06 12:10:55 +00:00
|
|
|
|
2018-01-05 15:23:44 +00:00
|
|
|
unless @key.valid?
|
2017-03-24 10:11:36 +00:00
|
|
|
flash[:alert] = @key.errors.full_messages.join(', ').html_safe
|
2012-01-17 23:00:40 +00:00
|
|
|
end
|
2018-01-11 16:34:01 +00:00
|
|
|
|
2020-04-24 18:09:46 +00:00
|
|
|
redirect_to_repository
|
2011-12-29 19:44:16 +00:00
|
|
|
end
|
|
|
|
|
2017-03-31 12:54:38 +00:00
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2020-05-27 21:08:05 +00:00
|
|
|
access_denied! unless deploy_key
|
|
|
|
|
2018-07-02 10:43:06 +00:00
|
|
|
if deploy_key.update(update_params)
|
2019-03-27 16:52:52 +00:00
|
|
|
flash[:notice] = _('Deploy key was successfully updated.')
|
2020-04-24 18:09:46 +00:00
|
|
|
redirect_to_repository
|
2017-03-31 12:54:38 +00:00
|
|
|
else
|
|
|
|
render 'edit'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-06 12:10:55 +00:00
|
|
|
def enable
|
2018-11-17 05:59:17 +00:00
|
|
|
key = Projects::EnableDeployKeyService.new(@project, current_user, params).execute
|
|
|
|
|
|
|
|
return render_404 unless key
|
2013-05-06 12:10:55 +00:00
|
|
|
|
2017-04-28 16:06:19 +00:00
|
|
|
respond_to do |format|
|
2020-04-24 18:09:46 +00:00
|
|
|
format.html { redirect_to_repository }
|
2017-04-28 16:06:19 +00:00
|
|
|
format.json { head :ok }
|
|
|
|
end
|
2013-05-06 12:10:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def disable
|
2018-11-17 05:59:17 +00:00
|
|
|
deploy_key_project = Projects::DisableDeployKeyService.new(@project, current_user, params).execute
|
2013-05-06 12:10:55 +00:00
|
|
|
|
2018-11-17 05:59:17 +00:00
|
|
|
return render_404 unless deploy_key_project
|
2017-04-28 16:06:19 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
2020-04-24 18:09:46 +00:00
|
|
|
format.html { redirect_to_repository }
|
2017-04-28 16:06:19 +00:00
|
|
|
format.json { head :ok }
|
|
|
|
end
|
2013-05-06 12:10:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2017-03-31 12:54:38 +00:00
|
|
|
def deploy_key
|
2017-06-20 09:37:07 +00:00
|
|
|
@deploy_key ||= DeployKey.find(params[:id])
|
2017-03-31 12:54:38 +00:00
|
|
|
end
|
|
|
|
|
2019-10-17 00:07:27 +00:00
|
|
|
def deploy_keys_project
|
|
|
|
@deploy_keys_project ||= deploy_key.deploy_keys_project_for(@project)
|
|
|
|
end
|
|
|
|
|
2017-03-31 12:54:38 +00:00
|
|
|
def create_params
|
2018-01-05 15:23:44 +00:00
|
|
|
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-06-26 20:24:17 +00:00
|
|
|
end
|
2017-03-31 12:54:38 +00:00
|
|
|
|
|
|
|
def update_params
|
2020-05-27 21:08:05 +00:00
|
|
|
permitted_params = [deploy_keys_projects_attributes: [:can_push]]
|
2019-10-17 00:07:27 +00:00
|
|
|
permitted_params << :title if can?(current_user, :update_deploy_key, deploy_key)
|
|
|
|
|
2020-05-27 21:08:05 +00:00
|
|
|
key_update_params = params.require(:deploy_key).permit(*permitted_params)
|
|
|
|
key_update_params.dig(:deploy_keys_projects_attributes, '0')&.merge!(id: deploy_keys_project.id)
|
|
|
|
key_update_params
|
2017-03-31 12:54:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_update_deploy_key!
|
2019-10-17 00:07:27 +00:00
|
|
|
if !can?(current_user, :update_deploy_key, deploy_key) &&
|
|
|
|
!can?(current_user, :update_deploy_keys_project, deploy_keys_project)
|
|
|
|
access_denied!
|
|
|
|
end
|
2017-03-31 12:54:38 +00:00
|
|
|
end
|
2020-03-02 15:08:01 +00:00
|
|
|
|
2020-04-24 18:09:46 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def redirect_to_repository
|
|
|
|
redirect_to_repository_settings(@project, anchor: 'js-deploy-keys-settings')
|
2020-03-02 15:08:01 +00:00
|
|
|
end
|
2011-12-29 19:44:16 +00:00
|
|
|
end
|