2019-08-05 16:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ContainerRepositoriesFinder
|
2019-11-04 10:07:36 -05:00
|
|
|
VALID_SUBJECTS = [Group, Project].freeze
|
|
|
|
|
|
|
|
def initialize(user:, subject:)
|
|
|
|
@user = user
|
|
|
|
@subject = subject
|
2019-08-05 16:00:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2019-11-04 10:07:36 -05:00
|
|
|
raise ArgumentError, "invalid subject_type" unless valid_subject_type?
|
|
|
|
return unless authorized?
|
|
|
|
|
|
|
|
return project_repositories if @subject.is_a?(Project)
|
|
|
|
return group_repositories if @subject.is_a?(Group)
|
2019-08-05 16:00:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
def valid_subject_type?
|
|
|
|
VALID_SUBJECTS.include?(@subject.class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_repositories
|
|
|
|
return unless @subject.container_registry_enabled
|
2019-08-05 16:00:50 -04:00
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
@subject.container_repositories
|
2019-08-05 16:00:50 -04:00
|
|
|
end
|
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
def group_repositories
|
|
|
|
ContainerRepository.for_group_and_its_subgroups(@subject)
|
2019-08-05 16:00:50 -04:00
|
|
|
end
|
|
|
|
|
2019-11-04 10:07:36 -05:00
|
|
|
def authorized?
|
|
|
|
Ability.allowed?(@user, :read_container_image, @subject)
|
2019-08-05 16:00:50 -04:00
|
|
|
end
|
|
|
|
end
|