debian-mirror-gitlab/app/models/project_group_link.rb

56 lines
1.4 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class ProjectGroupLink < ApplicationRecord
2016-09-13 17:45:13 +05:30
include Expirable
2021-06-08 01:23:25 +05:30
include EachBatch
2016-09-13 17:45:13 +05:30
2016-06-02 11:05:42 +05:30
belongs_to :project
belongs_to :group
validates :project_id, presence: true
2016-11-03 12:29:30 +05:30
validates :group, presence: true
2022-11-25 23:54:43 +05:30
validates :group_id, uniqueness: { scope: [:project_id], message: N_("already shared with this group") }
2016-06-02 11:05:42 +05:30
validates :group_access, presence: true
validates :group_access, inclusion: { in: Gitlab::Access.values }, presence: true
validate :different_group
2020-04-22 19:07:51 +05:30
scope :non_guests, -> { where('group_access > ?', Gitlab::Access::GUEST) }
2021-10-29 20:43:33 +05:30
scope :in_group, -> (group_ids) { where(group_id: group_ids) }
2020-04-22 19:07:51 +05:30
2020-03-13 15:44:24 +05:30
alias_method :shared_with_group, :group
2022-06-21 17:19:12 +05:30
alias_method :shared_from, :project
2020-03-13 15:44:24 +05:30
2016-06-02 11:05:42 +05:30
def self.access_options
Gitlab::Access.options
end
def self.default_access
2020-04-22 19:07:51 +05:30
Gitlab::Access::DEVELOPER
2016-06-02 11:05:42 +05:30
end
2020-03-13 15:44:24 +05:30
def self.search(query)
joins(:group).merge(Group.search(query))
end
2016-06-02 11:05:42 +05:30
def human_access
self.class.access_options.key(self.group_access)
end
2016-09-13 17:45:13 +05:30
private
2016-06-02 11:05:42 +05:30
def different_group
2017-08-17 22:00:37 +05:30
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-07-31 22:56:46 +05:30
errors.add(:base, _("Project cannot be shared with the group it is in or one of its ancestors."))
2016-06-02 11:05:42 +05:30
end
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
ProjectGroupLink.prepend_mod_with('ProjectGroupLink')