-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add record (aka request logs) list and edit pages (#27)
* feat: add record (aka request logs) list and edit pages * feat: add record (aka request logs) list and edit pages * Add AddRecord() * refactor BaseListPage * Resolved code inconsistency * Update record.go --------- Co-authored-by: HeartLinked <lifeiyang@zju.edu.cn> Co-authored-by: Gucheng <85475922+nomeguy@users.noreply.github.com>
- Loading branch information
1 parent
7162aa2
commit df2e76b
Showing
12 changed files
with
814 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2024 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 controllers | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/casbin/caswaf/object" | ||
) | ||
|
||
func (c *ApiController) GetRecords() { | ||
if c.RequireSignedIn() { | ||
return | ||
} | ||
|
||
owner := c.Input().Get("owner") | ||
if owner == "admin" { | ||
owner = "" | ||
} | ||
|
||
sites, err := object.GetRecords(owner) | ||
if err != nil { | ||
c.ResponseError(err.Error()) | ||
return | ||
} | ||
|
||
// object.GetMaskedSites(sites, util.GetHostname()) | ||
c.ResponseOk(sites) | ||
} | ||
|
||
func (c *ApiController) DeleteRecord() { | ||
if c.RequireSignedIn() { | ||
return | ||
} | ||
|
||
var record object.Record | ||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &record) | ||
if err != nil { | ||
c.ResponseError(err.Error()) | ||
return | ||
} | ||
|
||
c.Data["json"] = wrapActionResponse(object.DeleteRecord(&record)) | ||
c.ServeJSON() | ||
} | ||
|
||
func (c *ApiController) UpdateRecord() { | ||
if c.RequireSignedIn() { | ||
return | ||
} | ||
|
||
owner := c.Input().Get("owner") | ||
id := c.Input().Get("id") | ||
|
||
var record object.Record | ||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &record) | ||
if err != nil { | ||
c.ResponseError(err.Error()) | ||
return | ||
} | ||
|
||
c.Data["json"] = wrapActionResponse(object.UpdateRecord(owner, id, &record)) | ||
c.ServeJSON() | ||
} | ||
|
||
func (c *ApiController) GetRecord() { | ||
if c.RequireSignedIn() { | ||
return | ||
} | ||
|
||
owner := c.Input().Get("owner") | ||
id := c.Input().Get("id") | ||
record, err := object.GetRecord(owner, id) | ||
if err != nil { | ||
c.ResponseError(err.Error()) | ||
return | ||
} | ||
|
||
c.ResponseOk(record) | ||
} | ||
|
||
func (c *ApiController) AddRecord() { | ||
if c.RequireSignedIn() { | ||
return | ||
} | ||
|
||
var record object.Record | ||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &record) | ||
if err != nil { | ||
c.ResponseError(err.Error()) | ||
return | ||
} | ||
|
||
c.Data["json"] = wrapActionResponse(object.AddRecord(&record)) | ||
c.ServeJSON() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2024 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 ( | ||
"strconv" | ||
|
||
"github.com/xorm-io/core" | ||
) | ||
|
||
type Record struct { | ||
Id int64 `xorm:"int notnull pk autoincr" json:"id"` | ||
Owner string `xorm:"varchar(100) notnull" json:"owner"` | ||
CreatedTime string `xorm:"varchar(100) notnull" json:"createdTime"` | ||
|
||
Method string `xorm:"varchar(100)" json:"method"` | ||
Host string `xorm:"varchar(100)" json:"host"` | ||
Path string `xorm:"varchar(100)" json:"path"` | ||
ClientIp string `xorm:"varchar(100)" json:"clientIp"` | ||
UserAgent string `xorm:"varchar(512)" json:"userAgent"` | ||
} | ||
|
||
func GetRecords(owner string) ([]*Record, error) { | ||
records := []*Record{} | ||
err := ormer.Engine.Asc("id").Asc("host").Find(&records, &Record{Owner: owner}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return records, nil | ||
} | ||
|
||
func AddRecord(record *Record) (bool, error) { | ||
affected, err := ormer.Engine.Insert(record) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return affected != 0, nil | ||
} | ||
|
||
func DeleteRecord(record *Record) (bool, error) { | ||
affected, err := ormer.Engine.ID(core.PK{record.Id}).Delete(&Record{}) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return affected != 0, nil | ||
} | ||
|
||
func UpdateRecord(owner string, id string, record *Record) (bool, error) { | ||
affected, err := ormer.Engine.ID(core.PK{record.Id}).AllCols().Update(record) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return affected != 0, nil | ||
} | ||
|
||
func GetRecord(owner string, id string) (*Record, error) { | ||
idNum, err := strconv.Atoi(id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
record, err := getRecord(owner, int64(idNum)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return record, nil | ||
} | ||
|
||
func getRecord(owner string, id int64) (*Record, error) { | ||
record := Record{Owner: owner, Id: id} | ||
existed, err := ormer.Engine.Get(&record) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if existed { | ||
return &record, nil | ||
} | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.