2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2021-07-07 14:59:03 +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 (
|
2021-11-27 21:12:13 +08:00
|
|
|
"fmt"
|
2021-11-07 16:51:16 +08:00
|
|
|
"strings"
|
|
|
|
|
2022-09-29 19:44:08 +08:00
|
|
|
"github.com/beego/beego/context"
|
2022-03-20 23:21:09 +08:00
|
|
|
"github.com/casdoor/casdoor/conf"
|
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-07-07 14:59:03 +08:00
|
|
|
)
|
|
|
|
|
2021-11-09 23:32:53 +08:00
|
|
|
var logPostOnly bool
|
|
|
|
|
|
|
|
func init() {
|
2023-05-30 15:49:39 +08:00
|
|
|
logPostOnly = conf.GetConfigBool("logPostOnly")
|
2021-11-09 23:32:53 +08:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:51:16 +08:00
|
|
|
type Record struct {
|
2023-08-19 23:15:02 +08:00
|
|
|
casvisorsdk.Record
|
2021-07-07 14:59:03 +08:00
|
|
|
}
|
|
|
|
|
2023-08-19 23:15:02 +08:00
|
|
|
func NewRecord(ctx *context.Context) *casvisorsdk.Record {
|
2021-11-07 16:51:16 +08:00
|
|
|
ip := strings.Replace(util.GetIPFromRequest(ctx.Request), ": ", "", -1)
|
|
|
|
action := strings.Replace(ctx.Request.URL.Path, "/api/", "", -1)
|
2021-12-13 09:49:43 +08:00
|
|
|
requestUri := util.FilterQuery(ctx.Request.RequestURI, []string{"accessToken"})
|
2021-11-12 00:01:37 +08:00
|
|
|
if len(requestUri) > 1000 {
|
|
|
|
requestUri = requestUri[0:1000]
|
|
|
|
}
|
2021-11-07 16:51:16 +08:00
|
|
|
|
2023-05-17 23:57:14 +08:00
|
|
|
object := ""
|
|
|
|
if ctx.Input.RequestBody != nil && len(ctx.Input.RequestBody) != 0 {
|
|
|
|
object = string(ctx.Input.RequestBody)
|
|
|
|
}
|
|
|
|
|
2023-08-19 23:15:02 +08:00
|
|
|
record := casvisorsdk.Record{
|
2021-11-07 17:36:52 +08:00
|
|
|
Name: util.GenerateId(),
|
|
|
|
CreatedTime: util.GetCurrentTime(),
|
|
|
|
ClientIp: ip,
|
|
|
|
User: "",
|
2021-11-09 23:24:13 +08:00
|
|
|
Method: ctx.Request.Method,
|
2021-11-12 00:01:37 +08:00
|
|
|
RequestUri: requestUri,
|
2021-11-07 17:36:52 +08:00
|
|
|
Action: action,
|
2023-05-17 23:57:14 +08:00
|
|
|
Object: object,
|
2021-11-07 23:53:17 +08:00
|
|
|
IsTriggered: false,
|
2021-11-07 16:51:16 +08:00
|
|
|
}
|
|
|
|
return &record
|
|
|
|
}
|
2021-07-07 14:59:03 +08:00
|
|
|
|
2023-08-19 23:15:02 +08:00
|
|
|
func AddRecord(record *casvisorsdk.Record) bool {
|
2021-11-09 23:32:53 +08:00
|
|
|
if logPostOnly {
|
|
|
|
if record.Method == "GET" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 16:21:15 +08:00
|
|
|
if record.Organization == "app" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-07 17:36:52 +08:00
|
|
|
record.Owner = record.Organization
|
|
|
|
|
2021-11-07 23:53:17 +08:00
|
|
|
errWebhook := SendWebhooks(record)
|
|
|
|
if errWebhook == nil {
|
|
|
|
record.IsTriggered = true
|
2021-11-27 21:12:13 +08:00
|
|
|
} else {
|
|
|
|
fmt.Println(errWebhook)
|
2021-11-07 23:53:17 +08:00
|
|
|
}
|
|
|
|
|
2023-08-19 23:15:02 +08:00
|
|
|
if casvisorsdk.GetClient() == nil {
|
|
|
|
return false
|
2021-11-06 11:32:22 +08:00
|
|
|
}
|
|
|
|
|
2023-08-19 23:15:02 +08:00
|
|
|
affected, err := casvisorsdk.AddRecord(record)
|
2021-07-07 14:59:03 +08:00
|
|
|
if err != nil {
|
2023-08-19 23:15:02 +08:00
|
|
|
panic(err)
|
2021-07-07 14:59:03 +08:00
|
|
|
}
|
|
|
|
|
2023-08-19 23:15:02 +08:00
|
|
|
return affected
|
2021-07-07 14:59:03 +08:00
|
|
|
}
|
2021-11-07 23:53:17 +08:00
|
|
|
|
2023-08-19 23:15:02 +08:00
|
|
|
func SendWebhooks(record *casvisorsdk.Record) error {
|
2023-05-30 15:49:39 +08:00
|
|
|
webhooks, err := getWebhooksByOrganization(record.Organization)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:53:17 +08:00
|
|
|
for _, webhook := range webhooks {
|
2021-12-26 21:02:56 +08:00
|
|
|
if !webhook.IsEnabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:53:17 +08:00
|
|
|
matched := false
|
|
|
|
for _, event := range webhook.Events {
|
|
|
|
if record.Action == event {
|
|
|
|
matched = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if matched {
|
2023-08-19 23:15:02 +08:00
|
|
|
var user *User
|
2022-01-01 11:15:04 +08:00
|
|
|
if webhook.IsUserExtended {
|
2023-08-19 23:15:02 +08:00
|
|
|
user, err = getUser(record.Organization, record.User)
|
2023-07-19 19:14:53 +08:00
|
|
|
user, err = GetMaskedUser(user, false, err)
|
2023-05-30 15:49:39 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-01 11:15:04 +08:00
|
|
|
}
|
|
|
|
|
2023-08-19 23:15:02 +08:00
|
|
|
err = sendWebhook(webhook, record, user)
|
2021-11-07 23:53:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|