diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-01-20 21:10:05 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-01-20 21:10:05 +0300 |
commit | cc4e1c884cd6b8782fb6a247d840a2d1c7f4603e (patch) | |
tree | 8a54c659b82873efafe04887708140785caea153 /app/finders | |
parent | 709948b7a69597b1efe24df9b0f388cc0b493dd9 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/finders')
-rw-r--r-- | app/finders/groups/accepting_project_shares_finder.rb | 52 | ||||
-rw-r--r-- | app/finders/groups/base.rb | 2 |
2 files changed, 53 insertions, 1 deletions
diff --git a/app/finders/groups/accepting_project_shares_finder.rb b/app/finders/groups/accepting_project_shares_finder.rb new file mode 100644 index 00000000000..c4963fcc352 --- /dev/null +++ b/app/finders/groups/accepting_project_shares_finder.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +# AcceptingProjectSharesFinder +# +# Used to filter Shareable Groups by a set of params +# +# Arguments: +# current_user - which user is requesting groups +# params: +# search: string +module Groups + class AcceptingProjectSharesFinder < Base + def initialize(current_user, project_to_be_shared, params = {}) + @current_user = current_user + @params = params + @project_to_be_shared = project_to_be_shared + end + + def execute + return Group.none unless can_share_project? + + groups = if has_admin_access? + Group.all + else + groups_with_guest_access_plus + end + + groups = groups.search(params[:search]) if params[:search].present? + + sort(groups).with_route + end + + private + + attr_reader :current_user, :project_to_be_shared, :params + + def has_admin_access? + current_user&.can_read_all_resources? + end + + # rubocop: disable CodeReuse/Finder + def groups_with_guest_access_plus + GroupsFinder.new(current_user, min_access_level: Gitlab::Access::GUEST).execute + end + # rubocop: enable CodeReuse/Finder + + def can_share_project? + Ability.allowed?(current_user, :admin_project, project_to_be_shared) && + project_to_be_shared.allowed_to_share_with_group? + end + end +end diff --git a/app/finders/groups/base.rb b/app/finders/groups/base.rb index d7f56b1a7a6..9d2f9f60a63 100644 --- a/app/finders/groups/base.rb +++ b/app/finders/groups/base.rb @@ -5,7 +5,7 @@ module Groups private def sort(items) - items.order(Group.arel_table[:path].asc, Group.arel_table[:id].asc) # rubocop: disable CodeReuse/ActiveRecord + items.reorder(Group.arel_table[:path].asc, Group.arel_table[:id].asc) # rubocop: disable CodeReuse/ActiveRecord end def by_search(items) |