LCOV - code coverage report
Current view: top level - server/mail/send - DeleteKeySender.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 38 39 97.4 %
Date: 2022-11-19 15:00:39 Functions: 10 10 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2019 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.mail.send;
      16             : 
      17             : import com.google.common.base.Joiner;
      18             : import com.google.gerrit.common.Nullable;
      19             : import com.google.gerrit.exceptions.EmailException;
      20             : import com.google.gerrit.extensions.api.changes.RecipientType;
      21             : import com.google.gerrit.server.IdentifiedUser;
      22             : import com.google.gerrit.server.account.AccountSshKey;
      23             : import com.google.inject.assistedinject.Assisted;
      24             : import com.google.inject.assistedinject.AssistedInject;
      25             : import java.util.Collections;
      26             : import java.util.List;
      27             : 
      28             : /**
      29             :  * Sender that informs a user by email about the removal of an SSH or GPG key from their account.
      30             :  */
      31             : public class DeleteKeySender extends OutgoingEmail {
      32             :   public interface Factory {
      33             :     DeleteKeySender create(IdentifiedUser user, AccountSshKey sshKey);
      34             : 
      35             :     DeleteKeySender create(IdentifiedUser user, List<String> gpgKeyFingerprints);
      36             :   }
      37             : 
      38             :   private final IdentifiedUser user;
      39             :   private final AccountSshKey sshKey;
      40             :   private final List<String> gpgKeyFingerprints;
      41             :   private final MessageIdGenerator messageIdGenerator;
      42             : 
      43             :   @AssistedInject
      44             :   public DeleteKeySender(
      45             :       EmailArguments args,
      46             :       MessageIdGenerator messageIdGenerator,
      47             :       @Assisted IdentifiedUser user,
      48             :       @Assisted AccountSshKey sshKey) {
      49           3 :     super(args, "deletekey");
      50           3 :     this.messageIdGenerator = messageIdGenerator;
      51           3 :     this.user = user;
      52           3 :     this.gpgKeyFingerprints = Collections.emptyList();
      53           3 :     this.sshKey = sshKey;
      54           3 :   }
      55             : 
      56             :   @AssistedInject
      57             :   public DeleteKeySender(
      58             :       EmailArguments args,
      59             :       MessageIdGenerator messageIdGenerator,
      60             :       @Assisted IdentifiedUser user,
      61             :       @Assisted List<String> gpgKeyFingerprints) {
      62           2 :     super(args, "deletekey");
      63           2 :     this.messageIdGenerator = messageIdGenerator;
      64           2 :     this.user = user;
      65           2 :     this.gpgKeyFingerprints = gpgKeyFingerprints;
      66           2 :     this.sshKey = null;
      67           2 :   }
      68             : 
      69             :   @Override
      70             :   protected void init() throws EmailException {
      71           3 :     super.init();
      72           3 :     setHeader("Subject", String.format("[Gerrit Code Review] %s Keys Deleted", getKeyType()));
      73           3 :     setMessageId(messageIdGenerator.fromAccountUpdate(user.getAccountId()));
      74           3 :     add(RecipientType.TO, user.getAccountId());
      75           3 :   }
      76             : 
      77             :   @Override
      78             :   protected boolean shouldSendMessage() {
      79           3 :     return true;
      80             :   }
      81             : 
      82             :   @Override
      83             :   protected void format() throws EmailException {
      84           3 :     appendText(textTemplate("DeleteKey"));
      85           3 :     if (useHtml()) {
      86           3 :       appendHtml(soyHtmlTemplate("DeleteKeyHtml"));
      87             :     }
      88           3 :   }
      89             : 
      90             :   @Override
      91             :   protected void setupSoyContext() {
      92           3 :     super.setupSoyContext();
      93           3 :     soyContextEmailData.put("email", getEmail());
      94           3 :     soyContextEmailData.put("gpgKeyFingerprints", getGpgKeyFingerprints());
      95           3 :     soyContextEmailData.put("keyType", getKeyType());
      96           3 :     soyContextEmailData.put("sshKey", getSshKey());
      97           3 :     soyContextEmailData.put("userNameEmail", getUserNameEmailFor(user.getAccountId()));
      98           3 :   }
      99             : 
     100             :   private String getEmail() {
     101           3 :     return user.getAccount().preferredEmail();
     102             :   }
     103             : 
     104             :   private String getKeyType() {
     105           3 :     if (sshKey != null) {
     106           3 :       return "SSH";
     107           2 :     } else if (gpgKeyFingerprints != null) {
     108           2 :       return "GPG";
     109             :     }
     110           0 :     throw new IllegalStateException("key type is not SSH or GPG");
     111             :   }
     112             : 
     113             :   @Nullable
     114             :   private String getSshKey() {
     115           3 :     return (sshKey != null) ? sshKey.sshPublicKey() + "\n" : null;
     116             :   }
     117             : 
     118             :   @Nullable
     119             :   private String getGpgKeyFingerprints() {
     120           3 :     if (!gpgKeyFingerprints.isEmpty()) {
     121           2 :       return Joiner.on("\n").join(gpgKeyFingerprints);
     122             :     }
     123           3 :     return null;
     124             :   }
     125             : }

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