LCOV - code coverage report
Current view: top level - server/restapi/account - PutAgreement.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 25 28 89.3 %
Date: 2022-11-19 15:00:39 Functions: 2 2 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2016 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.base.Strings;
      18             : import com.google.common.collect.ImmutableSet;
      19             : import com.google.gerrit.entities.AccountGroup;
      20             : import com.google.gerrit.entities.ContributorAgreement;
      21             : import com.google.gerrit.exceptions.NoSuchGroupException;
      22             : import com.google.gerrit.extensions.api.accounts.AgreementInput;
      23             : import com.google.gerrit.extensions.restapi.AuthException;
      24             : import com.google.gerrit.extensions.restapi.BadRequestException;
      25             : import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
      26             : import com.google.gerrit.extensions.restapi.ResourceConflictException;
      27             : import com.google.gerrit.extensions.restapi.Response;
      28             : import com.google.gerrit.extensions.restapi.RestApiException;
      29             : import com.google.gerrit.extensions.restapi.RestModifyView;
      30             : import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
      31             : import com.google.gerrit.server.IdentifiedUser;
      32             : import com.google.gerrit.server.account.AccountResource;
      33             : import com.google.gerrit.server.account.AccountState;
      34             : import com.google.gerrit.server.config.GerritServerConfig;
      35             : import com.google.gerrit.server.extensions.events.AgreementSignup;
      36             : import com.google.gerrit.server.project.ProjectCache;
      37             : import com.google.gerrit.server.restapi.group.AddMembers;
      38             : import com.google.inject.Inject;
      39             : import com.google.inject.Provider;
      40             : import com.google.inject.Singleton;
      41             : import java.io.IOException;
      42             : import org.eclipse.jgit.errors.ConfigInvalidException;
      43             : import org.eclipse.jgit.lib.Config;
      44             : 
      45             : /**
      46             :  * REST endpoint to sign a contributor agreement for an account.
      47             :  *
      48             :  * <p>This REST endpoint handles {@code PUT /accounts/<account-identifier>/agreements} requests.
      49             :  */
      50             : @Singleton
      51             : public class PutAgreement implements RestModifyView<AccountResource, AgreementInput> {
      52             :   private final ProjectCache projectCache;
      53             :   private final Provider<IdentifiedUser> self;
      54             :   private final AgreementSignup agreementSignup;
      55             :   private final AddMembers addMembers;
      56             :   private final boolean agreementsEnabled;
      57             : 
      58             :   @Inject
      59             :   PutAgreement(
      60             :       ProjectCache projectCache,
      61             :       Provider<IdentifiedUser> self,
      62             :       AgreementSignup agreementSignup,
      63             :       AddMembers addMembers,
      64         148 :       @GerritServerConfig Config config) {
      65         148 :     this.projectCache = projectCache;
      66         148 :     this.self = self;
      67         148 :     this.agreementSignup = agreementSignup;
      68         148 :     this.addMembers = addMembers;
      69         148 :     this.agreementsEnabled = config.getBoolean("auth", "contributorAgreements", false);
      70         148 :   }
      71             : 
      72             :   @Override
      73             :   public Response<String> apply(AccountResource resource, AgreementInput input)
      74             :       throws IOException, RestApiException, ConfigInvalidException {
      75           1 :     if (!agreementsEnabled) {
      76           1 :       throw new MethodNotAllowedException("contributor agreements disabled");
      77             :     }
      78             : 
      79           1 :     if (!self.get().hasSameAccountId(resource.getUser())) {
      80           1 :       throw new AuthException("not allowed to enter contributor agreement");
      81             :     }
      82             : 
      83           1 :     String agreementName = Strings.nullToEmpty(input.name);
      84           1 :     ContributorAgreement ca =
      85           1 :         projectCache.getAllProjects().getConfig().getContributorAgreements().get(agreementName);
      86           1 :     if (ca == null) {
      87           1 :       throw new UnprocessableEntityException("contributor agreement not found");
      88             :     }
      89             : 
      90           1 :     if (ca.getAutoVerify() == null) {
      91           1 :       throw new BadRequestException("cannot enter a non-autoVerify agreement");
      92             :     }
      93             : 
      94           1 :     AccountGroup.UUID uuid = ca.getAutoVerify().getUUID();
      95           1 :     if (uuid == null) {
      96           0 :       throw new ResourceConflictException("autoverify group uuid not found");
      97             :     }
      98             : 
      99           1 :     AccountState accountState = self.get().state();
     100             :     try {
     101           1 :       addMembers.addMembers(uuid, ImmutableSet.of(accountState.account().id()));
     102           0 :     } catch (NoSuchGroupException e) {
     103           0 :       throw new ResourceConflictException("autoverify group not found", e);
     104           1 :     }
     105           1 :     agreementSignup.fire(accountState, agreementName);
     106             : 
     107           1 :     return Response.ok(agreementName);
     108             :   }
     109             : }

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