LCOV - code coverage report
Current view: top level - server/restapi/group - GroupsCollection.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 26 31 83.9 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          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.group;
      16             : 
      17             : import com.google.common.collect.ListMultimap;
      18             : import com.google.gerrit.entities.GroupDescription;
      19             : import com.google.gerrit.extensions.registration.DynamicMap;
      20             : import com.google.gerrit.extensions.restapi.AuthException;
      21             : import com.google.gerrit.extensions.restapi.BadRequestException;
      22             : import com.google.gerrit.extensions.restapi.IdString;
      23             : import com.google.gerrit.extensions.restapi.NeedsParams;
      24             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      25             : import com.google.gerrit.extensions.restapi.RestCollection;
      26             : import com.google.gerrit.extensions.restapi.RestView;
      27             : import com.google.gerrit.extensions.restapi.TopLevelResource;
      28             : import com.google.gerrit.server.AnonymousUser;
      29             : import com.google.gerrit.server.CurrentUser;
      30             : import com.google.gerrit.server.account.GroupControl;
      31             : import com.google.gerrit.server.group.GroupResolver;
      32             : import com.google.gerrit.server.group.GroupResource;
      33             : import com.google.inject.Inject;
      34             : import com.google.inject.Provider;
      35             : 
      36             : public class GroupsCollection
      37             :     implements RestCollection<TopLevelResource, GroupResource>, NeedsParams {
      38             :   private final DynamicMap<RestView<GroupResource>> views;
      39             :   private final Provider<ListGroups> list;
      40             :   private final Provider<QueryGroups> queryGroups;
      41             :   private final GroupControl.Factory groupControlFactory;
      42             :   private final GroupResolver groupResolver;
      43             :   private final Provider<CurrentUser> self;
      44             : 
      45             :   private boolean hasQuery;
      46             : 
      47             :   @Inject
      48             :   public GroupsCollection(
      49             :       DynamicMap<RestView<GroupResource>> views,
      50             :       Provider<ListGroups> list,
      51             :       Provider<QueryGroups> queryGroups,
      52             :       GroupControl.Factory groupControlFactory,
      53             :       GroupResolver groupResolver,
      54         149 :       Provider<CurrentUser> self) {
      55         149 :     this.views = views;
      56         149 :     this.list = list;
      57         149 :     this.queryGroups = queryGroups;
      58         149 :     this.groupControlFactory = groupControlFactory;
      59         149 :     this.groupResolver = groupResolver;
      60         149 :     this.self = self;
      61         149 :   }
      62             : 
      63             :   @Override
      64             :   public void setParams(ListMultimap<String, String> params) throws BadRequestException {
      65           3 :     this.hasQuery = params.containsKey("query");
      66           3 :   }
      67             : 
      68             :   @Override
      69             :   public RestView<TopLevelResource> list() throws ResourceNotFoundException, AuthException {
      70           3 :     final CurrentUser user = self.get();
      71           3 :     if (user instanceof AnonymousUser) {
      72           0 :       throw new AuthException("Authentication required");
      73           3 :     } else if (!user.isIdentifiedUser()) {
      74           0 :       throw new ResourceNotFoundException();
      75             :     }
      76             : 
      77           3 :     if (hasQuery) {
      78           0 :       return queryGroups.get();
      79             :     }
      80             : 
      81           3 :     return list.get();
      82             :   }
      83             : 
      84             :   @Override
      85             :   public GroupResource parse(TopLevelResource parent, IdString id)
      86             :       throws AuthException, ResourceNotFoundException {
      87          32 :     final CurrentUser user = self.get();
      88          32 :     if (user instanceof AnonymousUser) {
      89           0 :       throw new AuthException("Authentication required");
      90          32 :     } else if (!(user.isIdentifiedUser() || user.isInternalUser())) {
      91           0 :       throw new ResourceNotFoundException(id);
      92             :     }
      93             : 
      94          32 :     GroupDescription.Basic group = groupResolver.parseId(id.get());
      95          32 :     if (group == null) {
      96          10 :       throw new ResourceNotFoundException(id.get());
      97             :     }
      98          31 :     GroupControl ctl = groupControlFactory.controlFor(group);
      99          31 :     if (!ctl.isVisible()) {
     100           1 :       throw new ResourceNotFoundException(id);
     101             :     }
     102          30 :     return new GroupResource(ctl);
     103             :   }
     104             : 
     105             :   @Override
     106             :   public DynamicMap<RestView<GroupResource>> views() {
     107           3 :     return views;
     108             :   }
     109             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750