Closes #165: smtp hostname/port/username/password configurables with env

This commit is contained in:
Benjamin Gamard 2017-11-18 19:34:13 +01:00
parent fdb95484c1
commit df1d013b1c
6 changed files with 168 additions and 35 deletions

View File

@ -55,6 +55,14 @@ public class Constants {
*/ */
public static final String DEFAULT_LANGUAGE_ENV = "DOCS_DEFAULT_LANGUAGE"; public static final String DEFAULT_LANGUAGE_ENV = "DOCS_DEFAULT_LANGUAGE";
/**
* SMTP configuration environnement variables.
*/
public static final String SMTP_HOSTNAME_ENV = "DOCS_SMTP_HOSTNAME";
public static final String SMTP_PORT_ENV = "DOCS_SMTP_PORT";
public static final String SMTP_USERNAME_ENV = "DOCS_SMTP_USERNAME";
public static final String SMTP_PASSWORD_ENV = "DOCS_SMTP_PASSWORD";
/** /**
* Expiration time of the password recovery in hours. * Expiration time of the password recovery in hours.
*/ */

View File

@ -73,15 +73,41 @@ public class EmailUtil {
// Build email headers // Build email headers
HtmlEmail email = new HtmlEmail(); HtmlEmail email = new HtmlEmail();
email.setCharset("UTF-8"); email.setCharset("UTF-8");
email.setHostName(ConfigUtil.getConfigStringValue(ConfigType.SMTP_HOSTNAME));
email.setSmtpPort(ConfigUtil.getConfigIntegerValue(ConfigType.SMTP_PORT));
ConfigDao configDao = new ConfigDao(); ConfigDao configDao = new ConfigDao();
Config usernameConfig = configDao.getById(ConfigType.SMTP_USERNAME);
Config passwordConfig = configDao.getById(ConfigType.SMTP_PASSWORD); // Hostname
if (usernameConfig != null && passwordConfig != null) { String envHostname = System.getenv(Constants.SMTP_HOSTNAME_ENV);
email.setAuthentication(usernameConfig.getValue(), passwordConfig.getValue()); if (envHostname == null) {
email.setHostName(ConfigUtil.getConfigStringValue(ConfigType.SMTP_HOSTNAME));
} else {
email.setHostName(envHostname);
} }
// Port
String envPort = System.getenv(Constants.SMTP_PORT_ENV);
if (envPort == null) {
email.setSmtpPort(ConfigUtil.getConfigIntegerValue(ConfigType.SMTP_PORT));
} else {
email.setSmtpPort(Integer.valueOf(envPort));
}
// Username and password
String envUsername = System.getenv(Constants.SMTP_USERNAME_ENV);
String envPassword = System.getenv(Constants.SMTP_PASSWORD_ENV);
if (envUsername == null || envPassword == null) {
Config usernameConfig = configDao.getById(ConfigType.SMTP_USERNAME);
Config passwordConfig = configDao.getById(ConfigType.SMTP_PASSWORD);
if (usernameConfig != null && passwordConfig != null) {
email.setAuthentication(usernameConfig.getValue(), passwordConfig.getValue());
}
} else {
email.setAuthentication(envUsername, envPassword);
}
// Recipient
email.addTo(recipientUser.getEmail(), recipientUser.getUsername()); email.addTo(recipientUser.getEmail(), recipientUser.getUsername());
// Application name
Config themeConfig = configDao.getById(ConfigType.THEME); Config themeConfig = configDao.getById(ConfigType.THEME);
String appName = "Sismics Docs"; String appName = "Sismics Docs";
if (themeConfig != null) { if (themeConfig != null) {
@ -90,8 +116,14 @@ public class EmailUtil {
appName = themeJson.getString("name", "Sismics Docs"); appName = themeJson.getString("name", "Sismics Docs");
} }
} }
// From email address (defined only by configuration value in the database)
email.setFrom(ConfigUtil.getConfigStringValue(ConfigType.SMTP_FROM), appName); email.setFrom(ConfigUtil.getConfigStringValue(ConfigType.SMTP_FROM), appName);
// Locale (defined only by environment variable)
java.util.Locale userLocale = LocaleUtil.getLocale(System.getenv(Constants.DEFAULT_LANGUAGE_ENV)); java.util.Locale userLocale = LocaleUtil.getLocale(System.getenv(Constants.DEFAULT_LANGUAGE_ENV));
// Subject and content
email.setSubject(appName + " - " + subject); email.setSubject(appName + " - " + subject);
email.setTextMsg(MessageUtil.getMessage(userLocale, "email.no_html.error")); email.setTextMsg(MessageUtil.getMessage(userLocale, "email.no_html.error"));

View File

@ -2,10 +2,12 @@ package com.sismics.docs.rest.resource;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.sismics.docs.core.constant.ConfigType; import com.sismics.docs.core.constant.ConfigType;
import com.sismics.docs.core.constant.Constants;
import com.sismics.docs.core.dao.jpa.ConfigDao; import com.sismics.docs.core.dao.jpa.ConfigDao;
import com.sismics.docs.core.dao.jpa.FileDao; import com.sismics.docs.core.dao.jpa.FileDao;
import com.sismics.docs.core.dao.jpa.UserDao; import com.sismics.docs.core.dao.jpa.UserDao;
import com.sismics.docs.core.event.RebuildIndexAsyncEvent; import com.sismics.docs.core.event.RebuildIndexAsyncEvent;
import com.sismics.docs.core.model.jpa.Config;
import com.sismics.docs.core.model.jpa.File; import com.sismics.docs.core.model.jpa.File;
import com.sismics.docs.core.model.jpa.User; import com.sismics.docs.core.model.jpa.User;
import com.sismics.docs.core.util.ConfigUtil; import com.sismics.docs.core.util.ConfigUtil;
@ -110,6 +112,75 @@ public class AppResource extends BaseResource {
return Response.ok().build(); return Response.ok().build();
} }
/**
* Get the SMTP server configuration.
*
* @api {get} /app/config_smtp Get the SMTP server configuration
* @apiName GetAppConfigSmtp
* @apiGroup App
* @apiSuccess {String} hostname SMTP hostname
* @apiSuccess {String} port
* @apiSuccess {String} username
* @apiSuccess {String} password
* @apiSuccess {String} from
* @apiError (client) ForbiddenError Access denied
* @apiPermission admin
* @apiVersion 1.5.0
*
* @return Response
*/
@GET
@Path("config_smtp")
public Response getConfigSmtp() {
if (!authenticate()) {
throw new ForbiddenClientException();
}
checkBaseFunction(BaseFunction.ADMIN);
ConfigDao configDao = new ConfigDao();
Config hostnameConfig = configDao.getById(ConfigType.SMTP_HOSTNAME);
Config portConfig = configDao.getById(ConfigType.SMTP_PORT);
Config usernameConfig = configDao.getById(ConfigType.SMTP_USERNAME);
Config passwordConfig = configDao.getById(ConfigType.SMTP_PASSWORD);
Config fromConfig = configDao.getById(ConfigType.SMTP_FROM);
JsonObjectBuilder response = Json.createObjectBuilder();
if (System.getenv(Constants.SMTP_HOSTNAME_ENV) == null) {
if (hostnameConfig == null) {
response.addNull("hostname");
} else {
response.add("hostname", hostnameConfig.getValue());
}
}
if (System.getenv(Constants.SMTP_PORT_ENV) == null) {
if (portConfig == null) {
response.addNull("port");
} else {
response.add("port", Integer.valueOf(portConfig.getValue()));
}
}
if (System.getenv(Constants.SMTP_USERNAME_ENV) == null) {
if (usernameConfig == null) {
response.addNull("username");
} else {
response.add("username", usernameConfig.getValue());
}
}
if (System.getenv(Constants.SMTP_PASSWORD_ENV) == null) {
if (passwordConfig == null) {
response.addNull("password");
} else {
response.add("password", passwordConfig.getValue());
}
}
if (fromConfig == null) {
response.addNull("from");
} else {
response.add("from", fromConfig.getValue());
}
return Response.ok().entity(response.build()).build();
}
/** /**
* Configure the SMTP server. * Configure the SMTP server.
@ -119,9 +190,9 @@ public class AppResource extends BaseResource {
* @apiGroup App * @apiGroup App
* @apiParam {String} hostname SMTP hostname * @apiParam {String} hostname SMTP hostname
* @apiParam {Integer} port SMTP port * @apiParam {Integer} port SMTP port
* @apiParam {String} from From address
* @apiParam {String} username SMTP username * @apiParam {String} username SMTP username
* @apiParam {String} password SMTP password * @apiParam {String} password SMTP password
* @apiParam {String} from From address
* @apiError (client) ForbiddenError Access denied * @apiError (client) ForbiddenError Access denied
* @apiError (client) ValidationError Validation error * @apiError (client) ValidationError Validation error
* @apiPermission admin * @apiPermission admin
@ -129,18 +200,18 @@ public class AppResource extends BaseResource {
* *
* @param hostname SMTP hostname * @param hostname SMTP hostname
* @param portStr SMTP port * @param portStr SMTP port
* @param from From address
* @param username SMTP username * @param username SMTP username
* @param password SMTP password * @param password SMTP password
* @param from From address
* @return Response * @return Response
*/ */
@POST @POST
@Path("config_smtp") @Path("config_smtp")
public Response configSmtp(@FormParam("hostname") String hostname, public Response configSmtp(@FormParam("hostname") String hostname,
@FormParam("port") String portStr, @FormParam("port") String portStr,
@FormParam("from") String from,
@FormParam("username") String username, @FormParam("username") String username,
@FormParam("password") String password) { @FormParam("password") String password,
@FormParam("from") String from) {
if (!authenticate()) { if (!authenticate()) {
throw new ForbiddenClientException(); throw new ForbiddenClientException();
} }
@ -157,15 +228,15 @@ public class AppResource extends BaseResource {
if (!Strings.isNullOrEmpty(portStr)) { if (!Strings.isNullOrEmpty(portStr)) {
configDao.update(ConfigType.SMTP_PORT, portStr); configDao.update(ConfigType.SMTP_PORT, portStr);
} }
if (!Strings.isNullOrEmpty(from)) {
configDao.update(ConfigType.SMTP_FROM, from);
}
if (!Strings.isNullOrEmpty(username)) { if (!Strings.isNullOrEmpty(username)) {
configDao.update(ConfigType.SMTP_USERNAME, username); configDao.update(ConfigType.SMTP_USERNAME, username);
} }
if (!Strings.isNullOrEmpty(password)) { if (!Strings.isNullOrEmpty(password)) {
configDao.update(ConfigType.SMTP_PASSWORD, password); configDao.update(ConfigType.SMTP_PASSWORD, password);
} }
if (!Strings.isNullOrEmpty(from)) {
configDao.update(ConfigType.SMTP_FROM, from);
}
return Response.ok().build(); return Response.ok().build();
} }

View File

@ -65,10 +65,13 @@ angular.module('docs').controller('SettingsConfig', function($scope, $rootScope,
}); });
}; };
// Load SMTP config
Restangular.one('app/config_smtp').get().then(function (data) {
$scope.smtp = data;
});
// Edit SMTP config // Edit SMTP config
$scope.editSmtpConfig = function () { $scope.editSmtpConfig = function () {
Restangular.one('app').post('config_smtp', $scope.smtp).then(function () { Restangular.one('app').post('config_smtp', $scope.smtp);
$scope.smtpUpdated = true;
});
}; };
}); });

View File

@ -76,22 +76,35 @@
</form> </form>
<h1 translate="settings.config.title_smtp"></h1> <h1 translate="settings.config.title_smtp"></h1>
<div uib-alert ng-class="'alert-success'" ng-show="smtpUpdated">{{ 'settings.config.smtp_updated' | translate }}</div> <form class="form-horizontal" name="smtpForm" novalidate>
<form class="form-horizontal" name="smtpForm" ng-show="!smtpUpdated" novalidate> <div class="form-group" ng-show="smtp.hasOwnProperty('hostname')" ng-class="{ 'has-error': !smtpForm.hostname.$valid && smtpForm.$dirty }">
<div class="form-group" ng-class="{ 'has-error': !smtpForm.hostname.$valid && smtpForm.$dirty }">
<label class="col-sm-2 control-label" for="smtpHostname">{{ 'settings.config.smtp_hostname' | translate }}</label> <label class="col-sm-2 control-label" for="smtpHostname">{{ 'settings.config.smtp_hostname' | translate }}</label>
<div class="col-sm-7"> <div class="col-sm-7">
<input name="hostname" type="text" class="form-control" id="smtpHostname" ng-model="smtp.hostname" /> <input name="hostname" type="text" class="form-control" id="smtpHostname" ng-model="smtp.hostname" />
</div> </div>
</div> </div>
<div class="form-group" ng-class="{ 'has-error': !smtpForm.port.$valid && smtpForm.$dirty }"> <div class="form-group" ng-show="smtp.hasOwnProperty('port')" ng-class="{ 'has-error': !smtpForm.port.$valid && smtpForm.$dirty }">
<label class="col-sm-2 control-label" for="smtpPort">{{ 'settings.config.smtp_port' | translate }}</label> <label class="col-sm-2 control-label" for="smtpPort">{{ 'settings.config.smtp_port' | translate }}</label>
<div class="col-sm-7"> <div class="col-sm-7">
<input name="port" type="number" class="form-control" id="smtpPort" ng-model="smtp.port" /> <input name="port" type="number" class="form-control" id="smtpPort" ng-model="smtp.port" />
</div> </div>
</div> </div>
<div class="form-group" ng-show="smtp.hasOwnProperty('username')">
<label class="col-sm-2 control-label" for="smtpUsername">{{ 'settings.config.smtp_username' | translate }}</label>
<div class="col-sm-7">
<input name="username" type="text" class="form-control" id="smtpUsername" ng-model="smtp.username" />
</div>
</div>
<div class="form-group" ng-show="smtp.hasOwnProperty('password')">
<label class="col-sm-2 control-label" for="smtpPassword">{{ 'settings.config.smtp_password' | translate }}</label>
<div class="col-sm-7">
<input name="password" type="password" class="form-control" id="smtpPassword" ng-model="smtp.password" />
</div>
</div>
<div class="form-group" ng-class="{ 'has-error': !smtpForm.from.$valid && smtpForm.$dirty }"> <div class="form-group" ng-class="{ 'has-error': !smtpForm.from.$valid && smtpForm.$dirty }">
<label class="col-sm-2 control-label" for="smtpFrom">{{ 'settings.config.smtp_from' | translate }}</label> <label class="col-sm-2 control-label" for="smtpFrom">{{ 'settings.config.smtp_from' | translate }}</label>
<div class="col-sm-7"> <div class="col-sm-7">
@ -102,20 +115,6 @@
</div> </div>
</div> </div>
<div class="form-group">
<label class="col-sm-2 control-label" for="smtpUsername">{{ 'settings.config.smtp_username' | translate }}</label>
<div class="col-sm-7">
<input name="username" type="text" class="form-control" id="smtpUsername" ng-model="smtp.username" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="smtpPassword">{{ 'settings.config.smtp_password' | translate }}</label>
<div class="col-sm-7">
<input name="password" type="password" class="form-control" id="smtpPassword" ng-model="smtp.password" />
</div>
</div>
<div class="form-group"> <div class="form-group">
<div class="col-sm-offset-2 col-sm-10"> <div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary" ng-click="editSmtpConfig()" ng-disabled="!smtpForm.$valid"> <button type="submit" class="btn btn-primary" ng-click="editSmtpConfig()" ng-disabled="!smtpForm.$valid">

View File

@ -163,14 +163,34 @@ public class TestAppResource extends BaseJerseyTest {
// Login admin // Login admin
String adminToken = clientUtil.login("admin", "admin", false); String adminToken = clientUtil.login("admin", "admin", false);
// Get SMTP configuration
JsonObject json = target().path("/app/config_smtp").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.get(JsonObject.class);
Assert.assertTrue(json.isNull("hostname"));
Assert.assertTrue(json.isNull("port"));
Assert.assertTrue(json.isNull("username"));
Assert.assertTrue(json.isNull("password"));
Assert.assertTrue(json.isNull("from"));
// Change SMTP configuration // Change SMTP configuration
target().path("/app/config_smtp").request() target().path("/app/config_smtp").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.post(Entity.form(new Form() .post(Entity.form(new Form()
.param("hostname", "smtp.sismics.com") .param("hostname", "smtp.sismics.com")
.param("port", "1234") .param("port", "1234")
.param("from", "contact@sismics.com")
.param("username", "sismics") .param("username", "sismics")
.param("from", "contact@sismics.com")
), JsonObject.class); ), JsonObject.class);
// Get SMTP configuration
json = target().path("/app/config_smtp").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.get(JsonObject.class);
Assert.assertEquals("smtp.sismics.com", json.getString("hostname"));
Assert.assertEquals(1234, json.getInt("port"));
Assert.assertEquals("sismics", json.getString("username"));
Assert.assertTrue(json.isNull("password"));
Assert.assertEquals("contact@sismics.com", json.getString("from"));
} }
} }