From 4ca6ebf017e93686ee885ee1a28dc5c6934c9d39 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Thu, 5 Jun 2014 20:36:59 +0300 Subject: [PATCH] Add GroupFinder for collection all groups user has access to --- app/finders/groups_finder.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+), 0 deletions(-) create mode 100644 app/finders/groups_finder.rb diff --git a/app/finders/groups_finder.rb b/app/finders/groups_finder.rb new file mode 100644 index 0000000..d3597ef --- /dev/null +++ b/app/finders/groups_finder.rb @@ -0,0 +1,38 @@ +class GroupsFinder + def execute(current_user, options = {}) + all_groups(current_user) + end + + private + + def all_groups(current_user) + if current_user + if current_user.authorized_groups.any? + # User has access to groups + # + # Return only: + # groups with public projects + # groups with internal projects + # groups with joined projects + # + group_ids = Project.public_and_internal_only.pluck(:namespace_id) + + current_user.authorized_groups.pluck(:id) + Group.where(id: group_ids) + else + # User has no group membership + # + # Return only: + # groups with public projects + # groups with internal projects + # + Group.where(id: Project.public_and_internal_only.pluck(:namespace_id)) + end + else + # Not authenticated + # + # Return only: + # groups with public projects + Group.where(id: Project.public_only.pluck(:namespace_id)) + end + end +end -- libgit2 0.21.2