2019-10-30 15:14:17 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class GroupGroupLink < ApplicationRecord
|
|
|
|
include Expirable
|
|
|
|
|
|
|
|
belongs_to :shared_group, class_name: 'Group', foreign_key: :shared_group_id
|
|
|
|
belongs_to :shared_with_group, class_name: 'Group', foreign_key: :shared_with_group_id
|
|
|
|
|
|
|
|
validates :shared_group, presence: true
|
|
|
|
validates :shared_group_id, uniqueness: { scope: [:shared_with_group_id],
|
|
|
|
message: _('The group has already been shared with this group') }
|
|
|
|
validates :shared_with_group, presence: true
|
2020-02-03 00:09:03 +00:00
|
|
|
validates :group_access, inclusion: { in: Gitlab::Access.all_values },
|
2019-10-30 15:14:17 +00:00
|
|
|
presence: true
|
|
|
|
|
2020-04-09 18:09:34 +00:00
|
|
|
scope :non_guests, -> { where('group_access > ?', Gitlab::Access::GUEST) }
|
2022-08-09 15:11:31 +00:00
|
|
|
|
|
|
|
scope :with_owner_or_maintainer_access, -> do
|
|
|
|
where(group_access: [Gitlab::Access::OWNER, Gitlab::Access::MAINTAINER])
|
|
|
|
end
|
|
|
|
|
2022-09-13 18:12:21 +00:00
|
|
|
scope :with_owner_access, -> do
|
|
|
|
where(group_access: [Gitlab::Access::OWNER])
|
|
|
|
end
|
|
|
|
|
2022-08-09 15:11:31 +00:00
|
|
|
scope :groups_accessible_via, -> (shared_with_group_ids) do
|
|
|
|
links = where(shared_with_group_id: shared_with_group_ids)
|
|
|
|
# a group share also gives you access to the descendants of the group being shared,
|
|
|
|
# so we must include the descendants as well in the result.
|
|
|
|
Group.id_in(links.select(:shared_group_id)).self_and_descendants
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :groups_having_access_to, -> (shared_group_ids) do
|
|
|
|
links = where(shared_group_id: shared_group_ids)
|
|
|
|
Group.id_in(links.select(:shared_with_group_id))
|
|
|
|
end
|
|
|
|
|
2022-01-17 00:14:49 +00:00
|
|
|
scope :preload_shared_with_groups, -> { preload(:shared_with_group) }
|
2020-04-09 18:09:34 +00:00
|
|
|
|
2022-04-12 09:08:22 +00:00
|
|
|
scope :distinct_on_shared_with_group_id_with_group_access, -> do
|
|
|
|
distinct_group_links = select('DISTINCT ON (shared_with_group_id) *')
|
|
|
|
.order('shared_with_group_id, group_access DESC, expires_at DESC, created_at ASC')
|
|
|
|
|
|
|
|
unscoped.from(distinct_group_links, :group_group_links)
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :shared_from, :shared_group
|
|
|
|
|
|
|
|
def self.search(query)
|
|
|
|
joins(:shared_with_group).merge(Group.search(query))
|
|
|
|
end
|
|
|
|
|
2019-10-30 15:14:17 +00:00
|
|
|
def self.access_options
|
2020-02-03 00:09:03 +00:00
|
|
|
Gitlab::Access.options_with_owner
|
2019-10-30 15:14:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.default_access
|
|
|
|
Gitlab::Access::DEVELOPER
|
|
|
|
end
|
2020-01-13 15:07:53 +00:00
|
|
|
|
|
|
|
def human_access
|
|
|
|
Gitlab::Access.human_access(self.group_access)
|
|
|
|
end
|
2019-10-30 15:14:17 +00:00
|
|
|
end
|
2022-05-13 15:07:43 +00:00
|
|
|
|
|
|
|
GroupGroupLink.prepend_mod_with('GroupGroupLink')
|