itizawa 5 лет назад
Родитель
Сommit
67532a8377
2 измененных файлов с 50 добавлено и 18 удалено
  1. 39 18
      src/server/routes/apiv3/app-settings.js
  2. 11 0
      src/server/service/mail.js

+ 39 - 18
src/server/routes/apiv3/app-settings.js

@@ -134,7 +134,7 @@ module.exports = (crowi) => {
     ],
     ],
     smtpSetting: [
     smtpSetting: [
       body('smtpHost').trim(),
       body('smtpHost').trim(),
-      body('smtpPort').trim().isPort(),
+      body('smtpPort').trim().if(value => value !== '').isPort(),
       body('smtpUser').trim(),
       body('smtpUser').trim(),
       body('smtpPassword').trim(),
       body('smtpPassword').trim(),
     ],
     ],
@@ -313,14 +313,14 @@ module.exports = (crowi) => {
   /**
   /**
    * validate mail setting send test mail
    * 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');
     const fromAddress = configManager.getConfig('crowi', 'mail:from');
     if (fromAddress == null) {
     if (fromAddress == null) {
       throw Error('fromAddress is not setup');
       throw Error('fromAddress is not setup');
@@ -366,6 +366,7 @@ module.exports = (crowi) => {
     mailService.publishUpdatedMessage();
     mailService.publishUpdatedMessage();
 
 
     return {
     return {
+      isMailerSetup: mailService.isMailerSetup,
       smtpHost: configManager.getConfig('crowi', 'mail:smtpHost'),
       smtpHost: configManager.getConfig('crowi', 'mail:smtpHost'),
       smtpPort: configManager.getConfig('crowi', 'mail:smtpPort'),
       smtpPort: configManager.getConfig('crowi', 'mail:smtpPort'),
       smtpUser: configManager.getConfig('crowi', 'mail:smtpUser'),
       smtpUser: configManager.getConfig('crowi', 'mail:smtpUser'),
@@ -400,7 +401,6 @@ module.exports = (crowi) => {
 
 
     try {
     try {
       const mailSettingParams = await updateMailSettinConfig({ 'mail:from': req.body.fromAddress });
       const mailSettingParams = await updateMailSettinConfig({ 'mail:from': req.body.fromAddress });
-
       return res.apiv3({ mailSettingParams });
       return res.apiv3({ mailSettingParams });
     }
     }
     catch (err) {
     catch (err) {
@@ -435,17 +435,6 @@ module.exports = (crowi) => {
    *                  $ref: '#/components/schemas/SmtpSettingParams'
    *                  $ref: '#/components/schemas/SmtpSettingParams'
    */
    */
   router.put('/smtp-setting', loginRequiredStrictly, adminRequired, csrf, validator.smtpSetting, apiV3FormValidator, async(req, res) => {
   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 = {
     const requestMailSettingParams = {
       'mail:smtpHost': req.body.smtpHost,
       'mail:smtpHost': req.body.smtpHost,
       'mail:smtpPort': req.body.smtpPort,
       'mail:smtpPort': req.body.smtpPort,
@@ -464,6 +453,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
    * @swagger
    *
    *

+ 11 - 0
src/server/service/mail.js

@@ -20,6 +20,11 @@ class MailService extends S2sMessageHandlable {
     this.mailConfig = {};
     this.mailConfig = {};
     this.mailer = {};
     this.mailer = {};
 
 
+    /**
+     * the flag whether mailer is set up successfully
+     */
+    this.isMailerSetup = false;
+
     this.initialize();
     this.initialize();
   }
   }
 
 
@@ -65,6 +70,8 @@ class MailService extends S2sMessageHandlable {
   initialize() {
   initialize() {
     const { appService, configManager } = this;
     const { appService, configManager } = this;
 
 
+    this.isMailerSetup = false;
+
     if (!configManager.getConfig('crowi', 'mail:from')) {
     if (!configManager.getConfig('crowi', 'mail:from')) {
       this.mailer = null;
       this.mailer = null;
       return;
       return;
@@ -113,6 +120,8 @@ class MailService extends S2sMessageHandlable {
     const client = nodemailer.createTransport(option);
     const client = nodemailer.createTransport(option);
 
 
     logger.debug('mailer set up for SMTP', client);
     logger.debug('mailer set up for SMTP', client);
+    this.isMailerSetup = true;
+
     return client;
     return client;
   }
   }
 
 
@@ -130,6 +139,8 @@ class MailService extends S2sMessageHandlable {
     const client = nodemailer.createTransport(ses(option));
     const client = nodemailer.createTransport(ses(option));
 
 
     logger.debug('mailer set up for SES', client);
     logger.debug('mailer set up for SES', client);
+    this.isMailerSetup = true;
+
     return client;
     return client;
   }
   }