mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-09 01:13:41 +08:00
Make webhook work.
This commit is contained in:
@ -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"
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
@ -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
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
|
||||||
|
}
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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} />
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user