BigW Consortium Gitlab

project_group_link.rb 1.21 KB
Newer Older
1
class ProjectGroupLink < ActiveRecord::Base
2 3
  include Expirable

4 5 6 7 8 9 10 11 12
  GUEST     = 10
  REPORTER  = 20
  DEVELOPER = 30
  MASTER    = 40

  belongs_to :project
  belongs_to :group

  validates :project_id, presence: true
13
  validates :group, presence: true
14 15 16 17 18
  validates :group_id, uniqueness: { scope: [:project_id], message: "already shared with this group" }
  validates :group_access, presence: true
  validates :group_access, inclusion: { in: Gitlab::Access.values }, presence: true
  validate :different_group

19
  after_commit :refresh_group_members_authorized_projects
20

21 22 23 24 25 26 27 28 29 30 31 32
  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

33
  private
34 35

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

  def refresh_group_members_authorized_projects
    group.refresh_members_authorized_projects
  end
51
end