Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pagination to rule and record pages #61

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions controllers/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package controllers
import (
"encoding/json"

"github.com/beego/beego/utils/pagination"
nomeguy marked this conversation as resolved.
Show resolved Hide resolved
"github.com/casbin/caswaf/util"

"github.com/casbin/caswaf/object"
)

Expand All @@ -29,15 +32,39 @@ func (c *ApiController) GetRecords() {
if owner == "admin" {
owner = ""
}

sites, err := object.GetRecords(owner)
if err != nil {
c.ResponseError(err.Error())
return
limit := c.Input().Get("pageSize")
page := c.Input().Get("p")
field := c.Input().Get("field")
value := c.Input().Get("value")
sortField := c.Input().Get("sortField")
sortOrder := c.Input().Get("sortOrder")

if limit == "" || page == "" {
records, err := object.GetRecords(owner)
if err != nil {
c.ResponseError(err.Error())
return
}

// object.GetMaskedSites(sites, util.GetHostname())
c.ResponseOk(records)
} else {
limit := util.ParseInt(limit)
count, err := object.GetRecordCount(owner, field, value)
if err != nil {
c.ResponseError(err.Error())
return
}

paginator := pagination.SetPaginator(c.Ctx, limit, count)
records, err := object.GetPaginationRecords(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(records, paginator.Nums())
}

// object.GetMaskedSites(sites, util.GetHostname())
c.ResponseOk(sites)
}

func (c *ApiController) DeleteRecord() {
Expand Down
40 changes: 33 additions & 7 deletions controllers/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import (
"net"
"strings"

"github.com/beego/beego/utils/pagination"
nomeguy marked this conversation as resolved.
Show resolved Hide resolved
"github.com/hsluoyz/modsecurity-go/seclang/parser"

"github.com/casbin/caswaf/object"
"github.com/casbin/caswaf/util"
"github.com/hsluoyz/modsecurity-go/seclang/parser"
)

func (c *ApiController) GetRules() {
Expand All @@ -33,14 +35,38 @@ func (c *ApiController) GetRules() {
if owner == "admin" {
owner = ""
}
limit := c.Input().Get("pageSize")
page := c.Input().Get("p")
field := c.Input().Get("field")
value := c.Input().Get("value")
sortField := c.Input().Get("sortField")
sortOrder := c.Input().Get("sortOrder")

rules, err := object.GetRules(owner)
if err != nil {
c.ResponseError(err.Error())
return
}
if limit == "" || page == "" {
rules, err := object.GetRules(owner)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(rules)
} else {
limit := util.ParseInt(limit)
count, err := object.GetRuleCount(owner, field, value)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(rules)
paginator := pagination.SetPaginator(c.Ctx, limit, count)
rules, err := object.GetPaginationRules(owner, paginator.Offset(), limit, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(rules, paginator.Nums())
}
}

func (c *ApiController) GetRule() {
Expand Down
16 changes: 16 additions & 0 deletions object/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,19 @@ func timeType2Format(timeType string) string {
}
return "%Y-%m-%d %H"
}

func GetRecordCount(owner, field, value string) (int64, error) {
session := GetSession(owner, -1, -1, field, value, "", "")
return session.Count(&Record{})
}

func GetPaginationRecords(owner string, offset, limit int, field, value, sortField, sortOrder string) ([]*Record, error) {
records := []*Record{}
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder)
err := session.Where("owner = ? or owner = ?", "admin", owner).Find(&records)
if err != nil {
return records, err
}

return records, nil
}
16 changes: 16 additions & 0 deletions object/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,19 @@ func DeleteRule(rule *Rule) (bool, error) {
func (rule *Rule) GetId() string {
return fmt.Sprintf("%s/%s", rule.Owner, rule.Name)
}

func GetRuleCount(owner, field, value string) (int64, error) {
session := GetSession(owner, -1, -1, field, value, "", "")
return session.Count(&Rule{})
}

func GetPaginationRules(owner string, offset, limit int, field, value, sortField, sortOrder string) ([]*Rule, error) {
rules := []*Rule{}
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder)
err := session.Where("owner = ? or owner = ?", "admin", owner).Find(&rules)
if err != nil {
return rules, err
}

return rules, nil
}
6 changes: 5 additions & 1 deletion web/src/RecordListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ class RecordListPage extends BaseListPage {
}

fetch = (params = {}) => {
const sortField = params.sortField, sortOrder = params.sortOrder;
if (!params.pagination) {
params.pagination = {current: 1, pageSize: 10};
}
this.setState({loading: true});
RecordBackend.getRecords(this.props.account.name)
RecordBackend.getRecords(this.props.account.name, params.pagination.current, params.pagination.pageSize, sortField, sortOrder)
.then((res) => {
this.setState({
loading: false,
Expand Down
10 changes: 7 additions & 3 deletions web/src/RuleListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ class RuleListPage extends BaseListPage {
this.fetch();
}

fetch() {
fetch = (params = {}) => {
const sortField = params.sortField, sortOrder = params.sortOrder;
if (!params.pagination) {
params.pagination = {current: 1, pageSize: 10};
}
this.setState({
loading: true,
});
RuleBackend.getRules(this.props.account.name).then((res) => {
RuleBackend.getRules(this.props.account.name, params.pagination.current, params.pagination.pageSize, sortField, sortOrder).then((res) => {
this.setState({
data: res.data,
loading: false,
});
});
}
};

addRule() {
const newRule = this.newRule();
Expand Down
4 changes: 2 additions & 2 deletions web/src/backend/RecordBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import * as Setting from "../Setting";

export function getRecords(owner) {
return fetch(`${Setting.ServerUrl}/api/get-records?owner=${owner}`, {
export function getRecords(owner, page = "", pageSize = "", sortField = "", sortOrder = "") {
return fetch(`${Setting.ServerUrl}/api/get-records?owner=${owner}&p=${page}&pageSize=${pageSize}&sortField=${sortField}&sortOrder=${sortOrder}`, {
method: "GET",
credentials: "include",
}).then(res => res.json());
Expand Down
4 changes: 2 additions & 2 deletions web/src/backend/RuleBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import * as Setting from "../Setting";

export function getRules(owner) {
return fetch(`${Setting.ServerUrl}/api/get-rules?owner=${owner}`, {
export function getRules(owner, page = "", pageSize = "", sortField = "", sortOrder = "") {
return fetch(`${Setting.ServerUrl}/api/get-rules?owner=${owner}&p=${page}&pageSize=${pageSize}&sortField=${sortField}&sortOrder=${sortOrder}`, {
method: "GET",
credentials: "include",
}).then(res => res.json());
Expand Down
Loading