mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 12:30:19 +08:00
Use Casvisor for records
This commit is contained in:
@ -437,14 +437,6 @@ func organizationChangeTrigger(oldName string, newName string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
record := new(Record)
|
||||
record.Owner = newName
|
||||
record.Organization = newName
|
||||
_, err = session.Where("organization=?", oldName).Update(record)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resource := new(Resource)
|
||||
resource.Owner = newName
|
||||
_, err = session.Where("owner=?", oldName).Update(resource)
|
||||
|
@ -246,11 +246,6 @@ func (a *Ormer) createTable() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = a.Engine.Sync2(new(Record))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = a.Engine.Sync2(new(Webhook))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -21,6 +21,7 @@ import (
|
||||
"github.com/beego/beego/context"
|
||||
"github.com/casdoor/casdoor/conf"
|
||||
"github.com/casdoor/casdoor/util"
|
||||
"github.com/casvisor/casvisor-go-sdk/casvisorsdk"
|
||||
)
|
||||
|
||||
var logPostOnly bool
|
||||
@ -30,26 +31,10 @@ func init() {
|
||||
}
|
||||
|
||||
type Record struct {
|
||||
Id int `xorm:"int notnull pk autoincr" json:"id"`
|
||||
|
||||
Owner string `xorm:"varchar(100) index" json:"owner"`
|
||||
Name string `xorm:"varchar(100) index" json:"name"`
|
||||
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
|
||||
|
||||
Organization string `xorm:"varchar(100)" json:"organization"`
|
||||
ClientIp string `xorm:"varchar(100)" json:"clientIp"`
|
||||
User string `xorm:"varchar(100)" json:"user"`
|
||||
Method string `xorm:"varchar(100)" json:"method"`
|
||||
RequestUri string `xorm:"varchar(1000)" json:"requestUri"`
|
||||
Action string `xorm:"varchar(1000)" json:"action"`
|
||||
|
||||
Object string `xorm:"-" json:"object"`
|
||||
ExtendedUser *User `xorm:"-" json:"extendedUser"`
|
||||
|
||||
IsTriggered bool `json:"isTriggered"`
|
||||
casvisorsdk.Record
|
||||
}
|
||||
|
||||
func NewRecord(ctx *context.Context) *Record {
|
||||
func NewRecord(ctx *context.Context) *casvisorsdk.Record {
|
||||
ip := strings.Replace(util.GetIPFromRequest(ctx.Request), ": ", "", -1)
|
||||
action := strings.Replace(ctx.Request.URL.Path, "/api/", "", -1)
|
||||
requestUri := util.FilterQuery(ctx.Request.RequestURI, []string{"accessToken"})
|
||||
@ -62,7 +47,7 @@ func NewRecord(ctx *context.Context) *Record {
|
||||
object = string(ctx.Input.RequestBody)
|
||||
}
|
||||
|
||||
record := Record{
|
||||
record := casvisorsdk.Record{
|
||||
Name: util.GenerateId(),
|
||||
CreatedTime: util.GetCurrentTime(),
|
||||
ClientIp: ip,
|
||||
@ -76,7 +61,7 @@ func NewRecord(ctx *context.Context) *Record {
|
||||
return &record
|
||||
}
|
||||
|
||||
func AddRecord(record *Record) bool {
|
||||
func AddRecord(record *casvisorsdk.Record) bool {
|
||||
if logPostOnly {
|
||||
if record.Method == "GET" {
|
||||
return false
|
||||
@ -96,51 +81,19 @@ func AddRecord(record *Record) bool {
|
||||
fmt.Println(errWebhook)
|
||||
}
|
||||
|
||||
affected, err := ormer.Engine.Insert(record)
|
||||
if casvisorsdk.GetClient() == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
affected, err := casvisorsdk.AddRecord(record)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return affected != 0
|
||||
return affected
|
||||
}
|
||||
|
||||
func GetRecordCount(field, value string, filterRecord *Record) (int64, error) {
|
||||
session := GetSession("", -1, -1, field, value, "", "")
|
||||
return session.Count(filterRecord)
|
||||
}
|
||||
|
||||
func GetRecords() ([]*Record, error) {
|
||||
records := []*Record{}
|
||||
err := ormer.Engine.Desc("id").Find(&records)
|
||||
if err != nil {
|
||||
return records, err
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func GetPaginationRecords(offset, limit int, field, value, sortField, sortOrder string, filterRecord *Record) ([]*Record, error) {
|
||||
records := []*Record{}
|
||||
session := GetSession("", offset, limit, field, value, sortField, sortOrder)
|
||||
err := session.Find(&records, filterRecord)
|
||||
if err != nil {
|
||||
return records, err
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func GetRecordsByField(record *Record) ([]*Record, error) {
|
||||
records := []*Record{}
|
||||
err := ormer.Engine.Find(&records, record)
|
||||
if err != nil {
|
||||
return records, err
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func SendWebhooks(record *Record) error {
|
||||
func SendWebhooks(record *casvisorsdk.Record) error {
|
||||
webhooks, err := getWebhooksByOrganization(record.Organization)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -160,17 +113,16 @@ func SendWebhooks(record *Record) error {
|
||||
}
|
||||
|
||||
if matched {
|
||||
var user *User
|
||||
if webhook.IsUserExtended {
|
||||
user, err := getUser(record.Organization, record.User)
|
||||
user, err = getUser(record.Organization, record.User)
|
||||
user, err = GetMaskedUser(user, false, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
record.ExtendedUser = user
|
||||
}
|
||||
|
||||
err := sendWebhook(webhook, record)
|
||||
err = sendWebhook(webhook, record, user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
50
object/record_casvisor.go
Normal file
50
object/record_casvisor.go
Normal file
@ -0,0 +1,50 @@
|
||||
// Copyright 2023 The Casdoor 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 (
|
||||
"strings"
|
||||
|
||||
"github.com/casvisor/casvisor-go-sdk/casvisorsdk"
|
||||
)
|
||||
|
||||
func getCasvisorApplication() *Application {
|
||||
applications, err := GetApplications("admin")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, application := range applications {
|
||||
if strings.Contains(strings.ToLower(application.Name), "casvisor") {
|
||||
return application
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func InitCasvisorConfig() {
|
||||
application := getCasvisorApplication()
|
||||
if application == nil {
|
||||
return
|
||||
}
|
||||
|
||||
casvisorEndpoint := application.HomepageUrl
|
||||
clientId := application.ClientId
|
||||
clientSecret := application.ClientSecret
|
||||
casdoorOrganization := application.Organization
|
||||
casdoorApplication := application.Name
|
||||
|
||||
casvisorsdk.InitConfig(casvisorEndpoint, clientId, clientSecret, casdoorOrganization, casdoorApplication)
|
||||
}
|
@ -19,12 +19,22 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/casdoor/casdoor/util"
|
||||
"github.com/casvisor/casvisor-go-sdk/casvisorsdk"
|
||||
)
|
||||
|
||||
func sendWebhook(webhook *Webhook, record *Record) error {
|
||||
func sendWebhook(webhook *Webhook, record *casvisorsdk.Record, extendedUser *User) error {
|
||||
client := &http.Client{}
|
||||
|
||||
body := strings.NewReader(util.StructToJson(record))
|
||||
type RecordEx struct {
|
||||
casvisorsdk.Record
|
||||
ExtendedUser *User `xorm:"-" json:"extendedUser"`
|
||||
}
|
||||
recordEx := &RecordEx{
|
||||
Record: *record,
|
||||
ExtendedUser: extendedUser,
|
||||
}
|
||||
|
||||
body := strings.NewReader(util.StructToJson(recordEx))
|
||||
|
||||
req, err := http.NewRequest(webhook.Method, webhook.Url, body)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user