debian-mirror-gitlab/app/presenters/projects/settings/deploy_keys_presenter.rb

92 lines
2.6 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Projects
module Settings
class DeployKeysPresenter < Gitlab::View::Presenter::Simple
2020-01-01 13:55:28 +05:30
include Gitlab::Utils::StrongMemoize
2017-08-17 22:00:37 +05:30
presents :project
delegate :size, to: :enabled_keys, prefix: true
delegate :size, to: :available_project_keys, prefix: true
delegate :size, to: :available_public_keys, prefix: true
def new_key
2018-03-17 18:26:18 +05:30
@key ||= DeployKey.new.tap { |dk| dk.deploy_keys_projects.build }
2017-08-17 22:00:37 +05:30
end
def enabled_keys
2020-01-01 13:55:28 +05:30
strong_memoize(:enabled_keys) do
project.deploy_keys.with_projects
end
2017-08-17 22:00:37 +05:30
end
def available_keys
2020-01-01 13:55:28 +05:30
strong_memoize(:available_keys) do
current_user
.accessible_deploy_keys
.id_not_in(enabled_keys.select(:id))
.with_projects
end
2017-08-17 22:00:37 +05:30
end
def available_project_keys
2020-01-01 13:55:28 +05:30
strong_memoize(:available_project_keys) do
current_user
.project_deploy_keys
.id_not_in(enabled_keys.select(:id))
.with_projects
end
2017-08-17 22:00:37 +05:30
end
def available_public_keys
2020-01-01 13:55:28 +05:30
strong_memoize(:available_public_keys) do
DeployKey
.are_public
.id_not_in(enabled_keys.select(:id))
.id_not_in(available_project_keys.select(:id))
.with_projects
end
2017-08-17 22:00:37 +05:30
end
def as_json
2018-12-05 23:21:45 +05:30
serializer = DeployKeySerializer.new # rubocop: disable CodeReuse/Serializer
2020-01-01 13:55:28 +05:30
opts = { user: current_user, project: project, readable_project_ids: readable_project_ids }
2017-08-17 22:00:37 +05:30
{
2020-01-01 13:55:28 +05:30
enabled_keys: serializer.represent(enabled_keys, opts),
2017-08-17 22:00:37 +05:30
available_project_keys: serializer.represent(available_project_keys, opts),
public_keys: serializer.represent(available_public_keys, opts)
}
end
def to_partial_path
'projects/deploy_keys/index'
end
def form_partial_path
'projects/deploy_keys/form'
end
2020-01-01 13:55:28 +05:30
private
# Caching all readable project ids for the user that are associated with the queried deploy keys
def readable_project_ids
strong_memoize(:readable_projects_by_id) do
Set.new(user_readable_project_ids)
end
end
# rubocop: disable CodeReuse/ActiveRecord
def user_readable_project_ids
project_ids = (available_keys + available_project_keys + available_public_keys)
.flat_map { |deploy_key| deploy_key.deploy_keys_projects.map(&:project_id) }
.compact
.uniq
current_user.authorized_projects(Gitlab::Access::GUEST).id_in(project_ids).pluck(:id)
end
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
end
end
end