Read about our upcoming Code of Conduct on this issue

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

3
class ProjectGroupLink < ApplicationRecord
4
5
  include Expirable

6
7
8
  GUEST     = 10
  REPORTER  = 20
  DEVELOPER = 30
9
10
  MAINTAINER = 40
  MASTER = MAINTAINER # @deprecated
11
12
13
14
15

  belongs_to :project
  belongs_to :group

  validates :project_id, presence: true
16
  validates :group, presence: true
17
  validates :group_id, uniqueness: { scope: [:project_id], message: _("already shared with this group") }
18
19
20
21
  validates :group_access, presence: true
  validates :group_access, inclusion: { in: Gitlab::Access.values }, presence: true
  validate :different_group

22
  after_commit :refresh_group_members_authorized_projects
23

24
25
26
27
28
29
30
31
32
33
34
35
  def self.access_options
    Gitlab::Access.options
  end

  def self.default_access
    DEVELOPER
  end

  def human_access
    self.class.access_options.key(self.group_access)
  end

36
  private
37
38

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

  def refresh_group_members_authorized_projects
    group.refresh_members_authorized_projects
  end
54
end