Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.restapi.account; 16 : 17 : import com.google.common.flogger.FluentLogger; 18 : import com.google.gerrit.entities.Account; 19 : import com.google.gerrit.entities.AccountGroup; 20 : import com.google.gerrit.exceptions.NoSuchGroupException; 21 : import com.google.gerrit.extensions.common.GroupInfo; 22 : import com.google.gerrit.extensions.restapi.Response; 23 : import com.google.gerrit.extensions.restapi.RestReadView; 24 : import com.google.gerrit.server.IdentifiedUser; 25 : import com.google.gerrit.server.account.AccountResource; 26 : import com.google.gerrit.server.account.GroupControl; 27 : import com.google.gerrit.server.permissions.PermissionBackendException; 28 : import com.google.gerrit.server.restapi.group.GroupJson; 29 : import com.google.inject.Inject; 30 : import com.google.inject.Singleton; 31 : import java.util.ArrayList; 32 : import java.util.List; 33 : import java.util.Set; 34 : 35 : /** 36 : * REST endpoint to get all known groups of an account (groups that contain the account as member). 37 : * 38 : * <p>This REST endpoint handles {@code GET /accounts/<account-identifier>/groups} requests. 39 : * 40 : * <p>The response may not contain all groups of the account as not all groups may be known (see 41 : * {@link com.google.gerrit.server.account.GroupMembership#getKnownGroups()}). In addition groups 42 : * that are not visible to the calling user are filtered out. 43 : */ 44 : @Singleton 45 : public class GetGroups implements RestReadView<AccountResource> { 46 148 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 47 : 48 : private final GroupControl.Factory groupControlFactory; 49 : private final GroupJson json; 50 : 51 : @Inject 52 148 : GetGroups(GroupControl.Factory groupControlFactory, GroupJson json) { 53 148 : this.groupControlFactory = groupControlFactory; 54 148 : this.json = json; 55 148 : } 56 : 57 : @Override 58 : public Response<List<GroupInfo>> apply(AccountResource resource) 59 : throws PermissionBackendException { 60 2 : IdentifiedUser user = resource.getUser(); 61 2 : Account.Id userId = user.getAccountId(); 62 2 : Set<AccountGroup.UUID> knownGroups = user.getEffectiveGroups().getKnownGroups(); 63 2 : List<GroupInfo> visibleGroups = new ArrayList<>(); 64 2 : for (AccountGroup.UUID uuid : knownGroups) { 65 : GroupControl ctl; 66 : try { 67 2 : ctl = groupControlFactory.controlFor(uuid); 68 0 : } catch (NoSuchGroupException e) { 69 0 : logger.atFine().log("skipping non-existing group %s", uuid); 70 0 : continue; 71 2 : } 72 : 73 2 : if (!ctl.isVisible()) { 74 0 : logger.atFine().log("skipping non-visible group %s", uuid); 75 0 : continue; 76 : } 77 : 78 2 : if (!ctl.canSeeMember(userId)) { 79 0 : logger.atFine().log( 80 0 : "skipping group %s because member %d cannot be seen", uuid, userId.get()); 81 0 : continue; 82 : } 83 : 84 2 : visibleGroups.add(json.format(ctl.getGroup())); 85 2 : } 86 2 : return Response.ok(visibleGroups); 87 : } 88 : }