Просмотр исходного кода

Merge branch 'imprv/can-update-SES-setting-in-the-mail-setting-form' into imprv/add-transmission-method-radio

itizawa 5 лет назад
Родитель
Сommit
1abf8f0c15

+ 50 - 0
src/migrations/20200901034313-update-mail-transmission.js

@@ -0,0 +1,50 @@
+require('module-alias/register');
+const logger = require('@alias/logger')('growi:migrate:update-mail-transmission');
+
+const mongoose = require('mongoose');
+const config = require('@root/config/migrate');
+
+const { getModelSafely } = require('@commons/util/mongoose-utils');
+
+module.exports = {
+  async up(db, client) {
+    logger.info('Apply migration');
+    mongoose.connect(config.mongoUri, config.mongodb.options);
+
+    const Config = getModelSafely('Config') || require('@server/models/config')();
+
+    const sesExist = await Config.findOne({
+      ns: 'crowi',
+      key: 'mail:sesAccessKeyId',
+    });
+
+    if (sesExist == null) {
+      return logger.info('Document does not exist, value of transmission method will be set smtp automatically.');
+    }
+    const value = (
+      sesExist.value != null ? 'ses' : 'smtp'
+    );
+    await Config.create({
+      ns: 'crowi',
+      key: 'mail:transmissionMethod',
+      value,
+    });
+    logger.info('Migration has successfully applied');
+
+  },
+
+  async down(db, client) {
+    logger.info('Rollback migration');
+    mongoose.connect(config.mongoUri, config.mongodb.options);
+
+    const Config = getModelSafely('Config') || require('@server/models/config')();
+
+    // remote 'mail:transmissionMethod'
+    await Config.findOneAndDelete({
+      ns: 'crowi',
+      key: 'mail:transmissionMethod',
+    });
+
+    logger.info('Migration has been successfully rollbacked');
+  },
+};

+ 40 - 19
src/server/routes/apiv3/app-settings.js

@@ -138,7 +138,7 @@ module.exports = (crowi) => {
     ],
     smtpSetting: [
       body('smtpHost').trim(),
-      body('smtpPort').trim().isPort(),
+      body('smtpPort').trim().if(value => value !== '').isPort(),
       body('smtpUser').trim(),
       body('smtpPassword').trim(),
     ],
@@ -318,14 +318,14 @@ module.exports = (crowi) => {
   /**
    * validate mail setting send test mail
    */
-  async function validateMailSetting(req) {
+  async function sendTestEmail(req) {
 
-    // check passes if there is at least one blank
-    if (Object.values(req.body).some(value => value === '')) {
-      return;
+    const { configManager, mailService } = crowi;
+
+    if (!mailService.isMailerSetup) {
+      throw Error('mailService is not setup');
     }
 
-    const { configManager, mailService } = crowi;
     const fromAddress = configManager.getConfig('crowi', 'mail:from');
     if (fromAddress == null) {
       throw Error('fromAddress is not setup');
@@ -371,6 +371,7 @@ module.exports = (crowi) => {
     mailService.publishUpdatedMessage();
 
     return {
+      isMailerSetup: mailService.isMailerSetup,
       smtpHost: configManager.getConfig('crowi', 'mail:smtpHost'),
       smtpPort: configManager.getConfig('crowi', 'mail:smtpPort'),
       smtpUser: configManager.getConfig('crowi', 'mail:smtpUser'),
@@ -409,8 +410,7 @@ module.exports = (crowi) => {
     };
 
     try {
-      const mailSettingParams = await updateMailSettinConfig(requestSesSettingParams);
-
+      const mailSettingParams = await updateMailSettinConfig({ 'mail:from': req.body.fromAddress });
       return res.apiv3({ mailSettingParams });
     }
     catch (err) {
@@ -445,17 +445,6 @@ module.exports = (crowi) => {
    *                  $ref: '#/components/schemas/SmtpSettingParams'
    */
   router.put('/smtp-setting', loginRequiredStrictly, adminRequired, csrf, validator.smtpSetting, apiV3FormValidator, async(req, res) => {
-    try {
-      await validateMailSetting(req);
-    }
-    catch (err) {
-      const msg = req.t('validation.failed_to_send_a_test_email');
-      logger.error('Error', err);
-      debug('Error validate mail setting: ', err);
-      return res.apiv3Err(new ErrorV3(msg, 'update-mailSetting-failed'));
-    }
-
-
     const requestMailSettingParams = {
       'mail:smtpHost': req.body.smtpHost,
       'mail:smtpPort': req.body.smtpPort,
@@ -474,6 +463,38 @@ module.exports = (crowi) => {
     }
   });
 
+  /**
+   * @swagger
+   *
+   *    /app-settings/smtp-test:
+   *      post:
+   *        tags: [AppSettings]
+   *        operationId: posyAppSettingSmtpTast
+   *        summary: /app-settings/smtp-setting
+   *        description: Send test mail for smtp
+   *        requestBody:
+   *          required: true
+   *          content:
+   *            application/json:
+   *              schema:
+   *                $ref: '#/components/schemas/SmtpSettingParams'
+   *        responses:
+   *          200:
+   *            description: Succeeded to send test mail for smtp
+   */
+  router.post('/smtp-test', loginRequiredStrictly, adminRequired, csrf, validator.smtpSetting, apiV3FormValidator, async(req, res) => {
+    try {
+      await sendTestEmail(req);
+      return res.apiv3({});
+    }
+    catch (err) {
+      const msg = req.t('validation.failed_to_send_a_test_email');
+      logger.error('Error', err);
+      debug('Error validate mail setting: ', err);
+      return res.apiv3Err(new ErrorV3(msg, 'update-mailSetting-failed'));
+    }
+  });
+
   /**
    * @swagger
    *

+ 15 - 4
src/server/service/mail.js

@@ -20,6 +20,11 @@ class MailService extends S2sMessageHandlable {
     this.mailConfig = {};
     this.mailer = {};
 
+    /**
+     * the flag whether mailer is set up successfully
+     */
+    this.isMailerSetup = false;
+
     this.initialize();
   }
 
@@ -65,18 +70,22 @@ class MailService extends S2sMessageHandlable {
   initialize() {
     const { appService, configManager } = this;
 
+    this.isMailerSetup = false;
+
     if (!configManager.getConfig('crowi', 'mail:from')) {
       this.mailer = null;
       return;
     }
 
-    // Priority 1. SMTP
-    if (configManager.getConfig('crowi', 'mail:smtpHost') && configManager.getConfig('crowi', 'mail:smtpPort')) {
+    const transmissionMethod = configManager.getConfig('crowi', 'mail:transmissionMethod');
+
+    if (transmissionMethod === 'smtp') {
       this.mailer = this.createSMTPClient();
+      this.isMailerSetup = true;
     }
-    // Priority 2. SES
-    else if (configManager.getConfig('crowi', 'mail:sesAccessKeyId') && configManager.getConfig('crowi', 'mail:sesSecretAccessKey')) {
+    else if (transmissionMethod === 'ses') {
       this.mailer = this.createSESClient();
+      this.isMailerSetup = true;
     }
     else {
       this.mailer = null;
@@ -113,6 +122,7 @@ class MailService extends S2sMessageHandlable {
     const client = nodemailer.createTransport(option);
 
     logger.debug('mailer set up for SMTP', client);
+
     return client;
   }
 
@@ -130,6 +140,7 @@ class MailService extends S2sMessageHandlable {
     const client = nodemailer.createTransport(ses(option));
 
     logger.debug('mailer set up for SES', client);
+
     return client;
   }