Line data Source code
1 : // Copyright (C) 2014 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.account; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.common.collect.Sets; 19 : import com.google.gerrit.extensions.client.AccountFieldName; 20 : import com.google.gerrit.server.IdentifiedUser; 21 : import com.google.gerrit.server.account.externalids.ExternalId; 22 : import com.google.gerrit.server.mail.send.EmailSender; 23 : import com.google.inject.Inject; 24 : import java.util.Collection; 25 : import java.util.HashSet; 26 : import java.util.Set; 27 : 28 : /** Basic implementation of {@link Realm}. */ 29 152 : public abstract class AbstractRealm implements Realm { 30 : private EmailSender emailSender; 31 : 32 : @Inject(optional = true) 33 : void setEmailSender(EmailSender emailSender) { 34 152 : this.emailSender = emailSender; 35 152 : } 36 : 37 : @Override 38 : public Set<AccountFieldName> getEditableFields() { 39 3 : Set<AccountFieldName> fields = new HashSet<>(); 40 3 : for (AccountFieldName n : AccountFieldName.values()) { 41 3 : if (allowsEdit(n)) { 42 3 : if (n == AccountFieldName.REGISTER_NEW_EMAIL) { 43 3 : if (emailSender != null && emailSender.isEnabled()) { 44 3 : fields.add(n); 45 : } 46 : } else { 47 3 : fields.add(n); 48 : } 49 : } 50 : } 51 3 : return fields; 52 : } 53 : 54 : @Override 55 : public boolean hasEmailAddress(IdentifiedUser user, String email) { 56 108 : for (ExternalId ext : user.state().externalIds()) { 57 108 : if (email != null && email.equalsIgnoreCase(ext.email())) { 58 104 : return true; 59 : } 60 44 : } 61 38 : return false; 62 : } 63 : 64 : @Override 65 : public Set<String> getEmailAddresses(IdentifiedUser user) { 66 51 : Collection<ExternalId> ids = user.state().externalIds(); 67 51 : Set<String> emails = Sets.newHashSetWithExpectedSize(ids.size()); 68 51 : for (ExternalId ext : ids) { 69 51 : if (!Strings.isNullOrEmpty(ext.email())) { 70 51 : emails.add(ext.email()); 71 : } 72 51 : } 73 51 : return emails; 74 : } 75 : }