2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-06 05:26:36 -04:00
|
|
|
class DeployKey < Key
|
2019-08-01 11:22:56 -04:00
|
|
|
include FromUnion
|
2019-12-01 01:06:11 -05:00
|
|
|
include IgnorableColumns
|
2018-01-05 10:23:44 -05:00
|
|
|
|
|
|
|
has_many :deploy_keys_projects, inverse_of: :deploy_key, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
|
2018-04-18 09:41:42 -04:00
|
|
|
has_many :projects, through: :deploy_keys_projects
|
2020-11-13 10:09:24 -05:00
|
|
|
has_many :protected_branch_push_access_levels, class_name: '::ProtectedBranch::PushAccessLevel'
|
2013-05-06 08:10:55 -04:00
|
|
|
|
2020-11-10 10:09:14 -05: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-11 19:07:43 -05:00
|
|
|
scope :with_projects, -> { includes(deploy_keys_projects: { project: [:route, namespace: :route] }) }
|
2015-03-27 09:43:48 -04:00
|
|
|
|
2019-12-01 01:06:11 -05:00
|
|
|
ignore_column :can_push, remove_after: '2019-12-15', remove_with: '12.6'
|
2018-01-05 10:23:44 -05:00
|
|
|
|
|
|
|
accepts_nested_attributes_for :deploy_keys_projects
|
|
|
|
|
2015-03-27 09:43:48 -04:00
|
|
|
def private?
|
|
|
|
!public?
|
|
|
|
end
|
2015-04-03 06:22:44 -04:00
|
|
|
|
|
|
|
def orphaned?
|
2018-09-18 03:36:24 -04:00
|
|
|
self.deploy_keys_projects.empty?
|
2015-04-03 06:22:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def almost_orphaned?
|
2019-12-11 19:07:43 -05:00
|
|
|
self.deploy_keys_projects.size == 1
|
2015-04-03 06:22:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroyed_when_orphaned?
|
|
|
|
self.private?
|
|
|
|
end
|
2016-11-11 08:44:33 -05:00
|
|
|
|
2018-03-28 12:54:15 -04:00
|
|
|
def user
|
|
|
|
super || User.ghost
|
|
|
|
end
|
|
|
|
|
2016-11-16 07:31:08 -05:00
|
|
|
def has_access_to?(project)
|
2018-01-05 10:23:44 -05:00
|
|
|
deploy_keys_project_for(project).present?
|
2016-11-16 07:31:08 -05:00
|
|
|
end
|
|
|
|
|
2016-11-11 08:44:33 -05:00
|
|
|
def can_push_to?(project)
|
2018-01-05 10:23:44 -05:00
|
|
|
!!deploy_keys_project_for(project)&.can_push?
|
|
|
|
end
|
|
|
|
|
|
|
|
def deploy_keys_project_for(project)
|
2019-12-11 19:07:43 -05: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 10:23:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def projects_with_write_access
|
2019-08-01 12:18:17 -04:00
|
|
|
Project.with_route.where(id: deploy_keys_projects.with_write_access.select(:project_id))
|
2016-11-11 08:44:33 -05:00
|
|
|
end
|
2020-11-10 10:09:14 -05: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
|
2013-05-06 05:26:36 -04:00
|
|
|
end
|