Skip to content

Commit

Permalink
Fix date postgres and make SMTP port configurable (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmgaldi committed Aug 22, 2024
1 parent 92c2e74 commit 9d06a34
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
11 changes: 11 additions & 0 deletions Model/lib/rng/wdkModel-config.rng
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
<optional>
<attribute name="smtpPassword" />
</optional>
<optional>
<attribute name="smtpTlsEnabled">
<data type="boolean" />
</attribute>
</optional>
<optional>
<attribute name="smtpPort">
<data type="unsignedShort" />
</attribute>
</optional>

<optional>
<attribute name="webAppUrl" />
</optional>
Expand Down
16 changes: 12 additions & 4 deletions Model/src/main/java/org/gusdb/wdk/model/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,18 @@ public static void sendEmail(String smtpServer, String sendTos, String reply,
String subject, String content, String ccAddresses, Attachment[] attachments)
throws WdkModelException {
// call the 8 parameter one
sendEmail(smtpServer, null, null, sendTos, reply, subject, content, ccAddresses, null, attachments);
sendEmail(smtpServer, null, null, sendTos, reply, subject, content, ccAddresses, null, attachments, 25, false);
}

public static void sendEmail(String smtpServer, String sendTos, String reply,
String subject, String content, String ccAddresses, String bccAddresses,
Attachment[] attachments) throws WdkModelException {
sendEmail(smtpServer, null, null, sendTos, reply, subject, content, ccAddresses, bccAddresses, attachments);
sendEmail(smtpServer, null, null, sendTos, reply, subject, content, ccAddresses, bccAddresses, attachments, 25, false);
}

// sendEmail() all 10 parameters
// sendEmail() all 12 parameters
public static void sendEmail(String smtpServer, String username, String password, String sendTos, String reply,
String subject, String content, String ccAddresses, String bccAddresses, Attachment[] attachments) throws WdkModelException {
String subject, String content, String ccAddresses, String bccAddresses, Attachment[] attachments, int smtpPort, boolean tlsEnabled) throws WdkModelException {

LOG.debug("Sending message to: " + sendTos + ", bcc to: " + bccAddresses +
",reply: " + reply + ", using SMPT: " + smtpServer);
Expand All @@ -237,6 +237,14 @@ public static void sendEmail(String smtpServer, String username, String password
Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.debug", "true");
props.put("mail.smtp.port", smtpPort);

if (tlsEnabled) {
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
props.put("mail.smtp.ssl.trust", smtpServer);
}

Authenticator auth = null;

if (username != null && password != null) {
Expand Down
26 changes: 24 additions & 2 deletions Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public String getName() {
private final Optional<String> _smtpPassword;


/**
* the SMTP port to connect to on SMTP server.
*/
private final int _smtpPort;

/**
* Determines whether to use TLS when connecting to SMTP server.
*/
private final boolean _smtpTlsEnabled;


/**
* the reply of the registration & recover password emails.
*/
Expand Down Expand Up @@ -155,8 +166,8 @@ public String getName() {

public ModelConfig(String modelName, String projectId, Path gusHome, boolean caching, boolean useWeights,
String paramRegex, Optional<Path> secretKeyFile, String secretKey, Path wdkTempDir, String webServiceUrl, String assetsUrl,
String smtpServer, Optional<String> smtpUser, Optional<String> smtpPassword, String supportEmail, List<String> adminEmails, String emailSubject,
String emailContent, ModelConfigUserDB userDB, ModelConfigAppDB appDB,
String smtpServer, Optional<String> smtpUser, Optional<String> smtpPassword, int smtpPort, boolean smtpTlsEnabled, String supportEmail,
List<String> adminEmails, String emailSubject, String emailContent, ModelConfigUserDB userDB, ModelConfigAppDB appDB,
ModelConfigUserDatasetStore userDatasetStoreConfig, QueryMonitor queryMonitor,
boolean monitorBlockedThreads, int blockedThreshold, AuthenticationMethod authenticationMethod,
String oauthUrl, String oauthClientId, String oauthClientSecret, String changePasswordUrl,
Expand Down Expand Up @@ -185,6 +196,9 @@ public ModelConfig(String modelName, String projectId, Path gusHome, boolean cac
_smtpServer = smtpServer;
_smtpUserName = smtpUser;
_smtpPassword = smtpPassword;
_smtpPort = smtpPort;
_smtpTlsEnabled = smtpTlsEnabled;

_supportEmail = supportEmail;
_adminEmails = adminEmails;
_emailSubject = emailSubject;
Expand Down Expand Up @@ -277,6 +291,14 @@ public Optional<String> getSmtpPassword() {
return _smtpPassword;
}

public int getSmtpPort() {
return _smtpPort;
}

public boolean isSmtpTlsEnabled() {
return _smtpTlsEnabled;
}

/**
* @return Returns the emailContent.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class ModelConfigBuilder {
private String _smtpServer;
private String _smtpUsername;
private String _smtpPassword;
private int _smtpPort;
private boolean _smtpTlsEnabled;
private String _supportEmail;
private List<String> _adminEmails = Collections.emptyList();
private String _emailSubject = "";
Expand Down Expand Up @@ -124,6 +126,8 @@ public ModelConfig build() throws WdkModelException {
smtpServer,
smtpUsername,
smtpPassword,
_smtpPort,
_smtpTlsEnabled,
_supportEmail,
_adminEmails,
_emailSubject,
Expand Down Expand Up @@ -214,6 +218,14 @@ public void setSmtpPassword(String smtpPassword) {
_smtpPassword = smtpPassword;
}

public void setSmtpPort(int smtpPort) {
_smtpPort = smtpPort;
}

public void setSmtpTlsEnabled(boolean smtpTlsEnabled) {
_smtpTlsEnabled = smtpTlsEnabled;
}

/**
* @param emailContent
* The emailContent to set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public void emailTemporaryPassword(User user, String password) throws WdkModelEx
// Unwrap optionals here to maintain consistency with nullable arguments in Utilities.sendEmail method.
String smtpUser = wdkModelConfig.getSmtpUserName().orElse(null);
String smtpPass = wdkModelConfig.getSmtpPassword().orElse(null);
int smtpPort = wdkModelConfig.getSmtpPort();
boolean smtpTlsEnabled = wdkModelConfig.isSmtpTlsEnabled();

// populate email content macros with user data
String emailContent = wdkModelConfig.getEmailContent()
Expand All @@ -52,6 +54,6 @@ public void emailTemporaryPassword(User user, String password) throws WdkModelEx
.replaceAll("\\$\\$" + EMAIL_MACRO_PASSWORD + "\\$\\$",
Matcher.quoteReplacement(password));

Utilities.sendEmail(smtpServer, smtpUser, smtpPass, user.getEmail(), supportEmail, emailSubject, emailContent, null, null, new Attachment[]{});
Utilities.sendEmail(smtpServer, smtpUser, smtpPass, user.getEmail(), supportEmail, emailSubject, emailContent, null, null, new Attachment[]{}, smtpPort, smtpTlsEnabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class UserReferenceFactory {

public static final String USER_SCHEMA_MACRO = "$$USER_SCHEMA$$";
private static final String IS_GUEST_VALUE_MACRO = "$$IS_GUEST$$";
private static final String FIRST_ACCESS_MACRO = "$$FIRST_ACCESS$$";

// types used by SQL returned by getInsertUserRefSql() below
private static final Integer[] INSERT_USER_REF_PARAM_TYPES = { Types.BIGINT, Types.TIMESTAMP };
private static final Integer[] INSERT_USER_REF_PARAM_TYPES = { Types.BIGINT };

// SQL and types to select user ref by ID
private static final String SELECT_USER_REF_BY_ID_SQL =
Expand Down Expand Up @@ -72,12 +73,13 @@ public int addUserReference(User user) throws WdkModelException {
try {
long userId = user.getUserId();
boolean isGuest = user.isGuest();
Timestamp insertedOn = new Timestamp(new Date().getTime());
Date insertedOn = new Date();
String sql = getInsertUserRefSql()
.replace(USER_SCHEMA_MACRO, _userSchema)
.replace(IS_GUEST_VALUE_MACRO, _userDb.getPlatform().convertBoolean(isGuest).toString());
.replace(IS_GUEST_VALUE_MACRO, _userDb.getPlatform().convertBoolean(isGuest).toString())
.replace(FIRST_ACCESS_MACRO, _userDb.getPlatform().toDbDateSqlValue(insertedOn));
return new SQLRunner(_userDb.getDataSource(), sql, "insert-user-ref")
.executeUpdate(new Object[]{ userId, insertedOn }, INSERT_USER_REF_PARAM_TYPES);
.executeUpdate(new Object[]{userId}, INSERT_USER_REF_PARAM_TYPES);
}
catch (SQLRunnerException e) {
throw WdkModelException.translateFrom(e);
Expand All @@ -86,7 +88,7 @@ public int addUserReference(User user) throws WdkModelException {

private String getInsertUserRefSql() {
return "MERGE INTO " + USER_SCHEMA_MACRO + TABLE_USERS + " tgt " +
"USING (SELECT ? AS user_id, ? AS first_access, " + IS_GUEST_VALUE_MACRO + " AS is_guest" + _userDb.getPlatform().getDummyTable() + ") src " +
"USING (SELECT ? AS user_id, " + FIRST_ACCESS_MACRO + " AS first_access, " + IS_GUEST_VALUE_MACRO + " AS is_guest" + _userDb.getPlatform().getDummyTable() + ") src " +
"ON (tgt." + COL_USER_ID + " = src.user_id) " +
"WHEN NOT MATCHED THEN " +
"INSERT (" + COL_USER_ID + ", " + COL_IS_GUEST + ", " + COL_FIRST_ACCESS + ") " +
Expand Down

0 comments on commit 9d06a34

Please sign in to comment.