|
@@ -2,6 +2,8 @@ const loggerFactory = require('@alias/logger');
|
|
|
|
|
|
|
|
const logger = loggerFactory('growi:routes:apiv3:app-settings');
|
|
const logger = loggerFactory('growi:routes:apiv3:app-settings');
|
|
|
|
|
|
|
|
|
|
+const debug = require('debug')('growi:routes:admin');
|
|
|
|
|
+
|
|
|
const express = require('express');
|
|
const express = require('express');
|
|
|
|
|
|
|
|
const router = express.Router();
|
|
const router = express.Router();
|
|
@@ -223,6 +225,36 @@ module.exports = (crowi) => {
|
|
|
|
|
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ function validateMailSetting(req, callback) {
|
|
|
|
|
+ const mailer = crowi.mailer;
|
|
|
|
|
+ const option = {
|
|
|
|
|
+ host: req.body.smtpHost,
|
|
|
|
|
+ port: req.body.smtpPort,
|
|
|
|
|
+ };
|
|
|
|
|
+ if (req.body.smtpUser && req.body.smtpPassword) {
|
|
|
|
|
+ option.auth = {
|
|
|
|
|
+ user: req.body.smtpUser,
|
|
|
|
|
+ pass: req.body.smtpPassword,
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+ if (option.port === 465) {
|
|
|
|
|
+ option.secure = true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const smtpClient = mailer.createSMTPClient(option);
|
|
|
|
|
+ debug('mailer setup for validate SMTP setting', smtpClient);
|
|
|
|
|
+
|
|
|
|
|
+ smtpClient.sendMail(
|
|
|
|
|
+ {
|
|
|
|
|
+ from: req.body.fromAddress,
|
|
|
|
|
+ to: req.user.email,
|
|
|
|
|
+ subject: 'Wiki管理設定のアップデートによるメール通知',
|
|
|
|
|
+ text: 'このメールは、WikiのSMTP設定のアップデートにより送信されています。',
|
|
|
|
|
+ },
|
|
|
|
|
+ callback,
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* @swagger
|
|
* @swagger
|
|
|
*
|
|
*
|
|
@@ -249,34 +281,39 @@ module.exports = (crowi) => {
|
|
|
* $ref: '#/components/schemas/appSettingParams/MailSettingParams'
|
|
* $ref: '#/components/schemas/appSettingParams/MailSettingParams'
|
|
|
*/
|
|
*/
|
|
|
router.put('/mail-setting', loginRequiredStrictly, adminRequired, csrf, validator.mailSetting, ApiV3FormValidator, async(req, res) => {
|
|
router.put('/mail-setting', loginRequiredStrictly, adminRequired, csrf, validator.mailSetting, ApiV3FormValidator, async(req, res) => {
|
|
|
|
|
+ validateMailSetting(req, async(err, data) => {
|
|
|
|
|
+ debug('Error validate mail setting: ', err, data);
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ req.form.errors.push('SMTPを利用したテストメール送信に失敗しました。設定をみなおしてください。');
|
|
|
|
|
+ return res.json({ status: false, message: req.form.errors.join('\n') });
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- const requestMailSettingParams = {
|
|
|
|
|
- 'mail:from': req.body.fromAddress,
|
|
|
|
|
- 'mail:smtpHost': req.body.smtpHost,
|
|
|
|
|
- 'mail:smtpPort': req.body.smtpPort,
|
|
|
|
|
- 'mail:smtpUser': req.body.smtpUser,
|
|
|
|
|
- 'mail:smtpPassword': req.body.smtpPassword,
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestMailSettingParams);
|
|
|
|
|
- const mailSettingParams = {
|
|
|
|
|
- fromAddress: crowi.configManager.getConfig('crowi', 'mail:from'),
|
|
|
|
|
- smtpHost: crowi.configManager.getConfig('crowi', 'mail:smtpHost'),
|
|
|
|
|
- smtpPort: crowi.configManager.getConfig('crowi', 'mail:smtpPort'),
|
|
|
|
|
- smtpUser: crowi.configManager.getConfig('crowi', 'mail:smtpUser'),
|
|
|
|
|
- smtpPassword: crowi.configManager.getConfig('crowi', 'mail:smtpPassword'),
|
|
|
|
|
|
|
+ const requestMailSettingParams = {
|
|
|
|
|
+ 'mail:from': req.body.fromAddress,
|
|
|
|
|
+ 'mail:smtpHost': req.body.smtpHost,
|
|
|
|
|
+ 'mail:smtpPort': req.body.smtpPort,
|
|
|
|
|
+ 'mail:smtpUser': req.body.smtpUser,
|
|
|
|
|
+ 'mail:smtpPassword': req.body.smtpPassword,
|
|
|
};
|
|
};
|
|
|
- return res.apiv3({ mailSettingParams });
|
|
|
|
|
- }
|
|
|
|
|
- catch (err) {
|
|
|
|
|
- const msg = 'Error occurred in updating mail setting';
|
|
|
|
|
- logger.error('Error', err);
|
|
|
|
|
- return res.apiv3Err(new ErrorV3(msg, 'update-mailSetting-failed'));
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestMailSettingParams);
|
|
|
|
|
+ const mailSettingParams = {
|
|
|
|
|
+ fromAddress: crowi.configManager.getConfig('crowi', 'mail:from'),
|
|
|
|
|
+ smtpHost: crowi.configManager.getConfig('crowi', 'mail:smtpHost'),
|
|
|
|
|
+ smtpPort: crowi.configManager.getConfig('crowi', 'mail:smtpPort'),
|
|
|
|
|
+ smtpUser: crowi.configManager.getConfig('crowi', 'mail:smtpUser'),
|
|
|
|
|
+ smtpPassword: crowi.configManager.getConfig('crowi', 'mail:smtpPassword'),
|
|
|
|
|
+ };
|
|
|
|
|
+ return res.apiv3({ mailSettingParams });
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (err) {
|
|
|
|
|
+ const msg = 'Error occurred in updating mail setting';
|
|
|
|
|
+ logger.error('Error', err);
|
|
|
|
|
+ return res.apiv3Err(new ErrorV3(msg, 'update-mailSetting-failed'));
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
-
|
|
|
|
|
return router;
|
|
return router;
|
|
|
};
|
|
};
|