2021-07-07 14:59:03 +08:00
|
|
|
// 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 (
|
2021-11-07 16:51:16 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/astaxie/beego/context"
|
2021-07-25 09:34:25 +08:00
|
|
|
"github.com/casbin/casdoor/util"
|
2021-07-07 14:59:03 +08:00
|
|
|
)
|
|
|
|
|
2021-11-07 16:51:16 +08:00
|
|
|
type Record struct {
|
|
|
|
Id int `xorm:"int notnull pk autoincr" json:"id"`
|
|
|
|
|
2021-11-07 17:36:52 +08:00
|
|
|
Owner string `xorm:"varchar(100) index" json:"owner"`
|
|
|
|
Name string `xorm:"varchar(100) index" json:"name"`
|
|
|
|
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
|
|
|
|
|
2021-11-07 16:51:16 +08:00
|
|
|
Organization string `xorm:"varchar(100)" json:"organization"`
|
2021-11-07 17:36:52 +08:00
|
|
|
ClientIp string `xorm:"varchar(100)" json:"clientIp"`
|
2021-11-07 17:20:15 +08:00
|
|
|
User string `xorm:"varchar(100)" json:"user"`
|
2021-11-07 16:51:16 +08:00
|
|
|
RequestUri string `xorm:"varchar(1000)" json:"requestUri"`
|
|
|
|
Action string `xorm:"varchar(1000)" json:"action"`
|
2021-11-07 17:42:41 +08:00
|
|
|
|
|
|
|
IsTriggered bool `json:"isTriggered"`
|
2021-07-07 14:59:03 +08:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:51:16 +08:00
|
|
|
func NewRecord(ctx *context.Context) *Record {
|
|
|
|
ip := strings.Replace(util.GetIPFromRequest(ctx.Request), ": ", "", -1)
|
|
|
|
action := strings.Replace(ctx.Request.URL.Path, "/api/", "", -1)
|
|
|
|
|
|
|
|
record := Record{
|
2021-11-07 17:36:52 +08:00
|
|
|
Name: util.GenerateId(),
|
|
|
|
CreatedTime: util.GetCurrentTime(),
|
|
|
|
ClientIp: ip,
|
|
|
|
RequestUri: ctx.Request.RequestURI,
|
|
|
|
User: "",
|
|
|
|
Action: action,
|
2021-11-07 16:51:16 +08:00
|
|
|
}
|
|
|
|
return &record
|
|
|
|
}
|
2021-07-07 14:59:03 +08:00
|
|
|
|
2021-11-07 16:51:16 +08:00
|
|
|
func AddRecord(record *Record) bool {
|
2021-11-07 17:36:52 +08:00
|
|
|
record.Owner = record.Organization
|
|
|
|
|
2021-11-07 16:51:16 +08:00
|
|
|
affected, err := adapter.Engine.Insert(record)
|
2021-07-07 14:59:03 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRecordCount() int {
|
2021-11-07 16:51:16 +08:00
|
|
|
count, err := adapter.Engine.Count(&Record{})
|
2021-07-07 14:59:03 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(count)
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:51:16 +08:00
|
|
|
func GetRecords() []*Record {
|
|
|
|
records := []*Record{}
|
2021-07-07 14:59:03 +08:00
|
|
|
err := adapter.Engine.Desc("id").Find(&records)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return records
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:51:16 +08:00
|
|
|
func GetPaginationRecords(offset, limit int) []*Record {
|
|
|
|
records := []*Record{}
|
2021-11-06 11:32:22 +08:00
|
|
|
err := adapter.Engine.Desc("id").Limit(limit, offset).Find(&records)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return records
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:51:16 +08:00
|
|
|
func GetRecordsByField(record *Record) []*Record {
|
|
|
|
records := []*Record{}
|
2021-07-07 14:59:03 +08:00
|
|
|
err := adapter.Engine.Find(&records, record)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return records
|
|
|
|
}
|