diff --git a/docs-core/src/main/java/com/sismics/docs/core/constant/Constants.java b/docs-core/src/main/java/com/sismics/docs/core/constant/Constants.java index 9ac980b2..50cbcbd4 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/constant/Constants.java +++ b/docs-core/src/main/java/com/sismics/docs/core/constant/Constants.java @@ -55,6 +55,14 @@ public class Constants { */ 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. */ diff --git a/docs-core/src/main/java/com/sismics/util/EmailUtil.java b/docs-core/src/main/java/com/sismics/util/EmailUtil.java index 51b4b2e9..88269e0d 100644 --- a/docs-core/src/main/java/com/sismics/util/EmailUtil.java +++ b/docs-core/src/main/java/com/sismics/util/EmailUtil.java @@ -73,15 +73,41 @@ public class EmailUtil { // Build email headers HtmlEmail email = new HtmlEmail(); email.setCharset("UTF-8"); - email.setHostName(ConfigUtil.getConfigStringValue(ConfigType.SMTP_HOSTNAME)); - email.setSmtpPort(ConfigUtil.getConfigIntegerValue(ConfigType.SMTP_PORT)); ConfigDao configDao = new ConfigDao(); - 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()); + + // Hostname + String envHostname = System.getenv(Constants.SMTP_HOSTNAME_ENV); + 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()); + + // Application name Config themeConfig = configDao.getById(ConfigType.THEME); String appName = "Sismics Docs"; if (themeConfig != null) { @@ -90,8 +116,14 @@ public class EmailUtil { 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); + + // Locale (defined only by environment variable) java.util.Locale userLocale = LocaleUtil.getLocale(System.getenv(Constants.DEFAULT_LANGUAGE_ENV)); + + // Subject and content email.setSubject(appName + " - " + subject); email.setTextMsg(MessageUtil.getMessage(userLocale, "email.no_html.error")); diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java index ca422cbe..e190fea9 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java @@ -2,10 +2,12 @@ package com.sismics.docs.rest.resource; import com.google.common.base.Strings; 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.FileDao; import com.sismics.docs.core.dao.jpa.UserDao; 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.User; import com.sismics.docs.core.util.ConfigUtil; @@ -110,6 +112,75 @@ public class AppResource extends BaseResource { 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. @@ -119,9 +190,9 @@ public class AppResource extends BaseResource { * @apiGroup App * @apiParam {String} hostname SMTP hostname * @apiParam {Integer} port SMTP port - * @apiParam {String} from From address * @apiParam {String} username SMTP username * @apiParam {String} password SMTP password + * @apiParam {String} from From address * @apiError (client) ForbiddenError Access denied * @apiError (client) ValidationError Validation error * @apiPermission admin @@ -129,18 +200,18 @@ public class AppResource extends BaseResource { * * @param hostname SMTP hostname * @param portStr SMTP port - * @param from From address * @param username SMTP username * @param password SMTP password + * @param from From address * @return Response */ @POST @Path("config_smtp") public Response configSmtp(@FormParam("hostname") String hostname, @FormParam("port") String portStr, - @FormParam("from") String from, @FormParam("username") String username, - @FormParam("password") String password) { + @FormParam("password") String password, + @FormParam("from") String from) { if (!authenticate()) { throw new ForbiddenClientException(); } @@ -157,15 +228,15 @@ public class AppResource extends BaseResource { if (!Strings.isNullOrEmpty(portStr)) { configDao.update(ConfigType.SMTP_PORT, portStr); } - if (!Strings.isNullOrEmpty(from)) { - configDao.update(ConfigType.SMTP_FROM, from); - } if (!Strings.isNullOrEmpty(username)) { configDao.update(ConfigType.SMTP_USERNAME, username); } if (!Strings.isNullOrEmpty(password)) { configDao.update(ConfigType.SMTP_PASSWORD, password); } + if (!Strings.isNullOrEmpty(from)) { + configDao.update(ConfigType.SMTP_FROM, from); + } return Response.ok().build(); } diff --git a/docs-web/src/main/webapp/src/app/docs/controller/settings/SettingsConfig.js b/docs-web/src/main/webapp/src/app/docs/controller/settings/SettingsConfig.js index 32d55dd0..932b41d0 100644 --- a/docs-web/src/main/webapp/src/app/docs/controller/settings/SettingsConfig.js +++ b/docs-web/src/main/webapp/src/app/docs/controller/settings/SettingsConfig.js @@ -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 $scope.editSmtpConfig = function () { - Restangular.one('app').post('config_smtp', $scope.smtp).then(function () { - $scope.smtpUpdated = true; - }); + Restangular.one('app').post('config_smtp', $scope.smtp); }; }); \ No newline at end of file diff --git a/docs-web/src/main/webapp/src/partial/docs/settings.config.html b/docs-web/src/main/webapp/src/partial/docs/settings.config.html index b4cb36ac..c47bbab5 100644 --- a/docs-web/src/main/webapp/src/partial/docs/settings.config.html +++ b/docs-web/src/main/webapp/src/partial/docs/settings.config.html @@ -76,22 +76,35 @@
-