mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 12:30:19 +08:00
Make webhook work.
This commit is contained in:
@ -48,6 +48,7 @@ func NewRecord(ctx *context.Context) *Record {
|
||||
RequestUri: ctx.Request.RequestURI,
|
||||
User: "",
|
||||
Action: action,
|
||||
IsTriggered: false,
|
||||
}
|
||||
return &record
|
||||
}
|
||||
@ -55,11 +56,20 @@ func NewRecord(ctx *context.Context) *Record {
|
||||
func AddRecord(record *Record) bool {
|
||||
record.Owner = record.Organization
|
||||
|
||||
errWebhook := SendWebhooks(record)
|
||||
if errWebhook == nil {
|
||||
record.IsTriggered = true
|
||||
}
|
||||
|
||||
affected, err := adapter.Engine.Insert(record)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if errWebhook != nil {
|
||||
panic(errWebhook)
|
||||
}
|
||||
|
||||
return affected != 0
|
||||
}
|
||||
|
||||
@ -101,3 +111,25 @@ func GetRecordsByField(record *Record) []*Record {
|
||||
|
||||
return records
|
||||
}
|
||||
|
||||
func SendWebhooks(record *Record) error {
|
||||
webhooks := getWebhooksByOrganization(record.Organization)
|
||||
for _, webhook := range webhooks {
|
||||
matched := false
|
||||
for _, event := range webhook.Events {
|
||||
if record.Action == event {
|
||||
matched = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if matched {
|
||||
err := sendWebhook(webhook, record)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ type Webhook struct {
|
||||
ContentType string `xorm:"varchar(100)" json:"contentType"`
|
||||
Events []string `xorm:"varchar(100)" json:"events"`
|
||||
|
||||
Organization string `xorm:"varchar(100)" json:"organization"`
|
||||
Organization string `xorm:"varchar(100) index" json:"organization"`
|
||||
}
|
||||
|
||||
func GetWebhookCount(owner string) int {
|
||||
@ -62,6 +62,16 @@ func GetPaginationWebhooks(owner string, offset, limit int) []*Webhook {
|
||||
return webhooks
|
||||
}
|
||||
|
||||
func getWebhooksByOrganization(organization string) []*Webhook {
|
||||
webhooks := []*Webhook{}
|
||||
err := adapter.Engine.Desc("created_time").Find(&webhooks, &Webhook{Organization: organization})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return webhooks
|
||||
}
|
||||
|
||||
func getWebhook(owner string, name string) *Webhook {
|
||||
if owner == "" || name == "" {
|
||||
return nil
|
||||
|
38
object/webhook_util.go
Normal file
38
object/webhook_util.go
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
||||
//
|
||||
// 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 (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/casbin/casdoor/util"
|
||||
)
|
||||
|
||||
func sendWebhook(webhook *Webhook, record *Record) error {
|
||||
client := &http.Client{}
|
||||
|
||||
body := strings.NewReader(util.StructToJson(record))
|
||||
|
||||
req, err := http.NewRequest("POST", webhook.Url, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", webhook.ContentType)
|
||||
|
||||
_, err = client.Do(req)
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user