Skip to content

The Unified and Autonomous Development Platform

refactor(email): improve noreply email handling and validation

Robin Shene519312a8b32
diff --git a/server-core/src/main/java/io/onedev/server/model/User.java b/server-core/src/main/java/io/onedev/server/model/User.java
index 33fa95f3c7..e7018a11d3 100644
--- a/server-core/src/main/java/io/onedev/server/model/User.java
+++ b/server-core/src/main/java/io/onedev/server/model/User.java
@@ -1340,7 +1340,7 @@ public class User extends AbstractEntity implements AuthenticationInfo {
@Nullable
public static String getLoginName(String noReplyEmailAddress) {
var noreplyEmailSuffix = "@" + getNoreplyEmailDomain();
- if (noReplyEmailAddress.endsWith(noreplyEmailSuffix))
+ if (noReplyEmailAddress.toLowerCase().endsWith(noreplyEmailSuffix.toLowerCase()))
return noReplyEmailAddress.substring(0, noReplyEmailAddress.length() - noreplyEmailSuffix.length());
else
return null;
diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/SystemSetting.java b/server-core/src/main/java/io/onedev/server/model/support/administration/SystemSetting.java
index a4dede1170..6456569f08 100644
--- a/server-core/src/main/java/io/onedev/server/model/support/administration/SystemSetting.java
+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/SystemSetting.java
@@ -54,7 +54,7 @@ public class SystemSetting implements Serializable, Validatable {
public static final String PROP_NOREPLY_EMAIL_DOMAIN = "noreplyEmailDomain";
- public static final String DEFAULT_NOREPLY_EMAIL_DOMAIN = "noreply.onedev";
+ public static final String DEFAULT_NOREPLY_EMAIL_DOMAIN = "noreply.localhost";
private String serverUrl;
diff --git a/server-core/src/main/java/io/onedev/server/rest/resource/EmailAddressResource.java b/server-core/src/main/java/io/onedev/server/rest/resource/EmailAddressResource.java
index 0751d12bfd..c7dac2499c 100644
--- a/server-core/src/main/java/io/onedev/server/rest/resource/EmailAddressResource.java
+++ b/server-core/src/main/java/io/onedev/server/rest/resource/EmailAddressResource.java
@@ -19,6 +19,7 @@ import javax.ws.rs.core.Response;
import org.apache.shiro.authz.UnauthorizedException;
import io.onedev.commons.utils.ExplicitException;
+import io.onedev.server.exception.NotAcceptableException;
import io.onedev.server.model.EmailAddress;
import io.onedev.server.model.User;
import io.onedev.server.rest.annotation.Api;
@@ -73,11 +74,13 @@ public class EmailAddressResource {
if (!SecurityUtils.isAdministrator() && !owner.equals(getAuthUser()))
throw new UnauthorizedException();
else if (owner.isDisabled())
- throw new ExplicitException("Cannot set email address for disabled user");
+ throw new NotAcceptableException("Cannot set email address for disabled user");
else if (owner.getType() != User.Type.ORDINARY)
- throw new ExplicitException("Cannot set email address for service or ai user");
+ throw new NotAcceptableException("Cannot set email address for service or ai user");
+ else if (User.getLoginName(emailAddress.getValue()) != null)
+ throw new NotAcceptableException("Email address with noreply domain is not allowed");
else if (emailAddressService.findByValue(emailAddress.getValue()) != null)
- throw new ExplicitException("This email address is already used by another user");
+ throw new NotAcceptableException("This email address is already used by another user");
if (SecurityUtils.isAdministrator())
emailAddress.setVerificationCode(null);
diff --git a/server-core/src/main/java/io/onedev/server/service/impl/DefaultSettingService.java b/server-core/src/main/java/io/onedev/server/service/impl/DefaultSettingService.java
index 23d2735145..9f694f6654 100644
--- a/server-core/src/main/java/io/onedev/server/service/impl/DefaultSettingService.java
+++ b/server-core/src/main/java/io/onedev/server/service/impl/DefaultSettingService.java
@@ -289,7 +289,7 @@ public class DefaultSettingService extends BaseEntityService<Setting> implements
if (gpgSetting != null && gpgSetting.getEncodedSigningKey() != null) {
try {
PGPKeyRingGenerator generator = GpgUtils.generateKeyRingGenerator(
- User.SYSTEM_NAME + "<system@" + systemSetting.getNoreplyEmailDomain() + ">");
+ User.SYSTEM_NAME + "<" + User.getSystemNoreplyEmailAddress() + ">");
var baos = new ByteArrayOutputStream();
generator.generateSecretKeyRing().encode(baos);
gpgSetting.setEncodedSigningKey(baos.toByteArray());
diff --git a/server-core/src/main/java/io/onedev/server/web/component/user/emailaddresses/EmailAddressesPanel.java b/server-core/src/main/java/io/onedev/server/web/component/user/emailaddresses/EmailAddressesPanel.java
index 8b6929e814..37a8ca7900 100644
--- a/server-core/src/main/java/io/onedev/server/web/component/user/emailaddresses/EmailAddressesPanel.java
+++ b/server-core/src/main/java/io/onedev/server/web/component/user/emailaddresses/EmailAddressesPanel.java
@@ -385,6 +385,13 @@ public class EmailAddressesPanel extends GenericPanel<User> {
return _T("Malformed email address");
}
});
+ } else if (User.getLoginName(validatable.getValue()) != null) {
+ validatable.error(new IValidationError() {
+ @Override
+ public Serializable getErrorMessage(IErrorMessageSource messageSource) {
+ return _T("Email address with noreply domain is not allowed");
+ }
+ });
}
}
};