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 com.google.gerrit.extensions.client.AuthType.DEVELOPMENT_BECOME_ANY_ACCOUNT; 18 : 19 : import com.google.common.flogger.FluentLogger; 20 : import com.google.gerrit.common.UsedAt; 21 : import com.google.gerrit.exceptions.EmailException; 22 : import com.google.gerrit.extensions.api.accounts.EmailInput; 23 : import com.google.gerrit.extensions.client.AccountFieldName; 24 : import com.google.gerrit.extensions.common.EmailInfo; 25 : import com.google.gerrit.extensions.restapi.BadRequestException; 26 : import com.google.gerrit.extensions.restapi.IdString; 27 : import com.google.gerrit.extensions.restapi.MethodNotAllowedException; 28 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 29 : import com.google.gerrit.extensions.restapi.Response; 30 : import com.google.gerrit.extensions.restapi.RestApiException; 31 : import com.google.gerrit.extensions.restapi.RestCollectionCreateView; 32 : import com.google.gerrit.server.CurrentUser; 33 : import com.google.gerrit.server.IdentifiedUser; 34 : import com.google.gerrit.server.account.AccountException; 35 : import com.google.gerrit.server.account.AccountManager; 36 : import com.google.gerrit.server.account.AccountResource; 37 : import com.google.gerrit.server.account.AuthRequest; 38 : import com.google.gerrit.server.account.Realm; 39 : import com.google.gerrit.server.config.AuthConfig; 40 : import com.google.gerrit.server.mail.send.MessageIdGenerator; 41 : import com.google.gerrit.server.mail.send.OutgoingEmailValidator; 42 : import com.google.gerrit.server.mail.send.RegisterNewEmailSender; 43 : import com.google.gerrit.server.permissions.GlobalPermission; 44 : import com.google.gerrit.server.permissions.PermissionBackend; 45 : import com.google.gerrit.server.permissions.PermissionBackendException; 46 : import com.google.inject.Inject; 47 : import com.google.inject.Provider; 48 : import com.google.inject.Singleton; 49 : import java.io.IOException; 50 : import org.eclipse.jgit.errors.ConfigInvalidException; 51 : 52 : /** 53 : * REST endpoint for registering a new email address for an account. 54 : * 55 : * <p>This REST endpoint handles {@code PUT 56 : * /accounts/<account-identifier>/emails/<email-identifier>} requests if the specified email doesn't 57 : * exist for the account yet. If it already exists, the request is handled by {@link PutEmail}. 58 : * 59 : * <p>Whether an email address can be registered for the account depends on whether the used {@link 60 : * Realm} supports this. 61 : * 62 : * <p>When a new email address is registered an email with a confirmation link is sent to that 63 : * address. Only when the receiver confirms the email by clicking on the confirmation link, the 64 : * email address is added to the account (see {@link 65 : * com.google.gerrit.server.restapi.config.ConfirmEmail}). Confirming an email address for an 66 : * account creates an external ID that links the email address to the account. An email address can 67 : * only be added to an account if it is not assigned to any other account yet. 68 : * 69 : * <p>In some cases it is allowed to skip the email confirmation and add the email directly (calling 70 : * user has 'Modify Account' capability or server is running in dev mode). 71 : */ 72 : @Singleton 73 : public class CreateEmail 74 : implements RestCollectionCreateView<AccountResource, AccountResource.Email, EmailInput> { 75 148 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 76 : 77 : private final Provider<CurrentUser> self; 78 : private final Realm realm; 79 : private final PermissionBackend permissionBackend; 80 : private final AccountManager accountManager; 81 : private final RegisterNewEmailSender.Factory registerNewEmailFactory; 82 : private final PutPreferred putPreferred; 83 : private final OutgoingEmailValidator validator; 84 : private final MessageIdGenerator messageIdGenerator; 85 : private final boolean isDevMode; 86 : private final AuthRequest.Factory authRequestFactory; 87 : 88 : @Inject 89 : CreateEmail( 90 : Provider<CurrentUser> self, 91 : Realm realm, 92 : PermissionBackend permissionBackend, 93 : AuthConfig authConfig, 94 : AccountManager accountManager, 95 : RegisterNewEmailSender.Factory registerNewEmailFactory, 96 : PutPreferred putPreferred, 97 : OutgoingEmailValidator validator, 98 : MessageIdGenerator messageIdGenerator, 99 148 : AuthRequest.Factory authRequestFactory) { 100 148 : this.self = self; 101 148 : this.realm = realm; 102 148 : this.permissionBackend = permissionBackend; 103 148 : this.accountManager = accountManager; 104 148 : this.registerNewEmailFactory = registerNewEmailFactory; 105 148 : this.putPreferred = putPreferred; 106 148 : this.validator = validator; 107 148 : this.isDevMode = authConfig.getAuthType() == DEVELOPMENT_BECOME_ANY_ACCOUNT; 108 148 : this.messageIdGenerator = messageIdGenerator; 109 148 : this.authRequestFactory = authRequestFactory; 110 148 : } 111 : 112 : @Override 113 : public Response<EmailInfo> apply(AccountResource rsrc, IdString id, EmailInput input) 114 : throws RestApiException, EmailException, MethodNotAllowedException, IOException, 115 : ConfigInvalidException, PermissionBackendException { 116 4 : if (input == null) { 117 0 : input = new EmailInput(); 118 : } 119 : 120 4 : if (!self.get().hasSameAccountId(rsrc.getUser()) || input.noConfirmation) { 121 3 : permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT); 122 : } 123 : 124 4 : if (!realm.allowsEdit(AccountFieldName.REGISTER_NEW_EMAIL)) { 125 0 : throw new MethodNotAllowedException("realm does not allow adding emails"); 126 : } 127 : 128 4 : return Response.created(apply(rsrc.getUser(), id, input)); 129 : } 130 : 131 : /** To be used from plugins that want to create emails without permission checks. */ 132 : @UsedAt(UsedAt.Project.PLUGIN_SERVICEUSER) 133 : public EmailInfo apply(IdentifiedUser user, IdString id, EmailInput input) 134 : throws RestApiException, EmailException, MethodNotAllowedException, IOException, 135 : ConfigInvalidException, PermissionBackendException { 136 4 : String email = id.get().trim(); 137 : 138 4 : if (input == null) { 139 0 : input = new EmailInput(); 140 : } 141 : 142 4 : if (input.email != null && !email.equals(input.email)) { 143 0 : throw new BadRequestException("email address must match URL"); 144 : } 145 : 146 4 : if (!validator.isValid(email)) { 147 1 : throw new BadRequestException("invalid email address"); 148 : } 149 : 150 4 : EmailInfo info = new EmailInfo(); 151 4 : info.email = email; 152 4 : if (input.noConfirmation || isDevMode) { 153 3 : if (isDevMode) { 154 0 : logger.atWarning().log("skipping email validation in developer mode"); 155 : } 156 : try { 157 3 : accountManager.link(user.getAccountId(), authRequestFactory.createForEmail(email)); 158 1 : } catch (AccountException e) { 159 1 : throw new ResourceConflictException(e.getMessage()); 160 3 : } 161 3 : if (input.preferred) { 162 1 : putPreferred.apply(new AccountResource.Email(user, email), null); 163 1 : info.preferred = true; 164 : } 165 : } else { 166 : try { 167 2 : RegisterNewEmailSender emailSender = registerNewEmailFactory.create(email); 168 2 : if (!emailSender.isAllowed()) { 169 0 : throw new MethodNotAllowedException("Not allowed to add email address " + email); 170 : } 171 2 : emailSender.setMessageId(messageIdGenerator.fromAccountUpdate(user.getAccountId())); 172 2 : emailSender.send(); 173 2 : info.pendingConfirmation = true; 174 0 : } catch (EmailException | RuntimeException e) { 175 0 : logger.atSevere().withCause(e).log("Cannot send email verification message to %s", email); 176 0 : throw e; 177 2 : } 178 : } 179 4 : return info; 180 : } 181 : }