debian-mirror-gitlab/app/models/deploy_key.rb

65 lines
1.8 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
class DeployKey < Key
2019-10-12 21:52:04 +05:30
include FromUnion
2020-01-01 13:55:28 +05:30
include IgnorableColumns
2018-03-17 18:26:18 +05:30
has_many :deploy_keys_projects, inverse_of: :deploy_key, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
2014-09-02 18:07:02 +05:30
has_many :projects, through: :deploy_keys_projects
2021-01-29 00:20:46 +05:30
has_many :protected_branch_push_access_levels, class_name: '::ProtectedBranch::PushAccessLevel'
2014-09-02 18:07:02 +05:30
2021-01-29 00:20:46 +05:30
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) }
2020-01-01 13:55:28 +05:30
scope :with_projects, -> { includes(deploy_keys_projects: { project: [:route, namespace: :route] }) }
2015-04-26 12:48:37 +05:30
2018-03-17 18:26:18 +05:30
accepts_nested_attributes_for :deploy_keys_projects
2015-04-26 12:48:37 +05:30
def private?
!public?
end
def orphaned?
2018-12-05 23:21:45 +05:30
self.deploy_keys_projects.empty?
2015-04-26 12:48:37 +05:30
end
def almost_orphaned?
2020-01-01 13:55:28 +05:30
self.deploy_keys_projects.size == 1
2015-04-26 12:48:37 +05:30
end
def destroyed_when_orphaned?
self.private?
end
2017-08-17 22:00:37 +05:30
2018-05-09 12:01:36 +05:30
def user
super || User.ghost
end
2017-08-17 22:00:37 +05:30
def has_access_to?(project)
2018-03-17 18:26:18 +05:30
deploy_keys_project_for(project).present?
2017-08-17 22:00:37 +05:30
end
def can_push_to?(project)
2018-03-17 18:26:18 +05:30
!!deploy_keys_project_for(project)&.can_push?
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
def deploy_keys_project_for(project)
2020-01-01 13:55:28 +05:30
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-03-17 18:26:18 +05:30
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
def projects_with_write_access
2019-10-12 21:52:04 +05:30
Project.with_route.where(id: deploy_keys_projects.with_write_access.select(:project_id))
2017-08-17 22:00:37 +05:30
end
2021-01-29 00:20:46 +05:30
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
2014-09-02 18:07:02 +05:30
end