mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Improve SendEmail() and SendSms() APIs.
This commit is contained in:
parent
1c5ce46bd5
commit
88aa444ad1
@ -19,6 +19,7 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/casbin/casdoor/object"
|
"github.com/casbin/casdoor/object"
|
||||||
"github.com/casbin/casdoor/util"
|
"github.com/casbin/casdoor/util"
|
||||||
@ -29,22 +30,13 @@ import (
|
|||||||
// @Title SendEmail
|
// @Title SendEmail
|
||||||
// @Description This API is not for Casdoor frontend to call, it is for Casdoor SDKs.
|
// @Description This API is not for Casdoor frontend to call, it is for Casdoor SDKs.
|
||||||
// @Param clientId query string true "The clientId of the application"
|
// @Param clientId query string true "The clientId of the application"
|
||||||
// @Param clientSecret query string true "The clientSecret of the application"
|
// @Param clientSecret query string true "The clientSecret of the application"
|
||||||
// @Param body body emailForm true "Details of the email request"
|
// @Param body body emailForm true "Details of the email request"
|
||||||
// @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() {
|
||||||
clientId := c.Input().Get("clientId")
|
provider, _, ok := c.GetProviderFromContext("Email")
|
||||||
clientSecret := c.Input().Get("clientSecret")
|
if !ok {
|
||||||
app := object.GetApplicationByClientIdAndSecret(clientId, clientSecret)
|
|
||||||
if app == nil {
|
|
||||||
c.ResponseError("Invalid clientId or clientSecret.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
provider := app.GetEmailProvider()
|
|
||||||
if provider == nil {
|
|
||||||
c.ResponseError("No Email provider is found")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,66 +46,61 @@ func (c *ApiController) SendEmail() {
|
|||||||
Receivers []string `json:"receivers"`
|
Receivers []string `json:"receivers"`
|
||||||
Sender string `json:"sender"`
|
Sender string `json:"sender"`
|
||||||
}
|
}
|
||||||
|
|
||||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &emailForm)
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &emailForm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ResponseError("Request body error.")
|
c.ResponseError(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if util.IsStrsEmpty(emailForm.Title, emailForm.Content, emailForm.Sender) {
|
if util.IsStrsEmpty(emailForm.Title, emailForm.Content, emailForm.Sender) {
|
||||||
c.ResponseError("Missing parameters.")
|
c.ResponseError(fmt.Sprintf("Empty parameters for emailForm: %v", emailForm))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var invalidEmails []string
|
invalidReceivers := []string{}
|
||||||
for _, receiver := range emailForm.Receivers {
|
for _, receiver := range emailForm.Receivers {
|
||||||
if !util.IsEmailValid(receiver) {
|
if !util.IsEmailValid(receiver) {
|
||||||
invalidEmails = append(invalidEmails, receiver)
|
invalidReceivers = append(invalidReceivers, receiver)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(invalidEmails) != 0 {
|
if len(invalidReceivers) != 0 {
|
||||||
c.ResponseError("Invalid Email addresses", invalidEmails)
|
c.ResponseError(fmt.Sprintf("Invalid Email receivers: %s", invalidReceivers))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ok := 0
|
|
||||||
for _, receiver := range emailForm.Receivers {
|
for _, receiver := range emailForm.Receivers {
|
||||||
if err = object.SendEmail(
|
err = object.SendEmail(provider, emailForm.Title, emailForm.Content, receiver, emailForm.Sender)
|
||||||
provider,
|
if err != nil {
|
||||||
emailForm.Title,
|
c.ResponseError(err.Error())
|
||||||
emailForm.Content,
|
return
|
||||||
receiver,
|
|
||||||
emailForm.Sender); err == nil {
|
|
||||||
ok++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = Response{Status: "ok", Data: ok}
|
c.ResponseOk()
|
||||||
c.ServeJSON()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendSms
|
// SendSms
|
||||||
// @Title SendSms
|
// @Title SendSms
|
||||||
// @Description This API is not for Casdoor frontend to call, it is for Casdoor SDKs.
|
// @Description This API is not for Casdoor frontend to call, it is for Casdoor SDKs.
|
||||||
// @Param clientId query string true "The clientId of the application"
|
// @Param clientId query string true "The clientId of the application"
|
||||||
// @Param clientSecret query string true "The clientSecret of the application"
|
// @Param clientSecret query string true "The clientSecret of the application"
|
||||||
// @Param body body smsForm true "Details of the sms request"
|
// @Param body body smsForm true "Details of the sms request"
|
||||||
// @Success 200 {object} Response object
|
// @Success 200 {object} Response object
|
||||||
// @router /api/send-sms [post]
|
// @router /api/send-sms [post]
|
||||||
func (c *ApiController) SendSms() {
|
func (c *ApiController) SendSms() {
|
||||||
clientId := c.Input().Get("clientId")
|
provider, _, ok := c.GetProviderFromContext("SMS")
|
||||||
clientSecret := c.Input().Get("clientSecret")
|
if !ok {
|
||||||
app := object.GetApplicationByClientIdAndSecret(clientId, clientSecret)
|
|
||||||
if app == nil {
|
|
||||||
c.ResponseError("Invalid clientId or clientSecret.")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
provider := app.GetSmsProvider()
|
var smsForm struct {
|
||||||
if provider == nil {
|
Receivers []string `json:"receivers"`
|
||||||
c.ResponseError("No SMS provider is found")
|
Parameters map[string]string `json:"parameters"`
|
||||||
|
}
|
||||||
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &smsForm)
|
||||||
|
if err != nil {
|
||||||
|
c.ResponseError(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,18 +114,7 @@ func (c *ApiController) SendSms() {
|
|||||||
provider.AppId,
|
provider.AppId,
|
||||||
)
|
)
|
||||||
if client == nil {
|
if client == nil {
|
||||||
c.ResponseError("Invalid provider info.")
|
c.ResponseError("SMS client is null")
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var smsForm struct {
|
|
||||||
Receivers []string `json:"receivers"`
|
|
||||||
Parameters map[string]string `json:"parameters"`
|
|
||||||
}
|
|
||||||
|
|
||||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &smsForm)
|
|
||||||
if err != nil {
|
|
||||||
c.ResponseError("Request body error.")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,11 +126,11 @@ func (c *ApiController) SendSms() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(invalidReceivers) != 0 {
|
if len(invalidReceivers) != 0 {
|
||||||
c.ResponseError("Invalid phone numbers", invalidReceivers)
|
c.ResponseError(fmt.Sprintf("Invalid phone receivers: %s", invalidReceivers))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client.SendMessage(smsForm.Parameters, smsForm.Receivers...)
|
client.SendMessage(smsForm.Parameters, smsForm.Receivers...)
|
||||||
c.Data["json"] = Response{Status: "ok"}
|
|
||||||
c.ServeJSON()
|
c.ResponseOk()
|
||||||
}
|
}
|
||||||
|
@ -87,10 +87,16 @@ func (c *ApiController) GetProviderFromContext(category string) (*object.Provide
|
|||||||
}
|
}
|
||||||
|
|
||||||
application, user := object.GetApplicationByUserId(userId)
|
application, user := object.GetApplicationByUserId(userId)
|
||||||
|
if application == nil {
|
||||||
|
c.ResponseError(fmt.Sprintf("No application is found for userId: \"%s\"", userId))
|
||||||
|
return nil, nil, false
|
||||||
|
}
|
||||||
|
|
||||||
provider := application.GetProviderByCategory(category)
|
provider := application.GetProviderByCategory(category)
|
||||||
if provider == nil {
|
if provider == nil {
|
||||||
c.ResponseError(fmt.Sprintf("No provider for category: \"%s\" is found for application: %s", category, application.Name))
|
c.ResponseError(fmt.Sprintf("No provider for category: \"%s\" is found for application: %s", category, application.Name))
|
||||||
return nil, nil, false
|
return nil, nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
return provider, user, true
|
return provider, user, true
|
||||||
}
|
}
|
||||||
|
@ -23,15 +23,16 @@ import (
|
|||||||
func SendCodeToPhone(provider *Provider, phone, code string) error {
|
func SendCodeToPhone(provider *Provider, phone, code string) error {
|
||||||
client := go_sms_sender.NewSmsClient(provider.Type, provider.ClientId, provider.ClientSecret, provider.SignName, provider.RegionId, provider.TemplateCode, provider.AppId)
|
client := go_sms_sender.NewSmsClient(provider.Type, provider.ClientId, provider.ClientSecret, provider.SignName, provider.RegionId, provider.TemplateCode, provider.AppId)
|
||||||
if client == nil {
|
if client == nil {
|
||||||
return fmt.Errorf("Unsupported provider type: %s", provider.Type)
|
return fmt.Errorf("unsupported provider type: %s", provider.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
param := make(map[string]string)
|
param := map[string]string{}
|
||||||
if provider.Type == go_sms_sender.TencentCloud {
|
if provider.Type == go_sms_sender.TencentCloud {
|
||||||
param["0"] = code
|
param["0"] = code
|
||||||
} else {
|
} else {
|
||||||
param["code"] = code
|
param["code"] = code
|
||||||
}
|
}
|
||||||
|
|
||||||
client.SendMessage(param, phone)
|
client.SendMessage(param, phone)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user