Commit 4ca6ebf017e93686ee885ee1a28dc5c6934c9d39
1 parent
70a6af93
Exists in
spb-stable
and in
2 other branches
Add GroupFinder for collection all groups user has access to
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
1 changed file
with
38 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,38 @@ | @@ -0,0 +1,38 @@ | ||
1 | +class GroupsFinder | ||
2 | + def execute(current_user, options = {}) | ||
3 | + all_groups(current_user) | ||
4 | + end | ||
5 | + | ||
6 | + private | ||
7 | + | ||
8 | + def all_groups(current_user) | ||
9 | + if current_user | ||
10 | + if current_user.authorized_groups.any? | ||
11 | + # User has access to groups | ||
12 | + # | ||
13 | + # Return only: | ||
14 | + # groups with public projects | ||
15 | + # groups with internal projects | ||
16 | + # groups with joined projects | ||
17 | + # | ||
18 | + group_ids = Project.public_and_internal_only.pluck(:namespace_id) + | ||
19 | + current_user.authorized_groups.pluck(:id) | ||
20 | + Group.where(id: group_ids) | ||
21 | + else | ||
22 | + # User has no group membership | ||
23 | + # | ||
24 | + # Return only: | ||
25 | + # groups with public projects | ||
26 | + # groups with internal projects | ||
27 | + # | ||
28 | + Group.where(id: Project.public_and_internal_only.pluck(:namespace_id)) | ||
29 | + end | ||
30 | + else | ||
31 | + # Not authenticated | ||
32 | + # | ||
33 | + # Return only: | ||
34 | + # groups with public projects | ||
35 | + Group.where(id: Project.public_only.pluck(:namespace_id)) | ||
36 | + end | ||
37 | + end | ||
38 | +end |