Make webhook work.

This commit is contained in:
Gucheng Wang
2021-11-07 23:53:17 +08:00
parent 87e6fb63e1
commit 30a2fdef37
6 changed files with 91 additions and 5 deletions

View File

@ -198,7 +198,7 @@ func (c *ApiController) Login() {
record := object.NewRecord(c.Ctx) record := object.NewRecord(c.Ctx)
record.Organization = application.Organization record.Organization = application.Organization
record.User = user.Name record.User = user.Name
object.AddRecord(record) go object.AddRecord(record)
} }
} else if form.Provider != "" { } else if form.Provider != "" {
application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application)) application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application))
@ -262,7 +262,7 @@ func (c *ApiController) Login() {
record := object.NewRecord(c.Ctx) record := object.NewRecord(c.Ctx)
record.Organization = application.Organization record.Organization = application.Organization
record.User = user.Name record.User = user.Name
object.AddRecord(record) go object.AddRecord(record)
} else { } else {
// Sign up via OAuth // Sign up via OAuth
if !application.EnableSignUp { if !application.EnableSignUp {
@ -307,7 +307,7 @@ func (c *ApiController) Login() {
record := object.NewRecord(c.Ctx) record := object.NewRecord(c.Ctx)
record.Organization = application.Organization record.Organization = application.Organization
record.User = user.Name record.User = user.Name
object.AddRecord(record) go object.AddRecord(record)
} }
//resp = &Response{Status: "ok", Msg: "", Data: res} //resp = &Response{Status: "ok", Msg: "", Data: res}
} else { // form.Method != "signup" } else { // form.Method != "signup"

View File

@ -48,6 +48,7 @@ func NewRecord(ctx *context.Context) *Record {
RequestUri: ctx.Request.RequestURI, RequestUri: ctx.Request.RequestURI,
User: "", User: "",
Action: action, Action: action,
IsTriggered: false,
} }
return &record return &record
} }
@ -55,11 +56,20 @@ func NewRecord(ctx *context.Context) *Record {
func AddRecord(record *Record) bool { func AddRecord(record *Record) bool {
record.Owner = record.Organization record.Owner = record.Organization
errWebhook := SendWebhooks(record)
if errWebhook == nil {
record.IsTriggered = true
}
affected, err := adapter.Engine.Insert(record) affected, err := adapter.Engine.Insert(record)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if errWebhook != nil {
panic(errWebhook)
}
return affected != 0 return affected != 0
} }
@ -101,3 +111,25 @@ func GetRecordsByField(record *Record) []*Record {
return records 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
}

View File

@ -30,7 +30,7 @@ type Webhook struct {
ContentType string `xorm:"varchar(100)" json:"contentType"` ContentType string `xorm:"varchar(100)" json:"contentType"`
Events []string `xorm:"varchar(100)" json:"events"` 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 { func GetWebhookCount(owner string) int {
@ -62,6 +62,16 @@ func GetPaginationWebhooks(owner string, offset, limit int) []*Webhook {
return webhooks 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 { func getWebhook(owner string, name string) *Webhook {
if owner == "" || name == "" { if owner == "" || name == "" {
return nil return nil

38
object/webhook_util.go Normal file
View 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
}

View File

@ -65,5 +65,5 @@ func RecordMessage(ctx *context.Context) {
record.Organization, record.User = util.GetOwnerAndNameFromId(userId) record.Organization, record.User = util.GetOwnerAndNameFromId(userId)
} }
object.AddRecord(record) go object.AddRecord(record)
} }

View File

@ -142,6 +142,7 @@ class RecordListPage extends React.Component {
key: 'action', key: 'action',
width: '200px', width: '200px',
sorter: (a, b) => a.action.localeCompare(b.action), sorter: (a, b) => a.action.localeCompare(b.action),
fixed: (Setting.isMobile()) ? "false" : "right",
render: (text, record, index) => { render: (text, record, index) => {
return text; return text;
} }
@ -152,7 +153,12 @@ class RecordListPage extends React.Component {
key: 'isTriggered', key: 'isTriggered',
width: '140px', width: '140px',
sorter: (a, b) => a.isTriggered - b.isTriggered, sorter: (a, b) => a.isTriggered - b.isTriggered,
fixed: (Setting.isMobile()) ? "false" : "right",
render: (text, record, index) => { render: (text, record, index) => {
if (!["signup", "login", "logout", "update-user"].includes(record.action)) {
return null;
}
return ( return (
<Switch disabled checkedChildren="ON" unCheckedChildren="OFF" checked={text} /> <Switch disabled checkedChildren="ON" unCheckedChildren="OFF" checked={text} />
) )