2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2021-11-07 23:53:17 +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.
|
|
|
|
|
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2024-05-12 20:30:15 +08:00
|
|
|
"io"
|
2021-11-07 23:53:17 +08:00
|
|
|
"net/http"
|
2025-03-04 14:16:16 +08:00
|
|
|
"reflect"
|
2021-11-07 23:53:17 +08:00
|
|
|
"strings"
|
|
|
|
|
2022-01-20 14:11:46 +08:00
|
|
|
"github.com/casdoor/casdoor/util"
|
2023-08-19 23:15:02 +08:00
|
|
|
"github.com/casvisor/casvisor-go-sdk/casvisorsdk"
|
2021-11-07 23:53:17 +08:00
|
|
|
)
|
|
|
|
|
2024-05-12 20:30:15 +08:00
|
|
|
func sendWebhook(webhook *Webhook, record *casvisorsdk.Record, extendedUser *User) (int, string, error) {
|
2021-11-07 23:53:17 +08:00
|
|
|
client := &http.Client{}
|
2025-03-04 14:16:16 +08:00
|
|
|
userMap := make(map[string]interface{})
|
|
|
|
var body io.Reader
|
2021-11-07 23:53:17 +08:00
|
|
|
|
2025-03-04 14:16:16 +08:00
|
|
|
if webhook.TokenFields != nil && len(webhook.TokenFields) > 0 && extendedUser != nil {
|
|
|
|
userValue := reflect.ValueOf(extendedUser).Elem()
|
|
|
|
|
|
|
|
for _, field := range webhook.TokenFields {
|
|
|
|
userField := userValue.FieldByName(field)
|
|
|
|
if userField.IsValid() {
|
|
|
|
newfield := util.SnakeToCamel(util.CamelToSnakeCase(field))
|
|
|
|
userMap[newfield] = userField.Interface()
|
|
|
|
}
|
|
|
|
}
|
2023-08-19 23:15:02 +08:00
|
|
|
|
2025-03-08 00:20:59 +08:00
|
|
|
type RecordEx struct {
|
|
|
|
casvisorsdk.Record
|
|
|
|
ExtendedUser map[string]interface{} `json:"extendedUser"`
|
|
|
|
}
|
|
|
|
|
|
|
|
recordEx := &RecordEx{
|
|
|
|
Record: *record,
|
|
|
|
ExtendedUser: userMap,
|
|
|
|
}
|
|
|
|
|
|
|
|
body = strings.NewReader(util.StructToJson(recordEx))
|
2025-03-04 14:16:16 +08:00
|
|
|
} else {
|
|
|
|
type RecordEx struct {
|
|
|
|
casvisorsdk.Record
|
|
|
|
ExtendedUser *User `xorm:"-" json:"extendedUser"`
|
|
|
|
}
|
|
|
|
recordEx := &RecordEx{
|
|
|
|
Record: *record,
|
|
|
|
ExtendedUser: extendedUser,
|
|
|
|
}
|
|
|
|
|
|
|
|
body = strings.NewReader(util.StructToJson(recordEx))
|
|
|
|
}
|
2021-11-07 23:53:17 +08:00
|
|
|
|
2021-12-26 19:56:02 +08:00
|
|
|
req, err := http.NewRequest(webhook.Method, webhook.Url, body)
|
2021-11-07 23:53:17 +08:00
|
|
|
if err != nil {
|
2024-05-12 20:30:15 +08:00
|
|
|
return 0, "", err
|
2021-11-07 23:53:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", webhook.ContentType)
|
|
|
|
|
2021-12-26 20:40:13 +08:00
|
|
|
for _, header := range webhook.Headers {
|
|
|
|
req.Header.Set(header.Name, header.Value)
|
|
|
|
}
|
|
|
|
|
2024-05-12 20:30:15 +08:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return 0, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
bodyBytes, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return 0, "", err
|
|
|
|
}
|
|
|
|
return resp.StatusCode, string(bodyBytes), err
|
2021-11-07 23:53:17 +08:00
|
|
|
}
|