2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2021-07-30 14:15:10 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Casdoor will expose its providers as services to SDK
|
|
|
|
// We are going to implement those services as APIs here
|
|
|
|
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-09-05 10:30:51 +08:00
|
|
|
"fmt"
|
2021-07-30 14:15:10 +08:00
|
|
|
|
2022-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/object"
|
|
|
|
"github.com/casdoor/casdoor/util"
|
2021-07-30 14:15:10 +08:00
|
|
|
)
|
|
|
|
|
2022-06-21 23:11:29 +08:00
|
|
|
type EmailForm struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
Sender string `json:"sender"`
|
|
|
|
Receivers []string `json:"receivers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SmsForm struct {
|
|
|
|
Content string `json:"content"`
|
|
|
|
Receivers []string `json:"receivers"`
|
|
|
|
OrgId string `json:"organizationId"` // e.g. "admin/built-in"
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// SendEmail
|
2021-07-30 14:15:10 +08:00
|
|
|
// @Title SendEmail
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Service API
|
2021-07-30 14:15:10 +08:00
|
|
|
// @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"
|
2021-09-05 10:30:51 +08:00
|
|
|
// @Param clientSecret query string true "The clientSecret of the application"
|
2022-06-21 23:11:29 +08:00
|
|
|
// @Param from body controllers.EmailForm true "Details of the email request"
|
2021-07-30 14:15:10 +08:00
|
|
|
// @Success 200 {object} Response object
|
|
|
|
// @router /api/send-email [post]
|
|
|
|
func (c *ApiController) SendEmail() {
|
2021-09-05 10:30:51 +08:00
|
|
|
provider, _, ok := c.GetProviderFromContext("Email")
|
|
|
|
if !ok {
|
2021-07-30 14:15:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-21 23:11:29 +08:00
|
|
|
var emailForm EmailForm
|
|
|
|
|
2021-07-30 14:15:10 +08:00
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &emailForm)
|
|
|
|
if err != nil {
|
2021-09-05 10:30:51 +08:00
|
|
|
c.ResponseError(err.Error())
|
2021-07-30 14:15:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if util.IsStrsEmpty(emailForm.Title, emailForm.Content, emailForm.Sender) {
|
2021-09-05 10:30:51 +08:00
|
|
|
c.ResponseError(fmt.Sprintf("Empty parameters for emailForm: %v", emailForm))
|
2021-07-30 14:15:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-05 10:30:51 +08:00
|
|
|
invalidReceivers := []string{}
|
2021-07-30 14:15:10 +08:00
|
|
|
for _, receiver := range emailForm.Receivers {
|
|
|
|
if !util.IsEmailValid(receiver) {
|
2021-09-05 10:30:51 +08:00
|
|
|
invalidReceivers = append(invalidReceivers, receiver)
|
2021-07-30 14:15:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 10:30:51 +08:00
|
|
|
if len(invalidReceivers) != 0 {
|
|
|
|
c.ResponseError(fmt.Sprintf("Invalid Email receivers: %s", invalidReceivers))
|
2021-07-30 14:15:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, receiver := range emailForm.Receivers {
|
2021-09-05 10:30:51 +08:00
|
|
|
err = object.SendEmail(provider, emailForm.Title, emailForm.Content, receiver, emailForm.Sender)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2021-07-30 14:15:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 10:30:51 +08:00
|
|
|
c.ResponseOk()
|
2021-07-30 14:15:10 +08:00
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
// SendSms
|
2021-07-30 14:15:10 +08:00
|
|
|
// @Title SendSms
|
2021-12-03 20:42:36 +08:00
|
|
|
// @Tag Service API
|
2021-07-30 14:15:10 +08:00
|
|
|
// @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"
|
2021-09-05 10:30:51 +08:00
|
|
|
// @Param clientSecret query string true "The clientSecret of the application"
|
2022-06-21 23:11:29 +08:00
|
|
|
// @Param from body controllers.SmsForm true "Details of the sms request"
|
2021-07-30 14:15:10 +08:00
|
|
|
// @Success 200 {object} Response object
|
|
|
|
// @router /api/send-sms [post]
|
|
|
|
func (c *ApiController) SendSms() {
|
2021-09-05 10:30:51 +08:00
|
|
|
provider, _, ok := c.GetProviderFromContext("SMS")
|
|
|
|
if !ok {
|
2021-07-30 14:15:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-21 23:11:29 +08:00
|
|
|
var smsForm SmsForm
|
2021-09-05 10:30:51 +08:00
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &smsForm)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
2021-07-30 14:15:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-31 08:49:39 +08:00
|
|
|
org := object.GetOrganization(smsForm.OrgId)
|
2021-07-30 14:15:10 +08:00
|
|
|
var invalidReceivers []string
|
2021-10-31 08:49:39 +08:00
|
|
|
for idx, receiver := range smsForm.Receivers {
|
2021-07-30 14:15:10 +08:00
|
|
|
if !util.IsPhoneCnValid(receiver) {
|
|
|
|
invalidReceivers = append(invalidReceivers, receiver)
|
2021-10-31 08:49:39 +08:00
|
|
|
} else {
|
|
|
|
smsForm.Receivers[idx] = fmt.Sprintf("+%s%s", org.PhonePrefix, receiver)
|
2021-07-30 14:15:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:02:56 +08:00
|
|
|
if len(invalidReceivers) != 0 {
|
2021-09-05 10:30:51 +08:00
|
|
|
c.ResponseError(fmt.Sprintf("Invalid phone receivers: %s", invalidReceivers))
|
2021-07-30 14:15:10 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-05 11:10:27 +08:00
|
|
|
err = object.SendSms(provider, smsForm.Content, smsForm.Receivers...)
|
2021-09-05 10:48:03 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-09-05 10:30:51 +08:00
|
|
|
|
|
|
|
c.ResponseOk()
|
2021-07-30 14:15:10 +08:00
|
|
|
}
|