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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
3.2 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
2021-11-18 22:05:49 +05:30
presents ::Project, as: :project
2017-08-17 22:00:37 +05:30
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
2021-04-29 21:17:54 +05:30
# It includes:
# - The deploy keys enabled in the project.
2017-08-17 22:00:37 +05:30
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
2021-04-29 21:17:54 +05:30
# NOTE: This method is redundant. Use `available_project_keys` and `available_public_keys` instead.
# It includes:
# - Enabled deploy keys in projects that can be accessed by the user.
# - Instance-level public deploy keys.
# It excludes:
# - The deploy keys enabled in the project.
2017-08-17 22:00:37 +05:30
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
2021-04-29 21:17:54 +05:30
# It includes:
# - Enabled deploy keys in projects that can be accessed by the user.
# It excludes:
# - The deploy keys enabled in the project
2017-08-17 22:00:37 +05:30
def available_project_keys
2020-01-01 13:55:28 +05:30
strong_memoize(:available_project_keys) do
2021-04-29 21:17:54 +05:30
current_user.project_deploy_keys.with_projects - enabled_keys
2020-01-01 13:55:28 +05:30
end
2017-08-17 22:00:37 +05:30
end
2021-04-29 21:17:54 +05:30
# It includes:
# - Instance-level public deploy keys.
# It excludes:
# - The deploy keys enabled in the project.
2017-08-17 22:00:37 +05:30
def available_public_keys
2020-01-01 13:55:28 +05:30
strong_memoize(:available_public_keys) do
2021-04-29 21:17:54 +05:30
DeployKey.are_public.with_projects - enabled_keys
2020-01-01 13:55:28 +05:30
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
2020-05-24 23:13:21 +05:30
'../../shared/deploy_keys/index'
2017-08-17 22:00:37 +05:30
end
def form_partial_path
2020-05-24 23:13:21 +05:30
'shared/deploy_keys/project_group_form'
2017-08-17 22:00:37 +05:30
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
2021-04-29 21:17:54 +05:30
project_ids = (available_project_keys + available_public_keys)
2020-01-01 13:55:28 +05:30
.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