debian-mirror-gitlab/app/controllers/projects/deploy_keys_controller.rb

91 lines
2.5 KiB
Ruby
Raw Normal View History

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
2017-08-17 22:00:37 +05:30
include RepositorySettingsRedirect
2014-09-02 18:07:02 +05:30
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|
2018-11-20 20:47:30 +05:30
format.html { redirect_to_repository_settings(@project, anchor: 'js-deploy-keys-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
2018-11-20 20:47:30 +05:30
redirect_to_repository_settings(@project, anchor: 'js-deploy-keys-settings')
2014-09-02 18:07:02 +05:30
end
def create
2019-02-13 22:33:31 +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
2018-11-20 20:47:30 +05:30
redirect_to_repository_settings(@project, anchor: 'js-deploy-keys-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)
2017-09-10 17:25:29 +05:30
flash[:notice] = 'Deploy key was successfully updated.'
2018-11-20 20:47:30 +05:30
redirect_to_repository_settings(@project, anchor: 'js-deploy-keys-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-13 22:33:31 +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|
2018-11-20 20:47:30 +05:30
format.html { redirect_to_repository_settings(@project, anchor: 'js-deploy-keys-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-13 22:33:31 +05:30
deploy_key_project = Projects::DisableDeployKeyService.new(@project, current_user, params).execute
2017-08-17 22:00:37 +05:30
2019-02-13 22:33:31 +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|
2018-11-20 20:47:30 +05:30
format.html { redirect_to_repository_settings(@project, anchor: 'js-deploy-keys-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
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
2018-03-17 18:26:18 +05:30
params.require(:deploy_key).permit(:title, deploy_keys_projects_attributes: [:id, :can_push])
2017-09-10 17:25:29 +05:30
end
def authorize_update_deploy_key!
access_denied! unless can?(current_user, :update_deploy_key, deploy_key)
end
2014-09-02 18:07:02 +05:30
end