feat: Add email config test on provider edit page (#819)

* feat: Add email config test on provider edit page

* Re-use send-email API

* Optimize code

Optimize code

* Update service.go

* Update service.go

Co-authored-by: Gucheng <85475922+nomeguy@users.noreply.github.com>
This commit is contained in:
Ryao 2022-06-24 01:47:10 +08:00 committed by GitHub
parent 249f83e764
commit 31b586e391
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 156 additions and 14 deletions

View File

@ -30,6 +30,7 @@ type EmailForm struct {
Content string `json:"content"` Content string `json:"content"`
Sender string `json:"sender"` Sender string `json:"sender"`
Receivers []string `json:"receivers"` Receivers []string `json:"receivers"`
Provider string `json:"provider"`
} }
type SmsForm struct { type SmsForm struct {
@ -48,11 +49,6 @@ type SmsForm struct {
// @Success 200 {object} Response object // @Success 200 {object} Response object
// @router /api/send-email [post] // @router /api/send-email [post]
func (c *ApiController) SendEmail() { func (c *ApiController) SendEmail() {
provider, _, ok := c.GetProviderFromContext("Email")
if !ok {
return
}
var emailForm EmailForm var emailForm EmailForm
err := json.Unmarshal(c.Ctx.Input.RequestBody, &emailForm) err := json.Unmarshal(c.Ctx.Input.RequestBody, &emailForm)
@ -61,6 +57,29 @@ func (c *ApiController) SendEmail() {
return return
} }
var provider *object.Provider
if emailForm.Provider != "" {
// called by frontend's TestEmailWidget, provider name is set by frontend
provider = object.GetProvider(fmt.Sprintf("admin/%s", emailForm.Provider))
} else {
// called by Casdoor SDK via Client ID & Client Secret, so the used Email provider will be the application' Email provider or the default Email provider
var ok bool
provider, _, ok = c.GetProviderFromContext("Email")
if !ok {
return
}
}
// when receiver is the reserved keyword: "TestSmtpServer", it means to test the SMTP server instead of sending a real Email
if len(emailForm.Receivers) == 1 && emailForm.Receivers[0] == "TestSmtpServer" {
err := object.DailSmtpServer(provider)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk()
}
if util.IsStrsEmpty(emailForm.Title, emailForm.Content, emailForm.Sender) { if util.IsStrsEmpty(emailForm.Title, emailForm.Content, emailForm.Sender) {
c.ResponseError(fmt.Sprintf("Empty parameters for emailForm: %v", emailForm)) c.ResponseError(fmt.Sprintf("Empty parameters for emailForm: %v", emailForm))
return return

View File

@ -29,3 +29,16 @@ func SendEmail(provider *Provider, title string, content string, dest string, se
return dialer.DialAndSend(message) return dialer.DialAndSend(message)
} }
// DailSmtpServer Dail Smtp server
func DailSmtpServer(provider *Provider) error {
dialer := gomail.NewDialer(provider.Host, provider.Port, provider.ClientId, provider.ClientSecret)
sender, err := dialer.Dial()
if err != nil {
return err
}
defer sender.Close()
return nil
}

View File

@ -19,6 +19,7 @@ import * as ProviderBackend from "./backend/ProviderBackend";
import * as Setting from "./Setting"; import * as Setting from "./Setting";
import i18next from "i18next"; import i18next from "i18next";
import { authConfig } from "./auth/Auth"; import { authConfig } from "./auth/Auth";
import * as ProviderEditTestEmail from "./TestEmailWidget";
import copy from 'copy-to-clipboard'; import copy from 'copy-to-clipboard';
import { CaptchaPreview } from "./common/CaptchaPreview"; import { CaptchaPreview } from "./common/CaptchaPreview";
@ -33,6 +34,7 @@ class ProviderEditPage extends React.Component {
providerName: props.match.params.providerName, providerName: props.match.params.providerName,
provider: null, provider: null,
mode: props.location.mode !== undefined ? props.location.mode : "edit", mode: props.location.mode !== undefined ? props.location.mode : "edit",
testEmail: this.props.account["email"] !== undefined ? this.props.account["email"] : "",
}; };
} }
@ -514,6 +516,27 @@ class ProviderEditPage extends React.Component {
}} /> }} />
</Col> </Col>
</Row> </Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("provider:Test Email"), i18next.t("provider:Test Email - Tooltip"))} :
</Col>
<Col span={4} >
<Input value={this.state.testEmail}
placeHolder = {i18next.t("user:Input your email")}
onChange={e => {
this.setState({testEmail: e.target.value})
}} />
</Col>
<Button style={{marginLeft: '10px', marginBottom: "5px"}} type="primary"
onClick={() => ProviderEditTestEmail.connectSmtpServer(this.state.provider)} >
{i18next.t("provider:Test Connection")}
</Button>
<Button style={{marginLeft: '10px', marginBottom: "5px"}} type="primary"
disabled={!Setting.isValidEmail(this.state.testEmail)}
onClick={() => ProviderEditTestEmail.sendTestEmail(this.state.provider, this.state.testEmail)} >
{i18next.t("provider:Send Test Email")}
</Button>
</Row>
</React.Fragment> </React.Fragment>
) : this.state.provider.category === "SMS" ? ( ) : this.state.provider.category === "SMS" ? (
<React.Fragment> <React.Fragment>

View File

@ -0,0 +1,59 @@
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as Setting from "./Setting";
export function sendTestEmail(provider, email) {
testEmailProvider(provider, email)
.then((res) => {
if (res.msg === "") {
Setting.showMessage("success", `Successfully send email`);
} else {
Setting.showMessage("error", res.msg);
}
})
.catch(error => {
Setting.showMessage("error", `Failed to connect to server: ${error}`);
});
}
export function connectSmtpServer(provider) {
testEmailProvider(provider)
.then((res) => {
if (res.msg === "") {
Setting.showMessage("success", `Successfully connecting smtp server`);
} else {
Setting.showMessage("error", res.msg);
}
})
.catch(error => {
Setting.showMessage("error", `Failed to connect to server: ${error}`);
});
}
function testEmailProvider(provider, email = "") {
let emailForm = {
title: provider.title,
content: provider.content,
sender: provider.displayName,
receivers: email === "" ? ["TestSmtpServer"] : [email],
provider: provider.name,
}
return fetch(`${Setting.ServerUrl}/api/send-email`, {
method: "POST",
credentials: "include",
body: JSON.stringify(emailForm)
}).then(res => res.json());
}

View File

@ -407,7 +407,7 @@
"Domain": "Domäne", "Domain": "Domäne",
"Domain - Tooltip": "Storage endpoint custom domain", "Domain - Tooltip": "Storage endpoint custom domain",
"Edit Provider": "Anbieter bearbeiten", "Edit Provider": "Anbieter bearbeiten",
"Email Content": "Email Content", "Email Content": "Email content",
"Email Content - Tooltip": "Unique string-style identifier", "Email Content - Tooltip": "Unique string-style identifier",
"Email Title": "E-Mail-Titel", "Email Title": "E-Mail-Titel",
"Email Title - Tooltip": "Unique string-style identifier", "Email Title - Tooltip": "Unique string-style identifier",
@ -448,6 +448,7 @@
"Secret key": "Secret key", "Secret key": "Secret key",
"Secret key - Tooltip": "Secret key - Tooltip", "Secret key - Tooltip": "Secret key - Tooltip",
"SecretAccessKey - Tooltip": "SecretAccessKey - Tooltip", "SecretAccessKey - Tooltip": "SecretAccessKey - Tooltip",
"Send Test Email": "Send Test Email",
"Sign Name": "Schild Name", "Sign Name": "Schild Name",
"Sign Name - Tooltip": "Unique string-style identifier", "Sign Name - Tooltip": "Unique string-style identifier",
"Sign request": "Signaturanfrage", "Sign request": "Signaturanfrage",
@ -466,6 +467,9 @@
"Template Code - Tooltip": "Unique string-style identifier", "Template Code - Tooltip": "Unique string-style identifier",
"Terms of Use": "Nutzungsbedingungen", "Terms of Use": "Nutzungsbedingungen",
"Terms of Use - Tooltip": "Nutzungsbedingungen - Tooltip", "Terms of Use - Tooltip": "Nutzungsbedingungen - Tooltip",
"Test Connection": "Test Smtp Connection",
"Test Email": "Test email config",
"Test Email - Tooltip": "Email Address",
"Token URL": "Token URL", "Token URL": "Token URL",
"Token URL - Tooltip": "Token URL - Tooltip", "Token URL - Tooltip": "Token URL - Tooltip",
"Type": "Typ", "Type": "Typ",

View File

@ -407,9 +407,9 @@
"Domain": "Domain", "Domain": "Domain",
"Domain - Tooltip": "Domain - Tooltip", "Domain - Tooltip": "Domain - Tooltip",
"Edit Provider": "Edit Provider", "Edit Provider": "Edit Provider",
"Email Content": "Email Content", "Email Content": "Email content",
"Email Content - Tooltip": "Email Content - Tooltip", "Email Content - Tooltip": "Email Content - Tooltip",
"Email Title": "Email Title", "Email Title": "Email title",
"Email Title - Tooltip": "Email Title - Tooltip", "Email Title - Tooltip": "Email Title - Tooltip",
"Endpoint": "Endpoint", "Endpoint": "Endpoint",
"Endpoint (Intranet)": "Endpoint (Intranet)", "Endpoint (Intranet)": "Endpoint (Intranet)",
@ -448,6 +448,7 @@
"Secret key": "Secret key", "Secret key": "Secret key",
"Secret key - Tooltip": "Secret key - Tooltip", "Secret key - Tooltip": "Secret key - Tooltip",
"SecretAccessKey - Tooltip": "SecretAccessKey - Tooltip", "SecretAccessKey - Tooltip": "SecretAccessKey - Tooltip",
"Send Test Email": "Send Test Email",
"Sign Name": "Sign Name", "Sign Name": "Sign Name",
"Sign Name - Tooltip": "Sign Name - Tooltip", "Sign Name - Tooltip": "Sign Name - Tooltip",
"Sign request": "Sign request", "Sign request": "Sign request",
@ -466,6 +467,9 @@
"Template Code - Tooltip": "Template Code - Tooltip", "Template Code - Tooltip": "Template Code - Tooltip",
"Terms of Use": "Terms of Use", "Terms of Use": "Terms of Use",
"Terms of Use - Tooltip": "Terms of Use - Tooltip", "Terms of Use - Tooltip": "Terms of Use - Tooltip",
"Test Connection": "Test Smtp Connection",
"Test Email": "Test email config",
"Test Email - Tooltip": "Email Address",
"Token URL": "Token URL", "Token URL": "Token URL",
"Token URL - Tooltip": "Token URL - Tooltip", "Token URL - Tooltip": "Token URL - Tooltip",
"Type": "Type", "Type": "Type",

View File

@ -407,7 +407,7 @@
"Domain": "Domaine", "Domain": "Domaine",
"Domain - Tooltip": "Storage endpoint custom domain", "Domain - Tooltip": "Storage endpoint custom domain",
"Edit Provider": "Modifier le fournisseur", "Edit Provider": "Modifier le fournisseur",
"Email Content": "Email Content", "Email Content": "Email content",
"Email Content - Tooltip": "Unique string-style identifier", "Email Content - Tooltip": "Unique string-style identifier",
"Email Title": "Titre de l'e-mail", "Email Title": "Titre de l'e-mail",
"Email Title - Tooltip": "Unique string-style identifier", "Email Title - Tooltip": "Unique string-style identifier",
@ -448,6 +448,7 @@
"Secret key": "Secret key", "Secret key": "Secret key",
"Secret key - Tooltip": "Secret key - Tooltip", "Secret key - Tooltip": "Secret key - Tooltip",
"SecretAccessKey - Tooltip": "SecretAccessKey - Infobulle", "SecretAccessKey - Tooltip": "SecretAccessKey - Infobulle",
"Send Test Email": "Send Test Email",
"Sign Name": "Nom du panneau", "Sign Name": "Nom du panneau",
"Sign Name - Tooltip": "Unique string-style identifier", "Sign Name - Tooltip": "Unique string-style identifier",
"Sign request": "Demande de signature", "Sign request": "Demande de signature",
@ -466,6 +467,9 @@
"Template Code - Tooltip": "Unique string-style identifier", "Template Code - Tooltip": "Unique string-style identifier",
"Terms of Use": "Conditions d'utilisation", "Terms of Use": "Conditions d'utilisation",
"Terms of Use - Tooltip": "Conditions d'utilisation - Info-bulle", "Terms of Use - Tooltip": "Conditions d'utilisation - Info-bulle",
"Test Connection": "Test Smtp Connection",
"Test Email": "Test email config",
"Test Email - Tooltip": "Email Address",
"Token URL": "Token URL", "Token URL": "Token URL",
"Token URL - Tooltip": "Token URL - Tooltip", "Token URL - Tooltip": "Token URL - Tooltip",
"Type": "Type de texte", "Type": "Type de texte",

View File

@ -407,7 +407,7 @@
"Domain": "ドメイン", "Domain": "ドメイン",
"Domain - Tooltip": "Storage endpoint custom domain", "Domain - Tooltip": "Storage endpoint custom domain",
"Edit Provider": "プロバイダーを編集", "Edit Provider": "プロバイダーを編集",
"Email Content": "Email Content", "Email Content": "Email content",
"Email Content - Tooltip": "Unique string-style identifier", "Email Content - Tooltip": "Unique string-style identifier",
"Email Title": "メールタイトル", "Email Title": "メールタイトル",
"Email Title - Tooltip": "Unique string-style identifier", "Email Title - Tooltip": "Unique string-style identifier",
@ -448,6 +448,7 @@
"Secret key": "Secret key", "Secret key": "Secret key",
"Secret key - Tooltip": "Secret key - Tooltip", "Secret key - Tooltip": "Secret key - Tooltip",
"SecretAccessKey - Tooltip": "シークレットアクセスキー - ツールチップ", "SecretAccessKey - Tooltip": "シークレットアクセスキー - ツールチップ",
"Send Test Email": "Send Test Email",
"Sign Name": "署名名", "Sign Name": "署名名",
"Sign Name - Tooltip": "Unique string-style identifier", "Sign Name - Tooltip": "Unique string-style identifier",
"Sign request": "サインリクエスト", "Sign request": "サインリクエスト",
@ -466,6 +467,9 @@
"Template Code - Tooltip": "Unique string-style identifier", "Template Code - Tooltip": "Unique string-style identifier",
"Terms of Use": "利用規約", "Terms of Use": "利用規約",
"Terms of Use - Tooltip": "利用規約 - ツールチップ", "Terms of Use - Tooltip": "利用規約 - ツールチップ",
"Test Connection": "Test Smtp Connection",
"Test Email": "Test email config",
"Test Email - Tooltip": "Email Address",
"Token URL": "Token URL", "Token URL": "Token URL",
"Token URL - Tooltip": "Token URL - Tooltip", "Token URL - Tooltip": "Token URL - Tooltip",
"Type": "タイプ", "Type": "タイプ",

View File

@ -407,9 +407,9 @@
"Domain": "Domain", "Domain": "Domain",
"Domain - Tooltip": "Storage endpoint custom domain", "Domain - Tooltip": "Storage endpoint custom domain",
"Edit Provider": "Edit Provider", "Edit Provider": "Edit Provider",
"Email Content": "Email Content", "Email Content": "Email content",
"Email Content - Tooltip": "Unique string-style identifier", "Email Content - Tooltip": "Unique string-style identifier",
"Email Title": "Email Title", "Email Title": "Email title",
"Email Title - Tooltip": "Unique string-style identifier", "Email Title - Tooltip": "Unique string-style identifier",
"Endpoint": "Endpoint", "Endpoint": "Endpoint",
"Endpoint (Intranet)": "Endpoint (Intranet)", "Endpoint (Intranet)": "Endpoint (Intranet)",
@ -448,6 +448,7 @@
"Secret key": "Secret key", "Secret key": "Secret key",
"Secret key - Tooltip": "Secret key - Tooltip", "Secret key - Tooltip": "Secret key - Tooltip",
"SecretAccessKey - Tooltip": "SecretAccessKey - Tooltip", "SecretAccessKey - Tooltip": "SecretAccessKey - Tooltip",
"Send Test Email": "Send Test Email",
"Sign Name": "Sign Name", "Sign Name": "Sign Name",
"Sign Name - Tooltip": "Unique string-style identifier", "Sign Name - Tooltip": "Unique string-style identifier",
"Sign request": "Sign request", "Sign request": "Sign request",
@ -466,6 +467,9 @@
"Template Code - Tooltip": "Unique string-style identifier", "Template Code - Tooltip": "Unique string-style identifier",
"Terms of Use": "Terms of Use", "Terms of Use": "Terms of Use",
"Terms of Use - Tooltip": "Terms of Use - Tooltip", "Terms of Use - Tooltip": "Terms of Use - Tooltip",
"Test Connection": "Test Smtp Connection",
"Test Email": "Test email config",
"Test Email - Tooltip": "Email Address",
"Token URL": "Token URL", "Token URL": "Token URL",
"Token URL - Tooltip": "Token URL - Tooltip", "Token URL - Tooltip": "Token URL - Tooltip",
"Type": "Type", "Type": "Type",

View File

@ -407,7 +407,7 @@
"Domain": "Домен", "Domain": "Домен",
"Domain - Tooltip": "Storage endpoint custom domain", "Domain - Tooltip": "Storage endpoint custom domain",
"Edit Provider": "Изменить провайдера", "Edit Provider": "Изменить провайдера",
"Email Content": "Email Content", "Email Content": "Email content",
"Email Content - Tooltip": "Unique string-style identifier", "Email Content - Tooltip": "Unique string-style identifier",
"Email Title": "Заголовок письма", "Email Title": "Заголовок письма",
"Email Title - Tooltip": "Unique string-style identifier", "Email Title - Tooltip": "Unique string-style identifier",
@ -448,6 +448,7 @@
"Secret key": "Secret key", "Secret key": "Secret key",
"Secret key - Tooltip": "Secret key - Tooltip", "Secret key - Tooltip": "Secret key - Tooltip",
"SecretAccessKey - Tooltip": "SecretAccessKey - Подсказка", "SecretAccessKey - Tooltip": "SecretAccessKey - Подсказка",
"Send Test Email": "Send Test Email",
"Sign Name": "Имя подписи", "Sign Name": "Имя подписи",
"Sign Name - Tooltip": "Unique string-style identifier", "Sign Name - Tooltip": "Unique string-style identifier",
"Sign request": "Запрос на подпись", "Sign request": "Запрос на подпись",
@ -466,6 +467,9 @@
"Template Code - Tooltip": "Unique string-style identifier", "Template Code - Tooltip": "Unique string-style identifier",
"Terms of Use": "Условия использования", "Terms of Use": "Условия использования",
"Terms of Use - Tooltip": "Условия использования - Tooltip", "Terms of Use - Tooltip": "Условия использования - Tooltip",
"Test Connection": "Test Smtp Connection",
"Test Email": "Test email config",
"Test Email - Tooltip": "Email Address",
"Token URL": "Token URL", "Token URL": "Token URL",
"Token URL - Tooltip": "Token URL - Tooltip", "Token URL - Tooltip": "Token URL - Tooltip",
"Type": "Тип", "Type": "Тип",

View File

@ -448,6 +448,7 @@
"Secret key": "Secret key", "Secret key": "Secret key",
"Secret key - Tooltip": "用于服务端调用验证码提供商API进行验证", "Secret key - Tooltip": "用于服务端调用验证码提供商API进行验证",
"SecretAccessKey - Tooltip": "访问密钥-工具提示", "SecretAccessKey - Tooltip": "访问密钥-工具提示",
"Send Test Email": "发送测试邮件",
"Sign Name": "签名名称", "Sign Name": "签名名称",
"Sign Name - Tooltip": "签名名称", "Sign Name - Tooltip": "签名名称",
"Sign request": "签名请求", "Sign request": "签名请求",
@ -466,6 +467,9 @@
"Template Code - Tooltip": "模板代码", "Template Code - Tooltip": "模板代码",
"Terms of Use": "使用条款", "Terms of Use": "使用条款",
"Terms of Use - Tooltip": "使用条款 - 工具提示", "Terms of Use - Tooltip": "使用条款 - 工具提示",
"Test Connection": "测试Smtp连接",
"Test Email": "测试Email配置",
"Test Email - Tooltip": "邮箱地址",
"Token URL": "Token URL", "Token URL": "Token URL",
"Token URL - Tooltip": "Token URL - 工具提示", "Token URL - Tooltip": "Token URL - 工具提示",
"Type": "类型", "Type": "类型",