2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-06 09:26:36 +00:00
|
|
|
class DeployKey < Key
|
2019-08-01 15:22:56 +00:00
|
|
|
include FromUnion
|
2019-12-01 06:06:11 +00:00
|
|
|
include IgnorableColumns
|
2021-07-03 00:06:54 +00:00
|
|
|
include PolicyActor
|
2022-10-06 06:08:33 +00:00
|
|
|
include Presentable
|
2018-01-05 15:23:44 +00:00
|
|
|
|
|
|
|
has_many :deploy_keys_projects, inverse_of: :deploy_key, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
|
2018-04-18 13:41:42 +00:00
|
|
|
has_many :projects, through: :deploy_keys_projects
|
2021-11-02 06:10:00 +00:00
|
|
|
|
|
|
|
has_many :deploy_keys_projects_with_write_access, -> { with_write_access }, class_name: "DeployKeysProject"
|
|
|
|
has_many :projects_with_write_access, -> { includes(:route) }, class_name: 'Project', through: :deploy_keys_projects_with_write_access, source: :project
|
2020-11-13 15:09:24 +00:00
|
|
|
has_many :protected_branch_push_access_levels, class_name: '::ProtectedBranch::PushAccessLevel'
|
2013-05-06 12:10:55 +00:00
|
|
|
|
2020-11-10 15:09:14 +00:00
|
|
|
scope :in_projects, ->(projects) { joins(:deploy_keys_projects).where(deploy_keys_projects: { project_id: projects }) }
|
|
|
|
scope :with_write_access, -> { joins(:deploy_keys_projects).merge(DeployKeysProject.with_write_access) }
|
|
|
|
scope :are_public, -> { where(public: true) }
|
2019-12-12 00:07:43 +00:00
|
|
|
scope :with_projects, -> { includes(deploy_keys_projects: { project: [:route, namespace: :route] }) }
|
2021-11-02 06:10:00 +00:00
|
|
|
scope :including_projects_with_write_access, -> { includes(:projects_with_write_access) }
|
2015-03-27 13:43:48 +00:00
|
|
|
|
2018-01-05 15:23:44 +00:00
|
|
|
accepts_nested_attributes_for :deploy_keys_projects
|
|
|
|
|
2015-03-27 13:43:48 +00:00
|
|
|
def private?
|
|
|
|
!public?
|
|
|
|
end
|
2015-04-03 10:22:44 +00:00
|
|
|
|
|
|
|
def orphaned?
|
2018-09-18 07:36:24 +00:00
|
|
|
self.deploy_keys_projects.empty?
|
2015-04-03 10:22:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def almost_orphaned?
|
2019-12-12 00:07:43 +00:00
|
|
|
self.deploy_keys_projects.size == 1
|
2015-04-03 10:22:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroyed_when_orphaned?
|
|
|
|
self.private?
|
|
|
|
end
|
2016-11-11 13:44:33 +00:00
|
|
|
|
2018-03-28 16:54:15 +00:00
|
|
|
def user
|
|
|
|
super || User.ghost
|
|
|
|
end
|
|
|
|
|
2022-07-20 15:08:38 +00:00
|
|
|
def audit_details
|
|
|
|
title
|
|
|
|
end
|
|
|
|
|
2016-11-16 12:31:08 +00:00
|
|
|
def has_access_to?(project)
|
2018-01-05 15:23:44 +00:00
|
|
|
deploy_keys_project_for(project).present?
|
2016-11-16 12:31:08 +00:00
|
|
|
end
|
|
|
|
|
2016-11-11 13:44:33 +00:00
|
|
|
def can_push_to?(project)
|
2018-01-05 15:23:44 +00:00
|
|
|
!!deploy_keys_project_for(project)&.can_push?
|
|
|
|
end
|
|
|
|
|
|
|
|
def deploy_keys_project_for(project)
|
2019-12-12 00:07:43 +00:00
|
|
|
if association(:deploy_keys_projects).loaded?
|
|
|
|
deploy_keys_projects.find { |dkp| dkp.project_id.eql?(project&.id) }
|
|
|
|
else
|
|
|
|
deploy_keys_projects.find_by(project: project)
|
|
|
|
end
|
2018-01-05 15:23:44 +00:00
|
|
|
end
|
|
|
|
|
2020-11-10 15:09:14 +00:00
|
|
|
def self.with_write_access_for_project(project, deploy_key: nil)
|
|
|
|
query = in_projects(project).with_write_access
|
|
|
|
query = query.where(id: deploy_key) if deploy_key
|
|
|
|
|
|
|
|
query
|
|
|
|
end
|
2022-07-29 18:08:58 +00:00
|
|
|
|
|
|
|
# This is used for the internal logic of AuditEvents::BuildService.
|
|
|
|
def impersonated?
|
|
|
|
false
|
|
|
|
end
|
2013-05-06 09:26:36 +00:00
|
|
|
end
|