Skip to content

Commit

Permalink
feat: add SMS test feature (casdoor#1606)
Browse files Browse the repository at this point in the history
* feat: add SMS test

* fix: Add missing translation

* fix: Delete redundant information

* fix: remove unnecessary field

* Update sms.go

---------

Co-authored-by: hsluoyz <hsluoyz@qq.com>
  • Loading branch information
OutOfEastGate and hsluoyz authored Mar 3, 2023
1 parent 46f0903 commit aad9201
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 12 deletions.
10 changes: 1 addition & 9 deletions controllers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,7 @@ func (c *ApiController) SendSms() {
return
}

var invalidReceivers []string
for idx, receiver := range smsForm.Receivers {
// The receiver phone format: E164 like +8613854673829 +441932567890
if !util.IsPhoneValid(receiver, "") {
invalidReceivers = append(invalidReceivers, receiver)
} else {
smsForm.Receivers[idx] = receiver
}
}
invalidReceivers := checkSmsReceivers(smsForm)

if len(invalidReceivers) != 0 {
c.ResponseError(fmt.Sprintf(c.T("service:Invalid phone receivers: %s"), invalidReceivers))
Expand Down
11 changes: 11 additions & 0 deletions controllers/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,14 @@ func checkQuotaForUser(count int) error {
}
return nil
}

func checkSmsReceivers(smsForm SmsForm) []string {
var invalidReceivers []string
for _, receiver := range smsForm.Receivers {
// The receiver phone format: E164 like +8613854673829 +441932567890
if !util.IsPhoneValid(receiver, "") {
invalidReceivers = append(invalidReceivers, receiver)
}
}
return invalidReceivers
}
12 changes: 11 additions & 1 deletion object/sms.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@ import (
"github.com/casdoor/go-sms-sender"
)

func SendSms(provider *Provider, content string, phoneNumbers ...string) error {
func getSmsClient(provider *Provider) (go_sms_sender.SmsClient, error) {
client, err := go_sms_sender.NewSmsClient(provider.Type, provider.ClientId, provider.ClientSecret, provider.SignName, provider.TemplateCode, provider.AppId)

if provider.Type == go_sms_sender.HuaweiCloud {
client, err = go_sms_sender.NewSmsClient(provider.Type, provider.ClientId, provider.ClientSecret, provider.SignName, provider.TemplateCode, provider.ProviderUrl, provider.AppId)
}
if err != nil {
return nil, err
}

return client, nil
}

func SendSms(provider *Provider, content string, phoneNumbers ...string) error {
client, err := getSmsClient(provider)
if err != nil {
return err
}
Expand Down
32 changes: 32 additions & 0 deletions web/src/ProviderEditPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import * as Setting from "./Setting";
import i18next from "i18next";
import {authConfig} from "./auth/Auth";
import * as ProviderEditTestEmail from "./TestEmailWidget";
import * as ProviderEditTestSms from "./TestSmsWidget";
import copy from "copy-to-clipboard";
import {CaptchaPreview} from "./common/CaptchaPreview";
import * as OrganizationBackend from "./backend/OrganizationBackend";
import {PhoneNumberInput} from "./common/PhoneNumberInput";

const {Option} = Select;
const {TextArea} = Input;
Expand Down Expand Up @@ -659,6 +661,36 @@ class ProviderEditPage extends React.Component {
}} />
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("provider:SMS Test"), i18next.t("provider:SMS Test - Tooltip"))} :
</Col>
<Col span={4} >
<Input.Group compact>
<PhoneNumberInput
style={{width: "30%"}}
value={this.state.provider.content}
onChange={(value) => {
this.updateProviderField("content", value);
}}
countryCodes={this.props.account.organization.countryCodes}
/>
<Input value={this.state.provider.receiver}
style={{width: "70%"}}
placeholder = {i18next.t("user:Input your phone number")}
onChange={e => {
this.updateProviderField("receiver", e.target.value);
}} />
</Input.Group>
</Col>
<Col span={2} >
<Button style={{marginLeft: "10px", marginBottom: "5px"}} type="primary"
disabled={!Setting.isValidPhone(this.state.provider.receiver)}
onClick={() => ProviderEditTestSms.sendTestSms(this.state.provider, "+" + Setting.getCountryCode(this.state.provider.content) + this.state.provider.receiver)} >
{i18next.t("provider:SMS Send Test")}
</Button>
</Col>
</Row>
</React.Fragment>
) : this.state.provider.category === "SAML" ? (
<React.Fragment>
Expand Down
4 changes: 2 additions & 2 deletions web/src/TestEmailWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function sendTestEmail(provider, email) {
testEmailProvider(provider, email)
.then((res) => {
if (res.status === "ok") {
Setting.showMessage("success", "Successfully send email");
Setting.showMessage("success", `${i18next.t("provider:Email sent successfully")}`);
} else {
Setting.showMessage("error", res.msg);
}
Expand All @@ -33,7 +33,7 @@ export function connectSmtpServer(provider) {
testEmailProvider(provider)
.then((res) => {
if (res.status === "ok") {
Setting.showMessage("success", "Successfully connecting smtp server");
Setting.showMessage("success", "provider:SMTP connected successfully");
} else {
Setting.showMessage("error", res.msg);
}
Expand Down
43 changes: 43 additions & 0 deletions web/src/TestSmsWidget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2023 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";
import i18next from "i18next";

export function sendTestSms(provider, phone) {
testSmsProvider(provider, phone)
.then((res) => {
if (res.status === "ok") {
Setting.showMessage("success", `${i18next.t("provider:SMS send Successfully")}`);
} else {
Setting.showMessage("error", res.msg);
}
})
.catch(error => {
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
});
}

function testSmsProvider(provider, phone = "") {
const SmsForm = {
content: provider.content,
receivers: [phone],
};

return fetch(`${Setting.ServerUrl}/api/send-sms?provider=` + provider.name, {
method: "POST",
credentials: "include",
body: JSON.stringify(SmsForm),
}).then(res => res.json());
}
7 changes: 7 additions & 0 deletions web/src/locales/zh/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@
"Email Content - Tooltip": "邮件内容",
"Email Title": "邮件标题",
"Email Title - Tooltip": "邮件标题",
"Email sent successfully": "邮件发送成功",
"Enable QR code": "扫码登陆",
"Enable QR code - Tooltip": "扫码登陆 - 工具提示",
"Endpoint": "地域节点 (外网)",
Expand Down Expand Up @@ -552,6 +553,11 @@
"SAML 2.0 Endpoint (HTTP)": "SAML 2.0 Endpoint (HTTP)",
"SMS account": "SMS account",
"SMS account - Tooltip": "SMS account - Tooltip",
"SMS Send Test": "发送测试短信",
"SMS Test": "SMS测试",
"SMS Test - Tooltip": "SMS测试 - 工具提示",
"SMS send Failed": "短信发送失败",
"SMS send Successfully": "短信发送成功",
"SP ACS URL": "SP ACS URL",
"SP ACS URL - Tooltip": "SP ACS URL - 工具提示",
"SP Entity ID": "SP 实体 ID",
Expand All @@ -576,6 +582,7 @@
"Signup HTML - Tooltip": "自定义HTML,用于替换默认的注册页面样式",
"Site key": "Site key",
"Site key - Tooltip": "用于前端嵌入页面",
"SMTP connected successfully": "SMTP连接成功",
"Sub type": "子类型",
"Sub type - Tooltip": "子类型",
"Template Code": "模板代码",
Expand Down

0 comments on commit aad9201

Please sign in to comment.