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

          Line data    Source code
       1             : // Copyright (C) 2009 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 static java.util.Objects.requireNonNull;
      18             : 
      19             : import com.google.gerrit.entities.Address;
      20             : import com.google.gerrit.exceptions.EmailException;
      21             : import com.google.gerrit.extensions.api.changes.RecipientType;
      22             : import com.google.gerrit.server.IdentifiedUser;
      23             : import com.google.gerrit.server.mail.EmailTokenVerifier;
      24             : import com.google.inject.Inject;
      25             : import com.google.inject.assistedinject.Assisted;
      26             : 
      27             : /**
      28             :  * Sender that informs a user by email about the registration of a new email address for their
      29             :  * account.
      30             :  */
      31             : public class RegisterNewEmailSender extends OutgoingEmail {
      32             :   public interface Factory {
      33             :     RegisterNewEmailSender create(String address);
      34             :   }
      35             : 
      36             :   private final EmailTokenVerifier tokenVerifier;
      37             :   private final IdentifiedUser user;
      38             :   private final String addr;
      39             :   private String emailToken;
      40             : 
      41             :   @Inject
      42             :   public RegisterNewEmailSender(
      43             :       EmailArguments args,
      44             :       EmailTokenVerifier tokenVerifier,
      45             :       IdentifiedUser callingUser,
      46             :       @Assisted final String address) {
      47           2 :     super(args, "registernewemail");
      48           2 :     this.tokenVerifier = tokenVerifier;
      49           2 :     this.user = callingUser;
      50           2 :     this.addr = address;
      51           2 :   }
      52             : 
      53             :   @Override
      54             :   protected void init() throws EmailException {
      55           2 :     super.init();
      56           2 :     setHeader("Subject", "[Gerrit Code Review] Email Verification");
      57           2 :     add(RecipientType.TO, Address.create(addr));
      58           2 :   }
      59             : 
      60             :   @Override
      61             :   protected void format() throws EmailException {
      62           2 :     appendText(textTemplate("RegisterNewEmail"));
      63           2 :     if (useHtml()) {
      64           2 :       appendHtml(soyHtmlTemplate("RegisterNewEmailHtml"));
      65             :     }
      66           2 :   }
      67             : 
      68             :   public boolean isAllowed() {
      69           2 :     return args.emailSender.canEmail(addr);
      70             :   }
      71             : 
      72             :   @Override
      73             :   protected void setupSoyContext() {
      74           2 :     super.setupSoyContext();
      75           2 :     soyContextEmailData.put("emailRegistrationToken", getEmailRegistrationToken());
      76           2 :     soyContextEmailData.put("userNameEmail", getUserNameEmailFor(user.getAccountId()));
      77           2 :   }
      78             : 
      79             :   private String getEmailRegistrationToken() {
      80           2 :     if (emailToken == null) {
      81           2 :       emailToken = requireNonNull(tokenVerifier.encode(user.getAccountId(), addr), "token");
      82             :     }
      83           2 :     return emailToken;
      84             :   }
      85             : }

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