Merge "Do not ask to register an email address if not supported by user store"
This commit is contained in:
@@ -18,9 +18,11 @@ import com.google.common.base.CharMatcher;
|
|||||||
import com.google.gerrit.common.Nullable;
|
import com.google.gerrit.common.Nullable;
|
||||||
import com.google.gerrit.common.PageLinks;
|
import com.google.gerrit.common.PageLinks;
|
||||||
import com.google.gerrit.extensions.registration.DynamicSet;
|
import com.google.gerrit.extensions.registration.DynamicSet;
|
||||||
|
import com.google.gerrit.reviewdb.client.Account;
|
||||||
import com.google.gerrit.reviewdb.client.RefNames;
|
import com.google.gerrit.reviewdb.client.RefNames;
|
||||||
import com.google.gerrit.server.GerritPersonIdent;
|
import com.google.gerrit.server.GerritPersonIdent;
|
||||||
import com.google.gerrit.server.IdentifiedUser;
|
import com.google.gerrit.server.IdentifiedUser;
|
||||||
|
import com.google.gerrit.server.account.Realm;
|
||||||
import com.google.gerrit.server.config.CanonicalWebUrl;
|
import com.google.gerrit.server.config.CanonicalWebUrl;
|
||||||
import com.google.gerrit.server.config.GerritServerConfig;
|
import com.google.gerrit.server.config.GerritServerConfig;
|
||||||
import com.google.gerrit.server.events.CommitReceivedEvent;
|
import com.google.gerrit.server.events.CommitReceivedEvent;
|
||||||
@@ -73,6 +75,7 @@ public class CommitValidators {
|
|||||||
private final SshInfo sshInfo;
|
private final SshInfo sshInfo;
|
||||||
private final Repository repo;
|
private final Repository repo;
|
||||||
private final DynamicSet<CommitValidationListener> commitValidationListeners;
|
private final DynamicSet<CommitValidationListener> commitValidationListeners;
|
||||||
|
private final Realm realm;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
CommitValidators(@GerritPersonIdent final PersonIdent gerritIdent,
|
CommitValidators(@GerritPersonIdent final PersonIdent gerritIdent,
|
||||||
@@ -80,7 +83,8 @@ public class CommitValidators {
|
|||||||
@GerritServerConfig final Config config,
|
@GerritServerConfig final Config config,
|
||||||
final DynamicSet<CommitValidationListener> commitValidationListeners,
|
final DynamicSet<CommitValidationListener> commitValidationListeners,
|
||||||
@Assisted final SshInfo sshInfo,
|
@Assisted final SshInfo sshInfo,
|
||||||
@Assisted final Repository repo, @Assisted final RefControl refControl) {
|
@Assisted final Repository repo, @Assisted final RefControl refControl,
|
||||||
|
Realm realm) {
|
||||||
this.gerritIdent = gerritIdent;
|
this.gerritIdent = gerritIdent;
|
||||||
this.refControl = refControl;
|
this.refControl = refControl;
|
||||||
this.canonicalWebUrl = canonicalWebUrl;
|
this.canonicalWebUrl = canonicalWebUrl;
|
||||||
@@ -89,6 +93,7 @@ public class CommitValidators {
|
|||||||
this.sshInfo = sshInfo;
|
this.sshInfo = sshInfo;
|
||||||
this.repo = repo;
|
this.repo = repo;
|
||||||
this.commitValidationListeners = commitValidationListeners;
|
this.commitValidationListeners = commitValidationListeners;
|
||||||
|
this.realm = realm;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<CommitValidationMessage> validateForReceiveCommits(
|
public List<CommitValidationMessage> validateForReceiveCommits(
|
||||||
@@ -100,8 +105,10 @@ public class CommitValidators {
|
|||||||
validators.add(new UploadMergesPermissionValidator(refControl));
|
validators.add(new UploadMergesPermissionValidator(refControl));
|
||||||
validators.add(new AmendedGerritMergeCommitValidationListener(
|
validators.add(new AmendedGerritMergeCommitValidationListener(
|
||||||
refControl, gerritIdent));
|
refControl, gerritIdent));
|
||||||
validators.add(new AuthorUploaderValidator(refControl, canonicalWebUrl));
|
validators.add(new AuthorUploaderValidator(refControl, canonicalWebUrl,
|
||||||
validators.add(new CommitterUploaderValidator(refControl, canonicalWebUrl));
|
realm));
|
||||||
|
validators.add(new CommitterUploaderValidator(refControl, canonicalWebUrl,
|
||||||
|
realm));
|
||||||
validators.add(new SignedOffByValidator(refControl, canonicalWebUrl));
|
validators.add(new SignedOffByValidator(refControl, canonicalWebUrl));
|
||||||
if (MagicBranch.isMagicBranch(receiveEvent.command.getRefName())
|
if (MagicBranch.isMagicBranch(receiveEvent.command.getRefName())
|
||||||
|| ReceiveCommits.NEW_PATCHSET.matcher(
|
|| ReceiveCommits.NEW_PATCHSET.matcher(
|
||||||
@@ -135,7 +142,8 @@ public class CommitValidators {
|
|||||||
validators.add(new UploadMergesPermissionValidator(refControl));
|
validators.add(new UploadMergesPermissionValidator(refControl));
|
||||||
validators.add(new AmendedGerritMergeCommitValidationListener(
|
validators.add(new AmendedGerritMergeCommitValidationListener(
|
||||||
refControl, gerritIdent));
|
refControl, gerritIdent));
|
||||||
validators.add(new AuthorUploaderValidator(refControl, canonicalWebUrl));
|
validators.add(new AuthorUploaderValidator(refControl, canonicalWebUrl,
|
||||||
|
realm));
|
||||||
validators.add(new SignedOffByValidator(refControl, canonicalWebUrl));
|
validators.add(new SignedOffByValidator(refControl, canonicalWebUrl));
|
||||||
if (MagicBranch.isMagicBranch(receiveEvent.command.getRefName())
|
if (MagicBranch.isMagicBranch(receiveEvent.command.getRefName())
|
||||||
|| ReceiveCommits.NEW_PATCHSET.matcher(
|
|| ReceiveCommits.NEW_PATCHSET.matcher(
|
||||||
@@ -435,10 +443,13 @@ public class CommitValidators {
|
|||||||
CommitValidationListener {
|
CommitValidationListener {
|
||||||
private final RefControl refControl;
|
private final RefControl refControl;
|
||||||
private final String canonicalWebUrl;
|
private final String canonicalWebUrl;
|
||||||
|
private final Realm realm;
|
||||||
|
|
||||||
public AuthorUploaderValidator(RefControl refControl, String canonicalWebUrl) {
|
public AuthorUploaderValidator(RefControl refControl,
|
||||||
|
String canonicalWebUrl, Realm realm) {
|
||||||
this.refControl = refControl;
|
this.refControl = refControl;
|
||||||
this.canonicalWebUrl = canonicalWebUrl;
|
this.canonicalWebUrl = canonicalWebUrl;
|
||||||
|
this.realm = realm;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -452,7 +463,7 @@ public class CommitValidators {
|
|||||||
List<CommitValidationMessage> messages = new LinkedList<>();
|
List<CommitValidationMessage> messages = new LinkedList<>();
|
||||||
|
|
||||||
messages.add(getInvalidEmailError(receiveEvent.commit, "author", author,
|
messages.add(getInvalidEmailError(receiveEvent.commit, "author", author,
|
||||||
currentUser, canonicalWebUrl));
|
currentUser, canonicalWebUrl, realm));
|
||||||
throw new CommitValidationException("invalid author", messages);
|
throw new CommitValidationException("invalid author", messages);
|
||||||
}
|
}
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
@@ -464,11 +475,13 @@ public class CommitValidators {
|
|||||||
CommitValidationListener {
|
CommitValidationListener {
|
||||||
private final RefControl refControl;
|
private final RefControl refControl;
|
||||||
private final String canonicalWebUrl;
|
private final String canonicalWebUrl;
|
||||||
|
private final Realm realm;
|
||||||
|
|
||||||
public CommitterUploaderValidator(RefControl refControl,
|
public CommitterUploaderValidator(RefControl refControl,
|
||||||
String canonicalWebUrl) {
|
String canonicalWebUrl, Realm realm) {
|
||||||
this.refControl = refControl;
|
this.refControl = refControl;
|
||||||
this.canonicalWebUrl = canonicalWebUrl;
|
this.canonicalWebUrl = canonicalWebUrl;
|
||||||
|
this.realm = realm;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -480,8 +493,8 @@ public class CommitValidators {
|
|||||||
.contains(committer.getEmailAddress())
|
.contains(committer.getEmailAddress())
|
||||||
&& !refControl.canForgeCommitter()) {
|
&& !refControl.canForgeCommitter()) {
|
||||||
List<CommitValidationMessage> messages = new LinkedList<>();
|
List<CommitValidationMessage> messages = new LinkedList<>();
|
||||||
messages.add(getInvalidEmailError(receiveEvent.commit, "committer", committer,
|
messages.add(getInvalidEmailError(receiveEvent.commit, "committer",
|
||||||
currentUser, canonicalWebUrl));
|
committer, currentUser, canonicalWebUrl, realm));
|
||||||
throw new CommitValidationException("invalid committer", messages);
|
throw new CommitValidationException("invalid committer", messages);
|
||||||
}
|
}
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
@@ -545,8 +558,9 @@ public class CommitValidators {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static CommitValidationMessage getInvalidEmailError(RevCommit c, String type,
|
private static CommitValidationMessage getInvalidEmailError(RevCommit c,
|
||||||
PersonIdent who, IdentifiedUser currentUser, String canonicalWebUrl) {
|
String type, PersonIdent who, IdentifiedUser currentUser,
|
||||||
|
String canonicalWebUrl, Realm realm) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("\n");
|
sb.append("\n");
|
||||||
sb.append("ERROR: In commit ").append(c.name()).append("\n");
|
sb.append("ERROR: In commit ").append(c.name()).append("\n");
|
||||||
@@ -557,21 +571,31 @@ public class CommitValidators {
|
|||||||
if (currentUser.getEmailAddresses().isEmpty()) {
|
if (currentUser.getEmailAddresses().isEmpty()) {
|
||||||
sb.append("ERROR: You have not registered any email addresses.\n");
|
sb.append("ERROR: You have not registered any email addresses.\n");
|
||||||
} else {
|
} else {
|
||||||
sb.append("ERROR: The following addresses are currently registered:\n");
|
if (currentUser.getEmailAddresses().size() == 1) {
|
||||||
|
sb.append("ERROR: The following address is currently "
|
||||||
|
+ "registered for you:\n");
|
||||||
|
} else {
|
||||||
|
sb.append("ERROR: The following addresses are currently "
|
||||||
|
+ "registered for you:\n");
|
||||||
|
}
|
||||||
for (String address : currentUser.getEmailAddresses()) {
|
for (String address : currentUser.getEmailAddresses()) {
|
||||||
sb.append("ERROR: ").append(address).append("\n");
|
sb.append("ERROR: ").append(address).append("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sb.append("ERROR:\n");
|
if (canonicalWebUrl != null && canRegisterNewEmail(realm)) {
|
||||||
if (canonicalWebUrl != null) {
|
sb.append("ERROR:\n");
|
||||||
sb.append("ERROR: To register an email address, please visit:\n");
|
sb.append("ERROR: To register an email address, please visit:\n");
|
||||||
sb.append("ERROR: ").append(canonicalWebUrl).append("#")
|
sb.append("ERROR: ").append(canonicalWebUrl).append("#")
|
||||||
.append(PageLinks.SETTINGS_CONTACT).append("\n");
|
.append(PageLinks.SETTINGS_CONTACT).append("\n");
|
||||||
}
|
}
|
||||||
sb.append("\n");
|
sb.append("\n");
|
||||||
return new CommitValidationMessage(sb.toString(), false);
|
return new CommitValidationMessage(sb.toString(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean canRegisterNewEmail(Realm realm) {
|
||||||
|
return realm.allowsEdit(Account.FieldName.REGISTER_NEW_EMAIL);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the Gerrit URL.
|
* Get the Gerrit URL.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user