Add hasEmailAddress to IdentifiedUser

This allows validation code to pass in an email address
for checking, and lets IdentifiedUser worry about how
that occurs.

This refactoring opens the door to delegating the check
to backend systems that are not able to supply the entire
valid email list for a user.

Change-Id: I3db2b34d311d0c8f6f5c0cfdaa109d44fd79e264
This commit is contained in:
Shawn Pearce
2014-12-19 15:36:14 -08:00
parent a1f795a12d
commit a33543d7ea
7 changed files with 12 additions and 10 deletions

View File

@@ -144,7 +144,7 @@ class AccountSecurityImpl extends BaseServiceImplementation implements
me.setFullName(Strings.emptyToNull(name));
}
if (!Strings.isNullOrEmpty(emailAddr)
&& !self.getEmailAddresses().contains(emailAddr)) {
&& !self.hasEmailAddress(emailAddr)) {
throw new Failure(new PermissionDeniedException("Email address must be verified"));
}
me.setPreferredEmail(Strings.emptyToNull(emailAddr));

View File

@@ -270,6 +270,10 @@ public class IdentifiedUser extends CurrentUser {
return diffPref;
}
public boolean hasEmailAddress(String email) {
return getEmailAddresses().contains(email);
}
public Set<String> getEmailAddresses() {
if (emailAddresses == null) {
emailAddresses = state().getEmailAddresses();

View File

@@ -66,7 +66,7 @@ public class Emails implements
throw new ResourceNotFoundException();
}
return new AccountResource.Email(rsrc.getUser(), email);
} else if (rsrc.getUser().getEmailAddresses().contains(id.get())) {
} else if (rsrc.getUser().hasEmailAddress(id.get())) {
return new AccountResource.Email(rsrc.getUser(), id.get());
} else {
throw new ResourceNotFoundException();

View File

@@ -379,8 +379,7 @@ public class MergeUtil {
final Timestamp dt = submitter.getGranted();
final TimeZone tz = myIdent.getTimeZone();
if (emails.size() == 1
&& who.getEmailAddresses().contains(emails.iterator().next())) {
if (emails.size() == 1 && who.hasEmailAddress(emails.iterator().next())) {
authorIdent =
new PersonIdent(codeReviewCommits.get(0).getAuthorIdent(), dt, tz);
} else {

View File

@@ -2279,7 +2279,7 @@ public class ReceiveCommits {
break;
}
if (defaultName && currentUser.getEmailAddresses().contains(
if (defaultName && currentUser.hasEmailAddress(
c.getCommitterIdent().getEmailAddress())) {
try {
Account a = db.accounts().get(currentUser.getAccountId());

View File

@@ -394,7 +394,7 @@ public class CommitValidators {
if (e != null) {
sboAuthor |= author.getEmailAddress().equals(e);
sboCommitter |= committer.getEmailAddress().equals(e);
sboMe |= currentUser.getEmailAddresses().contains(e);
sboMe |= currentUser.hasEmailAddress(e);
}
}
}
@@ -425,7 +425,7 @@ public class CommitValidators {
IdentifiedUser currentUser = (IdentifiedUser) refControl.getCurrentUser();
final PersonIdent author = receiveEvent.commit.getAuthorIdent();
if (!currentUser.getEmailAddresses().contains(author.getEmailAddress())
if (!currentUser.hasEmailAddress(author.getEmailAddress())
&& !refControl.canForgeAuthor()) {
List<CommitValidationMessage> messages = new LinkedList<>();
@@ -454,8 +454,7 @@ public class CommitValidators {
CommitReceivedEvent receiveEvent) throws CommitValidationException {
IdentifiedUser currentUser = (IdentifiedUser) refControl.getCurrentUser();
final PersonIdent committer = receiveEvent.commit.getCommitterIdent();
if (!currentUser.getEmailAddresses()
.contains(committer.getEmailAddress())
if (!currentUser.hasEmailAddress(committer.getEmailAddress())
&& !refControl.canForgeCommitter()) {
List<CommitValidationMessage> messages = new LinkedList<>();
messages.add(getInvalidEmailError(receiveEvent.commit, "committer", committer,

View File

@@ -297,7 +297,7 @@ public class RefControl {
if (getCurrentUser().isIdentifiedUser()) {
final IdentifiedUser user = (IdentifiedUser) getCurrentUser();
final String addr = tagger.getEmailAddress();
valid = user.getEmailAddresses().contains(addr);
valid = user.hasEmailAddress(addr);
} else {
valid = false;
}