Skip to content

Commit

Permalink
fix: fix refresh and delete in cert and site after sort
Browse files Browse the repository at this point in the history
  • Loading branch information
chatiti committed Aug 2, 2024
1 parent 4b5d42d commit 22998c9
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 91 deletions.
49 changes: 35 additions & 14 deletions controllers/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ package controllers

import (
"encoding/json"

"github.com/beego/beego/utils/pagination"
"github.com/casbin/caswaf/object"
"github.com/casbin/caswaf/util"
)

func (c *ApiController) GetGlobalCerts() {
Expand All @@ -44,13 +45,38 @@ func (c *ApiController) GetCerts() {
owner = ""
}

certs, err := object.GetCerts(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 == "" {
certs, err := object.GetCerts(owner)
if err != nil {
c.ResponseError(err.Error())
return
}

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

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

c.ResponseOk(object.GetMaskedCerts(certs), paginator.Nums())
}

c.ResponseOk(object.GetMaskedCerts(certs))
}

func (c *ApiController) GetCert() {
Expand Down Expand Up @@ -142,11 +168,6 @@ func (c *ApiController) UpdateCertDomainExpire() {
}
cert.DomainExpireTime = domainExpireTime

_, err = object.UpdateCert(id, cert)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(object.GetMaskedCert(cert))
c.Data["json"] = wrapActionResponse(object.UpdateCert(id, cert))
c.ServeJSON()
}
31 changes: 28 additions & 3 deletions controllers/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package controllers

import (
"encoding/json"

"github.com/beego/beego/utils/pagination"
"github.com/casbin/caswaf/object"
"github.com/casbin/caswaf/util"
)
Expand Down Expand Up @@ -45,13 +45,38 @@ func (c *ApiController) GetSites() {
owner = ""
}

sites, err := object.GetSites(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")

if limit == "" || page == "" {
sites, err := object.GetSites(owner)
if err != nil {
c.ResponseError(err.Error())
return
}
c.ResponseOk(object.GetMaskedSites(sites, util.GetHostname()))
return
}

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

c.ResponseOk(object.GetMaskedSites(sites, util.GetHostname()))
paginator := pagination.SetPaginator(c.Ctx, limitInt, count)
sites, err := object.GetPaginationSites(owner, paginator.Offset(), limitInt, field, value, sortField, sortOrder)
if err != nil {
c.ResponseError(err.Error())
return
}

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

func (c *ApiController) GetSite() {
Expand Down
16 changes: 16 additions & 0 deletions object/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,19 @@ func (cert *Cert) isCertNearExpire() (bool, error) {

return res, nil
}

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

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

return certs, nil
}
45 changes: 45 additions & 0 deletions object/ormer_session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2023 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 (
"fmt"
"github.com/casbin/caswaf/util"
"github.com/xorm-io/xorm"
)

func GetSession(owner string, offset, limit int, field, value, sortField, sortOrder string) *xorm.Session {
session := ormer.Engine.Prepare()
if offset != -1 && limit != -1 {
session.Limit(limit, offset)
}
if owner != "" {
session = session.And("owner=?", owner)
}
if field != "" && value != "" {
if util.FilterField(field) {
session = session.And(fmt.Sprintf("%s like ?", util.SnakeString(field)), fmt.Sprintf("%%%s%%", value))
}
}
if sortField == "" || sortOrder == "" {
sortField = "created_time"
}
if sortOrder == "ascend" {
session = session.Asc(util.SnakeString(sortField))
} else {
session = session.Desc(util.SnakeString(sortField))
}
return session
}
16 changes: 16 additions & 0 deletions object/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,19 @@ func (site *Site) checkNodes() error {

return nil
}

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

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

return sites, nil
}
18 changes: 18 additions & 0 deletions util/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,21 @@ func GenerateTwoUniqueRandomStrings() (string, string, error) {
}
return str1, str2, nil
}

func SnakeString(s string) string {
data := make([]byte, 0, len(s)*2)
j := false
num := len(s)
for i := 0; i < num; i++ {
d := s[i]
if i > 0 && d >= 'A' && d <= 'Z' && j {
data = append(data, '_')
}
if d != '_' {
j = true
}
data = append(data, d)
}
result := strings.ToLower(string(data[:]))
return strings.ReplaceAll(result, " ", "")
}
17 changes: 17 additions & 0 deletions util/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package util

import "regexp"

var (
ReWhiteSpace *regexp.Regexp
ReFieldWhiteList *regexp.Regexp
)

func init() {
ReWhiteSpace, _ = regexp.Compile(`\s`)
ReFieldWhiteList, _ = regexp.Compile(`^[A-Za-z0-9]+$`)
}

func FilterField(field string) bool {
return ReFieldWhiteList.MatchString(field)
}
14 changes: 11 additions & 3 deletions web/src/BaseListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,19 @@ class BaseListPage extends React.Component {
}

UNSAFE_componentWillMount() {
this.fetch();
const {pagination} = this.state;
this.fetch({pagination});
}

handleTableChange = () => {
this.fetch();
handleTableChange = (pagination, filters, sorter) => {
this.fetch({
sortField: sorter.field,
sortOrder: sorter.order,
pagination,
...filters,
searchText: this.state.searchText,
searchedColumn: this.state.searchedColumn,
});
};

render() {
Expand Down
Loading

0 comments on commit 22998c9

Please sign in to comment.