// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import * as Client from '../../utils/client.jsx'; import * as AsyncClient from '../../utils/async_client.jsx'; import crypto from 'crypto'; import {injectIntl, intlShape, defineMessages, FormattedMessage, FormattedHTMLMessage} from 'mm-intl'; var holders = defineMessages({ notificationDisplayExample: { id: 'admin.email.notificationDisplayExample', defaultMessage: 'Ex: "Mattermost Notification", "System", "No-Reply"' }, notificationEmailExample: { id: 'admin.email.notificationEmailExample', defaultMessage: 'Ex: "mattermost@yourcompany.com", "admin@yourcompany.com"' }, smtpUsernameExample: { id: 'admin.email.smtpUsernameExample', defaultMessage: 'Ex: "admin@yourcompany.com", "AKIADTOVBGERKLCBV"' }, smtpPasswordExample: { id: 'admin.email.smtpPasswordExample', defaultMessage: 'Ex: "yourpassword", "jcuS8PuvcpGhpgHhlcpT1Mx42pnqMxQY"' }, smtpServerExample: { id: 'admin.email.smtpServerExample', defaultMessage: 'Ex: "smtp.yourcompany.com", "email-smtp.us-east-1.amazonaws.com"' }, smtpPortExample: { id: 'admin.email.smtpPortExample', defaultMessage: 'Ex: "25", "465"' }, connectionSecurityNone: { id: 'admin.email.connectionSecurityNone', defaultMessage: 'None' }, connectionSecurityTls: { id: 'admin.email.connectionSecurityTls', defaultMessage: 'TLS (Recommended)' }, connectionSecurityStart: { id: 'admin.email.connectionSecurityStart', defaultMessage: 'STARTTLS' }, inviteSaltExample: { id: 'admin.email.inviteSaltExample', defaultMessage: 'Ex "bjlSR4QqkXFBr7TP4oDzlfZmcNuH9Yo"' }, passwordSaltExample: { id: 'admin.email.passwordSaltExample', defaultMessage: 'Ex "bjlSR4QqkXFBr7TP4oDzlfZmcNuH9Yo"' }, pushServerEx: { id: 'admin.email.pushServerEx', defaultMessage: 'E.g.: "https://push-test.mattermost.com"' }, testing: { id: 'admin.email.testing', defaultMessage: 'Testing...' }, saving: { id: 'admin.email.saving', defaultMessage: 'Saving Config...' } }); class EmailSettings extends React.Component { constructor(props) { super(props); this.handleChange = this.handleChange.bind(this); this.handleTestConnection = this.handleTestConnection.bind(this); this.handleSubmit = this.handleSubmit.bind(this); this.buildConfig = this.buildConfig.bind(this); this.handleGenerateInvite = this.handleGenerateInvite.bind(this); this.handleGenerateReset = this.handleGenerateReset.bind(this); this.state = { sendEmailNotifications: this.props.config.EmailSettings.SendEmailNotifications, sendPushNotifications: this.props.config.EmailSettings.SendPushNotifications, saveNeeded: false, serverError: null, emailSuccess: null, emailFail: null }; } handleChange(action) { var s = {saveNeeded: true, serverError: this.state.serverError}; if (action === 'sendEmailNotifications_true') { s.sendEmailNotifications = true; } if (action === 'sendEmailNotifications_false') { s.sendEmailNotifications = false; } if (action === 'sendPushNotifications_true') { s.sendPushNotifications = true; } if (action === 'sendPushNotifications_false') { s.sendPushNotifications = false; } this.setState(s); } buildConfig() { var config = this.props.config; config.EmailSettings.EnableSignUpWithEmail = ReactDOM.findDOMNode(this.refs.allowSignUpWithEmail).checked; config.EmailSettings.SendEmailNotifications = ReactDOM.findDOMNode(this.refs.sendEmailNotifications).checked; config.EmailSettings.SendPushNotifications = ReactDOM.findDOMNode(this.refs.sendPushNotifications).checked; config.EmailSettings.RequireEmailVerification = ReactDOM.findDOMNode(this.refs.requireEmailVerification).checked; config.EmailSettings.FeedbackName = ReactDOM.findDOMNode(this.refs.feedbackName).value.trim(); config.EmailSettings.FeedbackEmail = ReactDOM.findDOMNode(this.refs.feedbackEmail).value.trim(); config.EmailSettings.SMTPServer = ReactDOM.findDOMNode(this.refs.SMTPServer).value.trim(); config.EmailSettings.PushNotificationServer = ReactDOM.findDOMNode(this.refs.PushNotificationServer).value.trim(); config.EmailSettings.SMTPPort = ReactDOM.findDOMNode(this.refs.SMTPPort).value.trim(); config.EmailSettings.SMTPUsername = ReactDOM.findDOMNode(this.refs.SMTPUsername).value.trim(); config.EmailSettings.SMTPPassword = ReactDOM.findDOMNode(this.refs.SMTPPassword).value.trim(); config.EmailSettings.ConnectionSecurity = ReactDOM.findDOMNode(this.refs.ConnectionSecurity).value.trim(); config.EmailSettings.InviteSalt = ReactDOM.findDOMNode(this.refs.InviteSalt).value.trim(); if (config.EmailSettings.InviteSalt === '') { config.EmailSettings.InviteSalt = crypto.randomBytes(256).toString('base64').substring(0, 32); ReactDOM.findDOMNode(this.refs.InviteSalt).value = config.EmailSettings.InviteSalt; } config.EmailSettings.PasswordResetSalt = ReactDOM.findDOMNode(this.refs.PasswordResetSalt).value.trim(); if (config.EmailSettings.PasswordResetSalt === '') { config.EmailSettings.PasswordResetSalt = crypto.randomBytes(256).toString('base64').substring(0, 32); ReactDOM.findDOMNode(this.refs.PasswordResetSalt).value = config.EmailSettings.PasswordResetSalt; } return config; } handleGenerateInvite(e) { e.preventDefault(); ReactDOM.findDOMNode(this.refs.InviteSalt).value = crypto.randomBytes(256).toString('base64').substring(0, 32); var s = {saveNeeded: true, serverError: this.state.serverError}; this.setState(s); } handleGenerateReset(e) { e.preventDefault(); ReactDOM.findDOMNode(this.refs.PasswordResetSalt).value = crypto.randomBytes(256).toString('base64').substring(0, 32); var s = {saveNeeded: true, serverError: this.state.serverError}; this.setState(s); } handleTestConnection(e) { e.preventDefault(); $('#connection-button').button('loading'); var config = this.buildConfig(); Client.testEmail( config, () => { this.setState({ sendEmailNotifications: config.EmailSettings.SendEmailNotifications, serverError: null, saveNeeded: true, emailSuccess: true, emailFail: null }); $('#connection-button').button('reset'); }, (err) => { this.setState({ sendEmailNotifications: config.EmailSettings.SendEmailNotifications, serverError: null, saveNeeded: true, emailSuccess: null, emailFail: err.message + ' - ' + err.detailed_error }); $('#connection-button').button('reset'); } ); } handleSubmit(e) { e.preventDefault(); $('#save-button').button('loading'); var config = this.buildConfig(); Client.saveConfig( config, () => { AsyncClient.getConfig(); this.setState({ sendEmailNotifications: config.EmailSettings.SendEmailNotifications, serverError: null, saveNeeded: false, emailSuccess: null, emailFail: null }); $('#save-button').button('reset'); }, (err) => { this.setState({ sendEmailNotifications: config.EmailSettings.SendEmailNotifications, serverError: err.message, saveNeeded: true, emailSuccess: null, emailFail: null }); $('#save-button').button('reset'); } ); } render() { const {formatMessage} = this.props.intl; var serverError = ''; if (this.state.serverError) { serverError =
; } var saveClass = 'btn'; if (this.state.saveNeeded) { saveClass = 'btn btn-primary'; } var emailSuccess = ''; if (this.state.emailSuccess) { emailSuccess = (