casdoor/object/record.go

97 lines
2.3 KiB
Go
Raw Normal View History

// 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"
"github.com/casbin/casdoor/util"
)
2021-11-07 16:51:16 +08:00
type Record struct {
Id int `xorm:"int notnull pk autoincr" json:"id"`
ClientIp string `xorm:"varchar(100)" json:"clientIp"`
Timestamp string `xorm:"varchar(100)" json:"timestamp"`
Organization string `xorm:"varchar(100)" json:"organization"`
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 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{
ClientIp: ip,
Timestamp: util.GetCurrentTime(),
RequestUri: ctx.Request.RequestURI,
2021-11-07 17:20:15 +08:00
User: "",
2021-11-07 16:51:16 +08:00
Organization: "",
Action: action,
}
return &record
}
2021-11-07 16:51:16 +08:00
func AddRecord(record *Record) bool {
affected, err := adapter.Engine.Insert(record)
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{})
if err != nil {
panic(err)
}
return int(count)
}
2021-11-07 16:51:16 +08:00
func GetRecords() []*Record {
records := []*Record{}
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{}
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{}
err := adapter.Engine.Find(&records, record)
if err != nil {
panic(err)
}
return records
}