mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-24 08:20:31 +08:00
Refactor the record code.
This commit is contained in:
parent
0e71e603ac
commit
77fffcacac
@ -195,10 +195,9 @@ func (c *ApiController) Login() {
|
||||
application := object.GetApplication(fmt.Sprintf("admin/%s", form.Application))
|
||||
resp = c.HandleLoggedIn(application, user, &form)
|
||||
|
||||
record := util.Records(c.Ctx)
|
||||
record := object.NewRecord(c.Ctx)
|
||||
record.Organization = application.Organization
|
||||
record.Username = user.Name
|
||||
|
||||
object.AddRecord(record)
|
||||
}
|
||||
} else if form.Provider != "" {
|
||||
@ -260,10 +259,9 @@ func (c *ApiController) Login() {
|
||||
|
||||
resp = c.HandleLoggedIn(application, user, &form)
|
||||
|
||||
record := util.Records(c.Ctx)
|
||||
record := object.NewRecord(c.Ctx)
|
||||
record.Organization = application.Organization
|
||||
record.Username = user.Name
|
||||
|
||||
object.AddRecord(record)
|
||||
} else {
|
||||
// Sign up via OAuth
|
||||
@ -306,10 +304,9 @@ func (c *ApiController) Login() {
|
||||
|
||||
resp = c.HandleLoggedIn(application, user, &form)
|
||||
|
||||
record := util.Records(c.Ctx)
|
||||
record := object.NewRecord(c.Ctx)
|
||||
record.Organization = application.Organization
|
||||
record.Username = user.Name
|
||||
|
||||
object.AddRecord(record)
|
||||
}
|
||||
//resp = &Response{Status: "ok", Msg: "", Data: res}
|
||||
|
@ -15,8 +15,6 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/astaxie/beego/utils/pagination"
|
||||
"github.com/casbin/casdoor/object"
|
||||
"github.com/casbin/casdoor/util"
|
||||
@ -50,12 +48,14 @@ func (c *ApiController) GetRecords() {
|
||||
// @Success 200 {array} object.Records The Response object
|
||||
// @router /get-records-filter [post]
|
||||
func (c *ApiController) GetRecordsByFilter() {
|
||||
var record object.Records
|
||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &record)
|
||||
body := string(c.Ctx.Input.RequestBody)
|
||||
|
||||
record := &object.Record{}
|
||||
err := util.JsonToStruct(body, record)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
c.Data["json"] = object.GetRecordsByField(&record)
|
||||
c.Data["json"] = object.GetRecordsByField(record)
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
@ -139,7 +139,7 @@ func (a *Adapter) createTable() {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = a.Engine.Sync2(new(Records))
|
||||
err = a.Engine.Sync2(new(Record))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -15,19 +15,40 @@
|
||||
package object
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego/context"
|
||||
"github.com/casbin/casdoor/util"
|
||||
)
|
||||
|
||||
type Records struct {
|
||||
Id int `xorm:"int notnull pk autoincr" json:"id"`
|
||||
Record util.Record `xorm:"extends"`
|
||||
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"`
|
||||
Username string `xorm:"varchar(100)" json:"username"`
|
||||
RequestUri string `xorm:"varchar(1000)" json:"requestUri"`
|
||||
Action string `xorm:"varchar(1000)" json:"action"`
|
||||
}
|
||||
|
||||
func AddRecord(record *util.Record) bool {
|
||||
records := new(Records)
|
||||
records.Record = *record
|
||||
func NewRecord(ctx *context.Context) *Record {
|
||||
ip := strings.Replace(util.GetIPFromRequest(ctx.Request), ": ", "", -1)
|
||||
action := strings.Replace(ctx.Request.URL.Path, "/api/", "", -1)
|
||||
|
||||
affected, err := adapter.Engine.Insert(records)
|
||||
record := Record{
|
||||
ClientIp: ip,
|
||||
Timestamp: util.GetCurrentTime(),
|
||||
RequestUri: ctx.Request.RequestURI,
|
||||
Username: "",
|
||||
Organization: "",
|
||||
Action: action,
|
||||
}
|
||||
return &record
|
||||
}
|
||||
|
||||
func AddRecord(record *Record) bool {
|
||||
affected, err := adapter.Engine.Insert(record)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -36,7 +57,7 @@ func AddRecord(record *util.Record) bool {
|
||||
}
|
||||
|
||||
func GetRecordCount() int {
|
||||
count, err := adapter.Engine.Count(&Records{})
|
||||
count, err := adapter.Engine.Count(&Record{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -44,8 +65,8 @@ func GetRecordCount() int {
|
||||
return int(count)
|
||||
}
|
||||
|
||||
func GetRecords() []*Records {
|
||||
records := []*Records{}
|
||||
func GetRecords() []*Record {
|
||||
records := []*Record{}
|
||||
err := adapter.Engine.Desc("id").Find(&records)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -54,8 +75,8 @@ func GetRecords() []*Records {
|
||||
return records
|
||||
}
|
||||
|
||||
func GetPaginationRecords(offset, limit int) []*Records {
|
||||
records := []*Records{}
|
||||
func GetPaginationRecords(offset, limit int) []*Record {
|
||||
records := []*Record{}
|
||||
err := adapter.Engine.Desc("id").Limit(limit, offset).Find(&records)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -64,8 +85,8 @@ func GetPaginationRecords(offset, limit int) []*Records {
|
||||
return records
|
||||
}
|
||||
|
||||
func GetRecordsByField(record *Records) []*Records {
|
||||
records := []*Records{}
|
||||
func GetRecordsByField(record *Record) []*Record {
|
||||
records := []*Record{}
|
||||
err := adapter.Engine.Find(&records, record)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -58,7 +58,7 @@ func RecordMessage(ctx *context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
record := util.Records(ctx)
|
||||
record := object.NewRecord(ctx)
|
||||
|
||||
userId := getUser(ctx)
|
||||
if userId != "" {
|
||||
|
@ -1,47 +0,0 @@
|
||||
// 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 util
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego/context"
|
||||
)
|
||||
|
||||
type Record struct {
|
||||
ClientIp string `xorm:"varchar(100)" json:"clientIp"`
|
||||
Timestamp string `xorm:"varchar(100)" json:"timestamp"`
|
||||
Organization string `xorm:"varchar(100)" json:"organization"`
|
||||
Username string `xorm:"varchar(100)" json:"username"`
|
||||
RequestUri string `xorm:"varchar(1000)" json:"requestUri"`
|
||||
Action string `xorm:"varchar(1000)" json:"action"`
|
||||
}
|
||||
|
||||
func Records(ctx *context.Context) *Record {
|
||||
ip := strings.Replace(GetIPFromRequest(ctx.Request), ": ", "", -1)
|
||||
currenttime := GetCurrentTime()
|
||||
requesturi := ctx.Request.RequestURI
|
||||
action := strings.Replace(ctx.Request.URL.Path, "/api/", "", -1)
|
||||
|
||||
record := Record{
|
||||
ClientIp: ip,
|
||||
Timestamp: currenttime,
|
||||
RequestUri: requesturi,
|
||||
Username: "",
|
||||
Organization: "",
|
||||
Action: action,
|
||||
}
|
||||
return &record
|
||||
}
|
@ -48,46 +48,47 @@ class RecordListPage extends React.Component {
|
||||
newRecord() {
|
||||
return {
|
||||
id : "",
|
||||
Record:{
|
||||
clientIp:"",
|
||||
timestamp:"",
|
||||
organization:"",
|
||||
username:"",
|
||||
requestUri:"",
|
||||
action:"login",
|
||||
},
|
||||
clientIp: "",
|
||||
timestamp: "",
|
||||
organization: "",
|
||||
username: "",
|
||||
requestUri: "",
|
||||
action: "login",
|
||||
}
|
||||
}
|
||||
|
||||
renderTable(records) {
|
||||
const columns = [
|
||||
{
|
||||
title: i18next.t("general:Client ip"),
|
||||
dataIndex: ['Record', 'clientIp'],
|
||||
title: i18next.t("general:ID"),
|
||||
dataIndex: 'id',
|
||||
key: 'id',
|
||||
width: '120px',
|
||||
fixed: 'left',
|
||||
sorter: (a, b) => a.Record.clientIp.localeCompare(b.Record.clientIp),
|
||||
render: (text, record, index) => {
|
||||
return text;
|
||||
}
|
||||
sorter: (a, b) => a.id - b.id,
|
||||
},
|
||||
{
|
||||
title: i18next.t("general:Client IP"),
|
||||
dataIndex: 'clientIp',
|
||||
key: 'clientIp',
|
||||
width: '120px',
|
||||
sorter: (a, b) => a.clientIp.localeCompare(b.clientIp),
|
||||
},
|
||||
{
|
||||
title: i18next.t("general:Timestamp"),
|
||||
dataIndex: ['Record', 'timestamp'],
|
||||
key: 'id',
|
||||
dataIndex: 'timestamp',
|
||||
key: 'timestamp',
|
||||
width: '160px',
|
||||
sorter: (a, b) => a.Record.timestamp.localeCompare(b.Record.timestamp),
|
||||
sorter: (a, b) => a.timestamp.localeCompare(b.timestamp),
|
||||
render: (text, record, index) => {
|
||||
return Setting.getFormattedDate(text);
|
||||
}
|
||||
},
|
||||
{
|
||||
title: i18next.t("general:Organization"),
|
||||
dataIndex: ['Record', 'organization'],
|
||||
key: 'id',
|
||||
dataIndex: 'organization',
|
||||
key: 'organization',
|
||||
width: '120px',
|
||||
sorter: (a, b) => a.Record.organization.localeCompare(b.Record.organization),
|
||||
sorter: (a, b) => a.organization.localeCompare(b.organization),
|
||||
render: (text, record, index) => {
|
||||
return (
|
||||
<Link to={`/organizations/${text}`}>
|
||||
@ -97,31 +98,25 @@ class RecordListPage extends React.Component {
|
||||
}
|
||||
},
|
||||
{
|
||||
title: i18next.t("general:Username"),
|
||||
dataIndex: ['Record', 'username'],
|
||||
key: 'id',
|
||||
title: i18next.t("general:User"),
|
||||
dataIndex: 'user',
|
||||
key: 'user',
|
||||
width: '160px',
|
||||
sorter: (a, b) => a.Record.username.localeCompare(b.Record.username),
|
||||
render: (text, record, index) => {
|
||||
return text;
|
||||
}
|
||||
sorter: (a, b) => a.user.localeCompare(b.user),
|
||||
},
|
||||
{
|
||||
title: i18next.t("general:Request uri"),
|
||||
dataIndex: ['Record', 'requestUri'],
|
||||
key: 'id',
|
||||
title: i18next.t("general:Request URI"),
|
||||
dataIndex: 'requestUri',
|
||||
key: 'requestUri',
|
||||
width: '160px',
|
||||
sorter: (a, b) => a.Record.requestUri.localeCompare(b.Record.requestUri),
|
||||
render: (text, record, index) => {
|
||||
return text;
|
||||
}
|
||||
sorter: (a, b) => a.requestUri.localeCompare(b.requestUri),
|
||||
},
|
||||
{
|
||||
title: i18next.t("general:Action"),
|
||||
dataIndex: ['Record', 'action'],
|
||||
key: 'id',
|
||||
dataIndex: 'action',
|
||||
key: 'action',
|
||||
width: '160px',
|
||||
sorter: (a, b) => a.Record.action.localeCompare(b.Record.action),
|
||||
sorter: (a, b) => a.action.localeCompare(b.action),
|
||||
render: (text, record, index) => {
|
||||
return text;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user