2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class ProjectGroupLink < ApplicationRecord
|
2016-08-18 17:45:41 -04:00
|
|
|
include Expirable
|
|
|
|
|
2016-03-11 11:47:05 -05:00
|
|
|
belongs_to :project
|
|
|
|
belongs_to :group
|
|
|
|
|
|
|
|
validates :project_id, presence: true
|
2016-10-11 06:20:35 -04:00
|
|
|
validates :group, presence: true
|
2019-04-12 08:28:07 -04:00
|
|
|
validates :group_id, uniqueness: { scope: [:project_id], message: _("already shared with this group") }
|
2016-03-11 11:47:05 -05:00
|
|
|
validates :group_access, presence: true
|
|
|
|
validates :group_access, inclusion: { in: Gitlab::Access.values }, presence: true
|
|
|
|
validate :different_group
|
|
|
|
|
2020-04-09 14:09:34 -04:00
|
|
|
scope :non_guests, -> { where('group_access > ?', Gitlab::Access::GUEST) }
|
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
alias_method :shared_with_group, :group
|
|
|
|
|
2016-03-11 11:47:05 -05:00
|
|
|
def self.access_options
|
|
|
|
Gitlab::Access.options
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.default_access
|
2020-04-09 14:09:34 -04:00
|
|
|
Gitlab::Access::DEVELOPER
|
2016-03-11 11:47:05 -05:00
|
|
|
end
|
|
|
|
|
2020-01-31 07:08:33 -05:00
|
|
|
def self.search(query)
|
|
|
|
joins(:group).merge(Group.search(query))
|
|
|
|
end
|
|
|
|
|
2016-03-11 11:47:05 -05:00
|
|
|
def human_access
|
|
|
|
self.class.access_options.key(self.group_access)
|
|
|
|
end
|
|
|
|
|
2016-08-18 17:45:41 -04:00
|
|
|
private
|
2016-03-11 11:47:05 -05:00
|
|
|
|
|
|
|
def different_group
|
2017-02-27 17:38:45 -05:00
|
|
|
return unless self.group && self.project
|
|
|
|
|
|
|
|
project_group = self.project.group
|
|
|
|
return unless project_group
|
|
|
|
|
|
|
|
group_ids = project_group.ancestors.map(&:id).push(project_group.id)
|
|
|
|
|
|
|
|
if group_ids.include?(self.group.id)
|
2019-04-12 08:28:07 -04:00
|
|
|
errors.add(:base, _("Project cannot be shared with the group it is in or one of its ancestors."))
|
2016-03-11 11:47:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
ProjectGroupLink.prepend_if_ee('EE::ProjectGroupLink')
|