diff --git a/object/sms_custom.go b/notification/custom_http.go similarity index 76% rename from object/sms_custom.go rename to notification/custom_http.go index c7bd6d45..736e4278 100644 --- a/object/sms_custom.go +++ b/notification/custom_http.go @@ -12,37 +12,35 @@ // See the License for the specific language governing permissions and // limitations under the License. -package object +package notification import ( "bytes" + "context" "fmt" "net/http" "github.com/casdoor/casdoor/proxy" ) -type HttpSmsClient struct { +type HttpNotificationClient struct { endpoint string method string paramName string - text string } -func newHttpSmsClient(endpoint string, method string, paramName string, text string) (*HttpSmsClient, error) { - client := &HttpSmsClient{ +func NewCustomHttpProvider(endpoint string, method string, paramName string) (*HttpNotificationClient, error) { + client := &HttpNotificationClient{ endpoint: endpoint, method: method, paramName: paramName, - text: text, } return client, nil } -func (c *HttpSmsClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error { +func (c *HttpNotificationClient) Send(ctx context.Context, subject string, content string) error { var err error - content := param["code"] httpClient := proxy.DefaultHttpClient req, err := http.NewRequest(c.method, c.endpoint, bytes.NewBufferString(content)) @@ -68,7 +66,7 @@ func (c *HttpSmsClient) SendMessage(param map[string]string, targetPhoneNumber . defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - return fmt.Errorf("SendMessage() error, custom HTTP SMS request failed with status: %s", resp.Status) + return fmt.Errorf("SendMessage() error, custom HTTP Notification request failed with status: %s", resp.Status) } return err diff --git a/notification/provider.go b/notification/provider.go index d6951411..0e1242f9 100644 --- a/notification/provider.go +++ b/notification/provider.go @@ -16,9 +16,11 @@ package notification import "github.com/nikoksr/notify" -func GetNotificationProvider(typ string, appId string, receiver string) (notify.Notifier, error) { +func GetNotificationProvider(typ string, appId string, receiver string, method string, title string) (notify.Notifier, error) { if typ == "Telegram" { return NewTelegramProvider(appId, receiver) + } else if typ == "Custom HTTP" { + return NewCustomHttpProvider(receiver, method, title) } return nil, nil diff --git a/object/notification.go b/object/notification.go index 6dce8bdc..89c50d51 100644 --- a/object/notification.go +++ b/object/notification.go @@ -23,7 +23,7 @@ import ( func getNotificationClient(provider *Provider) (notify.Notifier, error) { var client notify.Notifier - client, err := notification.GetNotificationProvider(provider.Type, provider.AppId, provider.Receiver) + client, err := notification.GetNotificationProvider(provider.Type, provider.AppId, provider.Receiver, provider.Method, provider.Title) if err != nil { return nil, err } diff --git a/object/sms.go b/object/sms.go index 1e7acada..85f3d33b 100644 --- a/object/sms.go +++ b/object/sms.go @@ -26,8 +26,6 @@ func getSmsClient(provider *Provider) (sender.SmsClient, error) { if provider.Type == sender.HuaweiCloud || provider.Type == sender.AzureACS { client, err = sender.NewSmsClient(provider.Type, provider.ClientId, provider.ClientSecret, provider.SignName, provider.TemplateCode, provider.ProviderUrl, provider.AppId) - } else if provider.Type == "Custom HTTP SMS" { - client, err = newHttpSmsClient(provider.Endpoint, provider.Method, provider.ClientId, provider.Title) } else { client, err = sender.NewSmsClient(provider.Type, provider.ClientId, provider.ClientSecret, provider.SignName, provider.TemplateCode, provider.AppId) } diff --git a/web/src/ProviderEditPage.js b/web/src/ProviderEditPage.js index 079d8418..48f08dac 100644 --- a/web/src/ProviderEditPage.js +++ b/web/src/ProviderEditPage.js @@ -278,8 +278,8 @@ class ProviderEditPage extends React.Component { } } else if (provider.category === "Notification") { if (provider.type === "Telegram") { - text = i18next.t("provider:Api Token"); - tooltip = i18next.t("provider:Api Token - Tooltip"); + text = i18next.t("provider:App Key"); + tooltip = i18next.t("provider:App Key - Tooltip"); } } @@ -301,6 +301,36 @@ class ProviderEditPage extends React.Component { } } + getReceiverRow(provider) { + let text = ""; + let tooltip = ""; + + if (provider.type === "Telegram") { + text = i18next.t("provider:Chat ID"); + tooltip = i18next.t("provider:Chat ID - Tooltip"); + } else if (provider.type === "Custom HTTP") { + text = i18next.t("provider:Endpoint"); + tooltip = i18next.t("provider:Endpoint - Tooltip"); + } + + if (text === "" && tooltip === "") { + return null; + } else { + return ( + + + {Setting.getLabel(text, tooltip)} : + + + { + this.updateProviderField("receiver", e.target.value); + }} /> + + + ); + } + } + loadSamlConfiguration() { const parser = new DOMParser(); const xmlDoc = parser.parseFromString(this.state.provider.metadata, "text/xml"); @@ -421,10 +451,8 @@ class ProviderEditPage extends React.Component { this.updateProviderField("scopes", "openid profile email"); this.updateProviderField("customTokenUrl", "https://door.casdoor.com/api/login/oauth/access_token"); this.updateProviderField("customUserInfoUrl", "https://door.casdoor.com/api/userinfo"); - } else if (value === "Custom HTTP SMS") { - this.updateProviderField("endpoint", "https://door.casdoor.com/api/get-account"); + } else if (value === "Custom HTTP") { this.updateProviderField("method", "GET"); - this.updateProviderField("clientId", "param1"); this.updateProviderField("title", ""); } })}> @@ -562,7 +590,6 @@ class ProviderEditPage extends React.Component { } { (this.state.provider.category === "Captcha" && this.state.provider.type === "Default") || - (this.state.provider.category === "SMS" && this.state.provider.type === "Custom HTTP SMS") || (this.state.provider.category === "Web3") || (this.state.provider.category === "Storage" && this.state.provider.type === "Local File System" || (this.state.provider.category === "Notification")) ? null : ( @@ -647,7 +674,7 @@ class ProviderEditPage extends React.Component { ) } - {this.state.provider.category === "Storage" || this.state.provider.type === "Custom HTTP SMS" ? ( + {this.state.provider.category === "Storage" ? (
{["Local File System"].includes(this.state.provider.type) ? null : ( @@ -661,7 +688,7 @@ class ProviderEditPage extends React.Component { )} - {["Custom HTTP SMS", "Local File System", "MinIO", "Tencent Cloud COS", "Google Cloud Storage", "Qiniu Cloud Kodo"].includes(this.state.provider.type) ? null : ( + {["Local File System", "MinIO", "Tencent Cloud COS", "Google Cloud Storage", "Qiniu Cloud Kodo"].includes(this.state.provider.type) ? null : ( {Setting.getLabel(i18next.t("provider:Endpoint (Intranet)"), i18next.t("provider:Region endpoint for Intranet"))} : @@ -673,7 +700,7 @@ class ProviderEditPage extends React.Component { )} - {["Custom HTTP SMS", "Local File System"].includes(this.state.provider.type) ? null : ( + {["Local File System"].includes(this.state.provider.type) ? null : ( {Setting.getLabel(i18next.t("provider:Bucket"), i18next.t("provider:Bucket - Tooltip"))} : @@ -685,19 +712,17 @@ class ProviderEditPage extends React.Component { )} - {["Custom HTTP SMS"].includes(this.state.provider.type) ? null : ( - - - {Setting.getLabel(i18next.t("provider:Path prefix"), i18next.t("provider:Path prefix - Tooltip"))} : - - - { - this.updateProviderField("pathPrefix", e.target.value); - }} /> - - - )} - {["Custom HTTP SMS", "MinIO", "Google Cloud Storage", "Qiniu Cloud Kodo"].includes(this.state.provider.type) ? null : ( + + + {Setting.getLabel(i18next.t("provider:Path prefix"), i18next.t("provider:Path prefix - Tooltip"))} : + + + { + this.updateProviderField("pathPrefix", e.target.value); + }} /> + + + {["MinIO", "Google Cloud Storage", "Qiniu Cloud Kodo"].includes(this.state.provider.type) ? null : ( {Setting.getLabel(i18next.t("provider:Domain"), i18next.t("provider:Domain - Tooltip"))} : @@ -723,56 +748,44 @@ class ProviderEditPage extends React.Component { ) : null}
) : null} - { - this.state.provider.type !== "Custom HTTP SMS" ? null : ( - - - - {Setting.getLabel(i18next.t("general:Method"), i18next.t("provider:Method - Tooltip"))} : - - - - - - - - {Setting.getLabel(i18next.t("provider:Parameter name"), i18next.t("provider:Parameter name - Tooltip"))} : - - - { - this.updateProviderField("clientId", e.target.value); - }} /> - - - - - {Setting.getLabel(i18next.t("provider:Content"), i18next.t("provider:Content - Tooltip"))} : - - -