LCOV - code coverage report
Current view: top level - server/restapi/account - EmailsCollection.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 13 18 72.2 %
Date: 2022-11-19 15:00:39 Functions: 4 4 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 com.google.common.base.Strings;
      18             : import com.google.gerrit.extensions.registration.DynamicMap;
      19             : import com.google.gerrit.extensions.restapi.AuthException;
      20             : import com.google.gerrit.extensions.restapi.ChildCollection;
      21             : import com.google.gerrit.extensions.restapi.IdString;
      22             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      23             : import com.google.gerrit.extensions.restapi.RestView;
      24             : import com.google.gerrit.server.CurrentUser;
      25             : import com.google.gerrit.server.account.AccountResource;
      26             : import com.google.gerrit.server.account.AccountResource.Email;
      27             : import com.google.gerrit.server.permissions.GlobalPermission;
      28             : import com.google.gerrit.server.permissions.PermissionBackend;
      29             : import com.google.gerrit.server.permissions.PermissionBackendException;
      30             : import com.google.inject.Inject;
      31             : import com.google.inject.Provider;
      32             : import com.google.inject.Singleton;
      33             : 
      34             : @Singleton
      35             : public class EmailsCollection implements ChildCollection<AccountResource, AccountResource.Email> {
      36             :   private final DynamicMap<RestView<AccountResource.Email>> views;
      37             :   private final GetEmails list;
      38             :   private final Provider<CurrentUser> self;
      39             :   private final PermissionBackend permissionBackend;
      40             : 
      41             :   @Inject
      42             :   EmailsCollection(
      43             :       DynamicMap<RestView<AccountResource.Email>> views,
      44             :       GetEmails list,
      45             :       Provider<CurrentUser> self,
      46         138 :       PermissionBackend permissionBackend) {
      47         138 :     this.views = views;
      48         138 :     this.list = list;
      49         138 :     this.self = self;
      50         138 :     this.permissionBackend = permissionBackend;
      51         138 :   }
      52             : 
      53             :   @Override
      54             :   public RestView<AccountResource> list() {
      55           1 :     return list;
      56             :   }
      57             : 
      58             :   @Override
      59             :   public AccountResource.Email parse(AccountResource rsrc, IdString id)
      60             :       throws ResourceNotFoundException, PermissionBackendException, AuthException {
      61           2 :     if (!self.get().hasSameAccountId(rsrc.getUser())) {
      62           0 :       permissionBackend.currentUser().check(GlobalPermission.ADMINISTRATE_SERVER);
      63             :     }
      64             : 
      65           2 :     if ("preferred".equals(id.get())) {
      66           0 :       String email = rsrc.getUser().getAccount().preferredEmail();
      67           0 :       if (Strings.isNullOrEmpty(email)) {
      68           0 :         throw new ResourceNotFoundException(id);
      69             :       }
      70           0 :       return new AccountResource.Email(rsrc.getUser(), email);
      71           2 :     } else if (rsrc.getUser().hasEmailAddress(id.get())) {
      72           2 :       return new AccountResource.Email(rsrc.getUser(), id.get());
      73             :     } else {
      74           1 :       throw new ResourceNotFoundException(id);
      75             :     }
      76             :   }
      77             : 
      78             :   @Override
      79             :   public DynamicMap<RestView<Email>> views() {
      80           3 :     return views;
      81             :   }
      82             : }

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