project_group_link.rb 1.44 KB
Newer Older
1
2
# frozen_string_literal: true

3
class ProjectGroupLink < ApplicationRecord
4
5
  include Expirable

6
7
8
9
  belongs_to :project
  belongs_to :group

  validates :project_id, presence: true
10
  validates :group, presence: true
11
  validates :group_id, uniqueness: { scope: [:project_id], message: _("already shared with this group") }
12
13
14
15
  validates :group_access, presence: true
  validates :group_access, inclusion: { in: Gitlab::Access.values }, presence: true
  validate :different_group

16
17
  scope :non_guests, -> { where('group_access > ?', Gitlab::Access::GUEST) }

18
  after_commit :refresh_group_members_authorized_projects
19

20
21
  alias_method :shared_with_group, :group

22
23
24
25
26
  def self.access_options
    Gitlab::Access.options
  end

  def self.default_access
27
    Gitlab::Access::DEVELOPER
28
29
  end

30
31
32
33
  def self.search(query)
    joins(:group).merge(Group.search(query))
  end

34
35
36
37
  def human_access
    self.class.access_options.key(self.group_access)
  end

38
  private
39
40

  def different_group
41
42
43
44
45
46
47
48
    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)
49
      errors.add(:base, _("Project cannot be shared with the group it is in or one of its ancestors."))
50
51
    end
  end
52
53
54
55

  def refresh_group_members_authorized_projects
    group.refresh_members_authorized_projects
  end
56
end
57
58

ProjectGroupLink.prepend_if_ee('EE::ProjectGroupLink')