Skip to content

Commit

Permalink
feat: add action object (#72)
Browse files Browse the repository at this point in the history
* feat: add action object

* feat: remove updatedTime and set createdTime at the frontend
  • Loading branch information
love98ooo authored Sep 5, 2024
1 parent a9b1f4e commit a4b42f3
Show file tree
Hide file tree
Showing 9 changed files with 687 additions and 2 deletions.
102 changes: 102 additions & 0 deletions controllers/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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 controllers

import (
"encoding/json"

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

func (c *ApiController) GetActions() {
if c.RequireSignedIn() {
return
}
owner := c.Input().Get("owner")
if owner == "admin" {
owner = ""
}

actions, err := object.GetActions(owner)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(actions)
}

func (c *ApiController) GetAction() {
if c.RequireSignedIn() {
return
}

id := c.Input().Get("id")
action, err := object.GetAction(id)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(action)
}

func (c *ApiController) AddAction() {
if c.RequireSignedIn() {
return
}

var action object.Action
err := json.Unmarshal(c.Ctx.Input.RequestBody, &action)
if err != nil {
c.ResponseError(err.Error())
return
}
c.Data["json"] = wrapActionResponse(object.AddAction(&action))
c.ServeJSON()
}

func (c *ApiController) UpdateAction() {
if c.RequireSignedIn() {
return
}

var action object.Action
err := json.Unmarshal(c.Ctx.Input.RequestBody, &action)
if err != nil {
c.ResponseError(err.Error())
return
}

id := c.Input().Get("id")
c.Data["json"] = wrapActionResponse(object.UpdateAction(id, &action))
c.ServeJSON()
}

func (c *ApiController) DeleteAction() {
if c.RequireSignedIn() {
return
}

var action object.Action
err := json.Unmarshal(c.Ctx.Input.RequestBody, &action)
if err != nil {
c.ResponseError(err.Error())
return
}

c.Data["json"] = wrapActionResponse(object.DeleteAction(&action))
c.ServeJSON()
}
106 changes: 106 additions & 0 deletions object/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// 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 (
"fmt"

"github.com/casbin/caswaf/util"
"github.com/xorm-io/core"
)

type Action struct {
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
Name string `xorm:"varchar(100) notnull pk" json:"name"`
CreatedTime string `xorm:"varchar(100) notnull" json:"createdTime"`
Type string `xorm:"varchar(100) notnull" json:"type"`
StatusCode int `xorm:"int notnull" json:"statusCode"`
ImmunityTimes int `xorm:"int notnull" json:"immunityTimes"` // minutes
}

func GetGlobalActions() ([]*Action, error) {
actions := []*Action{}
err := ormer.Engine.Asc("owner").Desc("created_time").Find(&actions)
return actions, err
}

func GetActions(owner string) ([]*Action, error) {
actions := []*Action{}
err := ormer.Engine.Desc("updated_time").Find(&actions, &Action{Owner: owner})
return actions, err
}

func getAction(owner string, name string) (*Action, error) {
action := Action{Owner: owner, Name: name}
existed, err := ormer.Engine.Get(&action)
if err != nil {
return nil, err
}
if existed {
return &action, nil
} else {
return nil, nil
}
}

func GetAction(id string) (*Action, error) {
owner, name := util.GetOwnerAndNameFromId(id)
return getAction(owner, name)
}

func UpdateAction(id string, action *Action) (bool, error) {
owner, name := util.GetOwnerAndNameFromId(id)
if s, err := getAction(owner, name); err != nil {
return false, err
} else if s == nil {
return false, nil
}
_, err := ormer.Engine.ID(core.PK{owner, name}).AllCols().Update(action)
if err != nil {
return false, err
}
err = refreshActionMap()
if err != nil {
return false, err
}
return true, nil
}

func AddAction(action *Action) (bool, error) {
affected, err := ormer.Engine.Insert(action)
if err != nil {
return false, err
}
if affected != 0 {
err = refreshActionMap()
if err != nil {
return false, err
}
}
return affected != 0, nil
}

func DeleteAction(action *Action) (bool, error) {
affected, err := ormer.Engine.ID(core.PK{action.Owner, action.Name}).Delete(&Action{})
if err != nil {
return false, err
}

return affected != 0, nil
}

func (action *Action) GetId() string {
return fmt.Sprintf("%s/%s", action.Owner, action.Name)
}
57 changes: 57 additions & 0 deletions object/action_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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"
)

var actionMap = map[string]*Action{}

func InitActionMap() {
err := refreshActionMap()
if err != nil {
panic(err)
}
}

func refreshActionMap() error {
newActionMap := map[string]*Action{}
actions, err := GetGlobalActions()
if err != nil {
return err
}

for _, action := range actions {
newActionMap[util.GetIdFromOwnerAndName(action.Owner, action.Name)] = action
}

actionMap = newActionMap
return nil
}

func GetActionsByActionIds(ids []string) ([]*Action, error) {
var res []*Action
for _, id := range ids {
action, ok := actionMap[id]
if !ok {
return nil, fmt.Errorf("action: %s not found", id)
}
res = append(res, action)
}
return res, nil
}
9 changes: 7 additions & 2 deletions object/ormer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (

"github.com/beego/beego"
"github.com/casbin/caswaf/conf"
"github.com/xorm-io/core"
"github.com/xorm-io/xorm"
_ "github.com/denisenkom/go-mssqldb" // db = mssql
_ "github.com/go-sql-driver/mysql" // db = mysql
_ "github.com/lib/pq" // db = postgres
"github.com/xorm-io/core"
"github.com/xorm-io/xorm"
_ "modernc.org/sqlite" // db = sqlite
)

Expand Down Expand Up @@ -198,4 +198,9 @@ func (a *Ormer) createTable() {
if err != nil {
panic(err)
}

err = a.Engine.Sync2(new(Action))
if err != nil {
panic(err)
}
}
6 changes: 6 additions & 0 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,10 @@ func initAPI() {
beego.Router("/api/add-rule", &controllers.ApiController{}, "POST:AddRule")
beego.Router("/api/update-rule", &controllers.ApiController{}, "POST:UpdateRule")
beego.Router("/api/delete-rule", &controllers.ApiController{}, "POST:DeleteRule")

beego.Router("/api/get-actions", &controllers.ApiController{}, "GET:GetActions")
beego.Router("/api/get-action", &controllers.ApiController{}, "GET:GetAction")
beego.Router("/api/add-action", &controllers.ApiController{}, "POST:AddAction")
beego.Router("/api/update-action", &controllers.ApiController{}, "POST:UpdateAction")
beego.Router("/api/delete-action", &controllers.ApiController{}, "POST:DeleteAction")
}
Loading

0 comments on commit a4b42f3

Please sign in to comment.