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

53 lines
1.3 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-12-16 22:33:55 +05:30
include FromUnion
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
scope :in_projects, ->(projects) { joins(:deploy_keys_projects).where('deploy_keys_projects.project_id in (?)', projects) }
2015-04-26 12:48:37 +05:30
scope :are_public, -> { where(public: true) }
2019-12-16 22:33:55 +05:30
scope :with_projects, -> { includes(deploy_keys_projects: { project: [:route, :namespace] }) }
2015-04-26 12:48:37 +05:30
2019-12-16 22:33:55 +05:30
self.ignored_columns += %i[can_push]
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?
2019-12-16 22:33:55 +05:30
self.deploy_keys_projects.count == 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)
deploy_keys_projects.find_by(project: project)
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
def projects_with_write_access
2019-12-16 22:33:55 +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
2014-09-02 18:07:02 +05:30
end