LCOV - code coverage report
Current view: top level - server/restapi/account - GetExternalIds.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 23 24 95.8 %
Date: 2022-11-19 15:00:39 Functions: 3 3 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2017 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 static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USERNAME;
      18             : 
      19             : import com.google.common.collect.ImmutableList;
      20             : import com.google.common.collect.Lists;
      21             : import com.google.gerrit.common.Nullable;
      22             : import com.google.gerrit.extensions.common.AccountExternalIdInfo;
      23             : import com.google.gerrit.extensions.restapi.Response;
      24             : import com.google.gerrit.extensions.restapi.RestApiException;
      25             : import com.google.gerrit.extensions.restapi.RestReadView;
      26             : import com.google.gerrit.server.CurrentUser;
      27             : import com.google.gerrit.server.account.AccountResource;
      28             : import com.google.gerrit.server.account.externalids.ExternalId;
      29             : import com.google.gerrit.server.account.externalids.ExternalIds;
      30             : import com.google.gerrit.server.config.AuthConfig;
      31             : import com.google.gerrit.server.permissions.GlobalPermission;
      32             : import com.google.gerrit.server.permissions.PermissionBackend;
      33             : import com.google.gerrit.server.permissions.PermissionBackendException;
      34             : import com.google.inject.Inject;
      35             : import com.google.inject.Provider;
      36             : import com.google.inject.Singleton;
      37             : import java.io.IOException;
      38             : import java.util.Collection;
      39             : import java.util.Collections;
      40             : import java.util.List;
      41             : import java.util.Optional;
      42             : 
      43             : /**
      44             :  * REST endpoint to get the external IDs of an account.
      45             :  *
      46             :  * <p>This REST endpoint handles {@code GET /accounts/<account-identifier>/external.ids} requests.
      47             :  */
      48             : @Singleton
      49             : public class GetExternalIds implements RestReadView<AccountResource> {
      50             :   private final PermissionBackend permissionBackend;
      51             :   private final ExternalIds externalIds;
      52             :   private final Provider<CurrentUser> self;
      53             :   private final AuthConfig authConfig;
      54             : 
      55             :   @Inject
      56             :   GetExternalIds(
      57             :       PermissionBackend permissionBackend,
      58             :       ExternalIds externalIds,
      59             :       Provider<CurrentUser> self,
      60         148 :       AuthConfig authConfig) {
      61         148 :     this.permissionBackend = permissionBackend;
      62         148 :     this.externalIds = externalIds;
      63         148 :     this.self = self;
      64         148 :     this.authConfig = authConfig;
      65         148 :   }
      66             : 
      67             :   @Override
      68             :   public Response<List<AccountExternalIdInfo>> apply(AccountResource resource)
      69             :       throws RestApiException, IOException, PermissionBackendException {
      70           4 :     if (!self.get().hasSameAccountId(resource.getUser())) {
      71           2 :       permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT);
      72             :     }
      73             : 
      74           4 :     Collection<ExternalId> ids = externalIds.byAccount(resource.getUser().getAccountId());
      75           4 :     if (ids.isEmpty()) {
      76           0 :       return Response.ok(ImmutableList.of());
      77             :     }
      78           4 :     List<AccountExternalIdInfo> result = Lists.newArrayListWithCapacity(ids.size());
      79           4 :     for (ExternalId id : ids) {
      80           4 :       AccountExternalIdInfo info = new AccountExternalIdInfo();
      81           4 :       info.identity = id.key().get();
      82           4 :       info.emailAddress = id.email();
      83           4 :       info.trusted = toBoolean(authConfig.isIdentityTrustable(Collections.singleton(id)));
      84             :       // The identity can be deleted only if its not the one used to
      85             :       // establish this web session, and if only if an identity was
      86             :       // actually used to establish this web session.
      87           4 :       if (!id.isScheme(SCHEME_USERNAME)) {
      88           4 :         Optional<ExternalId.Key> last = resource.getUser().getLastLoginExternalIdKey();
      89           4 :         info.canDelete = toBoolean(!last.isPresent() || !last.get().get().equals(info.identity));
      90             :       }
      91           4 :       result.add(info);
      92           4 :     }
      93           4 :     return Response.ok(result);
      94             :   }
      95             : 
      96             :   @Nullable
      97             :   private static Boolean toBoolean(boolean v) {
      98           4 :     return v ? Boolean.TRUE : null;
      99             :   }
     100             : }

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