LCOV - code coverage report
Current view: top level - server/restapi/account - GetEmails.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 16 16 100.0 %
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.account;
      16             : 
      17             : import static java.util.Comparator.comparing;
      18             : import static java.util.stream.Collectors.toList;
      19             : 
      20             : import com.google.gerrit.extensions.common.EmailInfo;
      21             : import com.google.gerrit.extensions.restapi.AuthException;
      22             : import com.google.gerrit.extensions.restapi.Response;
      23             : import com.google.gerrit.extensions.restapi.RestReadView;
      24             : import com.google.gerrit.server.CurrentUser;
      25             : import com.google.gerrit.server.account.AccountResource;
      26             : import com.google.gerrit.server.permissions.GlobalPermission;
      27             : import com.google.gerrit.server.permissions.PermissionBackend;
      28             : import com.google.gerrit.server.permissions.PermissionBackendException;
      29             : import com.google.inject.Inject;
      30             : import com.google.inject.Provider;
      31             : import com.google.inject.Singleton;
      32             : import java.util.List;
      33             : import java.util.Objects;
      34             : 
      35             : /**
      36             :  * REST endpoint to list the emails of an account.
      37             :  *
      38             :  * <p>This REST endpoint handles {@code GET /accounts/<account-identifier>/emails/} requests.
      39             :  */
      40             : @Singleton
      41             : public class GetEmails implements RestReadView<AccountResource> {
      42             :   private final Provider<CurrentUser> self;
      43             :   private final PermissionBackend permissionBackend;
      44             : 
      45             :   @Inject
      46         148 :   GetEmails(Provider<CurrentUser> self, PermissionBackend permissionBackend) {
      47         148 :     this.self = self;
      48         148 :     this.permissionBackend = permissionBackend;
      49         148 :   }
      50             : 
      51             :   @Override
      52             :   public Response<List<EmailInfo>> apply(AccountResource rsrc)
      53             :       throws AuthException, PermissionBackendException {
      54           2 :     if (!self.get().hasSameAccountId(rsrc.getUser())) {
      55           1 :       permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT);
      56             :     }
      57           2 :     return Response.ok(
      58           2 :         rsrc.getUser().getEmailAddresses().stream()
      59           2 :             .filter(Objects::nonNull)
      60           2 :             .map(e -> toEmailInfo(rsrc, e))
      61           2 :             .sorted(comparing((EmailInfo e) -> e.email))
      62           2 :             .collect(toList()));
      63             :   }
      64             : 
      65             :   private static EmailInfo toEmailInfo(AccountResource rsrc, String email) {
      66           2 :     EmailInfo e = new EmailInfo();
      67           2 :     e.email = email;
      68           2 :     e.preferred(rsrc.getUser().getAccount().preferredEmail());
      69           2 :     return e;
      70             :   }
      71             : }

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