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.gerrit.exceptions.EmailException; 18 : import com.google.gerrit.extensions.api.changes.RecipientType; 19 : import com.google.gerrit.server.IdentifiedUser; 20 : import com.google.gerrit.server.util.time.TimeUtil; 21 : import com.google.inject.assistedinject.Assisted; 22 : import com.google.inject.assistedinject.AssistedInject; 23 : 24 : /** Sender that informs a user by email that the HTTP password of their account was updated. */ 25 : public class HttpPasswordUpdateSender extends OutgoingEmail { 26 : public interface Factory { 27 : HttpPasswordUpdateSender create(IdentifiedUser user, String operation); 28 : } 29 : 30 : private final IdentifiedUser user; 31 : private final String operation; 32 : private final MessageIdGenerator messageIdGenerator; 33 : 34 : @AssistedInject 35 : public HttpPasswordUpdateSender( 36 : EmailArguments args, 37 : MessageIdGenerator messageIdGenerator, 38 : @Assisted IdentifiedUser user, 39 : @Assisted String operation) { 40 6 : super(args, "HttpPasswordUpdate"); 41 6 : this.messageIdGenerator = messageIdGenerator; 42 6 : this.user = user; 43 6 : this.operation = operation; 44 6 : } 45 : 46 : @Override 47 : protected void init() throws EmailException { 48 6 : super.init(); 49 6 : setHeader("Subject", "[Gerrit Code Review] HTTP password was " + operation); 50 6 : setMessageId( 51 6 : messageIdGenerator.fromReasonAccountIdAndTimestamp( 52 6 : "HTTP_password_change", user.getAccountId(), TimeUtil.now())); 53 6 : add(RecipientType.TO, user.getAccountId()); 54 6 : } 55 : 56 : @Override 57 : protected boolean shouldSendMessage() { 58 : // Always send an email if the HTTP password is updated. 59 6 : return true; 60 : } 61 : 62 : @Override 63 : protected void format() throws EmailException { 64 6 : appendText(textTemplate("HttpPasswordUpdate")); 65 6 : if (useHtml()) { 66 6 : appendHtml(soyHtmlTemplate("HttpPasswordUpdateHtml")); 67 : } 68 6 : } 69 : 70 : @Override 71 : protected void setupSoyContext() { 72 6 : super.setupSoyContext(); 73 6 : soyContextEmailData.put("email", getEmail()); 74 6 : soyContextEmailData.put("userNameEmail", getUserNameEmailFor(user.getAccountId())); 75 6 : soyContextEmailData.put("operation", operation); 76 6 : } 77 : 78 : private String getEmail() { 79 6 : return user.getAccount().preferredEmail(); 80 : } 81 : }